forked from MatrixPilot/MatrixPilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makefile
330 lines (262 loc) · 10.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
#------------------------------------------------------------------------------
# NOTE: (Windows) this makefile assumes that the MinGW\bin & MinGW\msys\1.0\bin
# directories are added to the PATH environment variable.
#
# 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 ?= SILSIM
#$(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 := ,
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 := -ru
################################################################################
# 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))
#$(warning SOURCE_DIR: $(SOURCE_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))
#INCPATH := $(addprefix $(SOURCE_DIR)/,$(cfgpath)) $(addprefix $(SOURCE_DIR)/,$(incpath))
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
TARGET_NAME := $(TARGET_NAME)-$(DEVICE)-$(TOOLCHAIN)
TARGET_MAP := $(TARGET_NAME).map
TARGET := $(TARGET_NAME).$(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)))
# $(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 %.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))
$(warning *******************************************************************************)
$(warning Building $(TARGET))
$(warning modules: $(subst $(SOURCE_DIR)/,,$(modules)))
all:
include $(addsuffix /module.mk,$(modules))
#include $(patsubst %,$(SOURCE_DIR)/%/module.mk,$(modules))
$(warning library: $(subst $(SOURCE_DIR)/,,$(libraries)))
#$(warning incpath: $(incpath))
#INCPATH += $(incpath)
$(warning INCPATH: $(INCPATH))
INCLUDES += $(addprefix -I,$(INCPATH))
#$(warning INCLUDES: $(subst -I$(SOURCE_DIR)/,,$(INCLUDES)))
#$(warning INCLUDES: $(INCLUDES))
#$(warning objects = $(objects))
#DEFINES += $(addprefix -D,$(DEVICE)=1 $(defines))
DEFINES += $(addprefix -D,$(DEVICE)=1 $(DEFS) $(defines))
$(warning DEFINES: $(subst -D,,$(DEFINES)))
#$(warning *******************************************************************************)
################################################################################
# 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)
#.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
%.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: %.s
$(Q) $(CC) $(TARGET_ARCH) $(AFLAGS) $(DEFINES) $(INCLUDES) -M $< | \
$(SED) $(QT)s,\($(notdir $*)\.o\) *:,$(dir $@)\1 $@: ,$(QT) > $@.tmp
$(Q) $(MV) $@.tmp $@
%.o: %.s
$(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 $@ $<
#%.a: $(objects)
# $(AR) -ru $@ $(objects)
################################################################################
# Windows and *nix target rules
%.exe: $(objects) $(libraries)
$(CC) -o $@ $(LFLAGS) $(objects) $(libraries) $(LIBS)
%.out: %.exe
mv $< $@
################################################################################
# Microchip build tools rules
%.cof: $(objects) $(libraries)
$(CC) $(TARGET_ARCH) -o $@ $(objects) $(libraries) $(LFLAGS) $(LIBS)
%.hex: %.cof
$(Q) $(BIN2HEX) $<
%.elf: $(objects) $(libraries)
$(CC) $(TARGET_ARCH) $(objects) $(LFLAGS) $(libraries) $(LIBS) -o $@
%.bin: %.elf
$(OBJCOPY) -O binary $< $@
################################################################################
# PixHawk project PX4 loader image generation rules
%.px4: %.bin
$(MKFW) --prototype $(SOURCE_DIR)/libSTM/target.prototype --image $< --outfile $@
sleep 3
################################################################################
# State Machine Compiler (SMC) rules
%_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 $<