-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
578 lines (495 loc) · 16.7 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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
############################################################
# Makefile template for small/meduim C projects.
# Please follow recommended project layout in README.
# Author : Raphael CAUSSE
############################################################
## Target executable name
TARGET_EXE := prog
ifeq ($(OS),Windows_NT) # For Windows
TARGET_EXE := $(addsuffix .exe,$(TARGET_EXE))
endif
########################## PATHS ###########################
## Directories path set up.
DIR_BIN := bin
DIR_BUILD := build
DIR_SRC := src
## Sources list file, where all source files to compile should be declared, within the "src/" directory.
SOURCES_FILE_MK := sources.mk
## Include directories, indicate where to search for included header files. It should contain the space-separated list of directories where header files can be found.
INCLUDES_DIRS :=
## OS specific includes directories.
INCLUDES_DIRS_LINUX :=
INCLUDES_DIRS_WINDOWS :=
ifeq ($(OS),Windows_NT) # For Windows
INCLUDES_DIRS += $(INCLUDES_DIRS_WINDOWS)
else # For Linux
INCLUDES_DIRS += $(subst /,\,$(INCLUDES_DIRS_LINUX))
endif
## Libraries to link, given by their name without the "lib" prefix (libm => m). It should contain the space-separated list of libraries that are used by your programs.
LIBRAIRIES :=
## OS specific libraries.
LIBRAIRIES_LINUX :=
LIBRAIRIES_WINDOWS :=
ifeq ($(OS),Windows_NT) # For Windows
LIBRAIRIES += $(LIBRAIRIES_WINDOWS)
else # For Linux
LIBRAIRIES += $(LIBRAIRIES_LINUX)
endif
## Libraries directories, specify the linker where to search for the libraries. It should contain the space-separated list of directories where libraries can be found.
LIBRARIES_DIRS :=
## OS specific libraries directories.
LIBRARIES_DIRS_LINUX :=
LIBRARIES_DIRS_WINDOWS :=
ifeq ($(OS),Windows_NT) # For Windows
LIBRARIES_DIRS += $(LIBRARIES_DIRS_WINDOWS)
else # For Linux
LIBRARIES_DIRS += $(subst /,\,$(LIBRARIES_DIRS_LINUX))
endif
######################### COMPILER #########################
## Compiler and linker.
CC := gcc
## Compiler options.
CFLAGS := -pedantic -Wall -Wpedantic
## Preprocessor options.
CPPFLAGS := $(addprefix -I,$(INCLUDES_DIRS))
## Linker options.
LDFLAGS := $(addprefix -l,$(LIBRAIRIES)) $(addprefix -L,$(LIBRARIES_DIRS))
## Source files and object files set up.
-include $(SOURCES_FILE_MK)
SRC_FILES.c := $(filter %.c,$(addprefix $(DIR_SRC)/,$(filter-out \,$(SOURCES))))
OBJ_FILES := $(addsuffix .o,$(basename $(filter-out \,$(SOURCES))))
## Release build set up.
DIR_BIN_RELEASE := $(DIR_BIN)/release
DIR_BUILD_RELEASE := $(DIR_BUILD)/release
RELEASE_TARGET := $(DIR_BIN_RELEASE)/$(TARGET_EXE)
RELEASE_FLAGS := -O2
RELEASE_OBJS := $(addprefix $(DIR_BUILD_RELEASE)/,$(OBJ_FILES))
## Debug build set up.
DIR_BIN_DEBUG := $(DIR_BIN)/debug
DIR_BUILD_DEBUG := $(DIR_BUILD)/debug
DEBUG_TARGET := $(DIR_BIN_DEBUG)/$(TARGET_EXE)
DEBUG_FLAGS := -O0 -g3
DEBUG_OBJS := $(addprefix $(DIR_BUILD_DEBUG)/,$(OBJ_FILES))
########################## SHELL ###########################
ifeq ($(OS),Windows_NT) # For Windows
MKDIR := mkdir
RM := del /f/q
RM_RF := rmdir /S/Q
CP := copy /y
else # For Linux
MKDIR := mkdir -p
RM := rm -f
RM_RF := rm -rf
CP := cp
endif
########################## RULES ###########################
#### Default build.
default: debug
#### Initialize project layout
.PHONY: init
init:
@echo ================================== Initialize ==================================
@echo Initializing project layout...
ifeq ($(filter $(DIR_SRC),$(wildcard *)),)
ifeq ($(OS),Windows_NT) # For Windows
@echo Create directory "$(DIR_SRC)"
@$(MKDIR) $(subst /,\,$(DIR_SRC))
else # For Linux
@echo " Create directory '$(DIR_SRC)'"
@$(MKDIR) $(DIR_SRC)
endif
endif
ifeq ($(filter $(SOURCES_FILE_MK),$(wildcard $(SOURCES_FILE_MK))),)
ifeq ($(OS),Windows_NT) # For Windows
@echo Create file "$(subst /,\,$(SOURCES_FILE_MK))"
@echo # Declare all sources files in the SOURCES variable, within the "src/" directory.> $(subst /,\,$(SOURCES_FILE_MK))
@echo # Write source files names on one line separated by a space, or on multiple lines by adding a backslash at the end and going on a new line (no other characters after the backslash).>> $(subst /,\,$(SOURCES_FILE_MK))
@echo SOURCES := \>> $(subst /,\,$(SOURCES_FILE_MK))
else # For Linux
@echo " Create file '$(SOURCES_FILE_MK)'"
@echo "# Declare all sources files in the SOURCES variable, within the 'src/' directory." > $(SOURCES_FILE_MK)
@echo "# Write sources files names on one line separated by a space, or on multiple lines by adding a backslash at the end and going on a new line (no other characters after the backslash).">> $(SOURCES_FILE_MK)
@echo "SOURCES := \">> $(SOURCES_FILE_MK)
endif
endif
@echo Completed.
ifeq ($(OS),Windows_NT) # For Windows
@echo.
else # For Linux
@echo
endif
ifeq ($(OS),Windows_NT) # For Windows
@echo Declare all sources files in "$(subst /,\,$(SOURCES_FILE_MK))".
else # For Linux
@echo "Declare all sources files in '$(SOURCES_FILE_MK)'."
endif
@echo ================================================================================
ifeq ($(OS),Windows_NT) # For Windows
@echo.
else # For Linux
@echo
endif
#### Check existing project directories and create missing directories
.PHONY: check_directories
check_directories:
@echo ================================= Directories ==================================
@echo Checking project directories...
ifeq ($(filter $(DIR_BUILD_RELEASE),$(wildcard $(DIR_BUILD)/*)),)
ifeq ($(OS),Windows_NT) # For Windows
@echo Create directory "$(DIR_BUILD_RELEASE)"
@$(MKDIR) $(subst /,\,$(DIR_BUILD_RELEASE))
else # For Linux
@echo " Create directory '$(DIR_BUILD_RELEASE)'"
@$(MKDIR) $(DIR_BUILD_RELEASE)
endif
endif
ifeq ($(filter $(DIR_BIN_RELEASE),$(wildcard $(DIR_BIN)/*)),)
ifeq ($(OS),Windows_NT) # For Windows
@echo Create directory "$(DIR_BIN_RELEASE)"
@$(MKDIR) $(subst /,\,$(DIR_BIN_RELEASE))
else # For Linux
@echo " Create directory '$(DIR_BIN_RELEASE)'"
@$(MKDIR) $(DIR_BIN_RELEASE)
endif
endif
ifeq ($(filter $(DIR_BUILD_DEBUG),$(wildcard $(DIR_BUILD)/*)),)
ifeq ($(OS),Windows_NT) # For Windows
@echo Create directory "$(DIR_BUILD_DEBUG)"
@$(MKDIR) $(subst /,\,$(DIR_BUILD_DEBUG))
else # For Linux
@echo " Create directory '$(DIR_BUILD_DEBUG)'"
@$(MKDIR) $(DIR_BUILD_DEBUG)
endif
endif
ifeq ($(filter $(DIR_BIN_DEBUG),$(wildcard $(DIR_BIN)/*)),)
ifeq ($(OS),Windows_NT) # For Windows
@echo Create directory "$(DIR_BIN_DEBUG)"
@$(MKDIR) $(subst /,\,$(DIR_BIN_DEBUG))
else # For Linux
@echo " Create directory '$(DIR_BIN_DEBUG)'"
@$(MKDIR) $(DIR_BIN_DEBUG)
endif
endif
@echo Completed.
@echo ================================================================================
ifeq ($(OS),Windows_NT) # For Windows
@echo.
else # For Linux
@echo
endif
#### Release pre-build
.PHONY: pre_release release
pre_release:
@echo ================================ Build Release =================================
ifeq ($(filter-out \,$(SOURCES)),)
ifeq ($(OS),Windows_NT) # For Windows
$(error No source files declared in "$(subst /,\,$(SOURCES_FILE_MK))")
else # For Linux
$(error No source files declared in '$(SOURCES_FILE_MK)'.)
endif
else
ifeq ($(OS),Windows_NT) # For Windows
@echo Build configurations...
@echo OS: $(OS)
@echo Compiler: $(CC)
@echo CFLAGS: $(CFLAGS) $(RELEASE_FLAGS)
@echo CPPFLAGS: $(CPPFLAGS)
@echo LDFLAGS: $(LDFLAGS)
@echo.
@echo Building target "$(RELEASE_TARGET)"...
else # For Linux
@echo "Build configurations..."
@echo " OS: $(shell uname)"
@echo " Compiler: $(CC)"
@echo " CFLAGS: $(CFLAGS) $(RELEASE_FLAGS)"
@echo " CPPFLAGS: $(CPPFLAGS)"
@echo " LDFLAGS: $(LDFLAGS)"
@echo
@echo "Building target '$(RELEASE_TARGET)'..."
endif
endif
#### Release build
release: check_directories pre_release $(RELEASE_TARGET)
@echo Completed.
ifeq ($(OS),Windows_NT) # For Windows
@echo.
@echo ***** Build success *****
else # For Linux
@echo
@echo "***** Build success *****"
endif
@echo ================================================================================
ifeq ($(OS),Windows_NT) # For Windows
@echo.
else # For Linux
@echo
endif
#### Link object files for Release target.
$(RELEASE_TARGET): $(RELEASE_OBJS)
ifeq ($(OS),Windows_NT) # For Windows
@echo Link $(words $^) objects into "$(RELEASE_TARGET)"
else # For Linux
@echo " Link $(words $^) objects into '$(RELEASE_TARGET)'"
endif
@$(CC) -o $@ $^ $(LDFLAGS)
#### Compile C source files for Release build.
$(DIR_BUILD_RELEASE)/%.o: $(DIR_SRC)/%.c
ifeq ($(OS),Windows_NT) # For Windows
@if not exist "$(dir $@)" (\
$(MKDIR) "$(subst /,\,$(dir $@))" \
)
@echo Compile "$<" into "$@"
else # For Linux
@$(MKDIR) $(dir $@)
@echo " Compile '$<' into '$@'"
endif
@$(CC) $(CFLAGS) $(CPPFLAGS) $(RELEASE_FLAGS) -o $@ -c $<
#### Debug pre-build
.PHONY: pre_debug debug
pre_debug:
@echo ================================= Build Debug ==================================
ifeq ($(filter-out \,$(SOURCES)),)
ifeq ($(OS),Windows_NT) # For Windows
$(error No source files declared in "$(subst /,\,$(SOURCES_FILE_MK))")
else
$(error No source files declared in '$(SOURCES_FILE_MK)'.)
endif
else
ifeq ($(OS),Windows_NT) # For Windows
@echo Build configurations...
@echo OS: $(OS)
@echo Compiler: $(CC)
@echo CFLAGS: $(CFLAGS) $(DEBUG_FLAGS)
@echo CPPFLAGS: $(CPPFLAGS)
@echo LDFLAGS: $(LDFLAGS)
@echo.
@echo Building target "$(DEBUG_TARGET)"...
else # For Linux
@echo "Build configurations..."
@echo " OS: $(shell uname)"
@echo " Compiler: $(CC)"
@echo " CFLAGS: $(CFLAGS) $(DEBUG_FLAGS)"
@echo " CPPFLAGS: $(CPPFLAGS)"
@echo " LDFLAGS: $(LDFLAGS)"
@echo
@echo "Building target '$(DEBUG_TARGET)'..."
endif
endif
#### Debug build
debug: check_directories pre_debug $(DEBUG_TARGET)
@echo Completed.
ifeq ($(OS),Windows_NT) # For Windows
@echo.
@echo ***** Build success *****
else # For Linux
@echo
@echo "***** Build success *****"
endif
@echo ================================================================================
ifeq ($(OS),Windows_NT) # For Windows
@echo.
else # For Linux
@echo
endif
#### Link objects files for Debug target.
$(DEBUG_TARGET): $(DEBUG_OBJS)
ifeq ($(OS),Windows_NT) # For Windows
@echo Link $(words $^) objects into "$(DEBUG_TARGET)"
else # For Linux
@echo " Link $(words $^) objects into '$(DEBUG_TARGET)'"
endif
@$(CC) -o $@ $^ $(LDFLAGS)
#### Compile C source files for Debug build.
$(DIR_BUILD_DEBUG)/%.o: $(DIR_SRC)/%.c
ifeq ($(OS),Windows_NT) # For Windows
@if not exist "$(dir $@)" (\
$(MKDIR) "$(subst /,\,$(dir $@))" \
)
@echo Compile "$<" into "$@"
else # For Linux
@$(MKDIR) $(dir $@)
@echo " Compile '$<' into '$@'"
endif
@$(CC) $(CFLAGS) $(CPPFLAGS) $(DEBUG_FLAGS) -o $@ -c $<
#### Clean generated files, remove objects and targets.
.PHONY: clean
clean:
@echo ==================================== Clean =====================================
@echo Cleaning objects...
ifneq ($(wildcard $(DIR_BUILD_RELEASE)/*),)
ifeq ($(OS),Windows_NT) # For Windows
@echo Delete objects in "$(DIR_BUILD_RELEASE)" directory
@$(RM_RF) $(subst /,\,$(DIR_BUILD_RELEASE))
@$(MKDIR) $(subst /,\,$(DIR_BUILD_RELEASE))
else # For Linux
@echo " Delete objects in '$(DIR_BUILD_RELEASE)' directory"
@$(RM_RF) $(DIR_BUILD_RELEASE)
@$(MKDIR) $(DIR_BUILD_RELEASE)
endif
endif
ifneq ($(wildcard $(DIR_BUILD_DEBUG)/*),)
ifeq ($(OS),Windows_NT) # For Windows
@echo Delete objects in "$(DIR_BUILD_DEBUG)" directory
@$(RM_RF) $(subst /,\,$(DIR_BUILD_DEBUG))
@$(MKDIR) $(subst /,\,$(DIR_BUILD_DEBUG))
else # For Linux
@echo " Delete objects in '$(DIR_BUILD_DEBUG)' directory"
@$(RM_RF) $(DIR_BUILD_DEBUG)
@$(MKDIR) $(DIR_BUILD_DEBUG)
endif
endif
@echo Completed.
ifeq ($(OS),Windows_NT) # For Windows
@echo.
else # For Linux
@echo
endif
@echo Cleaning targets...
ifneq ($(filter $(RELEASE_TARGET),$(wildcard $(DIR_BIN_RELEASE)/*)),)
ifeq ($(OS),Windows_NT) # For Windows
@echo Delete target "$(RELEASE_TARGET)"
@$(RM) $(subst /,\,$(RELEASE_TARGET))
else # For Linux
@echo " Delete target '$(RELEASE_TARGET)'"
@$(RM) $(RELEASE_TARGET)
endif
endif
ifneq ($(filter $(DEBUG_TARGET),$(wildcard $(DIR_BIN_DEBUG)/*)),)
ifeq ($(OS),Windows_NT) # For Windows
@echo Delete target "$(DEBUG_TARGET)"
@$(RM) $(subst /,\,$(DEBUG_TARGET))
else # For Linux
@echo " Delete target '$(DEBUG_TARGET)'"
@$(RM) $(DEBUG_TARGET)
endif
endif
@echo Completed.
@echo ================================================================================
ifeq ($(OS),Windows_NT) # For Windows
@echo.
else # For Linux
@echo
endif
#### Run the release target
.PHONY: run
run:
@echo ============================== Run Release Target ==============================
ifneq ($(filter $(RELEASE_TARGET),$(wildcard $(DIR_BIN_RELEASE)/*)),)
-@$(RELEASE_TARGET) $(ARGS)
else
ifeq ($(OS),Windows_NT) # For Windows
$(warning Try to compile using the command "make release".)
$(error No executable "$(subst /,\,$(RELEASE_TARGET))" found)
else # For Linux
$(warning Try to compile using the command "make release".)
$(error No executable "$(RELEASE_TARGET)" found)
endif
endif
#### Run the debug target
.PHONY: rund
rund:
@echo =============================== Run Debug Target ===============================
ifneq ($(filter $(DEBUG_TARGET),$(wildcard $(DIR_BIN_DEBUG)/*)),)
-@$(DEBUG_TARGET) $(ARGS)
else
ifeq ($(OS),Windows_NT) # For Windows
$(warning Try to compile using the command "make debug".)
$(error No executable "$(subst /,\,$(DEBUG_TARGET))" found)
else # For Linux
$(warning Try to compile using the command "make debug".)
$(error No executable "$(DEBUG_TARGET)" found)
endif
endif
#### Display project files informations.
.PHONY: info
info:
@echo ================================ Project Infos =================================
ifeq ($(OS),Windows_NT) # For Windows
@echo Build configurations...
@echo OS: $(OS)
@echo Compiler: $(CC)
@echo CFLAGS: $(CFLAGS)
@echo CPPFLAGS: $(CPPFLAGS)
@echo LDFLAGS: $(LDFLAGS)
@echo.
else # For Linux
@echo "Build configurations..."
@echo " OS: $(shell uname)"
@echo " Compiler: $(CC)"
@echo " CFLAGS: $(CFLAGS)"
@echo " CPPFLAGS: $(CPPFLAGS)"
@echo " LDFLAGS: $(LDFLAGS)"
@echo
endif
ifeq ($(OS),Windows_NT) # For Windows
@echo C source files (.c) :
ifneq ($(SRC_FILES.c),)
@echo $(subst /,\,$(SRC_FILES.c))
endif
@echo.
@echo Object files (.o) :
ifneq ($(OBJ_FILES),)
@echo $(subst /,\,$(OBJ_FILES))
endif
else # For Linux
@echo "C source files (.c) :"
ifneq ($(SRC_FILES.c),)
@echo " $(SRC_FILES.c)"
endif
@echo
@echo Object files (.o) :
ifneq ($(OBJ_FILES),)
@echo " $(OBJ_FILES)"
endif
endif
@echo ================================================================================
ifeq ($(OS),Windows_NT) # For Windows
@echo.
else # For Linux
@echo
endif
#### Display usage help.
.PHONY: help
help:
@echo ===================================== Help =====================================
ifeq ($(OS),Windows_NT) # For Windows
@echo Usage:
@echo make Build project, in default mode (Debug).
@echo make release Build project, in Release mode.
@echo make debug Build project, in Debug mode.
@echo make run Run release target "$(RELEASE_TARGET)".
@echo make rund Run debug target "$(DEBUG_TARGET)".
@echo make clean Clean generated files, remove objects and targets.
@echo make info Display infos about project.
@echo make help Display this help message.
@echo make version Display compilers version.
else # For Linux
@echo "Usage:"
@echo " make Build project, in default mode (Debug)."
@echo " make release Build project, in Release mode."
@echo " make debug Build project, in Debug mode."
@echo " make run Run release target '$(RELEASE_TARGET)'."
@echo " make rund Run debug target '$(DEBUG_TARGET)'."
@echo " make clean Clean generated files, remove objects and targets."
@echo " make info Display infos about project."
@echo " make help Display this help message."
@echo " make version Display compiler version."
endif
@echo ================================================================================
ifeq ($(OS),Windows_NT) # For Windows
@echo.
else # For Linux
@echo
endif
#### Version info
.PHONY: version
version:
@echo ================================== C Compiler ==================================
@$(CC) --version
@$(CC) -v
@echo ================================================================================
ifeq ($(OS),Windows_NT) # For Windows
@echo.
else # For Linux
@echo
endif