-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakefile
More file actions
240 lines (200 loc) · 7.32 KB
/
Copy pathmakefile
File metadata and controls
240 lines (200 loc) · 7.32 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
#
# App: WeatherFlow Tempest UDP Relay
# Author: Mirco Caramori
# Copyright: (c) 2020 Mirco Caramori
# Repository: https://github.com/padus/tempest
#
# Description: application builder
#
# Usage: make run run release version build/relese/project
# make release (or just make) build release version build/relese/project -> bin/project
# make debug build development version build/debug/project
# make syntax FILE=./src/foo.cpp check the syntax of $(FILE)
# make clean clean or reset the building environment
#
# Environment: Linux -> gcc apt install build-essential gdb
# Windows -> gcc install mingw-w64 and either run mingw-w64.bat or add mingw/bin to the PATH
# copy mingw/bin/mingw32-make.exe to mingw/bin/make.exe
# Windows -> msvc install VS Build Tools and run VsDevCmd.bat (i.e. VsDevCmd.bat -arch=x64)
# Windows -> bash commands using git/usr/bin
#
# Notes: - only variables between dotted lines should need to be customized
# - filenames and directories cannot have spaces
# - directories must not end with /
# - extensions must begin with .
#
# Project: project/
# |
# |-- src/
# | |
# | |-- precomp.hpp
# | |-- *.hpp
# | -- *.cpp
# |
# |-- bin/<os>_<cpu>/
# | |
# | -- project (from build/release)
# |
# -- build/<os>_<cpu>/
# |
# |-- release/
# | |
# | |-- *.o
# | -- project
# |
# -- debug/
# |
# |-- precomp.hpp.gch
# |-- *.o
# -- project
#
#
# Functions
#
rwildcard = $(foreach d,$(wildcard $(1:=/*)),$(call rwildcard,$d,$2)$(filter $(subst *,%,$2),$d))
#
# Misc
#
define NEWLINE
endef
HASH := \#
#
# Variables
#
#-------------------------------------------------------------------------------------------------------------------------------
PROJECT := tempest
PRECOMP := system
ifeq ($(OS),Windows_NT)
UNAME := /dev/git/usr/bin/uname
TR := /dev/git/usr/bin/tr
ECHO := /dev/git/usr/bin/echo
RM := /dev/git/usr/bin/rm
CP := /dev/git/usr/bin/cp
MKDIR := /dev/git/usr/bin/mkdir
OS := windows
CC := msvc
else
UNAME := uname
TR := tr
ECHO := echo
RM := rm
CP := cp
MKDIR := mkdir
OS := $(shell $(UNAME) -s | $(TR) A-Z a-z)
CC := gcc
endif
#-------------------------------------------------------------------------------------------------------------------------------
CPU := $(shell $(UNAME) -m | $(TR) A-Z a-z)
HDR_EXT := .hpp
SRC_EXT := .cpp
ifeq ($(CC),msvc)
PCH_EXT := .pch
else
PCH_EXT := $(HDR_EXT).gch
endif
ifeq ($(OS),windows)
OBJ_EXT := .obj
EXE_EXT := .exe
else
OBJ_EXT := .o
EXE_EXT :=
endif
SRC_DIR := src
BIN_DIR := bin/$(OS)_$(CPU)
REL_DIR := build/$(OS)_$(CPU)/release
DBG_DIR := build/$(OS)_$(CPU)/debug
HDR_LST := $(sort $(call rwildcard,$(SRC_DIR),*$(HDR_EXT)))
SRC_LST := $(sort $(call rwildcard,$(SRC_DIR),*$(SRC_EXT)))
DIR_LST := $(patsubst %/,%,$(dir $(SRC_LST)))
REL_LST := $(sort $(REL_DIR) $(patsubst $(SRC_DIR)%,$(REL_DIR)%,$(DIR_LST)))
DBG_LST := $(sort $(DBG_DIR) $(patsubst $(SRC_DIR)%,$(DBG_DIR)%,$(DIR_LST)))
ifeq ($(CC),msvc)
#
# cl options: https://docs.microsoft.com/en-us/cpp/build/reference/compiler-options-listed-by-category
# https://docs.microsoft.com/en-us/cpp/build/reference/compiler-options-listed-alphabetically
# link options: https://docs.microsoft.com/en-us/cpp/build/reference/linker-options
#
#-----------------------------------------------------------------------------------------------------------------------------
REL_CFL := -std:c++17 -TP -EHsc -nologo -O2 -I$(SRC_DIR) -DNDEBUG
REL_LFL := -nologo
DBG_CFL := -std:c++17 -TP -EHsc -nologo -Zi -Wall -I$(DBG_DIR) -I$(SRC_DIR)
DBG_LFL := -nologo -debug
#-----------------------------------------------------------------------------------------------------------------------------
REL_CMP = cl $(REL_CFL) -c -Fo$@ $<
REL_LNK = link $(REL_LFL) -out:$@ $^
DBG_PCH = $(ECHO) "$(HASH)include <$(PRECOMP)$(HDR_EXT)>" > $(DBG_DIR)/$(PRECOMP)$(SRC_EXT)$(NEWLINE) \
cl $(DBG_CFL) -Yc$(PRECOMP)$(HDR_EXT) -Fd$(DBG_DIR)/ -Fp$(DBG_DIR)/$(PRECOMP)$(PCH_EXT) -Fo$(DBG_DIR)/$(PRECOMP)$(OBJ_EXT) -c $(DBG_DIR)/$(PRECOMP)$(SRC_EXT)$(NEWLINE) \
$(RM) -f $(DBG_DIR)/$(PRECOMP)$(SRC_EXT)
DBG_SYN = cl $(DBG_CFL) -Yu$(PRECOMP)$(HDR_EXT) -Fd$(DBG_DIR)/ -Fp$(DBG_DIR)/$(PRECOMP)$(PCH_EXT) -Zs $(FILE)
DBG_CMP = cl $(DBG_CFL) -Yu$(PRECOMP)$(HDR_EXT) -Fd$(DBG_DIR)/ -Fp$(DBG_DIR)/$(PRECOMP)$(PCH_EXT) -Fo$@ -c $<
DBG_LNK = link $(DBG_LFL) -out:$@ $^ $(DBG_DIR)/$(PRECOMP)$(OBJ_EXT)
else
#
# gcc/g++ options: https://gcc.gnu.org/onlinedocs/gcc/Invoking-GCC.html
#
#-----------------------------------------------------------------------------------------------------------------------------
REL_CFL := -std=c++17 -pthread -O3 -I$(SRC_DIR) -DNDEBUG
REL_LFL := -pthread -lcurl
DBG_CFL := -std=c++17 -pthread -ggdb -I$(DBG_DIR) -I$(SRC_DIR)
DBG_LFL := -pthread -lcurl
#-----------------------------------------------------------------------------------------------------------------------------
REL_CMP = g++ $(REL_CFL) -c $< -o $@
REL_LNK = g++ $(REL_LFL) $^ -o $@
DBG_PCH = g++ $(DBG_CFL) -x c++-header $< -o $@
DBG_SYN = g++ $(DBG_CFL) -fsyntax-only $(FILE)
DBG_CMP = g++ $(DBG_CFL) -c $< -o $@
DBG_LNK = g++ $(DBG_LFL) $^ -o $@
endif
#
# Dependencies & Tasks
#
.PHONY: all run release debug syntax clean info
# default build
all: release
# release: runs
run: $(REL_DIR)/$(PROJECT)$(EXE_EXT)
$<
# release: build
release: $(BIN_DIR)/$(PROJECT)$(EXE_EXT)
# release: copy release exe to bin folder
$(BIN_DIR)/$(PROJECT)$(EXE_EXT): $(REL_DIR)/$(PROJECT)$(EXE_EXT) | $(BIN_DIR)
$(CP) -f $< $@
# release: link
$(REL_DIR)/$(PROJECT)$(EXE_EXT): $(patsubst $(SRC_DIR)/%$(SRC_EXT),$(REL_DIR)/%$(OBJ_EXT),$(SRC_LST)) | $(REL_DIR)
$(REL_LNK)
# release: compile
$(REL_DIR)/%$(OBJ_EXT): $(SRC_DIR)/%$(SRC_EXT) $(HDR_LST) | $(REL_LST)
$(REL_CMP)
# debug: build
debug: $(DBG_DIR)/$(PROJECT)$(EXE_EXT)
# debug: link
$(DBG_DIR)/$(PROJECT)$(EXE_EXT): $(patsubst $(SRC_DIR)/%$(SRC_EXT),$(DBG_DIR)/%$(OBJ_EXT),$(SRC_LST)) | $(DBG_DIR)
$(DBG_LNK)
# debug: compile
$(DBG_DIR)/%$(OBJ_EXT): $(SRC_DIR)/%$(SRC_EXT) $(HDR_LST) $(DBG_DIR)/$(PRECOMP)$(PCH_EXT) | $(DBG_LST)
$(DBG_CMP)
# syntax test only
syntax: $(HDR_LST) $(DBG_DIR)/$(PRECOMP)$(PCH_EXT) | $(DBG_DIR)
$(DBG_SYN)
# debug: precomp
$(DBG_DIR)/$(PRECOMP)$(PCH_EXT): $(SRC_DIR)/$(PRECOMP)$(HDR_EXT) | $(DBG_DIR)
$(DBG_PCH)
# clean or reset build
clean: | $(REL_DIR) $(DBG_DIR) $(BIN_DIR)
$(RM) -fr $(REL_DIR)/* $(DBG_DIR)/* $(BIN_DIR)/*
# directory factory
$(REL_LST) $(DBG_LST) $(BIN_DIR):
$(MKDIR) -p $@
# makefile debug helper
info:
$(info $(HDR_LST))
$(info $(SRC_LST))
$(info $(DIR_LST))
$(info $(REL_LST))
$(info $(DBG_LST))
$(info $(OS))
#
# Recycle Bin
#
# $(warning OS=$(OS))
# EOF