]> git.lizzy.rs Git - shadowclad.git/blobdiff - Makefile
Add minimal, wonky scene tree system
[shadowclad.git] / Makefile
index 2e38020f24e1c14170cbeed3480b78e17fd97c45..4184930249cbd9488ca398ddcade09be85fc7d9f 100644 (file)
--- a/Makefile
+++ b/Makefile
-compileargs = -Wall -Wextra -Wpedantic
-linkargs = -lGL -lglut
-objects = out/main.o out/debugutil.o out/glut_janitor.o out/render.o \
-          out/tga.o out/level.o
+PLATFORM ?= x86_64-linux-gnu
 
-shadowclad : $(objects)
-       gcc -o out/shadowclad $(objects) $(linkargs)
+BUILDDIR ?= target/$(PLATFORM)
+SRCDIR ?= src
 
-run : shadowclad
-       out/shadowclad
+CPPFLAGS ::= -iquotesrc/ $(CPPFLAGS)
+CFLAGS ::= -g -std=c99 -Wall -Wextra -Wpedantic -Werror $(CFLAGS)
+LDFLAGS ::= $(LDFLAGS)
+LDLIBS ::= -lGL -lGLEW -lglut -lassimp $(LDLIBS)
 
-out/main.o : main.c debugutil.h glut_janitor.h render.h
-       gcc -c -o out/main.o main.c $(compileargs)
+# ######
+# Paths
+# ######
 
-out/debugutil.o : debugutil.c
-       gcc -c -o out/debugutil.o debugutil.c $(compileargs)
+sources ::= main.c \
+            engine/asset.c \
+            engine/logger.c \
+            engine/performance.c \
+            engine/render.c \
+            engine/scene.c \
+            engine/tga.c \
+            engine/ui.c \
+            game/level.c \
+            game/player.c
 
-out/glut_janitor.o : glut_janitor.c
-       gcc -c -o out/glut_janitor.o glut_janitor.c $(compileargs)
+srcfiles ::= $(addprefix $(SRCDIR)/, $(sources))
 
-out/render.o : render.c render.h typedefs.h
-       gcc -c -o out/render.o render.c $(compileargs)
+objects ::= $(addprefix $(BUILDDIR)/, $(addsuffix .o, $(srcfiles)))
+depfiles ::= $(addprefix $(BUILDDIR)/, $(addsuffix .mk, $(srcfiles)))
 
-out/tga.o : tga.c tga.h
-       gcc -c -o out/tga.o tga.c $(compileargs)
+# Set executable name for the platform
+# TODO Base this on target platform instead of host OS
+#ifeq ($(OS),Windows_NT)
+#      binext ::= .exe
+#else
+#      binext ::=
+#endif
+binary ::= $(BUILDDIR)/shadowclad #$(binext)
 
-out/level.o : level.c level.h tga.h
-       gcc -c -o out/level.o level.c $(compileargs)
+# ######
+# Main build rules
+# ######
+
+# Default rule: build executable
+$(binary): $(objects)
+       @mkdir -p $(@D)
+       @echo "Linking executable"
+       @$(CC) $(LDFLAGS) -o $(binary) $^ $(LOADLIBES) $(LDLIBS)
+
+# Build C translation units
+$(objects): $(BUILDDIR)/%.c.o: %.c $(BUILDDIR)/%.c.mk
+       @mkdir -p $(@D)
+       @echo "Building $@"
+       @$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
+
+# ######
+# Setup
+# ######
+
+# Initialise build environment
+init:
+       @echo "Creating build directory $(BUILDDIR)"
+       @mkdir -p $(BUILDDIR)
+.PHONY: init
+
+# ######
+# Aliases
+# ######
+
+# Build and run
+run: $(binary)
+       @echo
+       @$(binary)
+.PHONY: run
+
+# Build executable
+shadowclad: $(binary)
+.PHONY: shadowclad
+
+# ######
+# Prerequisites
+# ######
+
+# Generate C prerequisite makefiles
+$(depfiles): $(BUILDDIR)/%.c.mk: %.c Makefile
+       @mkdir -p $(@D)
+       @echo "Generating prerequisites for $<"
+       @$(CPP) -MM -MT $(BUILDDIR)/$*.c.o -MF $@ $(CPPFLAGS) $<
+# Give the same prerequisites to the prerequisite makefile,
+# so that it is regenerated whenever any of said prerequisites change
+       @sed -E -i 's|^([^\s:]+)([ :])|\1 $@\2|' $@
+
+# Include generated C prerequisites
+include $(foreach depfile, $(depfiles), $(shell [ -r "$(depfile)" ] && echo "$(depfile)"))
+
+# Do not automatically delete generated prerequisites
+.SECONDARY: $(depfiles)
+
+# ######
+# Cleanup rules
+# ######
+
+clean:
+       @echo "Removing $(BUILDDIR)"
+       @rm -rf $(BUILDDIR)
+.PHONY: clean