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