-
Notifications
You must be signed in to change notification settings - Fork 72
/
makefile
359 lines (286 loc) · 11.2 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
#------------------------------------------------------------------------------
# NOTE: (Windows) this makefile assumes that the MinGW\bin & MinGW\msys\1.0\bin
# directories are added to the PATH environment variable. (Peter pls note this anciant comment)
#
# Top level MatrixPilot makefile
#
# - This make system depends upon a mostly clean source tree.
# - It is designed to be invoked from a command script for multiple targets.
# - With the exception of source code generators, all build output is
# directed to a binary tree, which will be built.
#
# EXTENDING
# - to add another library (aka, directory) to the build process simply copy
# file 'module.mk' from one of the existing sub-directories and edit to suit.
#
# NOTES:
# - the build system now builds libraries where specified in module.mk
#
#
# SOME SIMPLE MAKE SYNTAX:
# = recursively expanded variable
# := simply expanded variables
#::= simply expanded variables (posix standard)
# ?= conditional variable assignment operator, only has an effect if the variable is not yet defined
#
# DEFINE 'V' ENVIRONMENT VARIABLE IN ORDERR TO BE VERBOSE: (set V=1, set V= to unset (Windows))
Q := $(if $(V),,@)
# Establish some sensible defaults for build system critical variables (if not already defined)
TARGET_NAME ?= MatrixPilot
#SOURCE_DIR ?= ..
DEVICE ?= SIL
# RobD - revist this, what is req.?
#$(if $(filter $(MAKE_VERSION),3.80 3.81 3.90 3.92),,\
# $(error This makefile requires one of GNU make version ….))
# Fetch the makefile directory with a trailing /
ROOT_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
#################################################################
# Do not build targets if make invoked from source tree root
NOMAKE:=show
ifeq (0, $(words $(findstring $(MAKECMDGOALS), $(NOMAKE))))
$(if $(filter $(ROOT_DIR),$(CURDIR)/),\
$(error Please run the makefile from the binary tree, or use the build-all script.))
endif
#################################################################
# Establish the host system build support tools
ifeq ($(OS),Windows_NT)
mkoutdir = $(shell for %%f in ($(subst /,\,$(subst ../,,$(1)))); do [ -d %%f ] || $(MKDIR) %%f)
QT = "
RM = del /Q /F
#MV ?= move /Y
MV ?= mv -f
CP = copy /Y
FIND := C:\MinGW\msys\1.0\bin\find.exe
DOT := "C:\Program Files (x86)\Graphviz2.38\bin\dot.exe"
ifdef ComSpec
SHELL := $(ComSpec)
endif
ifdef COMSPEC
SHELL := $(COMSPEC)
endif
MKDIR := mkdir
else
mkoutdir = $(shell for f in $(subst ../,,$(1)); do [ -d $$f ] || $(MKDIR) $$f; done)
QT = '
RM = rm -rf
MV ?= mv -f
CP = cp -f
FIND := find
DOT := dot
MKDIR := mkdir -p
endif
SED := sed
#TEST := [
space = $(empty) $(empty)
comma := ,
CPP := g++
SMC = java -jar $(SOURCE_DIR)/Tools/Smc.jar
ifeq ($(V),)
SMC_FLAGS = -c
else
SMC_FLAGS = -c -verbose
endif
# add or replace existing objects only if newer
ARFLAGS := -r
################################################################################
# From current directory determine relative path to makefile (source root)
define source-path-rel-dir
TMP_SRC_DIR += ..
endef
$(foreach l,$(subst /, ,$(subst $(ROOT_DIR),,$(CURDIR))),$(eval $(call source-path-rel-dir,$l)))
SOURCE_DIR ?= $(subst $(space),/,$(TMP_SRC_DIR))
MKFILES_DIR = $(SOURCE_DIR)/Tools/makefiles
################################################################################
# Collect information from each module in the following variables.
# Initialize them here as simple variables.
#libraries :=
#modules :=
#sources :=
#defines :=
#incpath :=
#cfgpath :=
##defines :=$(DEVICE)=1
################################################################################
# Include the target and device specific makefile to load our variables as above
include $(MKFILES_DIR)/target-$(TARGET_NAME).mk
include $(MKFILES_DIR)/device-$(DEVICE).mk
modules := $(addprefix $(SOURCE_DIR)/,$(modules))
ifneq ($(CONFIG),)
INCPATH += $(addprefix $(SOURCE_DIR)/,$(cfgpath)/$(CONFIG))
endif
INCPATH += $(addprefix $(SOURCE_DIR)/,$(cfgpath))
INCPATH += $(addprefix $(SOURCE_DIR)/,$(incpath))
#$(warning INCPATH: $(INCPATH))
################################################################################
# Determine the full target names and include the toolchain specific makefile
ifneq ($(CONFIG),)
TARGET_LNAME := $(TARGET_NAME)-$(DEVICE)-$(CONFIG)
else
TARGET_LNAME := $(TARGET_NAME)-$(DEVICE)
endif
TARGET_MAP := $(TARGET_LNAME).map
TARGET := $(TARGET_LNAME).$(TARGET_TYPE)
include $(MKFILES_DIR)/toolchain-$(TOOLCHAIN).mk
################################################################################
# Support routines/macros
# $(subdirectory)
subdirectory = $(patsubst $(SOURCE_DIR)/%/module.mk,%, \
$(word \
$(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)))
# $(call source-to-object, source-file-list)
source-to-object = $(subst $(SOURCE_DIR)/,,$(subst .c,.o,$(filter %.c,$1))) \
$(subst $(SOURCE_DIR)/,,$(subst .s,.o,$(filter %.s,$1))) \
$(subst $(SOURCE_DIR)/,,$(subst .cpp,.o,$(filter %.cpp,$1)))
# $(call make-library, library-name, source-file-list)
define make-library
libraries += $1
# sources += $2
$1: $(call source-to-object,$2)
$(Q) $(AR) $(ARFLAGS) $$@ $$^
endef
# $(call make-target, source-file-list)
define make-target
sources += $2
$1: $(call source-to-object,$2)
endef
################################################################################
# As we build in the output tree, let make know where to look for source files
vpath %.c $(SOURCE_DIR)
vpath %.s $(SOURCE_DIR)
vpath %.h $(INCPATH)
vpath %.cpp $(SOURCE_DIR)
vpath %.inc $(INCPATH)
vpath %.dot $(SOURCE_DIR)
vpath %.sm $(SOURCE_DIR)
################################################################################
# The final list of objects and a dependency file for each
objects = $(call source-to-object,$(sources))
dependencies = $(subst .o,.d,$(objects))
all:
include $(addsuffix /module.mk,$(modules))
INCLUDES += $(addprefix -I,$(INCPATH))
DEFINES += $(addprefix -D,$(DEVICE)=1 $(DEFS) $(defines))
ifneq ($(MAKE_RESTARTS),1)
$(warning *******************************************************************************)
$(warning $(PWD))
$(warning Building $(TARGET))
$(warning modules: $(subst $(SOURCE_DIR)/,,$(modules)))
$(warning library: $(subst $(SOURCE_DIR)/,,$(libraries)))
$(warning INCPATH: $(subst $(SOURCE_DIR)/,,$(INCPATH)))
$(warning DEFINES: $(subst -D,,$(DEFINES)))
endif
################################################################################
# Mirror the source tree structure into the build target directory
ifeq ($(OS),Windows_NT)
create-output-directories := \
$(Q) $(shell for %%f in ($(subst /,\,$(subst $(SOURCE_DIR)/,,$(modules)))); do [ -d %%f ] || $(MKDIR) %%f)
else
create-output-directories := \
$(shell for f in $(subst $(SOURCE_DIR)/,,$(modules)); do [ -d $$f ] || $(MKDIR) $$f; done)
endif
################################################################################
# Define makes targets
.PHONY: all
all: $(TARGET)
.PHONY: libraries
libraries: $(libraries)
.SECONDARY: $(TARGET_NAME).cof
.SECONDARY: $(TARGET_NAME).elf
.SECONDARY: $(TARGET_NAME).bin
#.PHONY : clean clean_with_libs
.PHONY: clean
clean:
$(RM) $(TARGET) $(objects) $(libraries) $(dependencies)
#.PHONY: graph
#graph: MatrixPilot/FlightState_sm.dot MatrixPilot/FlightState_sm.png
#graph: MatrixPilot/AppClass_sm.dot MatrixPilot/AppClass_sm.png
show:
@echo PROJECT = $(PROJECT)
@echo CONF = $(CONF)
@echo VPATH = $(VPATH)
@echo C_SRCS = $(C_SRCS)
@echo CPP_SRCS = $(CPP_SRCS)
@echo C_OBJS_EXT = $(C_OBJS_EXT)
@echo C_DEPS_EXT = $(C_DEPS_EXT)
@echo CPP_DEPS_EXT = $(CPP_DEPS_EXT)
################################################################################
# Make sure not to generate dependencies when doing cleans
NODEPS:=clean cleanall cleanlibs cleandirs
ifeq (0, $(words $(findstring $(MAKECMDGOALS), $(NODEPS))))
ifeq ($(V),)
-include $(dependencies) # suppress output with -
else
include $(dependencies)
endif
endif
################################################################################
# Dependency and Object generation rules
# With the GNU C compiler, you may wish to use the ‘-MM’ flag instead of ‘-M’.
# This omits prerequisites on system header files.
%.d: %.c
$(Q) $(CC) $(TARGET_ARCH) $(CFLAGS) $(DEFINES) $(INCLUDES) -M $< | \
$(SED) $(QT)s,\($(notdir $*)\.o\) *:,$(dir $@)\1 $@: ,$(QT) > $@.tmp
$(Q) $(MV) $@.tmp $@
%.o: %.c
$(Q) $(CC) $(TARGET_ARCH) -c $(CFLAGS) $(DEFINES) $(INCLUDES) -o $@ $<
%.d: %.cpp
$(Q) $(CPP) $(TARGET_ARCH) $(CFLAGS) $(DEFINES) $(INCLUDES) -M $< | \
$(SED) $(QT)s,\($(notdir $*)\.o\) *:,$(dir $@)\1 $@: ,$(QT) > $@.tmp
$(Q) $(MV) $@.tmp $@
%.o: %.cpp
$(Q) $(CPP) $(TARGET_ARCH) -c $(CFLAGS) $(DEFINES) $(INCLUDES) -o $@ $<
%.d: %.s
$(Q) $(CC) $(TARGET_ARCH) $(AFLAGS) $(DEFINES) $(INCLUDES) -M $< | \
$(SED) $(QT)s,\($(notdir $*)\.o\) *:,$(dir $@)\1 $@: ,$(QT) > $@.tmp
$(Q) $(MV) $@.tmp $@
# NOTE: this version supports the arm-none-eabi toolchain
#%.o: %.s
# $(Q) $(ASM) $(TARGET_ARCH) $(AFLAGS) -c $< -o $@ $(INCLUDES)
%.o: %.s
$(Q) $(ASM) $(TARGET_ARCH) -c $< -o $@ $(AFLAGS) $(INCLUDES)
# NOTE: this version may be needed to support the C8/MC16 microchip assembler
#%.o: %.s
# $(CC) $(TARGET_ARCH) -c $< -o $@ $(AFLAGS),$(subst $(space),$(comma),$(INCLUDES))
# Original microchip command line (works)
# $(Q) $(CC) $(TARGET_ARCH) -c -o $@ $< $(AFLAGS),$(subst $(space),$(comma),$(INCLUDES))
################################################################################
# Library generation rules (note: seems to work without them)
%.a: %.o
$(Q) $(AR) -ru $@ $<
################################################################################
# Windows and *nix target rules
%.exe: $(objects) $(libraries)
$(Q) $(CPP) -o $@ $(objects) $(libraries) $(LFLAGS) $(LIBS)
%.xpl: $(objects) $(libraries)
$(Q) $(CPP) -o $@ $(objects) $(libraries) $(LFLAGS) $(LIBS)
%.out: %.exe
$(Q) mv $< $@
################################################################################
# Microchip build tools rules
%.cof: $(objects) $(libraries)
$(Q) $(CC) $(TARGET_ARCH) -o $@ $(objects) $(libraries) $(LFLAGS) $(LIBS)
%.hex: %.cof
$(Q) $(BIN2HEX) $<
%.elf: $(objects) $(libraries)
$(Q) $(CC) $(TARGET_ARCH) $(objects) $(LFLAGS) $(libraries) $(LIBS) -o $@
%.bin: %.elf
$(Q) $(OBJCOPY) -O binary $< $@
################################################################################
# PixHawk project PX4 loader image generation rules
%.px4: %.bin
$(Q) $(MKFW) --prototype $(SOURCE_DIR)/libSTM/target.prototype --image $< --outfile $@
$(Q) sleep 1
################################################################################
# State Machine Compiler (SMC) rules (work in progress, aka, our state machines could better be defined with specialised tools
%_sm.h %_sm.c : %.sm
$(SMC) $(SMC_FLAGS) $<
%_sm.dot: %.sm
$(SMC) -graph -glevel 1 $<
%_sm.png: %_sm.dot
$(DOT) -T png -o $@ $<
%_sm.html: %.sm
$(SMC) -table $<
################################################################################
#
print-%:
@echo '$*=$($*)'