-
Notifications
You must be signed in to change notification settings - Fork 0
/
GNUMakeFile.py
466 lines (426 loc) · 13.7 KB
/
GNUMakeFile.py
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
v1 = """ #-----------------------------------------------------------------------------
# this GNU-makefile relies on the GCC toolchain
# nb: on Windows, the Mingw-w64 package provides the mingw32-make.exe command
# ( see http://www.enib.fr/~harrouet/s4prc/#environ.windows )
#~~~~ control global settings ~~~~
# make opt=2 --> enable optimisation, then disable debug, but keep symbols
# make opt=1 --> enable optimisation, then disable debug
# make opt=0 --> disable optimisation, then enable debug
opt=0
# make clang=1 --> use clang/clang++, not gcc/g++
# make clang=0 --> use gcc/g++, not clang/clang++
clang=0
# make wasm=1 --> target webassembly rather than the native platform
# make wasm=0 --> target the native platform rather than webassembly
wasm=0
#~~~~ build library or programs ~~~~
# if LIB_TARGET is provided, this library will be built (with its
# platform-specific name), otherwise ${EXE_PREFIX}* programs will be built
LIB_TARGET=
EXE_PREFIX=prog
#~~~~ detect operating system ~~~~
ifneq (${OS},Windows_NT)
ifneq (,${findstring Microsoft,${shell cat /proc/version 2>/dev/null}})
# Windows-Subsystem-for-Linux
OS:=WSL
else ifneq (,${findstring Ubuntu,${shell lsb_release -i 2>/dev/null}})
OS:=Ubuntu
else ifneq (,${findstring Raspbian,${shell lsb_release -i 2>/dev/null}})
# Standard distribution for Raspberry-Pi
OS:=Raspbian
else
OS:=${strip ${shell uname -s}}
endif
endif
#~~~~ adjust project-specific settings ~~~~
CPPFLAGS=
# CPPFLAGS+=-I header/path
LDFLAGS=
# LDFLAGS+=-L library/path -Wl,-rpath,library/path -l library_name
CFLAGS=
CXXFLAGS=
BINFLAGS=
ifeq (${OS},Windows_NT)
# nothing special for now
else
ifneq (,${strip ${LIB_TARGET}})
BINFLAGS+=-fPIC
endif
ifeq (${OS},Darwin)
# nothing special for now
else
ifeq (${OS},Ubuntu)
# sanitizer requires gold-linker on Ubuntu
LDFLAGS+=-fuse-ld=gold
else ifeq (${OS},Raspbian)
# some warnings may appear when mixing g++-6 and g++-7 on Raspbian
CXXFLAGS+=-Wno-psabi
else
# nothing special for now
endif
endif
endif
#~~~~ adjust platform-specific features (Posix/Windows/Emscripten...) ~~~~
ifeq (${OS},Windows_NT)
LIB_PREFIX=
LIB_SUFFIX=.dll
EXE_SUFFIX=.exe
SKIP_LINE=echo.
REMOVE=del /q
else
LIB_PREFIX=lib
ifeq (${OS},Darwin)
LIB_SUFFIX=.dylib
else
LIB_SUFFIX=.so
endif
EXE_SUFFIX=
SKIP_LINE=echo
REMOVE=rm -rf
endif
ifeq (${strip ${wasm}},1)
LIB_PREFIX:=lib
LIB_SUFFIX:=.bc
EXE_SUFFIX:=.html
endif
#~~~~ deduce file names ~~~~
ifneq (,${strip ${LIB_TARGET}})
LIB_TARGET:=${LIB_PREFIX}${strip ${LIB_TARGET}}${LIB_SUFFIX}
MAIN_C_FILES=
MAIN_CXX_FILES=
else
LIB_TARGET:=
MAIN_C_FILES=${wildcard ${strip ${EXE_PREFIX}}*.c}
MAIN_CXX_FILES=${wildcard ${strip ${EXE_PREFIX}}*.cpp}
endif
COMMON_C_FILES=${filter-out ${MAIN_C_FILES},${wildcard *.c}}
COMMON_CXX_FILES=${filter-out ${MAIN_CXX_FILES},${wildcard *.cpp}}
#
MAIN_OBJECT_FILES=${sort ${patsubst %.c,%.o,${MAIN_C_FILES}} \
${patsubst %.cpp,%.o,${MAIN_CXX_FILES}}}
COMMON_OBJECT_FILES=${sort ${patsubst %.c,%.o,${COMMON_C_FILES}} \
${patsubst %.cpp,%.o,${COMMON_CXX_FILES}}}
OBJECT_FILES=${MAIN_OBJECT_FILES} ${COMMON_OBJECT_FILES}
DEPEND_FILES=${patsubst %.o,%.d,${OBJECT_FILES}}
EXE_FILES=${sort ${patsubst %.c,%${EXE_SUFFIX},${MAIN_C_FILES}} \
${patsubst %.cpp,%${EXE_SUFFIX},${MAIN_CXX_FILES}}}
#
GENERATED_FILES=${DEPEND_FILES} ${OBJECT_FILES} ${LIB_TARGET} ${EXE_FILES}
ifeq (${OS},Darwin)
GENERATED_FILES+=${wildcard *.dSYM}
endif
ifeq (${strip ${wasm}},1)
GENERATED_FILES+=${patsubst %.html,%.js,${EXE_FILES}}
GENERATED_FILES+=${patsubst %.html,%.html.mem,${EXE_FILES}}
GENERATED_FILES+=${patsubst %.html,%.wasm,${EXE_FILES}}
GENERATED_FILES+=${patsubst %.html,%.wast,${EXE_FILES}}
endif
GENERATED_FILES+=${wildcard output_* *~}
#~~~~ compiler/linker settings ~~~~
CPPFLAGS+=-MMD -pedantic -Wall -Wextra -Wconversion
CPPFLAGS+=-Wno-unused -Wno-unused-parameter -Werror -Wfatal-errors
CFLAGS+=-std=c99 -Wc++-compat -Wwrite-strings -Wold-style-definition -Wvla
CXXFLAGS+=-std=c++17
CXXFLAGS+=-Wno-missing-braces -Wno-sign-conversion
LDFLAGS+=
BINFLAGS+=
ifeq (${strip ${wasm}},1)
CC=emcc
CXX=em++
CPPFLAGS+=-Wno-dollar-in-identifier-extension
LDFLAGS+=-s ALLOW_MEMORY_GROWTH=1
else ifeq (${strip ${clang}},1)
CC=clang
CXX=clang++
else
CC=gcc
CXX=g++
endif
#
ifneq (,${strip ${MAIN_CXX_FILES} ${COMMON_CXX_FILES}})
# force c++ link if there is at least one c++ source file
LD:=${CXX}
else
LD:=${CC}
endif
#~~~~ debug/optimisation settings ~~~~
ifneq (${strip ${opt}},0)
CPPFLAGS+=-DNDEBUG
BINFLAGS+=-O3 -ffast-math
# BINFLAGS+=-fopt-info-vec-optimized
ifneq (${strip ${wasm}},1)
BINFLAGS+=-march=native
endif
ifeq (${strip ${opt}},2)
# optimise but keep symbols for profiling
BINFLAGS+=-g -fno-omit-frame-pointer
else
BINFLAGS+=-fomit-frame-pointer
endif
else
CPPFLAGS+=-UNDEBUG
BINFLAGS+=-g -O0
ifeq (${strip ${wasm}},1)
# sanitizer is not available yet with Emscripten
else ifeq (${OS},Windows_NT)
# sanitizer is not available yet on Windows
else ifeq (${OS},WSL)
# sanitizer is not available yet on Windows-Subsystem-for-Linux
else
BINFLAGS+=-fsanitize=address,undefined
ifeq (${OS},Raspbian)
# dynamic sanitizer libraries may not be found on Raspbian
BINFLAGS+=-static-libasan -static-libubsan
endif
endif
endif
#~~~~ main target ~~~~
all : ${EXE_FILES} ${LIB_TARGET}
rebuild : clean all
.SUFFIXES:
.SECONDARY:
.PHONY: all clean rebuild
#~~~~ linker command to produce the library (if any) ~~~~
${LIB_TARGET} : ${COMMON_OBJECT_FILES}
@echo ==== linking [opt=${opt}] $@ ====
${LD} -shared -o $@ $^ ${BINFLAGS} ${LDFLAGS}
@${SKIP_LINE}
#~~~~ linker command to produce the executable files (if any) ~~~~
%${EXE_SUFFIX} : %.o ${COMMON_OBJECT_FILES}
@echo ==== linking [opt=${opt}] $@ ====
${LD} -o $@ $^ ${BINFLAGS} ${LDFLAGS}
@${SKIP_LINE}
#~~~~ compiler command for every source file ~~~~
%.o : %.c
@echo ==== compiling [opt=${opt}] $< ====
${CC} -o $@ $< -c ${BINFLAGS} ${CPPFLAGS} ${CFLAGS}
@${SKIP_LINE}
%.o : %.cpp
@echo ==== compiling [opt=${opt}] $< ====
${CXX} -o $@ $< -c ${BINFLAGS} ${CPPFLAGS} ${CXXFLAGS}
@${SKIP_LINE}
-include ${DEPEND_FILES}
#~~~~ remove generated files ~~~~
clean :
@echo ==== cleaning ====
${REMOVE} ${GENERATED_FILES}
@${SKIP_LINE}
#-----------------------------------------------------------------------------"""
v2 = """ #-----------------------------------------------------------------------------
# this GNU-makefile relies on the GCC toolchain
# nb: on Windows, the Mingw-w64 package provides the mingw32-make.exe command
# ( see http://www.enib.fr/~harrouet/s4prc/#environ.windows )
#~~~~ control global settings ~~~~
# make opt=2 --> enable optimisation, then disable debug, but keep symbols
# make opt=1 --> enable optimisation, then disable debug
# make opt=0 --> disable optimisation, then enable debug
opt=0
# make clang=1 --> use clang/clang++, not gcc/g++
# make clang=0 --> use gcc/g++, not clang/clang++
clang=0
# make wasm=1 --> target webassembly rather than the native platform
# make wasm=0 --> target the native platform rather than webassembly
wasm=0
#~~~~ build library or programs ~~~~
# if LIB_TARGET is provided, this library will be built (with its
# platform-specific name), otherwise ${EXE_PREFIX}* programs will be built
LIB_TARGET=
EXE_PREFIX=prog
#~~~~ detect operating system ~~~~
ifneq (${OS},Windows_NT)
ifneq (,${findstring Ubuntu,${shell lsb_release -i 2>/dev/null}})
OS:=Ubuntu
else ifneq (,${findstring Raspbian,${shell lsb_release -i 2>/dev/null}})
# Standard distribution for Raspberry-Pi
OS:=Raspbian
else
OS:=${strip ${shell uname -s}}
endif
ifneq (,${findstring Microsoft,${shell cat /proc/version 2>/dev/null}})
# Windows-Subsystem-for-Linux
OS:=${OS}_WSL
endif
endif
#-- location where SFML header files and libraries are installed --
ifneq (,${wildcard /home/TP/sujets/SFML-2.2})
# ENIB specific installation directory
SFMLDIR=/home/TP/sujets/SFML-2.2
else
# just in case SFML is installed in the local directory
SFMLDIR=./SFML-2.2
endif
#~~~~ adjust project-specific settings ~~~~
CPPFLAGS=
# CPPFLAGS+=-I header/path
CPPFLAGS+=-I ${SFMLDIR}/include
LDFLAGS=
# LDFLAGS+=-L library/path -Wl,-rpath,library/path -l library_name
# LDFLAGS+=-L ${SFMLDIR}/lib -Wl,-rpath,${SFMLDIR}/lib \
# -lsfml-graphics -lsfml-window -lsfml-system
CFLAGS=
CXXFLAGS=
BINFLAGS=
ifneq (,${findstring Windows_NT,${OS}})
# nothing special for now
else
ifneq (,${strip ${LIB_TARGET}})
BINFLAGS+=-fPIC
endif
ifneq (,${findstring Darwin,${OS}})
# nothing special for now
else
ifneq (,${findstring Ubuntu,${OS}})
# sanitizer sometimes requires gold-linker on Ubuntu
LDFLAGS+=-fuse-ld=gold
else ifneq (,${findstring Raspbian,${OS}})
# some warnings may appear when mixing g++-6 and g++-7 on Raspbian
CXXFLAGS+=-Wno-psabi
else
# nothing special for now
endif
endif
endif
#~~~~ adjust platform-specific features (Posix/Windows/Emscripten...) ~~~~
ifneq (,${findstring Windows_NT,${OS}})
LIB_PREFIX=
LIB_SUFFIX=.dll
EXE_SUFFIX=.exe
SKIP_LINE=echo.
REMOVE=del /q
else
LIB_PREFIX=lib
ifneq (,${findstring Darwin,${OS}})
LIB_SUFFIX=.dylib
else
LIB_SUFFIX=.so
endif
EXE_SUFFIX=
SKIP_LINE=echo
REMOVE=rm -rf
endif
ifeq (${strip ${wasm}},1)
LIB_PREFIX:=lib
LIB_SUFFIX:=.bc
EXE_SUFFIX:=.html
endif
#~~~~ deduce file names ~~~~
ifneq (,${strip ${LIB_TARGET}})
LIB_TARGET:=${LIB_PREFIX}${strip ${LIB_TARGET}}${LIB_SUFFIX}
MAIN_C_FILES=
MAIN_CXX_FILES=
else
LIB_TARGET:=
MAIN_C_FILES=${wildcard ${strip ${EXE_PREFIX}}*.c}
MAIN_CXX_FILES=${wildcard ${strip ${EXE_PREFIX}}*.cpp}
endif
COMMON_C_FILES=${filter-out ${MAIN_C_FILES},${wildcard *.c}}
COMMON_CXX_FILES=${filter-out ${MAIN_CXX_FILES},${wildcard *.cpp}}
#
MAIN_OBJECT_FILES=${sort ${patsubst %.c,%.o,${MAIN_C_FILES}} \
${patsubst %.cpp,%.o,${MAIN_CXX_FILES}}}
COMMON_OBJECT_FILES=${sort ${patsubst %.c,%.o,${COMMON_C_FILES}} \
${patsubst %.cpp,%.o,${COMMON_CXX_FILES}}}
OBJECT_FILES=${MAIN_OBJECT_FILES} ${COMMON_OBJECT_FILES}
DEPEND_FILES=${patsubst %.o,%.d,${OBJECT_FILES}}
EXE_FILES=${sort ${patsubst %.c,%${EXE_SUFFIX},${MAIN_C_FILES}} \
${patsubst %.cpp,%${EXE_SUFFIX},${MAIN_CXX_FILES}}}
#
GENERATED_FILES=${DEPEND_FILES} ${OBJECT_FILES} ${LIB_TARGET} ${EXE_FILES}
ifneq (,${findstring Darwin,${OS}})
GENERATED_FILES+=${wildcard *.dSYM}
endif
ifeq (${strip ${wasm}},1)
GENERATED_FILES+=${patsubst %.html,%.js,${EXE_FILES}}
GENERATED_FILES+=${patsubst %.html,%.html.mem,${EXE_FILES}}
GENERATED_FILES+=${patsubst %.html,%.wasm,${EXE_FILES}}
GENERATED_FILES+=${patsubst %.html,%.wast,${EXE_FILES}}
endif
GENERATED_FILES+=${wildcard output_* *~}
#~~~~ compiler/linker settings ~~~~
CPPFLAGS+=-MMD -pedantic -Wall -Wextra -Wconversion
CPPFLAGS+=-Wno-unused -Wno-unused-parameter -Werror -Wfatal-errors
CFLAGS+=-std=c99 -Wc++-compat -Wwrite-strings -Wold-style-definition -Wvla
CXXFLAGS+=-std=c++17
CXXFLAGS+=-Wno-missing-braces -Wno-sign-conversion
LDFLAGS+=
BINFLAGS+=
ifeq (${strip ${wasm}},1)
CC=emcc
CXX=em++
CPPFLAGS+=-Wno-dollar-in-identifier-extension
LDFLAGS+=-s ALLOW_MEMORY_GROWTH=1
else ifeq (${strip ${clang}},1)
CC=clang
CXX=clang++
else
CC=gcc
CXX=g++
endif
#
ifneq (,${strip ${MAIN_CXX_FILES} ${COMMON_CXX_FILES}})
# force c++ link if there is at least one c++ source file
LD:=${CXX}
else
LD:=${CC}
endif
#~~~~ debug/optimisation settings ~~~~
ifneq (${strip ${opt}},0)
CPPFLAGS+=-DNDEBUG
BINFLAGS+=-O3 -ffast-math
# BINFLAGS+=-fopt-info-vec-optimized
ifneq (${strip ${wasm}},1)
BINFLAGS+=-march=native
endif
ifeq (${strip ${opt}},2)
# optimise but keep symbols for profiling
BINFLAGS+=-g -fno-omit-frame-pointer
else
BINFLAGS+=-fomit-frame-pointer
endif
else
CPPFLAGS+=-UNDEBUG
BINFLAGS+=-g -O0
ifeq (${strip ${wasm}},1)
# sanitizer is not available yet with Emscripten
else ifneq (,${findstring Windows_NT,${OS}})
# sanitizer is not available yet on Windows
else
BINFLAGS+=-fsanitize=address,undefined
ifneq (,${findstring Raspbian,${OS}})
# dynamic sanitizer libraries may not be found on Raspbian
BINFLAGS+=-static-libasan -static-libubsan
endif
endif
endif
#~~~~ main target ~~~~
all : ${EXE_FILES} ${LIB_TARGET}
rebuild : clean all
.SUFFIXES:
.SECONDARY:
.PHONY: all clean rebuild
#~~~~ linker command to produce the library (if any) ~~~~
${LIB_TARGET} : ${COMMON_OBJECT_FILES}
@echo ==== linking [opt=${opt}] $@ ====
${LD} -shared -o $@ $^ ${BINFLAGS} ${LDFLAGS}
@${SKIP_LINE}
#~~~~ linker command to produce the executable files (if any) ~~~~
%${EXE_SUFFIX} : %.o ${COMMON_OBJECT_FILES}
@echo ==== linking [opt=${opt}] $@ ====
${LD} -o $@ $^ ${BINFLAGS} ${LDFLAGS}
@${SKIP_LINE}
#~~~~ compiler command for every source file ~~~~
%.o : %.c
@echo ==== compiling [opt=${opt}] $< ====
${CC} -o $@ $< -c ${BINFLAGS} ${CPPFLAGS} ${CFLAGS}
@${SKIP_LINE}
%.o : %.cpp
@echo ==== compiling [opt=${opt}] $< ====
${CXX} -o $@ $< -c ${BINFLAGS} ${CPPFLAGS} ${CXXFLAGS}
@${SKIP_LINE}
-include ${DEPEND_FILES}
#~~~~ remove generated files ~~~~
clean :
@echo ==== cleaning ====
${REMOVE} ${GENERATED_FILES}
@${SKIP_LINE}
#-----------------------------------------------------------------------------"""