Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
269 lines (233 loc) · 12.2 KB
/
Copy pathMakefile
File metadata and controls
269 lines (233 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# MGEngine build.
#
# The engine is a shared library; `editor/` is a separate app (the scene editor)
# that links against it through the public headers in `source/`.
#
# make -> build/{libmgengine.(dll|so),editor,mgeplayer}
# DEBUG build: -O0 -g, assertions on
# make release -> build/release/{...} PRODUCTION build: -O2 -DNDEBUG,
# stripped, dead code removed. Separate object cache, so
# `make` and `make release` coexist -- run both to
# keep a debug and a release set side by side.
# make lib -> just the engine library (of the current config)
# make vendor build GLFW + Assimp from source (needs cmake + ninja)
# make vendor-glfw / make vendor-assimp build just one
# make vendor-clean remove built vendor artifacts (keeps the source trees)
# make test build+run the unit tests (no window/GL needed)
# make clean-obj drop the object cache of the current config
# make clean remove build/ (leaves vendor/ -- use vendor-clean for that)
#
# On Windows use `mingw32-make`.
CC = gcc
CXX = g++
VENDOR = ./vendor
MLIB = $(VENDOR)/mlib
CFLAGS ?= -std=c11 -Wall -Wextra -g
CXXFLAGS ?= -std=c++17 -Wall -Wextra -g
# strict -std=c11/-std=c++17 defines __STRICT_ANSI__, which hides POSIX APIs the
# engine uses (clock_gettime, nanosleep, popen, strdup, usleep, ...). Ask glibc
# for them explicitly; a no-op on Windows/macOS.
CPPFLAGS = -DPLATFORM_DESKTOP -D_POSIX_C_SOURCE=200809L -D_DEFAULT_SOURCE
# production build flags (see the `release` target). -O2 is the big FPS win over
# the default -O0; NDEBUG drops assertions; the section flags + -s let the linker
# garbage-collect unused code and strip symbols.
RELEASE_CFLAGS = -std=c11 -Wall -Wextra -O2 -DNDEBUG -ffunction-sections -fdata-sections
RELEASE_CXXFLAGS = -std=c++17 -Wall -Wextra -O2 -DNDEBUG -ffunction-sections -fdata-sections
INCLUDES = -I./source \
-I$(VENDOR)/glad/include \
-I$(VENDOR)/stb \
-I$(VENDOR)/glfw/include \
-I$(VENDOR)/assimp/include \
-I$(MLIB) -I$(MLIB)/vec
# the editor additionally needs Dear ImGui + GLFW headers for editor/mge_gui.cpp
EDITOR_INCLUDES = $(INCLUDES) -I$(VENDOR)/imgui
# distros using GNUInstallDirs (Fedora, ...) install these to lib64/; keep both
LIB_DIR = -L$(VENDOR)/glfw/lib -L$(VENDOR)/glfw/lib64 \
-L$(VENDOR)/assimp/lib -L$(VENDOR)/assimp/lib64
LINK = g++
SOURCE_DIR = source
BUILD_DIR = build
# debug lands in build/ ; `make release` (RELEASE=1) lands in build/release/ --
# separate object caches, so the two configs coexist without a wipe.
ifdef RELEASE
CONF_DIR := $(BUILD_DIR)/release
else
CONF_DIR := $(BUILD_DIR)
endif
BUILD_OBJ_DIR = $(CONF_DIR)/obj
ifeq ($(OS),Windows_NT)
SHELL := cmd.exe
.SHELLFLAGS := /c
LIB_LINKS := -lglfw3 -lassimp -lzlibstatic -lopengl32 -lgdi32 -lwinmm -lkernel32 -lcomdlg32 -lshell32 -lole32
EXE := .exe
PICFLAG :=
LIB_NAME := libmgengine.dll
IMPLIB := $(CONF_DIR)/libmgengine.dll.a
# bake the C/C++/pthread runtimes into the DLL so the app needs only
# libmgengine.dll + system DLLs at run time
SHAREDFLAGS := -shared -static -static-libgcc -static-libstdc++ -Wl,--out-implib,$(IMPLIB)
APP_LIBS := -L$(CONF_DIR) -lmgengine
# embed a Win32 VERSIONINFO (all three) + an app manifest (the two exes) so
# AV / SmartScreen heuristics have real PE metadata to read
WINDRES ?= windres
# -P on the preprocessor drops '# line' markers, which the RC parser chokes on
WINDRESFLAGS := --preprocessor=gcc --preprocessor-arg=-E --preprocessor-arg=-xc \
--preprocessor-arg=-P --preprocessor-arg=-DRC_INVOKED -I resources -O coff
RES_LIB := $(BUILD_OBJ_DIR)/mgengine_lib.res
RES_EDITOR := $(BUILD_OBJ_DIR)/editor.res
RES_PLAYER := $(BUILD_OBJ_DIR)/mgeplayer.res
# the shipped game is a GUI-subsystem app: no console window. It keeps int
# main() (mingw provides the WinMain shim); stdout still works when launched
# from a shell, just no window is allocated on a double-click.
PLAYER_LDFLAGS := -mwindows
MKDIR = if not exist "$(subst /,\,$1)" mkdir "$(subst /,\,$1)"
CPDIR = if exist "$(subst /,\,$1)" xcopy /E /I /Y /Q "$(subst /,\,$1)" "$(subst /,\,$2)" >nul
CPHDR = copy /Y "$(subst /,\,$1)\*.h" "$(subst /,\,$2)\" >nul
RMDIR = if exist "$(subst /,\,$1)" rmdir /s /q "$(subst /,\,$1)"
else
LIB_LINKS := -lglfw3 -lassimp -lzlibstatic -lGL -lm -lpthread -ldl -lX11
EXE :=
PICFLAG := -fPIC
LIB_NAME := libmgengine.so
IMPLIB :=
SHAREDFLAGS := -shared
APP_LIBS := -L$(CONF_DIR) -lmgengine -lm -Wl,-rpath,'$$ORIGIN'
RES_LIB :=
RES_EDITOR :=
RES_PLAYER :=
PLAYER_LDFLAGS :=
MKDIR = mkdir -p $1
CPDIR = test -d "$1" && { mkdir -p "$2" && cp -r "$1"/. "$2"/; } || true
CPHDR = cp $1/*.h "$2"/
RMDIR = rm -rf $1
endif
# set by the `release` target (via a recursive $(MAKE) ... RELEASE=1). Picks the
# production flags here so the object cache under build/release/obj/ is built
# with them -- no shared cache, no wipe.
ifdef RELEASE
CFLAGS := $(RELEASE_CFLAGS)
CXXFLAGS := $(RELEASE_CXXFLAGS)
SHAREDFLAGS += -s -Wl,--gc-sections
APP_EXTRA := -s -Wl,--gc-sections
else
APP_EXTRA :=
endif
# every source/*.c is engine code (the editor app lives in editor/); the desktop
# platform file is #included by mge_core.c, not compiled on its own. The engine
# has no C++ of its own -- Dear ImGui + editor/mge_gui.cpp are the editor's (see
# the editor target below). The DLL is still linked with g++ because the bundled
# Assimp is C++.
CSOURCES = $(wildcard $(SOURCE_DIR)/*.c)
# glad: vendored source compiled straight into the engine
GLAD_SRC = $(VENDOR)/glad/glad.c
# Dear ImGui (editor only)
IMGUI_SRC = $(wildcard $(VENDOR)/imgui/*.cpp)
IMGUI_OBJ = $(patsubst $(VENDOR)/imgui/%.cpp,$(BUILD_OBJ_DIR)/imgui/%.o,$(IMGUI_SRC))
COBJECTS = $(patsubst $(SOURCE_DIR)/%.c,$(BUILD_OBJ_DIR)/%.o,$(CSOURCES)) \
$(BUILD_OBJ_DIR)/glad.o
# editor objects: its plain-C sources + the one C++ unit (Dear ImGui backend)
EDITOR_SRC = $(wildcard editor/*.c)
EDITOR_C_OBJ = $(patsubst editor/%.c,$(BUILD_OBJ_DIR)/editor/%.o,$(EDITOR_SRC))
EDITOR_GUI_OBJ = $(BUILD_OBJ_DIR)/editor/mge_gui.o
ENGINE_LIB = $(CONF_DIR)/$(LIB_NAME)
APP = $(CONF_DIR)/editor$(EXE)
PLAYER = $(CONF_DIR)/mgeplayer$(EXE)
.PHONY: all lib release clean clean-obj vendor vendor-glfw vendor-assimp vendor-clean test make_build_dir
all: make_build_dir $(APP) $(PLAYER)
lib: make_build_dir $(ENGINE_LIB)
# production build: -O2 -DNDEBUG, stripped, dead code removed. Its own object
# cache + outputs under build/release/, so `make` (debug) and `make release`
# coexist -- run both to have both.
release:
$(MAKE) all RELEASE=1
# create <conf>/obj, stage runtime data + the public headers next to the app so
# it runs -- and so an editor project can point its compile_flags.txt here for
# scene-script IntelliSense -- straight from <conf>/
make_build_dir:
$(call MKDIR,$(BUILD_OBJ_DIR))
$(call MKDIR,$(CONF_DIR)/include)
$(call CPHDR,$(SOURCE_DIR),$(CONF_DIR)/include)
$(call CPDIR,shaders,$(CONF_DIR)/shaders)
$(call CPDIR,assets,$(CONF_DIR)/assets)
# --- engine library ---
$(ENGINE_LIB): $(COBJECTS) $(RES_LIB)
$(LINK) $(CFLAGS) $(SHAREDFLAGS) -o $@ $(COBJECTS) $(RES_LIB) $(LIB_DIR) $(LIB_LINKS)
$(BUILD_OBJ_DIR):
$(call MKDIR,$(BUILD_OBJ_DIR))
# Win32 resources (VERSIONINFO + manifest) -> COFF the linker embeds. windres
# runs the C preprocessor over the .rc, so -I resources resolves version.rc.h /
# app.manifest. POSIX: RES_* are empty and this rule is never triggered.
$(BUILD_OBJ_DIR)/%.res: resources/%.rc resources/version.rc.h resources/app.manifest | $(BUILD_OBJ_DIR)
$(WINDRES) $(WINDRESFLAGS) $< $@
# -MMD -MP writes a .d beside each .o listing the headers it #included, so editing
# a header (e.g. mge.h, which changes struct sizes) rebuilds every dependent .o.
# Without this a stale object cache silently mixes ABIs -> memory corruption.
DEPFLAGS = -MMD -MP
$(BUILD_OBJ_DIR)/%.o: $(SOURCE_DIR)/%.c | $(BUILD_OBJ_DIR)
$(CC) $(CPPFLAGS) $(CFLAGS) $(DEPFLAGS) $(PICFLAG) $(INCLUDES) -c $< -o $@
$(BUILD_OBJ_DIR)/glad.o: $(GLAD_SRC) | $(BUILD_OBJ_DIR)
$(CC) $(CPPFLAGS) $(CFLAGS) $(DEPFLAGS) $(PICFLAG) $(INCLUDES) -c $< -o $@
-include $(COBJECTS:.o=.d)
-include $(IMGUI_OBJ:.o=.d) $(EDITOR_C_OBJ:.o=.d) $(EDITOR_GUI_OBJ:.o=.d)
# vendored Dear ImGui -- warnings silenced, own object subdir. Editor-only.
$(BUILD_OBJ_DIR)/imgui:
$(call MKDIR,$(BUILD_OBJ_DIR)/imgui)
$(BUILD_OBJ_DIR)/imgui/%.o: $(VENDOR)/imgui/%.cpp | $(BUILD_OBJ_DIR)/imgui
$(CXX) $(CPPFLAGS) -std=c++17 -O2 -w -ffunction-sections -fdata-sections $(DEPFLAGS) $(PICFLAG) $(EDITOR_INCLUDES) -c $< -o $@
# --- editor app: plain-C editor sources + the one C++ unit (mge_gui.cpp, the
# Dear ImGui backend) + Dear ImGui; linked with g++. The player and every
# example/test stay pure C -- none of this is in libmgengine. ---
$(BUILD_OBJ_DIR)/editor:
$(call MKDIR,$(BUILD_OBJ_DIR)/editor)
$(BUILD_OBJ_DIR)/editor/%.o: editor/%.c $(wildcard editor/*.h) $(wildcard $(SOURCE_DIR)/*.h) | $(BUILD_OBJ_DIR)/editor
$(CC) $(CPPFLAGS) $(CFLAGS) $(DEPFLAGS) -I$(SOURCE_DIR) -Ieditor -I$(MLIB) -c $< -o $@
$(EDITOR_GUI_OBJ): editor/mge_gui.cpp editor/mge_gui.h $(wildcard $(SOURCE_DIR)/*.h) | $(BUILD_OBJ_DIR)/editor
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(DEPFLAGS) $(EDITOR_INCLUDES) -c $< -o $@
# bake the C++ / gcc runtimes into editor.exe so it needs only libmgengine +
# system DLLs. libmgengine.dll also baked its own libgcc (for Assimp), so its
# import lib re-exports a few unwinder shims (_Unwind_Resume, ...) that collide
# with the editor's copy -- --allow-multiple-definition keeps the editor's, which
# is what its own code was compiled against. (SEH unwinding is OS-level; the two
# thin shims are interchangeable.)
EDITOR_STATIC = -static-libgcc -static-libstdc++ -Wl,--allow-multiple-definition
$(APP): $(EDITOR_C_OBJ) $(EDITOR_GUI_OBJ) $(IMGUI_OBJ) $(RES_EDITOR) $(ENGINE_LIB)
$(CXX) $(CFLAGS) $(EDITOR_C_OBJ) $(EDITOR_GUI_OBJ) $(IMGUI_OBJ) $(RES_EDITOR) -o $@ $(EDITOR_STATIC) $(APP_LIBS) $(APP_EXTRA) -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive,-Bdynamic
# --- standalone player: runs a built project. Reuses the editor's data layer
# (no GUI); what `Build Release` ships as <name>.exe.
PLAYER_SRC = runtime/player.c editor/scene.c editor/scene_io.c editor/project.c \
editor/project_io.c editor/pathutil.c editor/editor_camera.c editor/scene_runtime.c
$(PLAYER): $(PLAYER_SRC) $(wildcard editor/*.h) $(wildcard $(SOURCE_DIR)/*.h) $(RES_PLAYER) $(ENGINE_LIB)
$(CC) $(CPPFLAGS) $(CFLAGS) -I$(SOURCE_DIR) -Ieditor -I$(MLIB) $(PLAYER_SRC) $(RES_PLAYER) -o $@ $(APP_LIBS) $(APP_EXTRA) $(PLAYER_LDFLAGS)
vendor: vendor-glfw vendor-assimp
vendor-glfw:
cd $(VENDOR)/glfw/source && \
cmake -S . -B ../build -DCMAKE_INSTALL_PREFIX=../ -DGLFW_BUILD_EXAMPLES=OFF -DGLFW_BUILD_TESTS=OFF -DGLFW_BUILD_DOCS=OFF && \
cmake --build ../build && cmake --install ../build
# OBJ + glTF2 + FBX importers only, no exporters/tools/tests -> small static lib
vendor-assimp:
cd $(VENDOR)/assimp/source && \
cmake -S . -B ../build -DCMAKE_INSTALL_PREFIX=../ \
-DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DASSIMP_BUILD_ZLIB=ON \
-DASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT=OFF \
-DASSIMP_BUILD_OBJ_IMPORTER=ON -DASSIMP_BUILD_GLTF_IMPORTER=ON -DASSIMP_BUILD_FBX_IMPORTER=ON \
-DASSIMP_NO_EXPORT=ON -DASSIMP_BUILD_ASSIMP_TOOLS=OFF -DASSIMP_BUILD_SAMPLES=OFF \
-DASSIMP_BUILD_TESTS=OFF -DASSIMP_INSTALL=ON -DASSIMP_WARNINGS_AS_ERRORS=OFF && \
cmake --build ../build --config Release && cmake --install ../build --config Release
# drop everything `make vendor` produced; the committed source trees stay put
vendor-clean:
$(call RMDIR,$(VENDOR)/glfw/lib)
$(call RMDIR,$(VENDOR)/glfw/build)
$(call RMDIR,$(VENDOR)/assimp/lib)
$(call RMDIR,$(VENDOR)/assimp/lib64)
$(call RMDIR,$(VENDOR)/assimp/bin)
$(call RMDIR,$(VENDOR)/assimp/include)
$(call RMDIR,$(VENDOR)/assimp/share)
$(call RMDIR,$(VENDOR)/assimp/build)
$(call RMDIR,$(VENDOR)/assimp/source/build)
test:
$(MAKE) -C test
clean-obj:
$(call RMDIR,$(BUILD_OBJ_DIR))
clean:
$(MAKE) -C test clean
$(call RMDIR,$(BUILD_DIR))