2 # Copyright (c) 2020 Project CHIP Authors
3 # Copyright (c) 2020 Google LLC.
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
21 # Common makefile definitions for building applications based
22 # on the NXP K32W061 SDK.
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
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.:
35 # PROJECT_ROOT = $(realpath .)
37 # BUILD_SUPPORT_DIR = $(PROJECT_ROOT)/third_party/connectedhomeip/config/k32w061
39 # include $(BUILD_SUPPORT_DIR)/k32w061-app.mk
41 # APP := chip-k32w061-bringup
44 # $(PROJECT_ROOT)/main.cpp \
57 # $(call GenerateBuildRules)
59 # ==================================================
61 # ==================================================
63 ifndef K32W061_SDK_ROOT
64 $(error ENVIRONMENT ERROR: K32W061_SDK_ROOT not set)
66 ifndef GNU_INSTALL_ROOT
67 $(error ENVIRONMENT ERROR: GNU_INSTALL_ROOT not set)
70 # ==================================================
72 # ==================================================
76 APP_EXE = $(OUTPUT_DIR)/$(APP).out
77 APP_ELF = $(OUTPUT_DIR)/$(APP).elf
80 ALL_SRCS = $(SRCS) $(EXTRA_SRCS)
81 OBJS = $(foreach file,$(ALL_SRCS),$(call ObjFileName,$(file)))
83 OUTPUT_DIR = $(PROJECT_ROOT)/build
84 OBJS_DIR = $(OUTPUT_DIR)/objs
85 DEPS_DIR = $(OUTPUT_DIR)/deps
87 FLOAT_ABI = -mfloat-abi=soft
97 -fno-strict-aliasing \
112 -x assembler-with-cpp
120 --specs=nosys.specs \
121 $(foreach dir,$(LINKER_SCRIPT_INC_DIRS),-L$(dir)) \
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
142 STD_COMPILE_PREREQUISITES = k32w061-sdk-check
144 STD_LINK_PREREQUISITES = k32w061-sdk-check
146 DEFINE_FLAGS = $(foreach def,$(STD_DEFINES) $(DEFINES),-D$(def))
148 INC_FLAGS = $(foreach dir,$(INC_DIRS) $(STD_INC_DIRS),-I$(dir))
150 LINKER_SCRIPT = $(realpath $(PROJECT_ROOT))/$(APP).ld
152 LINKER_SCRIPT_INC_DIRS =
154 K32W061_SDK_TITLE = K32W061 SDK for Thread
156 # ==================================================
157 # Toolchain and external utilities / files
158 # ==================================================
160 TARGET_TUPLE = arm-none-eabi
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
174 INSTALL = /usr/bin/install
177 ifneq (, $(shell which ccache))
181 K32W061_PROG = $(K32W061_SDK_ROOT)/tools/imagetool/DK6Programmer.exe
183 # ==================================================
185 # ==================================================
193 HDR_PREFIX := ====================
200 DEBUG_FLAGS = -g3 -ggdb3
211 K32W061_PROG_FLAGS ?= -P 1000000 -V 0
213 # ==================================================
214 # Utility definitions
215 # ==================================================
217 # Convert source file name to object file name
219 $(OBJS_DIR)/$(notdir $1).o
222 # Convert source file name to dependency file name
224 $(DEPS_DIR)/$(notdir $1).d
231 # ==================================================
232 # General build rules
233 # ==================================================
234 .PHONY : $(APP) flash flash-app flash_app erase clean help k32w061-sdk-check
237 $(K32W061_SDK_ROOT)/tools/imagetool/sign_images.sh $(OUTPUT_DIR)
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
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
258 @echo "RM $(OUTPUT_DIR)"
259 $(NO_ECHO)rm -rf $(OUTPUT_DIR)
266 # Verify the K32W061 SDK is found
268 @test -d $(K32W061_SDK_ROOT) || { \
269 echo "ENVIRONMENT ERROR: ${K32W061_SDK_TITLE} not found at $(K32W061_SDK_ROOT)"; \
272 # ==================================================
273 # Late-bound rules for building the application
274 # ==================================================
281 # General target for building the application
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) $$@
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)))
296 # Rule to build and flash the application
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
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
310 # Rule to create the output directory / subdirectories
311 $(OUTPUT_DIR) $(OBJS_DIR) $(DEPS_DIR) :
313 $(NO_ECHO)mkdir -p "$$@"
315 # Include generated dependency files
316 include $$(wildcard $(DEPS_DIR)/*.d)
320 # Generates late-bound rule for building an object file from a C file
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)
330 # Generates late-bound rule for building an object file from a C++ file
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)
340 # Generates late-bound rule for building an object file from an assembly file
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
347 # ==================================================
348 # Function for generating all late-bound rules
349 # ==================================================
351 # List of variables containing late-bound rules that should
352 # be evaluated when GenerateBuildRules is called.
353 LATE_BOUND_RULES = AppBuildRules
355 define GenerateBuildRules
356 $(foreach rule,$(LATE_BOUND_RULES),$(eval $($(rule))))
358 # ==================================================
360 # ==================================================
362 # Desciptions of make targets
364 all Build the $(APP) application.
366 clean Clean all build outputs.
368 flash Build and flash the application onto the device.
370 flash-app Flash the application onto the device without building
373 flash-softdevice Flash the NXP image onto the device.
375 erase Wipe the device's flash memory.
377 help Print this help message.
380 # Desciptions of build options
382 DEBUG=[1|0] Build the application and all libraries with symbol
385 VERBOSE=[1|0] Show commands as they are being executed.
390 Makefile for building the $(APP) application.