1#
2#  Copyright (c) 2016, The OpenThread Authors.
3#  All rights reserved.
4#
5#  Redistribution and use in source and binary forms, with or without
6#  modification, are permitted provided that the following conditions are met:
7#  1. Redistributions of source code must retain the above copyright
8#     notice, this list of conditions and the following disclaimer.
9#  2. Redistributions in binary form must reproduce the above copyright
10#     notice, this list of conditions and the following disclaimer in the
11#     documentation and/or other materials provided with the distribution.
12#  3. Neither the name of the copyright holder nor the
13#     names of its contributors may be used to endorse or promote products
14#     derived from this software without specific prior written permission.
15#
16#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26#  POSSIBILITY OF SUCH DAMAGE.
27#
28
29.NOTPARALLEL:
30
31AR                              = arm-none-eabi-ar
32CCAS                            = arm-none-eabi-as
33CPP                             = arm-none-eabi-cpp
34CC                              = arm-none-eabi-gcc
35CXX                             = arm-none-eabi-g++
36LD                              = arm-none-eabi-ld
37STRIP                           = arm-none-eabi-strip
38NM                              = arm-none-eabi-nm
39RANLIB                          = arm-none-eabi-ranlib
40OBJCOPY                         = arm-none-eabi-objcopy
41
42BuildJobs                      ?= 10
43
44configure_OPTIONS               = \
45    --enable-cli                  \
46    --enable-ftd                  \
47    --enable-mtd                  \
48    --enable-ncp                  \
49    --enable-radio-only           \
50    --enable-linker-map           \
51    --with-examples=cc2538        \
52    $(NULL)
53
54TopSourceDir                    := $(dir $(shell readlink $(firstword $(MAKEFILE_LIST))))..
55AbsTopSourceDir                 := $(dir $(realpath $(firstword $(MAKEFILE_LIST))))..
56
57CC2538_CONFIG_FILE_CPPFLAGS  = -DOPENTHREAD_PROJECT_CORE_CONFIG_FILE='\"openthread-core-cc2538-config.h\"'
58CC2538_CONFIG_FILE_CPPFLAGS += -DOPENTHREAD_CORE_CONFIG_PLATFORM_CHECK_FILE='\"openthread-core-cc2538-config-check.h\"'
59CC2538_CONFIG_FILE_CPPFLAGS += -I$(AbsTopSourceDir)/examples/platforms/cc2538/
60
61COMMONCFLAGS                    := \
62    -fdata-sections                \
63    -ffunction-sections            \
64    -Os                            \
65    -g                             \
66    $(CC2538_CONFIG_FILE_CPPFLAGS) \
67    $(NULL)
68
69include $(dir $(abspath $(lastword $(MAKEFILE_LIST))))/common-switches.mk
70
71# Optional CC2592 options, first and foremost, whether to enable support for it
72# at all.
73ifeq ($(CC2592),1)
74COMMONCFLAGS += -DOPENTHREAD_CONFIG_CC2538_WITH_CC2592=1
75
76# If the PA_EN is on another port C pin, specify it with CC2592_PA_PIN.
77ifneq ($(CC2592_PA_EN),)
78COMMONCFLAGS += -DOPENTHREAD_CONFIG_CC2592_PA_EN_PIN=$(CC2592_PA_EN)
79endif
80
81# If the LNA_EN is on another port C pin, specify it with CC2592_LNA_PIN.
82ifneq ($(CC2592_LNA_EN),)
83COMMONCFLAGS += -DOPENTHREAD_CONFIG_CC2592_LNA_EN_PIN=$(CC2592_LNA_EN)
84endif
85
86# If we're not using HGM, set CC2538_USE_HGM to 0.
87ifeq ($(CC2592_USE_HGM),0)
88COMMONCFLAGS += -DOPENTHREAD_CONFIG_CC2592_USE_HGM=0
89else # CC2592_USE_HGM=1
90
91# HGM in use, if not on port D, specify the port here (A, B or C) with CC2592_HGM_PORT.
92ifneq ($(CC2592_HGM_PORT),)
93COMMONCFLAGS += -DOPENTHREAD_CONFIG_CC2592_HGM_PORT=GPIO_$(CC2592_HGM_PORT)_BASE
94endif
95
96# If HGM is not at pin 2, specify which pin here with CC2592_HGM_PIN.
97ifneq ($(CC2592_HGM_PIN),)
98COMMONCFLAGS += -DOPENTHREAD_CONFIG_CC2592_HGM_PIN=$(CC2592_HGM_PIN)
99endif
100
101# If we want it off by default, specify CC2592_HGM_DEFAULT_STATE=0
102ifeq ($(CC2592_HGM_DEFAULT_STATE),0)
103COMMONCFLAGS += -DOPENTHREAD_CONFIG_CC2592_HGM_DEFAULT_STATE=false
104endif
105
106endif # CC2592_USE_HGM
107
108endif # CC2592
109
110ifneq ($(CC2538_RECEIVE_SENSITIVITY),)
111COMMONCFLAGS += -DOPENTHREAD_CONFIG_CC2538_RECEIVE_SENSITIVITY=$(CC2538_RECEIVE_SENSITIVITY)
112endif
113
114ifneq ($(CC2538_RSSI_OFFSET),)
115COMMONCFLAGS += -DOPENTHREAD_CONFIG_CC2538_RSSI_OFFSET=$(CC2538_RSSI_OFFSET)
116endif
117
118CPPFLAGS                       += \
119    $(COMMONCFLAGS)               \
120    $(target_CPPFLAGS)            \
121    $(NULL)
122
123CFLAGS                         += \
124    $(COMMONCFLAGS)               \
125    $(target_CFLAGS)              \
126    $(NULL)
127
128CXXFLAGS                       += \
129    $(COMMONCFLAGS)               \
130    $(target_CXXFLAGS)            \
131    -fno-exceptions               \
132    -fno-rtti                     \
133    $(NULL)
134
135LDFLAGS                        += \
136    $(COMMONCFLAGS)               \
137    $(target_LDFLAGS)             \
138    -nostartfiles                 \
139    -specs=nano.specs             \
140    -specs=nosys.specs            \
141    -Wl,--gc-sections             \
142    $(NULL)
143
144ECHO                            := @echo
145MAKE                            := make
146MKDIR_P                         := mkdir -p
147LN_S                            := ln -s
148RM_F                            := rm -f
149
150INSTALL                         := /usr/bin/install
151INSTALLFLAGS                    := -p
152
153BuildPath                       = build
154TopBuildDir                     = $(BuildPath)
155AbsTopBuildDir                  = $(PWD)/$(TopBuildDir)
156
157ResultPath                      = output
158TopResultDir                    = $(ResultPath)
159AbsTopResultDir                 = $(PWD)/$(TopResultDir)
160
161TargetTuple                     = cc2538
162
163ARCHS                           = cortex-m3
164
165TopTargetLibDir                 = $(TopResultDir)/$(TargetTuple)/lib
166
167ifndef BuildJobs
168BuildJobs := $(shell getconf _NPROCESSORS_ONLN)
169endif
170JOBSFLAG := -j$(BuildJobs)
171
172#
173# configure-arch <arch>
174#
175# Configure OpenThread for the specified architecture.
176#
177#   arch - The architecture to configure.
178#
179define configure-arch
180$(ECHO) "  CONFIG   $(TargetTuple)..."
181(cd $(BuildPath)/$(TargetTuple) && $(AbsTopSourceDir)/configure \
182INSTALL="$(INSTALL) $(INSTALLFLAGS)" \
183CPP="$(CPP)" CC="$(CC)" CXX="$(CXX)" OBJC="$(OBJC)" OBJCXX="$(OBJCXX)" AR="$(AR)" RANLIB="$(RANLIB)" NM="$(NM)" STRIP="$(STRIP)" CPPFLAGS="$(CPPFLAGS)" CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" LDFLAGS="$(LDFLAGS)" \
184--host=arm-none-eabi \
185--prefix=/ \
186--exec-prefix=/$(TargetTuple) \
187$(configure_OPTIONS))
188endef # configure-arch
189
190#
191# build-arch <arch>
192#
193# Build the OpenThread intermediate build products for the specified
194# architecture.
195#
196#   arch - The architecture to build.
197#
198define build-arch
199$(ECHO) "  BUILD    $(TargetTuple)"
200$(MAKE) $(JOBSFLAG) -C $(BuildPath)/$(TargetTuple) --no-print-directory \
201all
202endef # build-arch
203
204#
205# stage-arch <arch>
206#
207# Stage (install) the OpenThread final build products for the specified
208# architecture.
209#
210#   arch - The architecture to stage.
211#
212define stage-arch
213$(ECHO) "  STAGE    $(TargetTuple)"
214$(MAKE) $(JOBSFLAG) -C $(BuildPath)/$(TargetTuple) --no-print-directory \
215DESTDIR=$(AbsTopResultDir) \
216install
217endef # stage-arch
218
219#
220# ARCH_template <arch>
221#
222# Define macros, targets and rules to configure, build, and stage the
223# OpenThread for a single architecture.
224#
225#   arch - The architecture to instantiate the template for.
226#
227define ARCH_template
228CONFIGURE_TARGETS += configure-$(1)
229BUILD_TARGETS     += do-build-$(1)
230STAGE_TARGETS     += stage-$(1)
231BUILD_DIRS        += $(BuildPath)/$(TargetTuple)
232DIRECTORIES       += $(BuildPath)/$(TargetTuple)
233
234configure-$(1): target_CPPFLAGS=$($(1)_target_CPPFLAGS)
235configure-$(1): target_CFLAGS=$($(1)_target_CFLAGS)
236configure-$(1): target_CXXFLAGS=$($(1)_target_CXXFLAGS)
237configure-$(1): target_LDFLAGS=$($(1)_target_LDFLAGS)
238
239configure-$(1): $(BuildPath)/$(TargetTuple)/config.status
240
241$(BuildPath)/$(TargetTuple)/config.status: | $(BuildPath)/$(TargetTuple)
242	$$(call configure-arch,$(1))
243
244do-build-$(1): configure-$(1)
245
246do-build-$(1):
247	+$$(call build-arch,$(1))
248
249stage-$(1): do-build-$(1)
250
251stage-$(1): | $(TopResultDir)
252	$$(call stage-arch,$(1))
253
254$(1): stage-$(1)
255endef # ARCH_template
256
257.DEFAULT_GOAL := all
258
259all: stage
260
261#
262# cortex-m3
263#
264
265cortex-m3_target_ABI                  = cortex-m3
266cortex-m3_target_CPPFLAGS             = -mcpu=cortex-m3 -mfloat-abi=soft -mthumb
267cortex-m3_target_CFLAGS               = -mcpu=cortex-m3 -mfloat-abi=soft -mthumb
268cortex-m3_target_CXXFLAGS             = -mcpu=cortex-m3 -mfloat-abi=soft -mthumb
269cortex-m3_target_LDFLAGS              = -mcpu=cortex-m3 -mfloat-abi=soft -mthumb
270
271# Instantiate an architecture-specific build template for each target
272# architecture.
273
274$(foreach arch,$(ARCHS),$(eval $(call ARCH_template,$(arch))))
275
276#
277# Common / Finalization
278#
279
280configure: $(CONFIGURE_TARGETS)
281
282build: $(BUILD_TARGETS)
283
284stage: $(STAGE_TARGETS)
285
286DIRECTORIES     = $(TopResultDir) $(TopResultDir)/$(TargetTuple)/lib $(BUILD_DIRS)
287
288CLEAN_DIRS      = $(TopResultDir) $(BUILD_DIRS)
289
290all: stage
291
292$(DIRECTORIES):
293	$(ECHO) "  MKDIR    $@"
294	@$(MKDIR_P) "$@"
295
296clean:
297	$(ECHO) "  CLEAN"
298	@$(RM_F) -r $(CLEAN_DIRS)
299
300help:
301	$(ECHO) "Simply type 'make -f $(firstword $(MAKEFILE_LIST))' to build OpenThread for the following "
302	$(ECHO) "architectures: "
303	$(ECHO) ""
304	$(ECHO) "    $(ARCHS)"
305	$(ECHO) ""
306	$(ECHO) "To build only a particular architecture, specify: "
307	$(ECHO) ""
308	$(ECHO) "    make -f $(firstword $(MAKEFILE_LIST)) <architecture>"
309	$(ECHO) ""
310