]> git.lizzy.rs Git - shadowclad.git/blob - Makefile
Refactor handling of subdirectories in Makefile
[shadowclad.git] / Makefile
1 PLATFORM ?= x86_64-linux-gnu
2
3 BUILDDIR ?= target/$(PLATFORM)
4 SRCDIR ?= src
5
6 CFLAGS ::= -g -std=c99 -Wall -Wextra -Wpedantic $(CFLAGS)
7 LDFLAGS ::= $(LDFLAGS)
8 LDLIBS ::= -L/usr/local/lib -lGL -lGLEW -lglut -lassimp $(LDLIBS)
9
10 # ######
11 # Paths
12 # ######
13
14 sources ::= main.c \
15             asset.c \
16             level.c \
17             logger.c \
18             performance.c \
19             player.c \
20             render.c \
21             tga.c \
22             ui.c
23
24 srcfiles ::= $(addprefix $(SRCDIR)/, $(sources))
25
26 objects ::= $(addprefix $(BUILDDIR)/, $(addsuffix .o, $(srcfiles)))
27 depfiles ::= $(addprefix $(BUILDDIR)/, $(addsuffix .mk, $(srcfiles)))
28
29 # Set executable name for the platform
30 # TODO Base this on target platform instead of host OS
31 #ifeq ($(OS),Windows_NT)
32 #       binext ::= .exe
33 #else
34 #       binext ::=
35 #endif
36 binary ::= $(BUILDDIR)/shadowclad #$(binext)
37
38 # ######
39 # Main build rules
40 # ######
41
42 # Default rule: build executable
43 $(binary): $(objects)
44         @mkdir -p $(@D)
45         @echo "###### Linking executable..."
46         $(CC) $(LDFLAGS) -o $(binary) $^ $(LOADLIBES) $(LDLIBS)
47
48 # Build C translation units
49 $(objects): $(BUILDDIR)/%.c.o: %.c $(BUILDDIR)/%.c.mk
50         @mkdir -p $(@D)
51         $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
52
53 # ######
54 # Setup
55 # ######
56
57 # Initialise build environment
58 init:
59         mkdir -p $(BUILDDIR)
60 .PHONY: init
61
62 # ######
63 # Aliases
64 # ######
65
66 # Build and run
67 run: $(binary)
68         @echo
69         @LD_LIBRARY_PATH=/usr/local/lib $(binary)
70 .PHONY: run
71
72 # Build executable
73 shadowclad: $(binary)
74 .PHONY: shadowclad
75
76 # ######
77 # Prerequisites
78 # ######
79
80 # Generate C prerequisite makefiles
81 $(depfiles): $(BUILDDIR)/%.c.mk: %.c Makefile
82         @mkdir -p $(@D)
83         @echo "Generating prerequisites for $<"
84         @$(CPP) -MM -MT $(BUILDDIR)/$*.c.o -MF $@ $(CPPFLAGS) $<
85 # Give the same prerequisites to the prerequisite makefile,
86 # so that it is regenerated whenever any of said prerequisites change
87         @sed -E -i 's|^([^\s:]+)([ :])|\1 $@\2|' $@
88
89 # Include generated C prerequisites
90 include $(foreach depfile, $(depfiles), $(shell [ -r "$(depfile)" ] && echo "$(depfile)"))
91
92 # Do not automatically delete generated prerequisites
93 .SECONDARY: $(depfiles)
94
95 # ######
96 # Cleanup rules
97 # ######
98
99 clean:
100         rm -rf $(BUILDDIR)
101 .PHONY: clean