forked from phkehl/ubloxcfg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.mk
272 lines (232 loc) · 8.91 KB
/
build.mk
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
########################################################################################################################
#
# u-blox 9 positioning receivers configuration library and tool
#
# Copyright (c) 2020 Philippe Kehl (flipflip at oinkzwurgl dot org),
# https://oinkzwurgl.org/hacking/ubloxcfg
#
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program.
# If not, see <https://www.gnu.org/licenses/>.
#
########################################################################################################################
########################################################################################################################
# Tools, toolchain, other variables
# Tools
PERL := perl
TOUCH := touch
CP := cp
MKDIR := mkdir
TOUCH := touch
ECHO := echo
CAT := cat
RM := rm
MV := mv
TRUE := true
FALSE := false
DOXYGEN := doxygen
SED := sed
ZIP := zip
VALGRIND := valgrind
XXD := xxd
# Toolchain (prefer environment variables over the defaults here; ignore Make's defaults for CC and LD)
ifeq ($(origin CC),default)
CC = gcc
endif
ifeq ($(origin CXX),default)
CXX = gcc
endif
ifeq ($(origin LD),default)
LD = gcc
endif
NM ?= nm
OBJCOPY ?= objcopy
OBJDUMP ?= objdump
RANLIB ?= ranlib
READELF ?= readelf
STRINGS ?= strings
STRIP ?= strip
CFLAGS ?=
CXXFLAGS ?=
LDFLAGS ?=
SCANBUILD ?= scan-build
# Verbosity helpers
ifeq ($(VERBOSE),1)
V =
V1 =
V2 =
V12 =
RM += -v
MV += -v
CP += -v
MKDIR += -v
else
ZIP += -q
UNZIP += -q
V = @
V1 = > /dev/null
V2 = 2> /dev/null
V12 = 2>&1 > /dev/null
endif
# Colours!
fancyterm := true
ifeq ($(TERM),dumb)
fancyterm := false
endif
ifeq ($(TERM),)
fancyterm := false
endif
ifneq ($(MSYSTEM),)
fancyterm := false
endif
ifeq ($(fancyterm),true)
HLR="\\e[31m"
HLG="\\e[32m"
HLGG="\\e[1\;32m"
HLY="\\e[33m"
HLM="\\e[35m"
HLC="\\e[36m"
HLO="\\e[m"
else
HLR=
HLG=
HLGG=
HLY=
HLM=
HLC=
HLO=
endif
########################################################################################################################
# Canned recipes to generate compile and link rules
make_deps := Makefile $(sort $(wildcard *.mk))
# Make rules for one target
define makeTarget
# 1) prog-build target name: "rtknav-release", "str2str-debug", ...
# 2) list of .c and .cpp files for the program
# 3) CFLAGS
# 4) CXXFLAGS
# 5) LDFLAGS
# $ (info makeTarget: [$(strip $1)] [$2])
# Make rules for compiling each source file, add to list of object files
OFILES_$(strip $1) :=
$$(foreach src, $$(filter %.c, $$(sort $2)), $$(eval $$(call makeCompileRuleC, $1, $$(src), $(BUILDDIR)/$(strip $1))))
$$(foreach src, $$(filter %.cpp, $$(sort $2)), $$(eval $$(call makeCompileRuleCxx, $1, $$(src), $(BUILDDIR)/$(strip $1))))
# Build (object) directory for this target (e.g. "build/rtknav-release")
$(BUILDDIR)/$(strip $1): | $(BUILDDIR)
$(V)$(MKDIR) $$@
# CFLAGS and CXXFLAGS for this target, add -I flags for this target for all directories
CFLAGS_$(strip $1) := $3 $(addprefix -I,$(sort $(dir $2))) -I$(BUILDDIR)
CXXFLAGS_$(strip $1) := $4 $(addprefix -I,$(sort $(dir $2))) -I$(BUILDDIR)
# LDFLAGS for this target
LDFLAGS_$(strip $1) := $5
# Link objects into executable
$(OUTPUTDIR)/$(strip $1): $$(OFILES_$(strip $1)) | $(OUTPUTDIR)
@echo "$(HLY)*$(HLO) $(HLC)LD$(HLO) $(HLGG)$$@$(HLO)"
$(V)$(CC) -o $$@ $$(OFILES_$(strip $1)) $(LDFLAGS) $$(LDFLAGS_$(strip $1))
# Create pure binary (output/foo-release -> build/foo-release/foo-release.bin)
$(BUILDDIR)/$(strip $1)/$(strip $1).bin: $(OUTPUTDIR)/$(strip $1)
@echo "$(HLY)*$(HLO) $(HLC)OBJCOPY$(HLO) $(HLG)$$@$(HLO) $(HLM)($$<)$(HLO)"
$(V)$(RM) -f $$@
$(V)$(OBJCOPY) $$< -O binary [email protected]
$(V)$(MV) [email protected] $$@
# Create listing (output/foo-release -> build/foo-release/foo-release.lst)
# $(BUILDDIR)/$(strip $1)/$(strip $1).lst: $(OUTPUTDIR)/$(strip $1)
# @echo "$(HLY)*$(HLO) $(HLC)OBJDUMP$(HLO) $(HLG)$$@$(HLO) $(HLM)($$<)$(HLO)"
# $(V)$(RM) -f $$@
# $(V)$(OBJDUMP) -d -S $$< > [email protected]
# $(V)$(MV) [email protected] $$@
# Create dump of all sections (output/foo-release -> build/foo-release/foo-release.dump)
$(BUILDDIR)/$(strip $1)/$(strip $1).dump: $(OUTPUTDIR)/$(strip $1)
@echo "$(HLY)*$(HLO) $(HLC)READELF$(HLO) $(HLG)$$@$(HLO) $(HLM)($$<)$(HLO)"
$(V)$(RM) -f $$@
$(V)$(READELF) --wide --section-headers $$< >> [email protected]
$(V)$(READELF) --wide --file-header $$< >> [email protected]
$(V)for section in $$$$($(PERL) -ne 'if (/^\s*\[\s*\d+\s*\]\s+(\S+)/ && ($$$$1 !~ m{^(NULL|\.debug_.+)$$$$})) { print "$$$$1\n"; }' < [email protected]); do \
$(READELF) --wide --hex-dump="$$$$section" $$<; \
done >> [email protected]
$(V)$(MV) [email protected] $$@
# Create dump of preprocessor defines
$(BUILDDIR)/$(strip $1)/$(strip $1).defines: $(make_deps) | $(BUILDDIR)/$(strip $1)
@echo "$(HLY)*$(HLO) $(HLC)GEN$(HLO) $(HLG)$$@$(HLO) $(HLM)($(CC))$(HLO)"
$(V)$(RM) -f $$@ [email protected]
$(V)$(ECHO) "// $(CC) $(CFLAGS) $$(CFLAGS_$(strip $1))" >> [email protected]
$(V)$(ECHO) | $(CC) $(CFLAGS) $$(CFLAGS_$(strip $1)) -dM -E - >> [email protected]
$(V)$(MV) [email protected] $$@
# Shortcut for the binary (rtknav-debug = output/rtknav-debug)
.PHONY: $(strip $1)
$(strip $1): $(OUTPUTDIR)/$(strip $1) $(foreach ext, bin dump defines, $(BUILDDIR)/$(strip $1)/$(strip $1).$(ext))
endef
# Make rule to compile single .c file
define makeCompileRuleC
# 1) app-build target name
# 2) .c file
# 3) build dir
# $ (info makeCompileRuleC [$(strip $1)] [$(strip $2)] [$(strip $3)])
# Compile src/foo/bar.c --> build/prog-build/src__foo__bar.o
$(strip $3)/$(subst /,__,$(patsubst %.c,%.o,$2)): $2 $(make_deps) | $3
@echo "$(HLY)*$(HLO) $(HLC)CC$(HLO) $(HLG)$$@$(HLO) $(HLM)($$<)$(HLO)"
$(V)$(CC) -c -o $$@ $(CFLAGS) $$(CFLAGS_$(strip $1)) $$< -MD -MF $$(@:%.o=%.d) -MT $$@
# Add to list of object files for this target
OFILES_$(strip $1) += $(strip $3)/$(subst /,__,$(patsubst %.c,%.o,$2))
endef
# Make rule to compile single .cpp file
define makeCompileRuleCxx
# 1) app-build target name
# 2) .cpp file
# 3) build dir
# $ (info makeCompileRuleCxx [$(strip $1)] [$(strip $2)] [$(strip $3)])
# Compile src/foo/bar.cpp --> build/prog-build/src__foo__bar.o
$(strip $3)/$(subst /,__,$(patsubst %.cpp,%.o,$2)): $2 $(make_deps) | $3
@echo "$(HLY)*$(HLO) $(HLC)CXX$(HLO) $(HLG)$$@$(HLO) $(HLM)($$<)$(HLO)"
$(V)$(CXX) -c -o $$@ $(CXXFLAGS) $$(CXXFLAGS_$(strip $1)) $$< -MD -MF $$(@:%.o=%.d) -MT $$@
# Add to list of object files for this target
OFILES_$(strip $1) += $(strip $3)/$(subst /,__,$(patsubst %.cpp,%.o,$2))
endef
########################################################################################################################
# Generated version
$(BUILDDIR)/std_version_gen.h: util/build/std_version_gen.h.pl $(make_deps) | $(BUILDDIR)
@echo "$(HLY)*$(HLO) $(HLC)GEN$(HLO) $(HLG)$@$(HLO) $(HLM)($<)$(HLO)"
$(V)$(RM) -f $@ [email protected]
$(V)$(PERL) $< >[email protected]
$(V)$(MV) [email protected] $@
src/std/std_version.c: $(BUILDDIR)/std_version_gen.h
########################################################################################################################
# Load dependency files
ifneq ($(MAKECMDGOALS),clean)
-include $(sort $(wildcard $(BUILDDIR)/*/*.d))
endif
########################################################################################################################
# Auxiliary targets
$(BUILDDIR):
$(V)$(MKDIR) $@
$(OUTPUTDIR):
$(V)$(MKDIR) $@
.PHONY: clean
clean:
$(V)$(RM) -rf $(BUILDDIR) $(OUTPUTDIR) core
.PHONY: debugmf
debugmf:
@echo "OUTPUTDIR = $(OUTPUTDIR) ($(origin OUTPUTDIR))"
@echo "BUILDDIR = $(BUILDDIR) ($(origin BUILDDIR))"
@echo "CC = $(CC) ($(origin CC))"
@echo "CXX = $(CXX) ($(origin CXX))"
@echo "LD = $(LD) ($(origin LD))"
@echo "NM = $(NM) ($(origin NM))"
@echo "OBJCOPY = $(OBJCOPY) ($(origin OBJCOPY))"
@echo "OBJDUMP = $(OBJDUMP) ($(origin OBJDUMP))"
@echo "RANLIB = $(RANLIB) ($(origin RANLIB))"
@echo "READELF = $(READELF) ($(origin READELF))"
@echo "STRINGS = $(STRINGS) ($(origin STRINGS))"
@echo "STRIP = $(STRIP) ($(origin STRIP))"
@echo "CFLAGS = $(CFLAGS) ($(origin CFLAGS))"
@echo "CXXFLAGS = $(CXXFLAGS) ($(origin CXXFLAGS))"
@echo "LDFLAGS = $(LDFLAGS) ($(origin LDFLAGS))"
@echo "SCANBUILD = $(SCANBUILD) ($(origin SCANBUILD))"
########################################################################################################################
# eof