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# Don't allow this top-level makefile's targets to be built in parallel.
30
31.NOTPARALLEL:
32
33COVERAGE                       ?= 0
34DEBUG                          ?= 0
35
36# Enable most features by default to cover most code
37
38BORDER_AGENT                   ?= 1
39BORDER_ROUTER                  ?= 1
40COAP                           ?= 1
41COAP_BLOCK                     ?= 1
42COAP_OBSERVE                   ?= 1
43COAPS                          ?= 1
44COMMISSIONER                   ?= 1
45CHANNEL_MANAGER                ?= 1
46CHANNEL_MONITOR                ?= 1
47CHILD_SUPERVISION              ?= 1
48DATASET_UPDATER                ?= 1
49DHCP6_CLIENT                   ?= 1
50DHCP6_SERVER                   ?= 1
51DIAGNOSTIC                     ?= 1
52DNS_CLIENT                     ?= 1
53DNSSD_SERVER                   ?= 1
54ECDSA                          ?= 1
55HISTORY_TRACKER                ?= 1
56IP6_FRAGM                      ?= 1
57JAM_DETECTION                  ?= 1
58JOINER                         ?= 1
59LEGACY                         ?= 1
60LINK_RAW                       ?= 1
61MAC_FILTER                     ?= 1
62MTD_NETDIAG                    ?= 1
63NEIGHBOR_DISCOVERY_AGENT       ?= 1
64NETDATA_PUBLISHER              ?= 1
65PING_SENDER                    ?= 1
66REFERENCE_DEVICE               ?= 1
67SERVICE                        ?= 1
68SNTP_CLIENT                    ?= 1
69SRP_CLIENT                     ?= 1
70SRP_SERVER                     ?= 1
71UDP_FORWARD                    ?= 1
72
73COMMONCFLAGS                   := \
74    -g                            \
75    $(NULL)
76
77# If the user has asserted COVERAGE, alter the configuration options
78# accordingly.
79
80configure_OPTIONS               = \
81    --enable-cli                  \
82    --enable-ftd                  \
83    --enable-mtd                  \
84    --enable-ncp                  \
85    --enable-radio-only           \
86    --with-examples=simulation    \
87    $(NULL)
88
89# Platform specific switches
90
91ifneq ($(DEBUG),1)
92COMMONCFLAGS                   += \
93    -O2                           \
94    $(NULL)
95endif
96
97ifeq ($(NCP_SPI),1)
98COMMONCFLAGS                   += -DOPENTHREAD_CONFIG_NCP_SPI_ENABLE=1
99else
100COMMONCFLAGS                   += -DOPENTHREAD_CONFIG_NCP_HDLC_ENABLE=1
101endif # NCP_SPI == 1
102
103ifeq ($(OTNS),1)
104VIRTUAL_TIME ?= 1
105endif
106
107ifeq ($(VIRTUAL_TIME),1)
108COMMONCFLAGS                   += -DOPENTHREAD_SIMULATION_VIRTUAL_TIME=1
109endif
110
111ifeq ($(VIRTUAL_TIME_UART),1)
112COMMONCFLAGS                   += -DOPENTHREAD_SIMULATION_VIRTUAL_TIME_UART=1
113endif
114
115ifneq ($(MAX_NETWORK_SIZE),)
116COMMONCFLAGS                   += -DOPENTHREAD_SIMULATION_MAX_NETWORK_SIZE=$(MAX_NETWORK_SIZE)
117endif
118
119include $(dir $(abspath $(lastword $(MAKEFILE_LIST))))/common-switches.mk
120
121TopSourceDir                   := $(dir $(shell readlink $(firstword $(MAKEFILE_LIST))))..
122AbsTopSourceDir                := $(dir $(realpath $(firstword $(MAKEFILE_LIST))))..
123
124CONFIG_FILE      = OPENTHREAD_PROJECT_CORE_CONFIG_FILE='\"openthread-core-simulation-config.h\"'
125CONFIG_FILE_PATH = $(AbsTopSourceDir)/examples/platforms/simulation/
126COMMONCFLAGS                   += \
127    -D$(CONFIG_FILE)              \
128    -I$(CONFIG_FILE_PATH)         \
129
130CPPFLAGS                       += \
131    $(COMMONCFLAGS)               \
132    $(NULL)
133
134CFLAGS                         += \
135    $(COMMONCFLAGS)               \
136    $(NULL)
137
138CXXFLAGS                       += \
139    $(COMMONCFLAGS)               \
140    $(NULL)
141
142LDFLAGS                        += \
143    $(COMMONCFLAGS)               \
144    $(NULL)
145
146ECHO                           := @echo
147INSTALL                        := /usr/bin/install
148INSTALLFLAGS                   := -p
149LN_S                           := ln -s
150MAKE                           := make
151MKDIR_P                        := mkdir -p
152RM_F                           := rm -f
153
154BuildJobs                      ?= 10
155BuildPath                       = build
156TopBuildDir                     = $(BuildPath)
157AbsTopBuildDir                  = $(PWD)/$(TopBuildDir)
158
159ResultPath                      = output
160TopResultDir                    = $(ResultPath)
161AbsTopResultDir                 = $(PWD)/$(TopResultDir)
162
163TargetTuple                     = simulation
164
165ifndef BuildJobs
166BuildJobs := $(shell getconf _NPROCESSORS_ONLN)
167endif
168JOBSFLAG := -j$(BuildJobs)
169
170#
171# configure-arch <target>
172#
173# Configure OpenThread for the specified target.
174#
175#   target - The target to configure.
176#
177define configure-target
178$(ECHO) "  CONFIG   $(1)..."
179(cd $(BuildPath)/$(1) && $(AbsTopSourceDir)/configure \
180INSTALL="$(INSTALL) $(INSTALLFLAGS)" \
181CPPFLAGS="$(CPPFLAGS)" CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" LDFLAGS="$(LDFLAGS)" \
182--prefix=/ \
183--exec-prefix=/$(1) \
184$(configure_OPTIONS))
185endef # configure-target
186
187#
188# build-target <target>
189#
190# Build the OpenThread intermediate build products for the specified
191# target.
192#
193#   target - The target to build.
194#
195define build-target
196$(ECHO) "  BUILD    $(1)"
197$(MAKE) $(JOBSFLAG) -C $(BuildPath)/$(1) --no-print-directory \
198all
199endef # build-target
200
201#
202# check-target <target>
203#
204# Check (run unit tests) OpenThread for the specified target.
205#
206#   target - The target to check.
207#
208define check-target
209$(ECHO) "  CHECK    $(1)"
210$(MAKE) $(JOBSFLAG) -C $(BuildPath)/$(1) --no-print-directory \
211check
212endef # check-target
213
214#
215# distcheck-target <target>
216#
217# Check (run unit tests) OpenThread for the specified target.
218#
219#   target - The target to distcheck.
220#
221define distcheck-target
222$(ECHO) "  DISTCHECK    $(1)"
223$(MAKE) $(JOBSFLAG) -C $(BuildPath)/$(1) --no-print-directory \
224distcheck
225endef # distcheck-target
226
227#
228# coverage-target <target>
229#
230# Generate code coverage from unit tests for OpenThread for the
231# specified target.
232#
233#   target - The target to generate code coverage for.
234#
235define coverage-target
236$(ECHO) "  COVERAGE $(1)"
237$(MAKE) $(JOBSFLAG) -C $(BuildPath)/$(1) --no-print-directory \
238coverage
239endef # coverage-target
240
241#
242# stage-target <target>
243#
244# Stage (install) the OpenThread final build products for the specified
245# target.
246#
247#   target - The target to stage.
248#
249define stage-target
250$(ECHO) "  STAGE    $(1)"
251$(MAKE) $(JOBSFLAG) -C $(BuildPath)/$(1) --no-print-directory \
252DESTDIR=$(AbsTopResultDir) \
253install
254endef # stage-target
255
256#
257# TARGET_template <target>
258#
259# Define macros, targets and rules to configure, build, and stage
260# OpenThread for a single target.
261#
262#   target - The target to instantiate the template for.
263#
264define TARGET_template
265CONFIGURE_TARGETS += configure-$(1)
266BUILD_TARGETS     += do-build-$(1)
267CHECK_TARGETS     += check-$(1)
268DISTCHECK_TARGETS += distcheck-$(1)
269COVERAGE_TARGETS  += coverage-$(1)
270STAGE_TARGETS     += stage-$(1)
271BUILD_DIRS        += $(BuildPath)/$(1)
272DIRECTORIES       += $(BuildPath)/$(1)
273
274configure-$(1): $(BuildPath)/$(1)/config.status
275
276$(BuildPath)/$(1)/config.status: | $(BuildPath)/$(1)
277	$$(call configure-target,$(1))
278
279do-build-$(1): configure-$(1)
280
281do-build-$(1):
282	+$$(call build-target,$(1))
283
284check-$(1): do-build-$(1)
285
286check-$(1):
287	+$$(call check-target,$(1))
288
289distcheck-$(1): do-build-$(1)
290
291distcheck-$(1):
292	+$$(call distcheck-target,$(1))
293
294coverage-$(1): do-build-$(1)
295
296coverage-$(1):
297	+$$(call coverage-target,$(1))
298
299stage-$(1): do-build-$(1)
300
301stage-$(1): | $(TopResultDir)
302	$$(call stage-target,$(1))
303
304$(1): stage-$(1)
305endef # TARGET_template
306
307.DEFAULT_GOAL := all
308
309all: stage
310
311# Instantiate an target-specific build template for the target.
312
313$(eval $(call TARGET_template,$(TargetTuple)))
314
315#
316# Common / Finalization
317#
318
319configure: $(CONFIGURE_TARGETS)
320
321build: $(BUILD_TARGETS)
322
323check: $(CHECK_TARGETS)
324
325distcheck: $(DISTCHECK_TARGETS)
326
327coverage: $(COVERAGE_TARGETS)
328
329stage: $(STAGE_TARGETS)
330
331DIRECTORIES     = $(TopResultDir) $(TopResultDir)/$(TargetTuple)/lib $(BUILD_DIRS)
332
333CLEAN_DIRS      = $(TopResultDir) $(BUILD_DIRS)
334
335all: stage
336
337$(DIRECTORIES):
338	$(ECHO) "  MKDIR    $@"
339	@$(MKDIR_P) "$@"
340
341clean:
342	$(ECHO) "  CLEAN"
343	@$(RM_F) -r $(CLEAN_DIRS)
344
345help:
346	$(ECHO) "Simply type 'make -f $(firstword $(MAKEFILE_LIST))' to build OpenThread for the following "
347	$(ECHO) "target:"
348	$(ECHO) ""
349	$(ECHO) "    $(TargetTuple)"
350	$(ECHO) ""
351