Skip to content

Commit 19a597b

Browse files
committed
add lto for cmake
1 parent ddc029c commit 19a597b

File tree

10 files changed

+203
-186
lines changed

10 files changed

+203
-186
lines changed

hw/bsp/family_support.cmake

Lines changed: 165 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -7,141 +7,145 @@ set(TOP "${CMAKE_CURRENT_LIST_DIR}/../..")
77

88
# Default to gcc
99
if (NOT DEFINED TOOLCHAIN)
10-
set(TOOLCHAIN gcc)
10+
set(TOOLCHAIN gcc)
1111
endif ()
1212

1313
if (NOT FAMILY)
14-
message(FATAL_ERROR "You must set a FAMILY variable for the build (e.g. rp2040, eps32s2, esp32s3). You can do this via -DFAMILY=xxx on the cmake command line")
14+
message(FATAL_ERROR "You must set a FAMILY variable for the build (e.g. rp2040, eps32s2, esp32s3). You can do this via -DFAMILY=xxx on the cmake command line")
1515
endif ()
1616

1717
if (NOT EXISTS ${CMAKE_CURRENT_LIST_DIR}/${FAMILY}/family.cmake)
18-
message(FATAL_ERROR "Family '${FAMILY}' is not known/supported")
18+
message(FATAL_ERROR "Family '${FAMILY}' is not known/supported")
1919
endif()
2020

21+
2122
function(family_filter RESULT DIR)
22-
get_filename_component(DIR ${DIR} ABSOLUTE BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
23-
24-
if (EXISTS "${DIR}/only.txt")
25-
file(READ "${DIR}/only.txt" ONLYS)
26-
# Replace newlines with semicolon so that it is treated as a list by CMake
27-
string(REPLACE "\n" ";" ONLYS_LINES ${ONLYS})
28-
# For each mcu
29-
foreach(MCU IN LISTS FAMILY_MCUS)
30-
# For each line in only.txt
31-
foreach(_line ${ONLYS_LINES})
32-
# If mcu:xxx exists for this mcu or board:xxx then include
33-
if (${_line} STREQUAL "mcu:${MCU}" OR ${_line} STREQUAL "board:${BOARD}")
34-
set(${RESULT} 1 PARENT_SCOPE)
35-
return()
36-
endif()
37-
endforeach()
38-
endforeach()
39-
40-
# Didn't find it in only file so don't build
41-
set(${RESULT} 0 PARENT_SCOPE)
42-
43-
elseif (EXISTS "${DIR}/skip.txt")
44-
file(READ "${DIR}/skip.txt" SKIPS)
45-
# Replace newlines with semicolon so that it is treated as a list by CMake
46-
string(REPLACE "\n" ";" SKIPS_LINES ${SKIPS})
47-
# For each mcu
48-
foreach(MCU IN LISTS FAMILY_MCUS)
49-
# For each line in only.txt
50-
foreach(_line ${SKIPS_LINES})
51-
# If mcu:xxx exists for this mcu then skip
52-
if (${_line} STREQUAL "mcu:${MCU}")
53-
set(${RESULT} 0 PARENT_SCOPE)
54-
return()
55-
endif()
56-
endforeach()
57-
endforeach()
58-
59-
# Didn't find in skip file so build
60-
set(${RESULT} 1 PARENT_SCOPE)
61-
62-
else()
63-
64-
# Didn't find skip or only file so build
65-
set(${RESULT} 1 PARENT_SCOPE)
23+
get_filename_component(DIR ${DIR} ABSOLUTE BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
24+
25+
if (EXISTS "${DIR}/only.txt")
26+
file(READ "${DIR}/only.txt" ONLYS)
27+
# Replace newlines with semicolon so that it is treated as a list by CMake
28+
string(REPLACE "\n" ";" ONLYS_LINES ${ONLYS})
29+
30+
# For each mcu
31+
foreach(MCU IN LISTS FAMILY_MCUS)
32+
# For each line in only.txt
33+
foreach(_line ${ONLYS_LINES})
34+
# If mcu:xxx exists for this mcu or board:xxx then include
35+
if (${_line} STREQUAL "mcu:${MCU}" OR ${_line} STREQUAL "board:${BOARD}")
36+
set(${RESULT} 1 PARENT_SCOPE)
37+
return()
38+
endif()
39+
endforeach()
40+
endforeach()
41+
42+
# Didn't find it in only file so don't build
43+
set(${RESULT} 0 PARENT_SCOPE)
44+
45+
elseif (EXISTS "${DIR}/skip.txt")
46+
file(READ "${DIR}/skip.txt" SKIPS)
47+
# Replace newlines with semicolon so that it is treated as a list by CMake
48+
string(REPLACE "\n" ";" SKIPS_LINES ${SKIPS})
49+
50+
# For each mcu
51+
foreach(MCU IN LISTS FAMILY_MCUS)
52+
# For each line in only.txt
53+
foreach(_line ${SKIPS_LINES})
54+
# If mcu:xxx exists for this mcu then skip
55+
if (${_line} STREQUAL "mcu:${MCU}")
56+
set(${RESULT} 0 PARENT_SCOPE)
57+
return()
58+
endif()
59+
endforeach()
60+
endforeach()
6661

67-
endif()
62+
# Didn't find in skip file so build
63+
set(${RESULT} 1 PARENT_SCOPE)
64+
else()
6865

66+
# Didn't find skip or only file so build
67+
set(${RESULT} 1 PARENT_SCOPE)
68+
endif()
6969
endfunction()
7070

71+
7172
function(family_add_subdirectory DIR)
72-
family_filter(SHOULD_ADD "${DIR}")
73-
if (SHOULD_ADD)
74-
add_subdirectory(${DIR})
75-
endif()
73+
family_filter(SHOULD_ADD "${DIR}")
74+
if (SHOULD_ADD)
75+
add_subdirectory(${DIR})
76+
endif()
7677
endfunction()
7778

79+
7880
function(family_get_project_name OUTPUT_NAME DIR)
79-
get_filename_component(SHORT_NAME ${DIR} NAME)
80-
set(${OUTPUT_NAME} ${TINYUSB_FAMILY_PROJECT_NAME_PREFIX}${SHORT_NAME} PARENT_SCOPE)
81+
get_filename_component(SHORT_NAME ${DIR} NAME)
82+
set(${OUTPUT_NAME} ${TINYUSB_FAMILY_PROJECT_NAME_PREFIX}${SHORT_NAME} PARENT_SCOPE)
8183
endfunction()
8284

85+
8386
function(family_initialize_project PROJECT DIR)
84-
family_filter(ALLOWED "${DIR}")
85-
if (NOT ALLOWED)
86-
get_filename_component(SHORT_NAME ${DIR} NAME)
87-
message(FATAL_ERROR "${SHORT_NAME} is not supported on FAMILY=${FAMILY}")
88-
endif()
87+
# set output suffix to .elf
88+
set(CMAKE_EXECUTABLE_SUFFIX .elf PARENT_SCOPE)
89+
90+
family_filter(ALLOWED "${DIR}")
91+
if (NOT ALLOWED)
92+
get_filename_component(SHORT_NAME ${DIR} NAME)
93+
message(FATAL_ERROR "${SHORT_NAME} is not supported on FAMILY=${FAMILY}")
94+
endif()
8995
endfunction()
9096

97+
9198
function(family_add_default_example_warnings TARGET)
92-
target_compile_options(${TARGET} PUBLIC
93-
-Wall
94-
-Wextra
95-
-Werror
96-
-Wfatal-errors
97-
-Wdouble-promotion
98-
-Wfloat-equal
99-
-Wshadow
100-
-Wwrite-strings
101-
-Wsign-compare
102-
-Wmissing-format-attribute
103-
-Wunreachable-code
104-
-Wcast-align
105-
-Wcast-qual
106-
-Wnull-dereference
107-
-Wuninitialized
108-
-Wunused
109-
-Wredundant-decls
110-
#-Wstrict-prototypes
111-
#-Werror-implicit-function-declaration
112-
#-Wundef
113-
)
114-
115-
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
116-
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 12.0)
117-
target_link_options(${TARGET} PUBLIC "LINKER:--no-warn-rwx-segments")
118-
endif()
99+
target_compile_options(${TARGET} PUBLIC
100+
-Wall
101+
-Wextra
102+
-Werror
103+
-Wfatal-errors
104+
-Wdouble-promotion
105+
-Wfloat-equal
106+
-Wshadow
107+
-Wwrite-strings
108+
-Wsign-compare
109+
-Wmissing-format-attribute
110+
-Wunreachable-code
111+
-Wcast-align
112+
-Wcast-qual
113+
-Wnull-dereference
114+
-Wuninitialized
115+
-Wunused
116+
-Wredundant-decls
117+
#-Wstrict-prototypes
118+
#-Werror-implicit-function-declaration
119+
#-Wundef
120+
)
119121

120-
# GCC 10
121-
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 10.0)
122-
target_compile_options(${TARGET} PUBLIC -Wconversion)
123-
endif()
122+
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
123+
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 12.0)
124+
target_link_options(${TARGET} PUBLIC "LINKER:--no-warn-rwx-segments")
125+
endif()
124126

125-
# GCC 8
126-
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 8.0)
127-
target_compile_options(${TARGET} PUBLIC -Wcast-function-type -Wstrict-overflow)
128-
endif()
127+
# GCC 10
128+
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 10.0)
129+
target_compile_options(${TARGET} PUBLIC -Wconversion)
130+
endif()
129131

130-
# GCC 6
131-
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 6.0)
132-
target_compile_options(${TARGET} PUBLIC -Wno-strict-aliasing)
133-
endif()
132+
# GCC 8
133+
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 8.0)
134+
target_compile_options(${TARGET} PUBLIC -Wcast-function-type -Wstrict-overflow)
134135
endif()
135-
endfunction()
136136

137+
# GCC 6
138+
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 6.0)
139+
target_compile_options(${TARGET} PUBLIC -Wno-strict-aliasing)
140+
endif()
141+
endif()
142+
endfunction()
137143

138-
function(family_support_configure_common TARGET)
139-
# set output name to .elf
140-
set_target_properties(${TARGET} PROPERTIES OUTPUT_NAME ${TARGET}.elf)
141144

145+
function(family_configure_common TARGET)
142146
# run size after build
143147
add_custom_command(TARGET ${TARGET} POST_BUILD
144-
COMMAND ${TOOLCHAIN_SIZE} $<TARGET_FILE:${TARGET}>
148+
COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${TARGET}>
145149
)
146150

147151
# Generate map file
@@ -151,11 +155,15 @@ function(family_support_configure_common TARGET)
151155
)
152156
endfunction()
153157

154-
# add_custom_command(TARGET ${TARGET} POST_BUILD
155-
# COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${TARGET}> ${TARGET}.hex
156-
# COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${TARGET}> ${TARGET}.bin
157-
# COMMENT "Creating ${TARGET}.hex and ${TARGET}.bin"
158-
# )
158+
159+
# Add bin/hex output
160+
function(family_add_bin_hex TARGET)
161+
add_custom_command(TARGET ${TARGET} POST_BUILD
162+
COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${TARGET}> $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.bin
163+
COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${TARGET}> $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.hex
164+
VERBATIM)
165+
endfunction()
166+
159167

160168
# Add flash jlink target
161169
function(family_flash_jlink TARGET)
@@ -178,78 +186,84 @@ exit"
178186
)
179187
endfunction()
180188

189+
181190
# Add flash pycod target
182191
function(family_flash_pyocd TARGET)
183-
if (NOT DEFINED PYOC)
184-
set(PYOCD pyocd)
185-
endif ()
192+
if (NOT DEFINED PYOC)
193+
set(PYOCD pyocd)
194+
endif ()
186195

187-
add_custom_target(${TARGET}-pyocd
188-
DEPENDS ${TARGET}
189-
COMMAND ${PYOCD} flash -t ${PYOCD_TARGET} $<TARGET_FILE:${TARGET}>
190-
)
196+
add_custom_target(${TARGET}-pyocd
197+
DEPENDS ${TARGET}
198+
COMMAND ${PYOCD} flash -t ${PYOCD_TARGET} $<TARGET_FILE:${TARGET}>
199+
)
191200
endfunction()
192201

202+
193203
# Add flash using NXP's LinkServer (redserver)
194204
# https://www.nxp.com/design/software/development-software/mcuxpresso-software-and-tools-/linkserver-for-microcontrollers:LINKERSERVER
195205
function(family_flash_nxplink TARGET)
196-
if (NOT DEFINED LINKSERVER)
197-
set(LINKSERVER LinkServer)
198-
endif ()
206+
if (NOT DEFINED LINKSERVER)
207+
set(LINKSERVER LinkServer)
208+
endif ()
199209

200-
# LinkServer has a bug that can only execute with full path otherwise it throws:
201-
# realpath error: No such file or directory
202-
execute_process(COMMAND which ${LINKSERVER} OUTPUT_VARIABLE LINKSERVER_PATH OUTPUT_STRIP_TRAILING_WHITESPACE)
210+
# LinkServer has a bug that can only execute with full path otherwise it throws:
211+
# realpath error: No such file or directory
212+
execute_process(COMMAND which ${LINKSERVER} OUTPUT_VARIABLE LINKSERVER_PATH OUTPUT_STRIP_TRAILING_WHITESPACE)
203213

204-
add_custom_target(${TARGET}-nxplink
205-
DEPENDS ${TARGET}
206-
COMMAND ${LINKSERVER_PATH} flash ${NXPLINK_DEVICE} load $<TARGET_FILE:${TARGET}>
207-
)
214+
add_custom_target(${TARGET}-nxplink
215+
DEPENDS ${TARGET}
216+
COMMAND ${LINKSERVER_PATH} flash ${NXPLINK_DEVICE} load $<TARGET_FILE:${TARGET}>
217+
)
208218
endfunction()
209219

220+
210221
# configure an executable target to link to tinyusb in device mode, and add the board implementation
211222
function(family_configure_device_example TARGET)
212-
# default implementation is empty, the function should be redefined in the FAMILY/family.cmake
223+
# default implementation is empty, the function should be redefined in the FAMILY/family.cmake
213224
endfunction()
214225

226+
215227
# configure an executable target to link to tinyusb in host mode, and add the board implementation
216228
function(family_configure_host_example TARGET)
217-
# default implementation is empty, the function should be redefined in the FAMILY/family.cmake
229+
# default implementation is empty, the function should be redefined in the FAMILY/family.cmake
218230
endfunction()
219231

232+
220233
# Add freeRTOS support to example, can be overridden by FAMILY/family.cmake
221234
function(family_add_freertos TARGET)
222-
# freeros config
223-
if (NOT TARGET freertos_config)
224-
add_library(freertos_config INTERFACE)
225-
target_include_directories(freertos_config SYSTEM INTERFACE
226-
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/${FAMILY}/FreeRTOSConfig
227-
)
228-
endif()
235+
# freeros config
236+
if (NOT TARGET freertos_config)
237+
add_library(freertos_config INTERFACE)
238+
target_include_directories(freertos_config SYSTEM INTERFACE
239+
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/${FAMILY}/FreeRTOSConfig
240+
)
241+
endif()
229242

230-
# freertos kernel should be generic as freertos_config however, CMAKE complains with missing variable
231-
# such as CMAKE_C_COMPILE_OBJECT
232-
if (NOT TARGET freertos_kernel)
233-
add_subdirectory(${TOP}/lib/FreeRTOS-Kernel ${CMAKE_BINARY_DIR}/lib/freertos_kernel)
234-
endif ()
243+
# freertos kernel should be generic as freertos_config however, CMAKE complains with missing variable
244+
# such as CMAKE_C_COMPILE_OBJECT
245+
if (NOT TARGET freertos_kernel)
246+
add_subdirectory(${TOP}/lib/FreeRTOS-Kernel ${CMAKE_BINARY_DIR}/lib/freertos_kernel)
247+
endif ()
235248

236-
# Add FreeRTOS option to tinyusb_config
237-
target_compile_definitions(${TARGET}-tinyusb_config INTERFACE
238-
CFG_TUSB_OS=OPT_OS_FREERTOS
239-
)
240-
# link tinyusb with freeRTOS kernel
241-
target_link_libraries(${TARGET}-tinyusb PUBLIC
242-
freertos_kernel
243-
)
244-
target_link_libraries(${TARGET} PUBLIC
245-
freertos_kernel
246-
)
249+
# Add FreeRTOS option to tinyusb_config
250+
target_compile_definitions(${TARGET}-tinyusb_config INTERFACE
251+
CFG_TUSB_OS=OPT_OS_FREERTOS
252+
)
253+
# link tinyusb with freeRTOS kernel
254+
target_link_libraries(${TARGET}-tinyusb PUBLIC
255+
freertos_kernel
256+
)
257+
target_link_libraries(${TARGET} PUBLIC
258+
freertos_kernel
259+
)
247260
endfunction()
248261

262+
249263
include(${CMAKE_CURRENT_LIST_DIR}/${FAMILY}/family.cmake)
250264

251265
if (NOT FAMILY_MCUS)
252-
set(FAMILY_MCUS ${FAMILY})
266+
set(FAMILY_MCUS ${FAMILY})
253267
endif()
254268

255269
# save it in case of re-inclusion

0 commit comments

Comments
 (0)