X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=Makefile;h=42225a5a91e9b15e93d2ede59c6f2cf1763f9793;hb=refs%2Fheads%2Fshading-bug-25;hp=2a3dfc1a640d3872ff40ae289ab8e63fcc758410;hpb=28822c9926bad090069c327914e6e1161b46fc31;p=shadowclad.git diff --git a/Makefile b/Makefile index 2a3dfc1..42225a5 100644 --- a/Makefile +++ b/Makefile @@ -1,56 +1,110 @@ -CFLAGS ::= -g -Wall -Wextra -Wpedantic -Wstrict-prototypes $(CFLAGS) +PLATFORM ?= x86_64-linux-gnu + +BUILDDIR ?= target/$(PLATFORM) +SRCDIR ?= src + +CPPFLAGS ::= -iquotesrc/ $(CPPFLAGS) +CFLAGS ::= -g -std=c99 -Wall -Wextra -Wpedantic -Werror \ + -Wno-error=unused-function -Wno-error=unused-parameter $(CFLAGS) LDFLAGS ::= $(LDFLAGS) -LDLIBS ::= -L/usr/local/lib -lGL -lGLEW -lglut -lassimp $(LDLIBS) - -# Prefix all object file names with the compilation directory -objects ::= $(addprefix out/, \ - main.o debugutil.o level.o logger.o \ - performance.o render.o tga.o ui.o) - -# Set executable extension for the platform -ifeq ($(OS),Windows_NT) - binext ::= .exe -else - binext ::= -endif -binary ::= out/shadowclad$(binext) - -# Default target: build executable -$(binary) : $(objects) | out - @echo "###### Linking executable..." - $(CC) $(LDFLAGS) -o $(binary) $(objects) $(LOADLIBES) $(LDLIBS) - -# Alias for default target -shadowclad : $(binary) -.PHONY : shadowclad +LDLIBS ::= -lm -lGL -lGLEW -lglut -lassimp $(LDLIBS) + +# ###### +# Paths +# ###### + +sources ::= main.c \ + engine/asset.c \ + engine/geometry.c \ + engine/logger.c \ + engine/performance.c \ + engine/render.c \ + engine/scene.c \ + engine/string.c \ + engine/tga.c \ + engine/ui.c \ + game/input.c \ + game/level.c \ + game/player.c + +srcfiles ::= $(addprefix $(SRCDIR)/, $(sources)) + +objects ::= $(addprefix $(BUILDDIR)/, $(addsuffix .o, $(srcfiles))) +depfiles ::= $(addprefix $(BUILDDIR)/, $(addsuffix .mk, $(srcfiles))) + +# 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) + +# ###### +# 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) +run: $(binary) @echo - LD_LIBRARY_PATH=/usr/local/lib $(binary) -.PHONY : run + @$(binary) +.PHONY: run + +# Build executable +shadowclad: $(binary) +.PHONY: shadowclad -# Create compilation directory -out : - mkdir out/ +# ###### +# Prerequisites +# ###### -# Alias for 'out' -init : out -.PHONY : init +# 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|' $@ -# Generate dependencies -out/%.make : %.c Makefile | out - $(CPP) -MM -MT out/$*.o -MF $@ $(CPPFLAGS) $< +# Include generated C prerequisites +include $(foreach depfile, $(depfiles), $(shell [ -r "$(depfile)" ] && echo "$(depfile)")) -# Include generated rules --include $(addsuffix .make, $(basename $(objects))) +# Do not automatically delete generated prerequisites +.SECONDARY: $(depfiles) -# Build compilation units -out/%.o : %.c out/%.make | out - $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $< +# ###### +# Cleanup rules +# ###### -# Delete build output -clean : out - rm -f $(binary) out/*.o out/*.make - rm -d out/ -.PHONY : clean +clean: + @echo "Removing $(BUILDDIR)" + @rm -rf $(BUILDDIR) +.PHONY: clean