aboutsummaryrefslogtreecommitdiff
path: root/makefiles/firmware.mk
blob: 1ecc7da95f979cf010ed4b0b6fc8c1a9c8c82fe7 (plain) (blame)
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
#
# Generic Makefile for PX4 firmware.
#
# Currently this assumes that we're just compiling SRCS
# and then linking the whole thing together.
#
# Requires:
#
# PLATFORM
#	Must be set to a platform name known to the PX4 distribution.
#
# Optional:
#
# PX4_BASE:
#	Points to a PX4 distribution. Normally determined based on the
#	path to this file.
#
# CONFIG:
#	Used to set the output filename; defaults to 'firmware'.
#
# WORK_DIR:
#	Sets the directory in which the firmware will be built. Defaults
#	to the directory 'build' under the directory containing the
#	parent Makefile.
#

################################################################################
# Paths and configuration
################################################################################

#
# Work out where this file is, so we can find other makefiles in the
# same directory.
#
# If PX4_BASE wasn't set previously, work out what it should be
# and set it here now.
#
export PX4_MK_INCLUDE	?= $(dir $(lastword $(MAKEFILE_LIST)))
ifeq ($(PX4_BASE),)
export PX4_BASE		:= $(abspath $(PX4_MK_INCLUDE)/..)
endif
$(info %% PX4_BASE $(PX4_BASE))

#
# If WORK_DIR is not set, create a 'build' directory next to the
# parent Makefile.
#
PARENT_MAKEFILE		:= $(lastword $(filter-out $(lastword $(MAKEFILE_LIST)),$(MAKEFILE_LIST)))
ifeq ($(WORK_DIR),)
export WORK_DIR		:= $(dir $(PARENT_MAKEFILE))/build
endif
$(info %% WORK_DIR $(WORK_DIR))

#
# Paths
#
export NUTTX_SRC	 = $(PX4_BASE)/nuttx
export NUTTX_APPS	 = $(PX4_BASE)/apps
export MAVLINK_SRC	 = $(PX4_BASE)/mavlink
export ROMFS_SRC	 = $(PX4_BASE)/ROMFS
export IMAGE_DIR	 = $(PX4_BASE)/Images
export BUILD_DIR	 = $(PX4_BASE)/Build
export ARCHIVE_DIR	 = $(PX4_BASE)/Archives

#
# Extra tools.
#
# XXX should be in a common toolchain config somewhere.
#
MKFW			 = $(PX4_BASE)/Tools/px_mkfw.py
UPLOADER		 = $(PX4_BASE)/Tools/px_uploader.py
COPY			 = cp
REMOVE			 = rm -f
RMDIR			 = rm -rf

#
# Sanity-check the PLATFORM variable and then get the platform config.
#
# The platform config in turn will fetch the toolchain configuration.
#
ifeq ($(PLATFORM),)
$(error The PLATFORM variable must be set before including firmware.mk)
endif
include $(PX4_MK_INCLUDE)/$(PLATFORM).mk

#
# Makefile debugging.
#
Q			:= $(if $(V),,@)

#
# Host-specific paths, hacks and fixups
#
SYSTYPE			:= $(shell uname -s)

ifeq ($(SYSTYPE),Darwin)
# Eclipse may not have the toolchain on its path.
export PATH		:= $(PATH):/usr/local/bin
endif

#
# Serial port defaults.
#
# XXX The uploader should be smarter than this.
#
ifeq ($(SYSTYPE),Darwin)
SERIAL_PORTS		?= "/dev/tty.usbmodemPX1,/dev/tty.usbmodemPX2,/dev/tty.usbmodemPX3,/dev/tty.usbmodemPX4,/dev/tty.usbmodem1,/dev/tty.usbmodem2,/dev/tty.usbmodem3,/dev/tty.usbmodem4"
endif
ifeq ($(SYSTYPE),Linux)
SERIAL_PORTS		?= "/dev/ttyACM5,/dev/ttyACM4,/dev/ttyACM3,/dev/ttyACM2,/dev/ttyACM1,/dev/ttyACM0"
endif
ifeq ($(SERIAL_PORTS),)
SERIAL_PORTS		 = "\\\\.\\COM32,\\\\.\\COM31,\\\\.\\COM30,\\\\.\\COM29,\\\\.\\COM28,\\\\.\\COM27,\\\\.\\COM26,\\\\.\\COM25,\\\\.\\COM24,\\\\.\\COM23,\\\\.\\COM22,\\\\.\\COM21,\\\\.\\COM20,\\\\.\\COM19,\\\\.\\COM18,\\\\.\\COM17,\\\\.\\COM16,\\\\.\\COM15,\\\\.\\COM14,\\\\.\\COM13,\\\\.\\COM12,\\\\.\\COM11,\\\\.\\COM10,\\\\.\\COM9,\\\\.\\COM8,\\\\.\\COM7,\\\\.\\COM6,\\\\.\\COM5,\\\\.\\COM4,\\\\.\\COM3,\\\\.\\COM2,\\\\.\\COM1,\\\\.\\COM0"
endif

################################################################################
# NuttX libraries and paths
################################################################################

#
# Check that the NuttX archive for the selected platform is available.
#
NUTTX_ARCHIVE		:= $(wildcard $(ARCHIVE_DIR)/$(PLATFORM).export)
ifeq ($(NUTTX_ARCHIVE),)
$(error The NuttX export archive for $(PLATFORM) is missing from $(ARCHIVE_DIR) - try 'make archives' in $(PX4_BASE))
endif

#
# The NuttX config header should always be present in the NuttX archive, and
# if it changes, everything should be rebuilt. So, use it as the trigger to
# unpack the NuttX archive.
#
NUTTX_EXPORT_DIR	 = $(WORK_DIR)/nuttx-export
NUTTX_CONFIG_HEADER	 = $(NUTTX_EXPORT_DIR)/include/nuttx/config.h

#
# Use the linker script from the NuttX export
#
LDSCRIPT		 = $(NUTTX_EXPORT_DIR)/build/ld.script

#
# Add directories from the NuttX export to the relevant search paths
#
INCLUDE_DIRS		+= $(NUTTX_EXPORT_DIR)/include
LIB_DIRS		+= $(NUTTX_EXPORT_DIR)/libs
LIBS			+= -lapps -lnuttx
LINK_DEPS		+= $(wildcard $(addsuffix /*.a,$(LIB_DIRS)))

################################################################################
# Build rules
################################################################################

#
# Things that, if they change, might affect everything
#
GLOBAL_DEPS		+= $(MAKEFILE_LIST) $(NUTTX_CONFIG_HEADER)

#
# What we're going to build.
#
PRODUCT_BUNDLE		 = $(WORK_DIR)/firmware.px4
PRODUCT_BIN		 = $(WORK_DIR)/firmware.bin
PRODUCT_SYM		 = $(WORK_DIR)/firmware.sym

.PHONY:			all
all:			$(PRODUCT_BUNDLE)

#
# Object files we will generate from sources
#
OBJS			 = $(foreach src,$(SRCS),$(WORK_DIR)/$(src).o)

#
# Rules
#

$(filter %.c.o,$(OBJS)): $(WORK_DIR)/%.c.o: %.c $(GLOBAL_DEPS)
	@mkdir -p $(dir $@)
	$(call COMPILE,$<,$@)

$(filter %.cpp.o,$(OBJS)): $(WORK_DIR)/%.cpp.o: %.cpp $(GLOBAL_DEPS)
	@mkdir -p $(dir $@)
	$(call COMPILEXX,$<,$@)

$(filter %.S.o,$(OBJS)): $(WORK_DIR)/%.S.o: %.S $(GLOBAL_DEPS)
	@mkdir -p $(dir $@)
	$(call ASSEMBLE,$<,$@)

$(NUTTX_CONFIG_HEADER):	$(NUTTX_ARCHIVE)
	@echo %% Unpacking $(NUTTX_ARCHIVE)
	$(Q) unzip -q -o -d $(WORK_DIR) $(NUTTX_ARCHIVE)
	$(Q) touch $@

$(PRODUCT_BUNDLE):	$(PRODUCT_BIN)
	@echo %% Generating $@
	$(Q) $(MKFW) --prototype $(IMAGE_DIR)/$(PLATFORM).prototype \
		--git_identity $(PX4_BASE) \
		--image $< > $@

$(PRODUCT_BIN):		$(PRODUCT_SYM)
	$(call SYM_TO_BIN,$<,$@)

$(PRODUCT_SYM):		$(OBJS) $(GLOBAL_DEPS) $(LINK_DEPS)
	$(call LINK,$@,$(OBJS))

upload:		$(PRODUCT_BUNDLE) $(UPLOADER)
	@python -u $(UPLOADER) --port $(SERIAL_PORTS) $(PRODUCT_BUNDLE)

clean:
	@echo %% cleaning
	$(Q) $(REMOVE) $(PRODUCT_BUNDLE) $(PRODUCT_BIN) $(PRODUCT_SYM)
	$(Q) $(REMOVE) $(OBJS) $(DEP_INCLUDES)
	$(Q) $(RMDIR) $(NUTTX_EXPORT_DIR)

#
# DEP_INCLUDES is defined by the toolchain include in terms of $(OBJS)
#
-include $(DEP_INCLUDES)