1- include_guard ()
1+ include_guard (GLOBAL )
22
33include (CMakePrintHelpers)
44find_package (Python COMPONENTS Interpreter)
55find_package (Git REQUIRED)
66
77# TOP is path to root directory
88set (TOP ${CMAKE_CURRENT_LIST_DIR} /..)
9- #get_filename_component(TOP "${TOP}" REALPATH)
9+ get_filename_component (TOP ${TOP} ABSOLUTE )
10+
11+ set (UF2CONV_PY ${TOP} /lib/uf2/utils/uf2conv.py)
12+
13+ #-------------------------------------------------------------
14+ # Toolchain
15+ # Can be changed via -DTOOLCHAIN=gcc|iar or -DCMAKE_C_COMPILER=
16+ #-------------------------------------------------------------
17+ # Detect toolchain based on CMAKE_C_COMPILER
18+ if (DEFINED CMAKE_C_COMPILER)
19+ string (FIND ${CMAKE_C_COMPILER} "iccarm" IS_IAR)
20+ string (FIND ${CMAKE_C_COMPILER} "clang" IS_CLANG)
21+ string (FIND ${CMAKE_C_COMPILER} "gcc" IS_GCC)
22+
23+ if (NOT IS_IAR EQUAL -1)
24+ set (TOOLCHAIN iar)
25+ elseif (NOT IS_CLANG EQUAL -1)
26+ set (TOOLCHAIN clang)
27+ elseif (NOT IS_GCC EQUAL -1)
28+ set (TOOLCHAIN gcc)
29+ endif ()
30+ endif ()
1031
11- # Default to gcc
1232if (NOT DEFINED TOOLCHAIN)
1333 set (TOOLCHAIN gcc)
1434endif ()
1535
36+ #-------------------------------------------------------------
37+ # FAMILY and BOARD
38+ #-------------------------------------------------------------
1639if (NOT BOARD)
1740 message (FATAL_ERROR "BOARD not specified" )
1841endif ()
@@ -33,8 +56,6 @@ if (NOT DEFINED FAMILY)
3356 list (GET BOARD_PATH 0 FAMILY)
3457endif ()
3558
36- set (UF2CONV_PY ${TOP} /lib/uf2/utils/uf2conv.py)
37-
3859# enable LTO if supported
3960include (CheckIPOSupported)
4061check_ipo_supported(RESULT IPO_SUPPORTED)
@@ -50,29 +71,34 @@ function(family_add_bin_hex TARGET)
5071 # placeholder, will be override by family specific
5172endfunction ()
5273
53- function (family_add_default_example_warnings TARGET )
54- # target_compile_options(${TARGET} PUBLIC
55- # -Wall
56- # -Wextra
57- # -Werror
58- # -Wfatal-errors
59- # -Wdouble-promotion
60- # -Wfloat-equal
61- # -Wshadow
62- # -Wwrite-strings
63- # -Wsign-compare
64- # -Wmissing-format-attribute
65- # -Wunreachable-code
66- # -Wcast-align
67- # -Wcast-qual
68- # -Wnull-dereference
69- # -Wuninitialized
70- # -Wunused
71- # -Wredundant-decls
72- # #-Wstrict-prototypes
73- # #-Werror-implicit-function-declaration
74- # #-Wundef
75- # )
74+ function (family_add_default_warnings TARGET )
75+ target_compile_options (${TARGET} PUBLIC
76+ -Wall
77+ -Wextra
78+ #-Werror
79+ -Wfatal-errors
80+ -Wdouble-promotion
81+ -Wstrict-prototypes
82+ -Wstrict-overflow
83+ -Werror-implicit-function-declaration
84+ -Wfloat-equal
85+ -Wundef
86+ -Wshadow
87+ -Wwrite-strings
88+ -Wsign-compare
89+ -Wmissing-format-attribute
90+ -Wunreachable-code
91+ -Wcast-align
92+ -Wcast-function-type
93+ -Wcast-qual
94+ -Wnull-dereference
95+ -Wuninitialized
96+ -Wunused
97+ -Wunused-function
98+ -Wreturn-type
99+ -Wredundant-decls
100+ -Wmissing-prototypes
101+ )
76102
77103 if (CMAKE_C_COMPILER_ID STREQUAL "GNU" )
78104 if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 12.0)
@@ -98,19 +124,18 @@ endfunction()
98124
99125
100126function (family_configure_common TARGET )
101- # run size after build
102- add_custom_command (TARGET ${TARGET} POST_BUILD
103- COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${TARGET} >
104- )
105-
106- # add hex, bin and uf2 targets
107- family_add_bin_hex(${TARGET} )
108-
109- # Add warnings flags
127+ # Add BOARD_${BOARD} define
128+ string (TOUPPER ${BOARD} BOARD_UPPER)
129+ string (REPLACE "-" "_" BOARD_UPPER ${BOARD_UPPER} )
130+ target_compile_definitions (${TARGET} PUBLIC
131+ BOARD_${BOARD_UPPER}
132+ )
110133
111- # Generate map file
112- target_link_options (${TARGET} PUBLIC "LINKER:-Map=$<TARGET_FILE:${TARGET} >.map" )
113- #family_add_linkermap(${TARGET})
134+ # compile define from command line
135+ if (DEFINED CFLAGS_CLI)
136+ separate_arguments (CFLAGS_CLI)
137+ target_compile_options (${TARGET} PUBLIC ${CFLAGS_CLI} )
138+ endif ()
114139
115140 # executable target linked with board target
116141 family_add_board_target(board_${BOARD} )
@@ -127,88 +152,60 @@ function(family_configure_common TARGET)
127152
128153 # Add segger rtt to example
129154 if (LOGGER STREQUAL "RTT" OR LOGGER STREQUAL "rtt" )
130- if (NOT TARGET segger_rtt)
131- add_library (segger_rtt STATIC ${TOP} /lib/SEGGER_RTT/RTT/SEGGER_RTT.c)
132- target_include_directories (segger_rtt PUBLIC ${TOP} /lib/SEGGER_RTT/RTT)
133- endif ()
134- target_link_libraries (${TARGET} PUBLIC segger_rtt)
155+ target_sources (${TARGET} PUBLIC ${TOP} /lib/SEGGER_RTT/RTT/SEGGER_RTT.c)
156+ target_include_directories (${TARGET} PUBLIC ${TOP} /lib/SEGGER_RTT/RTT)
157+ # target_compile_definitions(${TARGET} PUBLIC SEGGER_RTT_MODE_DEFAULT=SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL)
135158 endif ()
136159 endif ()
160+
161+ family_add_default_warnings(${TARGET} )
162+ target_link_options (${TARGET} PUBLIC "LINKER:-Map=$<TARGET_FILE:${TARGET} >.map" )
163+
164+ # run size after build
165+ add_custom_command (TARGET ${TARGET} POST_BUILD
166+ COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${TARGET} >
167+ )
168+
169+ # add hex, bin and uf2 targets
170+ family_add_bin_hex(${TARGET} )
171+
137172endfunction ()
138173
139174
140175# Add tinyusb to example
141- function (family_add_tinyusb TARGET OPT_MCU RTOS)
142- # tinyusb target is built for each example since it depends on example's tusb_config.h
143- set (TINYUSB_TARGET_PREFIX ${TARGET} -)
144- add_library (${TARGET} -tinyusb_config INTERFACE )
145-
146- # path to tusb_config.h
147- target_include_directories (${TARGET} -tinyusb_config INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} )
148- target_compile_definitions (${TARGET} -tinyusb_config INTERFACE CFG_TUSB_MCU=${OPT_MCU} )
176+ function (family_add_tinyusb TARGET OPT_MCU)
177+ # tinyusb's CMakeList.txt
178+ add_subdirectory (${TOP} /lib/tinyusb/src ${CMAKE_CURRENT_BINARY_DIR} /tinyusb)
149179
180+ # Add TinyUSB sources, include and common define
181+ tinyusb_target_add(${TARGET} )
182+ target_compile_definitions (${TARGET} PUBLIC CFG_TUSB_MCU=${OPT_MCU} )
150183 if (DEFINED LOG )
151- target_compile_definitions (${TARGET} -tinyusb_config INTERFACE CFG_TUSB_DEBUG=${LOG} )
152- if (LOG STREQUAL "4" )
153- # no inline for debug level 4
154- target_compile_definitions (${TARGET} -tinyusb_config INTERFACE TU_ATTR_ALWAYS_INLINE=)
184+ target_compile_definitions (${TARGET} PUBLIC CFG_TUSB_DEBUG=${LOG} )
185+ if (LOG STREQUAL "4" ) # no inline for debug level 4
186+ target_compile_definitions (${TARGET} PUBLIC TU_ATTR_ALWAYS_INLINE=)
155187 endif ()
156188 endif ()
189+ endfunction ()
157190
158- if (RTOS STREQUAL "freertos" )
159- target_compile_definitions (${TARGET} -tinyusb_config INTERFACE CFG_TUSB_OS=OPT_OS_FREERTOS)
160- endif ()
161-
162- # tinyusb's CMakeList.txt
163- add_subdirectory (${TOP} /lib/tinyusb/src ${CMAKE_CURRENT_BINARY_DIR} /tinyusb)
191+ function (family_configure_tinyuf2 TARGET OPT_MCU)
192+ family_configure_common(${TARGET} )
164193
165- if (RTOS STREQUAL "freertos" )
166- target_link_libraries (${TARGET} -tinyusb PUBLIC freertos_kernel)
167- endif ()
194+ include (${TOP} /src/tinyuf2.cmake)
195+ add_tinyuf2(${TARGET} )
168196
169- # tinyusb depends on board target for low level mcu header and function
170- target_link_libraries (${TARGET} -tinyusb PUBLIC board_${BOARD} )
171- target_link_libraries (${TARGET} PUBLIC ${TARGET} -tinyusb)
172- endfunction ()
197+ family_add_tinyusb(${TARGET} ${OPT_MCU} )
173198
174- function (family_add_uf2version TARGET DEPS_REPO)
199+ # Add uf2 version
175200 execute_process (COMMAND ${GIT_EXECUTABLE} describe --dirty --always --tags OUTPUT_VARIABLE GIT_VERSION)
176201 string (STRIP ${GIT_VERSION} GIT_VERSION)
177- # string(REPLACE ${TOP}/ "" DEPS_REPO "${DEPS_REPO}")
178- # foreach (DEP ${DEPS_REPO})
179- # execute_process(COMMAND ${GIT_EXECUTABLE} -C ${TOP} submodule status ${DEP}
180- # OUTPUT_VARIABLE DEP_VERSION
181- # )
182- # string(STRIP ${DEP_VERSION} DEP_VERSION)
183- # string(FIND "${DEP_VERSION}" " " SPACE_POS)
184- # string(SUBSTRING "${DEP_VERSION}" ${SPACE_POS} -1 DEP_VERSION)
185- # string(STRIP ${DEP_VERSION} DEP_VERSION)
186- #
187- # set(GIT_SUBMODULE_VERSIONS "${GIT_SUBMODULE_VERSIONS} ${DEP_VERSION}")
188- # endforeach ()
189- #
190- # string(STRIP ${GIT_SUBMODULE_VERSIONS} GIT_SUBMODULE_VERSIONS)
191- # string(REPLACE lib/ "" GIT_SUBMODULE_VERSIONS ${GIT_SUBMODULE_VERSIONS})
192-
193202 cmake_print_variables(GIT_VERSION)
194- cmake_print_variables(GIT_SUBMODULE_VERSIONS)
195-
196203 target_compile_definitions (${TARGET} PUBLIC
197204 UF2_VERSION_BASE="${GIT_VERSION} "
198- UF2_VERSION="${GIT_VERSION} - ${GIT_SUBMODULE_VERSIONS} "
205+ UF2_VERSION="${GIT_VERSION} "
199206 )
200207endfunction ()
201208
202- function (family_configure_tinyuf2 TARGET OPT_MCU)
203- family_configure_common(${TARGET} )
204-
205- include (${TOP} /src/tinyuf2.cmake)
206- add_tinyuf2(${TARGET} )
207-
208- family_add_uf2version(${TARGET} "${FAMILY_SUBMODULE_DEPS} " )
209- family_add_tinyusb(${TARGET} ${OPT_MCU} none)
210- endfunction ()
211-
212209
213210# Add bin/hex output
214211function (family_add_bin_hex TARGET )
@@ -239,12 +236,6 @@ function(family_add_uf2 TARGET FAMILY_ID)
239236 VERBATIM )
240237endfunction ()
241238
242- function (family_add_linkermap TARGET )
243- add_custom_command (TARGET ${TARGET} POST_BUILD
244- COMMAND linkermap -v $<TARGET_FILE:${TARGET} >.map
245- VERBATIM )
246- endfunction ()
247-
248239#----------------------------------
249240# Flashing target
250241#----------------------------------
0 commit comments