-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
251 lines (210 loc) · 6.2 KB
/
Makefile
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
# options
builddir =
mode = debug
sanitize = true
lto = false
std =
args =
target = gnu
O = 0
prefix =
empty :=
define newline
${empty}
endef
CPPFLAGS =
CFLAGS =
LDFLAGS =
DIRS = src
BUILDDIRS = ${addprefix ${builddir}/,${DIRS}}
SRC = ${subst /,${SEP},${wildcard src/*.c}}
OBJ = ${SRC:%.c=${builddir}${SEPSH}%${OBJEXT}}
DEP = ${OBJ:${OBJEXT}=.d}
EXEC = ${builddir}${SEP}shader-viewer${OUTEXT}
SDL_DIR = libs${SEP}SDL
SDL_BUILD_DIR = ${builddir}${SEP}libs${SEP}SDL
SDL_TTF_DIR = libs${SEP}SDL_ttf
SDL_TTF_BUILD_DIR = ${builddir}${SEP}libs${SEP}SDL_ttf
LIBS =
DLLS =
TRASH = ${builddir}${SEP}compile_commands.json
ifeq (${target},gnu)
SEP = /
SEPSH = /
CC = gcc
OUTEXT =
ifeq (${mode},debug)
builddir = build-gnu-debug
else
builddir = build-gnu-release
endif
LIBS += ${SDL_BUILD_DIR}/libSDL3.so ${SDL_TTF_BUILD_DIR}/libSDL3_ttf.so
prefix := LD_LIBRARY_PATH="${SDL_BUILD_DIR}:${SDL_TTF_BUILD_DIR}" ${prefix}
else ifeq (${target},msvc)
SEP = \${empty}
SEPSH = \\
sanitize = false
lto = false
CC = cl.exe /nologo
OUTEXT = .exe
ifeq (${mode},debug)
builddir = build-msvc-debug
LIBS += ${SDL_BUILD_DIR}${SEP}Debug${SEP}SDL3.dll ${SDL_TTF_BUILD_DIR}${SEP}Debug${SEP}SDL3_ttf.dll
else
builddir = build-msvc-release
LIBS += ${SDL_BUILD_DIR}${SEP}Release${SEP}SDL3.dll ${SDL_TTF_BUILD_DIR}${SEP}Release${SEP}SDL3_ttf.dll
endif
else ifeq (${target},web)
SEP = /
SEPSH = /
sanitize = false
CC = emcc
OUTEXT = .html
TRASH += ${builddir}/sdl3.wasm ${builddir}/sdl3.js ${builddir}/sdl3.data
LDFLAGS += --embed-file files -s FULL_ES2=1 --shell-file shell.html
ifeq (${mode},debug)
builddir = build-web-debug
LDFLAGS += --emrun
else
builddir = build-web-release
endif
LIBS += ${SDL_BUILD_DIR}/libSDL3.a ${SDL_TTF_BUILD_DIR}/libSDL3_ttf.a
else
$(error "Unknown target: ${target}")
endif
# flags
ifeq (${target},msvc)
std = clatest
OBJEXT = .obj
OUTARG = /Fe:
OBJARG = /c /Fo:
PDB = ${builddir}${SEP}shader-viewer.pdb
TRASH += ${PDB}
RM = erase
CPPFLAGS +=
CFLAGS += ${CPPFLAGS} /std:${std} /O$O
LDFLAGS += /O$O
ifeq (${mode},debug)
CFLAGS += /Zi /Fd${PDB}
LDFLAGS += /Zi /Fd${PDB}
SDL_CMAKE_FLAGS = -DCMAKE_BUILD_TYPE=Debug
O = d
else ifeq (${mode},release)
CFLAGS +=
LDFLAGS +=
SDL_CMAKE_FLAGS = -DCMAKE_BUILD_TYPE=Release
O = 2
else
$(error "Unknown mode: ${mode}")
endif
# link.exe args
LDFLAGS += /link /incremental:no /subsystem:windows
# SDL3
CPPFLAGS += /I${SDL_DIR}${SEP}include /I${SDL_TTF_DIR}${SEP}include
ifeq (${mode},debug)
LDFLAGS += /libpath:${SDL_BUILD_DIR}/Debug /libpath:${SDL_TTF_BUILD_DIR}/Debug
else
LDFLAGS += /libpath:${SDL_BUILD_DIR}/Release /libpath:${SDL_TTF_BUILD_DIR}/Release
endif
DLLS += ${builddir}${SEP}SDL3.dll ${builddir}${SEP}SDL3_ttf.dll
else # gnu-like
std = gnu23
OBJEXT = .o
OUTARG = -o
OBJARG = -c -o
CPPFLAGS += -MMD -MP -D_GNU_SOURCE
CFLAGS += ${CPPFLAGS} -std=${std} -O$O -Wall -Wextra -Wpedantic -Werror
LDFLAGS += -O$O
#SDL3
CPPFLAGS += -I${SDL_DIR}/include -I${SDL_TTF_DIR}/include
LDFLAGS += -L${SDL_BUILD_DIR} -L${SDL_TTF_BUILD_DIR} -lSDL3 -lSDL3_ttf
ifeq (${mode},debug)
CFLAGS += -g3
LDFLAGS += -g3
SDL_CMAKE_FLAGS = -DCMAKE_BUILD_TYPE=Debug
O = 0
else ifeq (${mode},release)
CFLAGS +=
LDFLAGS +=
SDL_CMAKE_FLAGS = -DCMAKE_BUILD_TYPE=Release
O = 2
else
$(error "Unknown mode: ${mode}")
endif
ifeq (${lto},true)
CFLAGS += -flto=auto -fuse-linker-plugin
LDFLAGS += -flto=auto
else ifneq (${lto},false)
${error "lto should be true or false"}
endif
ifeq (${sanitize},true)
CFLAGS += -fsanitize=address,leak,undefined -fno-omit-frame-pointer \
-fsanitize-address-use-after-scope
LDFLAGS += -fsanitize=address,leak,undefined
LIBS += ${builddir}/libs/dlopen/libdlopen.so
TRASH += ${builddir}/libs/dlopen/libdlopen.so
DIRS += ${builddir}/libs/dlopen
prefix := LD_PRELOAD="/usr/lib/libasan.so:${builddir}/libs/dlopen/libdlopen.so" \
ASAN_OPTIONS=strict_string_checks=1:detect_stack_use_after_return=1:\
check_initialization_order=1:strict_init_order=1 \
LSAN_OPTIONS=suppressions=lsan.ignore ${prefix}
else ifneq (${sanitize},false)
${error "sanitize should be true or false"}
endif
endif
all: ${EXEC}
obj: ${OBJ}
bear: clean
mkdir -p ${builddir}
bear --output ${builddir}/compile_commands.json -- make obj
run: ${EXEC} ${DLLS}
ifeq (${target},web)
emrun -- $< ${args}
else
${prefix} $< ${args}
endif
clean:
${RM} ${EXEC} ${OBJ} ${DEP} ${TRASH}
${foreach dir,${BUILDDIRS},if [[ -d ${dir} ]]; then rmdir ${dir}; fi${newline}}
-if [[ -d ${builddir} ]]; then rmdir ${builddir}; fi
clean_sdl: clean
${RM} -r ${SDL_BUILD_DIR}
${RM} -r ${SDL_TTF_BUILD_DIR}
-if [[ -d ${builddir}/libs ]]; then rmdir ${builddir}/libs; fi
.PHONY: all obj bear run clean clean_sdl
${EXEC}: ${OBJ} | ${LIBS}
${CC} ${OUTARG} $@ $^ ${LDFLAGS}
${builddir}${SEPSH}%${OBJEXT}: %.c
ifeq (${target},msvc)
if not exist ${dir $@}${SEP} mkdir ${dir $@}
else
mkdir -p ${dir $@}
endif
${CC} ${CFLAGS} ${OBJARG} $@ $<
${builddir}${SEP}SDL3.dll: ${SDL_BUILD_DIR}${SEP}Debug${SEP}SDL3.dll
copy $< $@
${builddir}${SEP}SDL3_ttf.dll: ${SDL_TTF_BUILD_DIR}${SEP}Debug${SEP}SDL3_ttf.dll
copy $< $@
${SDL_BUILD_DIR}${SEP}Debug${SEP}SDL3.dll ${SDL_BUILD_DIR}${SEP}Release${SEP}SDL3.dll:
cmake -S ${SDL_DIR} -B ${SDL_BUILD_DIR} ${SDL_CMAKE_FLAGS}
cmake --build ${SDL_BUILD_DIR}
${SDL_BUILD_DIR}${SEP}libSDL3.so:
cmake -S ${SDL_DIR} -B ${SDL_BUILD_DIR} ${SDL_CMAKE_FLAGS}
+cmake --build ${SDL_BUILD_DIR}
${SDL_BUILD_DIR}/libSDL3.a:
emcmake cmake -S ${SDL_DIR} -B ${SDL_BUILD_DIR} ${SDL_CMAKE_FLAGS}
+emmake make -C ${SDL_BUILD_DIR}
${SDL_TTF_BUILD_DIR}${SEP}Debug${SEP}SDL3_ttf.dll ${SDL_TTF_BUILD_DIR}${SEP}Release${SEP}SDL3_ttf.dll:
cmake -S ${SDL_TTF_DIR} -B ${SDL_TTF_BUILD_DIR} ${SDL_CMAKE_FLAGS} -DSDL3_DIR=${abspath ${SDL_BUILD_DIR}} -DSDLTTF_VENDORED=True
cmake --build ${SDL_TTF_BUILD_DIR}
${SDL_TTF_BUILD_DIR}/libSDL3_ttf.so: | ${SDL_BUILD_DIR}/libSDL3.so
cmake -S ${SDL_TTF_DIR} -B ${SDL_TTF_BUILD_DIR} ${SDL_CMAKE_FLAGS}
+cmake --build ${SDL_TTF_BUILD_DIR}
${SDL_TTF_BUILD_DIR}/libSDL3_ttf.a: | ${SDL_BUILD_DIR}/libSDL3.a
#cd ${SDL_TTF_DIR}; ./external/download.sh
emcmake cmake -S ${SDL_TTF_DIR} -B ${SDL_TTF_BUILD_DIR} ${SDL_CMAKE_FLAGS} -DSDL3_DIR="${PWD}/${SDL_BUILD_DIR}" -DSDLTTF_VENDORED=True -DBUILD_SHARED_LIBS=OFF
+emmake make -C ${SDL_TTF_BUILD_DIR}
${builddir}/libs/dlopen/libdlopen.so: libs/dlopen/dlopen.c
@mkdir -p ${dir $@}
${CC} -fpic --shared -o $@ $< -ldl
-include ${DEP}