From c88e59f0bab52f7bddc1360193bc9f2c2a1f9401 Mon Sep 17 00:00:00 2001 From: outfrost Date: Fri, 11 Oct 2019 04:07:27 +0200 Subject: [PATCH] Overhaul Makefile and directory structure * Move sources into src/ * Create platform-specific build directory * Clean up Makefile rules and variables * Prevent make from needlessly generating prerequisites --- Makefile | 124 ++++++++++++++++++--------- asset.c => src/asset.c | 0 asset.h => src/asset.h | 0 assimp_types.h => src/assimp_types.h | 0 debugutil.c => src/debugutil.c | 0 debugutil.h => src/debugutil.h | 0 level.c => src/level.c | 0 level.h => src/level.h | 0 logger.c => src/logger.c | 0 logger.h => src/logger.h | 0 main.c => src/main.c | 0 performance.c => src/performance.c | 0 performance.h => src/performance.h | 0 player.c => src/player.c | 0 player.h => src/player.h | 0 render.c => src/render.c | 0 render.h => src/render.h | 0 tga.c => src/tga.c | 0 tga.h => src/tga.h | 0 typedefs.h => src/typedefs.h | 0 ui.c => src/ui.c | 0 ui.h => src/ui.h | 0 22 files changed, 84 insertions(+), 40 deletions(-) rename asset.c => src/asset.c (100%) rename asset.h => src/asset.h (100%) rename assimp_types.h => src/assimp_types.h (100%) rename debugutil.c => src/debugutil.c (100%) rename debugutil.h => src/debugutil.h (100%) rename level.c => src/level.c (100%) rename level.h => src/level.h (100%) rename logger.c => src/logger.c (100%) rename logger.h => src/logger.h (100%) rename main.c => src/main.c (100%) rename performance.c => src/performance.c (100%) rename performance.h => src/performance.h (100%) rename player.c => src/player.c (100%) rename player.h => src/player.h (100%) rename render.c => src/render.c (100%) rename render.h => src/render.h (100%) rename tga.c => src/tga.c (100%) rename tga.h => src/tga.h (100%) rename typedefs.h => src/typedefs.h (100%) rename ui.c => src/ui.c (100%) rename ui.h => src/ui.h (100%) diff --git a/Makefile b/Makefile index 6dcf104..d4307a2 100644 --- a/Makefile +++ b/Makefile @@ -1,56 +1,100 @@ +PLATFORM ?= x86_64-linux-gnu + +BUILDDIR ?= target/$(PLATFORM) +SRCDIR ?= src + 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 asset.o level.o logger.o \ - performance.o player.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 +# ###### + +vpath %.c $(SRCDIR) +vpath %.h $(SRCDIR) + +sources ::= main.c \ + asset.c \ + level.c \ + logger.c \ + performance.c \ + player.c \ + render.c \ + tga.c \ + ui.c + +objects ::= $(addprefix $(BUILDDIR)/, $(addsuffix .o, $(sources))) +depfiles ::= $(addprefix $(BUILDDIR)/, $(addsuffix .mk, $(sources))) + +# 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) | $(BUILDDIR) @echo "###### Linking executable..." - $(CC) $(LDFLAGS) -o $(binary) $(objects) $(LOADLIBES) $(LDLIBS) + $(CC) $(LDFLAGS) -o $(binary) $^ $(LOADLIBES) $(LDLIBS) -# Alias for default target -shadowclad : $(binary) -.PHONY : shadowclad +# Build C translation units +$(BUILDDIR)/%.c.o: %.c $(BUILDDIR)/%.c.mk | $(BUILDDIR) + $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $< + +# ###### +# Setup +# ###### + +# Create build directory +$(BUILDDIR): + mkdir -p $(BUILDDIR) + +# ###### +# Aliases +# ###### # Build and run -run : $(binary) +run: $(binary) @echo LD_LIBRARY_PATH=/usr/local/lib $(binary) -.PHONY : run +.PHONY: run -# Create compilation directory -out : - mkdir out/ +# Initialise build environment +init: $(BUILDDIR) +.PHONY: init -# Alias for 'out' -init : out -.PHONY : init +# Build executable +shadowclad: $(binary) +.PHONY: shadowclad -# Generate dependencies -out/%.make : %.c Makefile | out - $(CPP) -MM -MT out/$*.o -MF $@ $(CPPFLAGS) $< +# ###### +# Prerequisites +# ###### -# Include generated rules --include $(addsuffix .make, $(basename $(objects))) +# Generate C prerequisite makefiles +$(BUILDDIR)/%.c.mk: %.c Makefile | $(BUILDDIR) + @echo "Generating prerequisites for $<" + @$(CPP) -MM -MT $(BUILDDIR)/$*.c.o -MF $@ $(CPPFLAGS) $< + @sed -E -i 's|^([^\s:]+)([ :])|\1 $@\2|' $@ -# Build compilation units -out/%.o : %.c out/%.make | out - $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $< +# Include generated C prerequisites +include $(foreach depfile, $(depfiles), $(shell [ -r "$(depfile)" ] && echo "$(depfile)")) + +# 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 diff --git a/asset.c b/src/asset.c similarity index 100% rename from asset.c rename to src/asset.c diff --git a/asset.h b/src/asset.h similarity index 100% rename from asset.h rename to src/asset.h diff --git a/assimp_types.h b/src/assimp_types.h similarity index 100% rename from assimp_types.h rename to src/assimp_types.h diff --git a/debugutil.c b/src/debugutil.c similarity index 100% rename from debugutil.c rename to src/debugutil.c diff --git a/debugutil.h b/src/debugutil.h similarity index 100% rename from debugutil.h rename to src/debugutil.h diff --git a/level.c b/src/level.c similarity index 100% rename from level.c rename to src/level.c diff --git a/level.h b/src/level.h similarity index 100% rename from level.h rename to src/level.h diff --git a/logger.c b/src/logger.c similarity index 100% rename from logger.c rename to src/logger.c diff --git a/logger.h b/src/logger.h similarity index 100% rename from logger.h rename to src/logger.h diff --git a/main.c b/src/main.c similarity index 100% rename from main.c rename to src/main.c diff --git a/performance.c b/src/performance.c similarity index 100% rename from performance.c rename to src/performance.c diff --git a/performance.h b/src/performance.h similarity index 100% rename from performance.h rename to src/performance.h diff --git a/player.c b/src/player.c similarity index 100% rename from player.c rename to src/player.c diff --git a/player.h b/src/player.h similarity index 100% rename from player.h rename to src/player.h diff --git a/render.c b/src/render.c similarity index 100% rename from render.c rename to src/render.c diff --git a/render.h b/src/render.h similarity index 100% rename from render.h rename to src/render.h diff --git a/tga.c b/src/tga.c similarity index 100% rename from tga.c rename to src/tga.c diff --git a/tga.h b/src/tga.h similarity index 100% rename from tga.h rename to src/tga.h diff --git a/typedefs.h b/src/typedefs.h similarity index 100% rename from typedefs.h rename to src/typedefs.h diff --git a/ui.c b/src/ui.c similarity index 100% rename from ui.c rename to src/ui.c diff --git a/ui.h b/src/ui.h similarity index 100% rename from ui.h rename to src/ui.h -- 2.44.0