This repository has been archived by the owner on Jun 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Makefile
648 lines (531 loc) · 14.8 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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
#
# Intro
#
$(info [ PsychoX' Makefile - version 2.5.3 ])
# Special variables
.SUFFIXES:
NULL :=
SPACE := ${NULL} ${NULL}
${SPACE} := ${SPACE}
TAB := ${NULL} ${NULL}
${TAB} := ${TAB}
define \n
endef
NEWLINE := ${\n}
# More settings file
SETTINGS_FILE := ./settings.mk
# Platform detection
PLATFORM_DETECT := $(shell uname -o | tr A-Z a-z)
ifeq ($(findstring linux,$(PLATFORM_DETECT)),)
SYSTEM_PLATFORM := win
PLATFORM := win
else
SYSTEM_PLATFORM := lin
PLATFORM := lin
endif
# Architecture detection
ARCH_DETECT := $(shell uname -m)
ifeq ($(findstring 64,$(ARCH_DETECT)),)
SYSTEM_ARCH := 32
ARCH := 32
else
SYSTEM_ARCH := 64
ARCH := 64
endif
#
# Target selection/detection
#
# Target normalize function
define normalize_target
# There MUST be so many 'eval's, because Makefile interpreter will try "optimalize" uses of variables...
$(eval override TARGET := $(shell echo $(TARGET) | tr A-Z a-z))
$(eval override TARGET_PLATFORM := $(findstring win,$(TARGET))$(findstring lin,$(TARGET)))
$(eval override TARGET_ARCH := $(findstring 32,$(TARGET))$(findstring 64,$(TARGET)))
$(eval override TARGET:=$(TARGET_PLATFORM)$(TARGET_ARCH))
endef
# Try to get target from settings file if no selected
ifndef TARGET
ifneq ("$(wildcard $(SETTINGS_FILE))","")
TARGET := $(shell grep -Po '^(?:TARGET[ ]*[:]?=[ ]*)\K.*$$' $(SETTINGS_FILE))
endif
endif
# Try to use system platfrom and arch if no selected
ifndef TARGET
TARGET:=$(PLATFORM)$(ARCH)
endif
# Target normalization
ifdef TARGET
$(eval $(call normalize_target))
endif
#
# Default configuration
#
# Output/runtime file name
OUT_NAME := Szczur
RUN_NAME := Szczur
# Run params
PARAMS :=
# Folders structure
INC_DIRS := src
SRC_DIRS := src
TEP_DIRS := src
OBJ_DIR := obj/$(TARGET)
OUT_DIR := out
RUN_DIR := out
# Extensions
SRC_EXTS := .cpp .c
INC_EXTS := .hpp .h
TEP_EXTS := .tpp .t
OBJ_EXT := .o
OUT_EXT := .exe
RUN_EXT := .exe
# Compiler and linker
CXX := g++
CXXFLAGS := -std=c++17 -Wall
LD := g++
LDFLAGS :=
# Using MXE? and its options
MXE := no
MXE_DIR := /usr/lib/mxe
# Packages
PKG_CONFIG := pkg-config
# Arch tags
ARCH_32 := i686
ARCH_64 := x86_64
# Use static linking
LINKING := static
CXXFLAGS_STATIC := -static
CXXFLAGS_DYNAMIC :=
LDFLAGS_STATIC := -static
LDFLAGS_DYNAMIC :=
# Threads model
THREADS := posix
# Optimalization
OPTIMALIZE := no
CXXFLAGS_OPTIMALIZATION := -flto -ffat-lto-objects -O3
LDFLAGS_OPTIMALIZATION := -flto -ffat-lto-objects -O3
# Debbuging
DEBUGGER := none
# Cleaning
ifeq ($(MAKECMDGOALS),clean)
CLEAN_FILES_CMD := rm
CLEAN_FILES := "./$(OBJ_DIR)/*/*$(OBJ_EXT)" "./$(OUT_DIR)/$(OUT_NAME)$(OUT_EXT)"
CLEAN_DIRS_CMD := rm -dr
CLEAN_DIRS := "./$(OBJ_DIR)/*"
endif
# Other tools
LS := ls -AdoGh --time-style long-iso
MKDIR = mkdir -p
# Libraries
LIB_LIST := BOOST IMGUI GLAD SFML LUA SOL2 JSON GLM OPENAL FFMPEG_AV
ifeq ($(TARGET_PLATFORM),win)
LIB_LIST += WINDOWS
endif
# Boost
PKG_CONFIG_NAME_BOOST := boost
CXXFLAGS_STATIC_BOOST :=
LDFLAGS_STATIC_BOOST :=
CXXFLAGS_DYNAMIC_BOOST :=
LDFLAGS_DYNAMIC_BOOST :=
# ImGUI
PKG_CONFIG_NAME_IMGUI := imgui
CXXFLAGS_STATIC_IMGUI :=
LDFLAGS_STATIC_IMGUI := -limgui
CXXFLAGS_DYNAMIC_IMGUI :=
LDFLAGS_DYNAMIC_IMGUI := -limgui
# GLAD
PKG_CONFIG_NAME_GLAD := glad
CXXFLAGS_STATIC_GLAD :=
LDFLAGS_STATIC_GLAD := -lglad
CXXFLAGS_DYNAMIC_GLAD :=
LDFLAGS_DYNAMIC_GLAD := -lglad
# SFML
PKG_CONFIG_NAME_SFML := sfml
CXXFLAGS_STATIC_SFML := -DSFML_STATIC
LDFLAGS_STATIC_SFML := -lsfml-audio-s -lsfml-graphics-s -lsfml-window-s -lsfml-system-s -lopengl32 -lfreetype -ljpeg -lopengl32 -lwinmm -lgdi32 -lopenal32 -lflac -lvorbisenc -lvorbisfile -lvorbis -logg -lws2_32 -lwinmm -DSFML_STATIC
CXXFLAGS_DYNAMIC_SFML :=
LDFLAGS_DYNAMIC_SFML := -lsfml-audio -lsfml-graphics -lsfml-window -lsfml-system -lopengl32 -lfreetype -ljpeg -lopengl32 -lwinmm -lgdi32 -lopenal32 -lflac -lvorbisenc -lvorbisfile -lvorbis -logg -lws2_32 -lwinmm
# Lua
PKG_CONFIG_NAME_LUA := lua
CXXFLAGS_STATIC_LUA :=
LDFLAGS_STATIC_LUA := -llua
CXXFLAGS_DYNAMIC_LUA :=
LDFLAGS_DYNAMIC_LUA := -llua
# Sol2
PKG_CONFIG_NAME_SOL2 := sol2
# JSON
PKG_CONFIG_NAME_JSON := json
# GLM
PKG_CONFIG_NAME_GLM := glm
# OpenAL
PKG_CONFIG_NAME_OPENAL := openal
# FFMPEG/libavformat
PKG_CONFIG_NAME_FFMPEG_AV := libavformat
LDFLAGS_STATIC_FFMPEG_AV := -lavformat -lavcodec -lavutil -lswresample
LDFLAGS_SHARED_FFMPEG_AV := -lavformat -lavcodec -lavutil -lswresample
LDFLAGS += -lswscale
# Windows
LDFLAGS_STATIC_WINDOWS := -lcomdlg32
LDFLAGS_SHARED_WINDOWS := -lcomdlg32
LIB_SUPPRESS_WARNING := yes
# Print definitions
COLORS := no
# Markers to grep code with
MARKERS := @todo @warn @err @debug
# Dependencies finding
DEPFLAGS := -MT $$@ -MMD -MP -MF
DEP_EXT := .d
DEP_TMP_UPDATE_SUFFIX := .tmp
#
# Load settings override
#
# Definition to overwrite if we want add more rules in settings
define more_rules
more_rules:
endef
# Include
ifneq ("$(wildcard $(SETTINGS_FILE))","")
include $(SETTINGS_FILE)
endif
#
# Cleaning related script
#
ifeq ($(MAKECMDGOALS),clean)
# Files and folders to clean out
CLEAN_FILES := $(shell find . -type f -path $(subst $(SPACE), -or -path ,$(CLEAN_FILES)))
CLEAN_DIRS := $(shell find . -type d -path $(subst $(SPACE), -or -path ,$(CLEAN_DIRS)) | tac)
# Cleaning the compile environment
.PHONY: clean
clean:
$(inform_cleaning)
ifeq ($(or $(CLEAN_FILES),$(CLEAN_DIRS),$(CLEAN_FORCE)),)
@echo "Nothing to clean."
else
$(V)-$(CLEAN_FILES_CMD) $(CLEAN_FILES) $(SLIENT) || :
$(V)-$(CLEAN_DIRS_CMD) $(CLEAN_DIRS) || :
@echo "Cleaned."
endif
endif
#
# Compiler selection
#
# Linking
override LINKING := $(shell echo $(LINKING) | tr a-z A-Z)
CXXFLAGS += $(CXXFLAGS_$(LINKING))
LDFLAGS += $( LDFLAGS_$(LINKING))
# Threads mode
override THREADS := $(shell echo $(THREADS) | tr a-z A-Z)
# Default cross compiler
ifeq ($(CROSS),)
CROSS := yes
endif
ifeq ($(CROSS),yes)
# Architecture
CROSS := $(ARCH_$(TARGET_ARCH))
ifeq ($(CROSS),)
$(error "Target architecture not selected propertly... TARGET_ARCH=$(TARGET_ARCH)")
endif
# Platform
# -> Windows...
ifeq ($(TARGET_PLATFORM),win)
# Select MINGW-W64
CROSS := $(CROSS)-w64-mingw32
# MXE support
ifeq ($(MXE),yes)
# Add MXE as cross-target compiling env
MXE_BIN_DIR := $(MXE_DIR)/usr/bin
PATH := $(PATH):$(MXE_BIN_DIR)
# Make shell use the specified in the makefile path
SHELL = env PATH='$(PATH)' /bin/bash
# Select static/shared compilator
ifeq ($(LINKING),STATIC)
CROSS := $(CROSS).static
else
CROSS := $(CROSS).shared
endif
# Select threads mode
ifeq ($(THREADS),POSIX)
CROSS := $(CROSS).posix
endif
endif
# -> Linux...
else ifeq ($(TARGET_PLATFORM),lin)
# Select Linux GNU
CROSS := $(CROSS)-linux-gnu
# -> Invaild platform
else
$(error "Target platform not selected propertly... TARGET_PLATFORM=$(TARGET_PLATFORM)")
endif
CROSS := $(CROSS)-
# If true 32 bit compiler not present, use universal
ifeq ($(shell bash -c "command -v $(CROSS)$(CXX)"),)
CROSS := $(subst i686,x86_64,$(CROSS))
$(warning "Using universal compiler... CROSS=$(CROSS)")
endif
# If no selective compiler, just try use 'g++'...
ifeq ($(shell bash -c "command -v $(CROSS)$(CXX)"),)
CROSS :=
$(warning "Using standard system compiler... (i.e. g++)")
endif
endif
# Use default system compiler
ifeq ($(CROSS),no)
CROSS :=
endif
ifeq ($(CROSS),none)
CROSS :=
endif
# Disabling pkg-config to force use stupid basic system
ifeq ($(PKG_CONFIG),no)
PKG_CONFIG :=
endif
ifeq ($(shell bash -c "command -v $(PKG_CONFIG)"),)
PKG_CONFIG :=
endif
# Select compiler, linker and other tools
CXX := $(CROSS)$(CXX)
LD := $(CROSS)$(LD)
PKG_CONFIG := $(CROSS)$(PKG_CONFIG)
#
# Library flags selection
#
ifeq ($(LIB_SUPPRESS_WARNING),yes)
LIB_INCLUDE_PREFIX := -isystem${ }
else
LIB_INCLUDE_PREFIX := -I
endif
define add_lib
$(eval TMP_CXXFLAGS :=)
$(eval TMP_LDFLAGS :=)
# Detect pkg-config package
$(if $(PKG_CONFIG),
$(eval TMP_CXXFLAGS := $(shell $(PKG_CONFIG) --cflags $(PKG_CONFIG_NAME_$(1)) 2>/dev/null))
$(eval TMP_LDFLAGS := $(shell $(PKG_CONFIG) --libs $(PKG_CONFIG_NAME_$(1)) 2>/dev/null))
)
$(if $(TMP_CXXFLAGS)$(TMP_LDFLAGS),
# Use package flags
$(eval CXXFLAGS += $(TMP_CXXFLAGS))
$(eval LDFLAGS += $(TMP_LDFLAGS))
,
# Use primitive method...
$(eval CXXFLAGS += $(CXXFLAGS_$(LINKING)_$(1)))
$(eval LDFLAGS += $(LDFLAGS_$(LINKING)_$(1)))
$(if $(INC_DIR_$(1)_$(TARGET_ARCH)),
$(eval CXXFLAGS += $(LIB_INCLUDE_PREFIX)$(INC_DIR_$(1)_$(TARGET_ARCH)))
)
$(if $(LIB_DIR_$(1)_$(TARGET_ARCH)),
$(eval LDFLAGS += -L$(LIB_DIR_$(1)_$(TARGET_ARCH)))
)
)
endef
Q := $(foreach LIB_NAME, $(LIB_LIST), $(call add_lib,$(LIB_NAME)))
#
# Other
#
# Replacements for illegal path characters
COLON_REPLACEMENT := _c0loN
PATHBACK_REPLACEMENT := _B4ckP4th
# Selecting target machine architecture
CXXFLAGS += -m$(TARGET_ARCH)
LDFLAGS += -m$(TARGET_ARCH)
# Adding headers and templates directories to search paths
CXXFLAGS += -I$(subst $(SPACE), -I,$(INC_DIRS))
CXXFLAGS += -I$(subst $(SPACE), -I,$(TEP_DIRS))
# Adding optimalization flags
ifeq ($(OPTIMALIZE),yes)
CXXFLAGS += $(CXXFLAGS_OPTIMALIZATION)
LDFLAGS += $(LDFLAGS_OPTIMALIZATION)
endif
# Adding debugger flags
ifeq (DEBUGGER,ggdb)
CXXFLAGS += -ggdb
LDFLAGS += -ggdb
endif
#
# Informing and colors
#
# Verbose flag
ifeq ($(or $(V),$(VERBOSE)),yes)
override V :=
else
override V := @
endif
# Markers grepping function
ifndef grep_markers
grep_markers := @xd(){ grep -i -C1 '$(subst $(SPACE),\|,$(MARKERS))' $$1 || :; }; xd
endif
# Informations
ifeq ($(COLORS),yes)
ifndef inform_executable
inform_executable := @printf "\033[32m[Executable] %s\033[0m \n"
endif
ifndef inform_object
inform_object := @printf "\033[33m[Object] %s\033[0m \n"
endif
ifndef inform_source
inform_source := @printf "\033[34m[Source] %s\033[0m \n"
endif
ifndef inform_header
inform_header := @printf "\033[36m[Header] %s\033[0m \n"
endif
ifndef inform_template
inform_template := @printf "\033[35m[Template] %s\033[0m \n"
endif
ifndef inform_running
inform_running := @printf "\033[37m[Running] \033[0m \n"
endif
ifndef inform_cleaning
inform_cleaning := @printf "\033[37m[Cleaning] \033[0m \n"
endif
else
ifndef inform_executable
inform_executable := @printf "[Executable] %s \n"
endif
ifndef inform_object
inform_object := @printf "[Object] %s \n"
endif
ifndef inform_source
inform_source := @printf "[Source] %s \n"
endif
ifndef inform_header
inform_header := @printf "[Header] %s \n"
endif
ifndef inform_template
inform_template := @printf "[Template] %s \n"
endif
ifndef inform_running
inform_running := @printf "[Running] \n"
endif
ifndef inform_cleaning
inform_cleaning := @printf "[Cleaning] \n"
endif
endif
ifeq ($(COLORS),yes)
CXXFLAGS += -fdiagnostics-color=always
LDFLAGS += -fdiagnostics-color=always
endif
#
# Files searching
#
# Output file to be linked to
OUT_FILE := $(OUT_DIR)/$(OUT_NAME)$(OUT_EXT)
# Runtime file to run for tests
RUN_FILE := $(RUN_DIR)/$(RUN_NAME)$(RUN_EXT)
# List of sources, headers and templates
SOURCES := $(shell find $(SRC_DIRS) -type f -name '*$(subst $(SPACE),' -or -name '*,$(SRC_EXTS))')
HEADERS := $(shell find $(INC_DIRS) -type f -name '*$(subst $(SPACE),' -or -name '*,$(INC_EXTS))')
TEMPLATES := $(shell find $(TEP_DIRS) -type f -name '*$(subst $(SPACE),' -or -name '*,$(TEP_EXTS))')
# List of objects to be linked
OBJECTS := $(subst ..,$(PATHBACK_REPLACEMENT),$(subst :,$(COLON_REPLACEMENT),$(patsubst %,$(OBJ_DIR)/%$(OBJ_EXT),$(SOURCES))))
# List of dependencies files
DEPENDENCIES := $(patsubst %$(OBJ_EXT),%$(DEP_EXT),$(OBJECTS))
#
# Compile recipes
#
# Standard options
.PHONY: default nothing all
defualt: all
nothing:
all: exe more_rules
# Linking
.PHONY: exe
exe: $(OUT_FILE)
$(OUT_FILE): $(OBJECTS)
$(inform_executable) $@
$(V)$(MKDIR) `dirname $@`
$(V)$(LD) $(OBJECTS) -o $@ $(LDFLAGS)
$(V)$(LS) $@
# Compiling
.PHONY: obj
obj: $(OBJECTS)
define recipe
$(eval OBJECT := $(1))
$(eval SOURCE := $(subst $(COLON_REPLACEMENT),:,$(subst $(PATHBACK_REPLACEMENT),..,$(patsubst $(OBJ_DIR)/%$(OBJ_EXT),%,$(OBJECT)))))
$(eval DEPENDENT := $(patsubst %$(OBJ_EXT),%$(DEP_EXT),$(OBJECT)))
$(OBJECT): $(SOURCE) $(DEPENDENT)
$(inform_object) $$@
$(V)$(MKDIR) `dirname $$@`
$(V)$(CXX) -c $$< -o $$@ $(CXXFLAGS) $(DEPFLAGS) $(DEPENDENT)$(DEP_TMP_UPDATE_SUFFIX)
$(V)mv -f $(DEPENDENT)$(DEP_TMP_UPDATE_SUFFIX) $(DEPENDENT) && touch $$@
$(V)$(LS) $$@
endef
$(foreach _, $(OBJECTS), $(eval $(call recipe,$(_))))
#
# Dependencies recipes
#
# Include deps maps
$(DEPENDENCIES): ;
.PRECIOUS: $(DEPENDENCIES)
ifneq ($(DEPENDENCIES),)
include $(wildcard $(DEPENDENCIES))
endif
#
# Code check
#
# @warn przynajmniej narazie xd ;f
#ifneq ($(MARKERS),)
ifeq ($(findstring code,$(MAKECMDGOALS)),code)
# Sources
.PHONY: src $(SOURCES)
src: $(SOURCES)
$(SOURCES):
$(inform_source) $@
$(grep_markers) $@
# Headers
.PHONY: inc $(HEADERS)
inc: $(HEADERS)
$(HEADERS):
$(inform_header) $@
$(grep_markers) $@
# Templates
.PHONY: tep $(TEMPLATES)
tep: $(TEMPLATES)
$(TEMPLATES):
$(inform_template) $@
$(grep_markers) $@
# Code
.PHONY: code
code: src inc tep
endif
#
# Other recipes
#
.PHONY: more_rules
$(eval $(call more_rules))
# Compile and run the output executable
.PHONY: run
run: all
$(inform_running)
$(V)-cp $(OUT_FILE) $(RUN_FILE) 1>&2 2>/dev/null || :
$(V)chmod +x $(RUN_FILE)
$(V)-cd ./$(RUN_DIR) ; ./$(RUN_NAME)$(RUN_EXT) $(PARAMS)
# Informations (for debug)
.PHONY: info echo
info: echo
echo:
@echo ""
@echo "SYSTEM_PLATFORM=$(SYSTEM_PLATFORM) SYSTEM_ARCH=$(SYSTEM_ARCH)"
@echo "TARGET_PLATFORM=$(TARGET_PLATFORM) TARGET_ARCH=$(TARGET_ARCH)"
@echo "MXE=$(MXE) MXE_DIR=$(MXE_DIR)"
@echo ""
@echo "PATH=$(PATH)"
@echo ""
@echo "Compilator:"
@echo "$(shell $(CXX) --version | head -n 1)"
@echo "Flags:"
@echo "$(CXXFLAGS)"
@echo ""
@echo "Linker:"
@echo "$(shell $(LD) --version | head -n 1)"
@echo "Flags:"
@echo "$(LDFLAGS)"
@echo ""
@echo "Sources:"
@echo -e "$(subst $(SPACE),\n,$(SOURCES))"
@echo ""
@echo "Headers:"
@echo -e "$(subst $(SPACE),\n,$(HEADERS))"
@echo ""
@echo "Templates:"
@echo -e "$(subst $(SPACE),\n,$(TEMPLATES))"
@echo ""