Change script for apply upstream code
[platform/upstream/connectedhomeip.git] / config / k32w / k32w061-app.mk
1 #
2 #   Copyright (c) 2020 Project CHIP Authors
3 #   Copyright (c) 2020 Google LLC.
4 #   All rights reserved.
5 #
6 #   Licensed under the Apache License, Version 2.0 (the "License");
7 #   you may not use this file except in compliance with the License.
8 #   You may obtain a copy of the License at
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #   Unless required by applicable law or agreed to in writing, software
13 #   distributed under the License is distributed on an "AS IS" BASIS,
14 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #   See the License for the specific language governing permissions and
16 #   limitations under the License.
17 #
18
19 #
20 #   @file
21 #         Common makefile definitions for building applications based
22 #         on the NXP K32W061 SDK.
23 #
24 #
25 #   This makefile is primarily intended to support building the
26 #   CHIP example applications on NXP platforms.  However
27 #   external developers should feel free to use it if they find
28 #   it useful.
29 #
30 #   To build an application using this makefile, include the file
31 #   in a project-specific Makefile, define make variables describing
32 #   the application and how it should be built, and then call the
33 #   GenerateBuildRules function.  E.g.:
34 #
35 #       PROJECT_ROOT = $(realpath .)
36 #
37 #       BUILD_SUPPORT_DIR = $(PROJECT_ROOT)/third_party/connectedhomeip/config/k32w061
38 #
39 #       include $(BUILD_SUPPORT_DIR)/k32w061-app.mk
40 #
41 #       APP := chip-k32w061-bringup
42 #
43 #       SRCS = \
44 #           $(PROJECT_ROOT)/main.cpp \
45 #           ...
46 #
47 #       INC_DIRS = \
48 #           $(PROJECT_ROOT) \
49 #           ...
50 #
51 #       LIBS = \
52 #           ...
53 #
54 #       DEFINES = \
55 #           ...
56 #
57 #       $(call GenerateBuildRules)
58 #
59 # ==================================================
60 # Sanity Checks
61 # ==================================================
62
63 ifndef K32W061_SDK_ROOT
64 $(error ENVIRONMENT ERROR: K32W061_SDK_ROOT not set)
65 endif
66 ifndef GNU_INSTALL_ROOT
67 $(error ENVIRONMENT ERROR: GNU_INSTALL_ROOT not set)
68 endif
69
70 # ==================================================
71 # General definitions
72 # ==================================================
73
74 .DEFAULT_GOAL := all
75
76 APP_EXE = $(OUTPUT_DIR)/$(APP).out
77 APP_ELF = $(OUTPUT_DIR)/$(APP).elf
78
79 SRCS =
80 ALL_SRCS = $(SRCS) $(EXTRA_SRCS)
81 OBJS = $(foreach file,$(ALL_SRCS),$(call ObjFileName,$(file)))
82
83 OUTPUT_DIR = $(PROJECT_ROOT)/build
84 OBJS_DIR = $(OUTPUT_DIR)/objs
85 DEPS_DIR = $(OUTPUT_DIR)/deps
86
87 FLOAT_ABI = -mfloat-abi=soft
88
89 STD_CFLAGS = \
90     -Wall \
91      $(FLOAT_ABI) \
92     -mcpu=cortex-m4 \
93     -mthumb \
94     -mabi=aapcs \
95     -ffunction-sections \
96     -fdata-sections \
97     -fno-strict-aliasing \
98     -fshort-enums \
99     --specs=nosys.specs
100
101 STD_CXXFLAGS = \
102     -fno-rtti \
103     -fno-exceptions \
104     -fno-unwind-tables
105
106 STD_ASFLAGS = \
107      $(FLOAT_ABI) \
108     -g3 \
109     -mcpu=cortex-m4 \
110     -mthumb \
111     -mabi=aapcs \
112     -x assembler-with-cpp
113
114 STD_LDFLAGS = \
115      $(FLOAT_ABI) \
116     -mthumb \
117     -mabi=aapcs \
118     -mcpu=cortex-m4 \
119     -Wl,--gc-sections \
120     --specs=nosys.specs \
121     $(foreach dir,$(LINKER_SCRIPT_INC_DIRS),-L$(dir)) \
122     -T$(LINKER_SCRIPT)
123
124 STD_LIBS = \
125     -lc \
126     -lstdc++ \
127     -lnosys \
128     -lm
129
130 STD_INC_DIRS += \
131     $(K32W061_SDK_ROOT)/CMSIS/Include                                                       \
132     $(K32W061_SDK_ROOT)/devices/K32W061                                                     \
133     $(K32W061_SDK_ROOT)/devices/K32W061/drivers                                             \
134     $(K32W061_SDK_ROOT)/devices/K32W061/utilities                                           \
135     $(K32W061_SDK_ROOT)/devices/K32W061/utilities/debug_console                             \
136     $(K32W061_SDK_ROOT)/devices/K32W061/utilities/str                                       \
137     $(CHIP_ROOT)/src/platform/K32W
138
139 STD_DEFINES = \
140     HAVE_CONFIG_H
141
142 STD_COMPILE_PREREQUISITES = k32w061-sdk-check
143
144 STD_LINK_PREREQUISITES = k32w061-sdk-check
145
146 DEFINE_FLAGS = $(foreach def,$(STD_DEFINES) $(DEFINES),-D$(def))
147
148 INC_FLAGS = $(foreach dir,$(INC_DIRS) $(STD_INC_DIRS),-I$(dir))
149
150 LINKER_SCRIPT = $(realpath $(PROJECT_ROOT))/$(APP).ld
151
152 LINKER_SCRIPT_INC_DIRS =
153
154 K32W061_SDK_TITLE = K32W061 SDK for Thread
155
156 # ==================================================
157 # Toolchain and external utilities / files
158 # ==================================================
159
160 TARGET_TUPLE = arm-none-eabi
161
162 CC      = $(GNU_INSTALL_ROOT)/$(TARGET_TUPLE)-gcc
163 CXX     = $(GNU_INSTALL_ROOT)/$(TARGET_TUPLE)-c++
164 CPP     = $(GNU_INSTALL_ROOT)/$(TARGET_TUPLE)-cpp
165 AS      = $(GNU_INSTALL_ROOT)/$(TARGET_TUPLE)-as
166 AR      = $(GNU_INSTALL_ROOT)/$(TARGET_TUPLE)-ar
167 LD      = $(GNU_INSTALL_ROOT)/$(TARGET_TUPLE)-ld
168 NM      = $(GNU_INSTALL_ROOT)/$(TARGET_TUPLE)-nm
169 OBJDUMP = $(GNU_INSTALL_ROOT)/$(TARGET_TUPLE)-objdump
170 OBJCOPY = $(GNU_INSTALL_ROOT)/$(TARGET_TUPLE)-objcopy
171 SIZE    = $(GNU_INSTALL_ROOT)/$(TARGET_TUPLE)-size
172 RANLIB  = $(GNU_INSTALL_ROOT)/$(TARGET_TUPLE)-ranlib
173
174 INSTALL = /usr/bin/install
175 INSTALLFLAGS = -C -v
176
177 ifneq (, $(shell which ccache))
178 CCACHE = ccache
179 endif
180
181 K32W061_PROG = $(K32W061_SDK_ROOT)/tools/imagetool/DK6Programmer.exe
182
183 # ==================================================
184 # Build options
185 # ==================================================
186
187 DEBUG ?= 1
188 OPT ?= 1
189 VERBOSE = 0
190
191 ifeq ($(VERBOSE),1)
192   NO_ECHO :=
193   HDR_PREFIX := ====================
194 else
195   NO_ECHO := @
196   HDR_PREFIX :=
197 endif
198
199 ifeq ($(DEBUG),1)
200 DEBUG_FLAGS = -g3 -ggdb3
201 else
202 DEBUG_FLAGS = -g0
203 endif
204
205 ifeq ($(OPT),1)
206 OPT_FLAGS = -Os
207 else
208 OPT_FLAGS = -Og
209 endif
210
211 K32W061_PROG_FLAGS ?= -P 1000000 -V 0
212
213 # ==================================================
214 # Utility definitions
215 # ==================================================
216
217 # Convert source file name to object file name
218 define ObjFileName
219 $(OBJS_DIR)/$(notdir $1).o
220 endef
221
222 # Convert source file name to dependency file name
223 define DepFileName
224 $(DEPS_DIR)/$(notdir $1).d
225 endef
226
227 # Newline
228 define NL
229
230 endef
231 # ==================================================
232 # General build rules
233 # ==================================================
234 .PHONY : $(APP) flash flash-app flash_app erase clean help k32w061-sdk-check
235
236 %.elf : %.out
237         $(K32W061_SDK_ROOT)/tools/imagetool/sign_images.sh $(OUTPUT_DIR)
238         $(NO_ECHO)mv $< $@
239         rm -rf $<.bin
240         rm -rf $@.bin
241
242 # Flash the SoftDevice
243 flash-softdevice flash_softdevice :
244         @echo "FLASH $$(APP_ELF)"
245         $(NO_ECHO)$(K32W061_PROG) -l
246         $(NO_ECHO)read -p "Please type a COM from the above list:" COM; \
247         $(K32W061_PROG) $(K32W061_PROG_FLAGS) -s $COM -p $(APP_ELF) || true
248
249 # Erase device
250 erase :
251         @echo "ERASE DEVICE"
252         $(NO_ECHO)$(K32W061_PROG) -l
253         $(NO_ECHO)read -p "Please type a COM from the above list:" COM; \
254         $(K32W061_PROG) -s $COM -e || true
255
256 # Clean build output
257 clean ::
258         @echo "RM $(OUTPUT_DIR)"
259         $(NO_ECHO)rm -rf $(OUTPUT_DIR)
260
261 # Print help
262 export HelpText
263 help :
264         @echo "$${HelpText}"
265
266 # Verify the K32W061 SDK is found
267 k32w061-sdk-check :
268         @test -d $(K32W061_SDK_ROOT) || { \
269             echo "ENVIRONMENT ERROR: ${K32W061_SDK_TITLE} not found at $(K32W061_SDK_ROOT)"; \
270             exit 1; \
271         };
272 # ==================================================
273 # Late-bound rules for building the application
274 # ==================================================
275
276 define AppBuildRules
277
278 # Default all rule
279 all : $(APP)
280
281 # General target for building the application
282 $(APP) : $(APP_ELF)
283
284 # Rule to link the application executable
285 $(APP_EXE) : $(OBJS) $(STD_LINK_PREREQUISITES) | $(OUTPUT_DIR)
286         @echo "$$(HDR_PREFIX)LD $$@"
287         $(NO_ECHO)$$(CC) $$(STD_LDFLAGS) $$(LDFLAGS) $$(DEBUG_FLAGS) $$(OPT_FLAGS) -Wl,-Map=$$(@:.out=.map) $$(OBJS) -Wl,--start-group $$(LIBS) $$(STD_LIBS) -Wl,--end-group -o $$@
288         $(NO_ECHO)$$(SIZE) $$@
289
290 # Individual build rules for each application source file
291 $(foreach file,$(filter %.c,$(ALL_SRCS)),$(call CCRule,$(file)))
292 $(foreach file,$(filter %.cpp,$(ALL_SRCS)),$(call CXXRule,$(file)))
293 $(foreach file,$(filter %.S,$(ALL_SRCS)),$(call ASRule,$(file)))
294 $(foreach file,$(filter %.s,$(ALL_SRCS)),$(call ASRule,$(file)))
295
296 # Rule to build and flash the application
297 flash : $(APP_ELF)
298         @echo "FLASH $$(APP_ELF)"
299         $(NO_ECHO)$(K32W061_PROG) -l
300         $(NO_ECHO)read -p "Please type a COM from the above list:" COM; \
301         $(K32W061_PROG) $(K32W061_PROG_FLAGS) -s $COM -p $(APP_ELF) || true
302
303 # Rule to flash a pre-built application
304 flash-app flash_app :
305         @echo "FLASH $$(APP_ELF)"
306         $(NO_ECHO)$(K32W061_PROG) -l
307         $(NO_ECHO)read -p "Please type a COM from the above list:" COM; \
308         $(K32W061_PROG) $(K32W061_PROG_FLAGS) -s $COM -p $(APP_ELF) || true
309
310 # Rule to create the output directory / subdirectories
311 $(OUTPUT_DIR) $(OBJS_DIR) $(DEPS_DIR) :
312         @echo "MKDIR $$@"
313         $(NO_ECHO)mkdir -p "$$@"
314
315 # Include generated dependency files
316 include $$(wildcard $(DEPS_DIR)/*.d)
317
318 endef
319
320 # Generates late-bound rule for building an object file from a C file
321 define CCRule
322 $(call DepFileName,$1) : ;
323 $(call ObjFileName,$1): $1 $(call DepFileName,$1) | $(OBJS_DIR) $(DEPS_DIR) $(STD_COMPILE_PREREQUISITES)
324         @echo "$$(HDR_PREFIX)CC $1"
325         $(NO_ECHO) $$(CCACHE) $$(CC) -c $$(STD_CFLAGS) $$(CFLAGS) $$(DEBUG_FLAGS) $$(OPT_FLAGS) $$(DEFINE_FLAGS) $$(INC_FLAGS) -MT $$@ -MMD -MP -MF $(call DepFileName,$1).tmp -o $$@ $1
326         $(NO_ECHO)mv $(call DepFileName,$1).tmp $(call DepFileName,$1)
327 $(NL)
328 endef
329
330 # Generates late-bound rule for building an object file from a C++ file
331 define CXXRule
332 $(call DepFileName,$1) : ;
333 $(call ObjFileName,$1): $1 $(call DepFileName,$1) | $(OBJS_DIR) $(DEPS_DIR) $(STD_COMPILE_PREREQUISITES)
334         @echo "$$(HDR_PREFIX)CXX $1"
335         $(NO_ECHO) $$(CCACHE) $$(CXX) -c $$(AUTODEP_FLAGS) $$(STD_CFLAGS) $$(STD_CXXFLAGS) $$(CFLAGS) $$(CXXFLAGS) $$(DEBUG_FLAGS) $$(OPT_FLAGS) $$(DEFINE_FLAGS) $$(INC_FLAGS) -MT $$@ -MMD -MP -MF $(call DepFileName,$1).tmp -o $$@ $1
336         $(NO_ECHO)mv $(call DepFileName,$1).tmp $(call DepFileName,$1)
337 $(NL)
338 endef
339
340 # Generates late-bound rule for building an object file from an assembly file
341 define ASRule
342 $(call ObjFileName,$1): $1 | $(OBJS_DIR) $(STD_COMPILE_PREREQUISITES)
343         @echo "$$(HDR_PREFIX)AS $1"
344         $(NO_ECHO)$$(CC) -c -x assembler-with-cpp $$(STD_ASFLAGS) $$(ASFLAGS) $$(DEBUG_FLAGS) $$(OPT_FLAGS) $$(DEFINE_FLAGS) $$(INC_FLAGS) -o $$@ $1
345 $(NL)
346 endef
347 # ==================================================
348 # Function for generating all late-bound rules
349 # ==================================================
350
351 # List of variables containing late-bound rules that should
352 # be evaluated when GenerateBuildRules is called.
353 LATE_BOUND_RULES = AppBuildRules
354
355 define GenerateBuildRules
356 $(foreach rule,$(LATE_BOUND_RULES),$(eval $($(rule))))
357 endef
358 # ==================================================
359 # Help Definitions
360 # ==================================================
361
362 # Desciptions of make targets
363 define TargetHelp
364   all                   Build the $(APP) application.
365
366   clean                 Clean all build outputs.
367
368   flash                 Build and flash the application onto the device.
369
370   flash-app             Flash the application onto the device without building
371                         it first.
372
373   flash-softdevice      Flash the NXP image onto the device.
374
375   erase                 Wipe the device's flash memory.
376
377   help                  Print this help message.
378 endef
379
380 # Desciptions of build options
381 define OptionHelp
382   DEBUG=[1|0]           Build the application and all libraries with symbol
383                         information.
384
385   VERBOSE=[1|0]         Show commands as they are being executed.
386 endef
387
388 # Overall help text
389 define HelpText
390 Makefile for building the $(APP) application.
391
392 Available targets:
393
394 $(TargetHelp)
395
396 Build options:
397
398 $(OptionHelp)
399
400 endef