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