X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=Makefile;h=28a06f246d4c087d8b212f748cf736dc21663c9c;hb=c78af1fbe8db3d24f5ffdb8bd272748abe7bd0e5;hp=668f3770558579304e301f5e57abaf4c1e921c26;hpb=94a54bd03c21ec9339c2fe9b984fa13db813795e;p=shadowclad.git diff --git a/Makefile b/Makefile index 668f377..28a06f2 100644 --- a/Makefile +++ b/Makefile @@ -1,56 +1,102 @@ -CFLAGS ::= -g -Wall -Wextra -Wpedantic $(CFLAGS) +PLATFORM ?= x86_64-linux-gnu + +BUILDDIR ?= target/$(PLATFORM) +SRCDIR ?= src + +CPPFLAGS ::= -iquotesrc/ $(CPPFLAGS) +CFLAGS ::= -g -std=c99 -Wall -Wextra -Wpedantic $(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 +# ###### +# Paths +# ###### + +sources ::= main.c \ + engine/asset.c \ + engine/logger.c \ + engine/performance.c \ + engine/render.c \ + engine/tga.c \ + engine/ui.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) $(objects) $(LOADLIBES) $(LDLIBS) + $(CC) $(LDFLAGS) -o $(binary) $^ $(LOADLIBES) $(LDLIBS) + +# Build C translation units +$(objects): $(BUILDDIR)/%.c.o: %.c $(BUILDDIR)/%.c.mk + @mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $< + +# ###### +# Setup +# ###### -# Alias for default target -shadowclad : $(binary) -.PHONY : shadowclad +# Initialise build environment +init: + mkdir -p $(BUILDDIR) +.PHONY: init + +# ###### +# Aliases +# ###### # Build and run -run : $(binary) +run: $(binary) @echo - LD_LIBRARY_PATH=/usr/local/lib $(binary) -.PHONY : run + @LD_LIBRARY_PATH=/usr/local/lib $(binary) +.PHONY: run -# Create compilation directory -out : - mkdir out/ +# Build executable +shadowclad: $(binary) +.PHONY: shadowclad -# Alias for 'out' -init : out -.PHONY : init +# ###### +# Prerequisites +# ###### -# Generate dependencies -out/%.make : %.c Makefile | out - $(CPP) -MM -MT out/$*.o -MF $@ $(CPPFLAGS) $< +# 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 rules --include $(addsuffix .make, $(basename $(objects))) +# Include generated C prerequisites +include $(foreach depfile, $(depfiles), $(shell [ -r "$(depfile)" ] && echo "$(depfile)")) -# Build compilation units -out/%.o : %.c out/%.make | out - $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $< +# Do not automatically delete generated prerequisites +.SECONDARY: $(depfiles) + +# ###### +# Cleanup rules +# ###### -# Delete build output -clean : out - rm -f $(binary) out/*.o out/*.make - rm -d out/ -.PHONY : clean +clean: + rm -rf $(BUILDDIR) +.PHONY: clean