Skip to content

Commit 77714e0

Browse files
authored
Merge pull request hathach#2088 from hathach/minor-cleanup
add lto for cmake
2 parents ddc029c + 62b2d05 commit 77714e0

File tree

10 files changed

+205
-186
lines changed

10 files changed

+205
-186
lines changed

hw/bsp/family_support.cmake

Lines changed: 167 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -7,141 +7,147 @@ 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 (skip espressif)
88+
if(NOT FAMILY STREQUAL "espressif")
89+
set(CMAKE_EXECUTABLE_SUFFIX .elf PARENT_SCOPE)
90+
endif()
91+
92+
family_filter(ALLOWED "${DIR}")
93+
if (NOT ALLOWED)
94+
get_filename_component(SHORT_NAME ${DIR} NAME)
95+
message(FATAL_ERROR "${SHORT_NAME} is not supported on FAMILY=${FAMILY}")
96+
endif()
8997
endfunction()
9098

99+
91100
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()
101+
target_compile_options(${TARGET} PUBLIC
102+
-Wall
103+
-Wextra
104+
-Werror
105+
-Wfatal-errors
106+
-Wdouble-promotion
107+
-Wfloat-equal
108+
-Wshadow
109+
-Wwrite-strings
110+
-Wsign-compare
111+
-Wmissing-format-attribute
112+
-Wunreachable-code
113+
-Wcast-align
114+
-Wcast-qual
115+
-Wnull-dereference
116+
-Wuninitialized
117+
-Wunused
118+
-Wredundant-decls
119+
#-Wstrict-prototypes
120+
#-Werror-implicit-function-declaration
121+
#-Wundef
122+
)
119123

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

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()
129+
# GCC 10
130+
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 10.0)
131+
target_compile_options(${TARGET} PUBLIC -Wconversion)
132+
endif()
129133

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()
134+
# GCC 8
135+
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 8.0)
136+
target_compile_options(${TARGET} PUBLIC -Wcast-function-type -Wstrict-overflow)
134137
endif()
135-
endfunction()
136138

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

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

147+
function(family_configure_common TARGET)
142148
# run size after build
143149
add_custom_command(TARGET ${TARGET} POST_BUILD
144-
COMMAND ${TOOLCHAIN_SIZE} $<TARGET_FILE:${TARGET}>
150+
COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${TARGET}>
145151
)
146152

147153
# Generate map file
@@ -151,11 +157,15 @@ function(family_support_configure_common TARGET)
151157
)
152158
endfunction()
153159

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-
# )
160+
161+
# Add bin/hex output
162+
function(family_add_bin_hex TARGET)
163+
add_custom_command(TARGET ${TARGET} POST_BUILD
164+
COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${TARGET}> $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.bin
165+
COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${TARGET}> $<TARGET_FILE_DIR:${TARGET}>/${TARGET}.hex
166+
VERBATIM)
167+
endfunction()
168+
159169

160170
# Add flash jlink target
161171
function(family_flash_jlink TARGET)
@@ -178,78 +188,84 @@ exit"
178188
)
179189
endfunction()
180190

191+
181192
# Add flash pycod target
182193
function(family_flash_pyocd TARGET)
183-
if (NOT DEFINED PYOC)
184-
set(PYOCD pyocd)
185-
endif ()
194+
if (NOT DEFINED PYOC)
195+
set(PYOCD pyocd)
196+
endif ()
186197

187-
add_custom_target(${TARGET}-pyocd
188-
DEPENDS ${TARGET}
189-
COMMAND ${PYOCD} flash -t ${PYOCD_TARGET} $<TARGET_FILE:${TARGET}>
190-
)
198+
add_custom_target(${TARGET}-pyocd
199+
DEPENDS ${TARGET}
200+
COMMAND ${PYOCD} flash -t ${PYOCD_TARGET} $<TARGET_FILE:${TARGET}>
201+
)
191202
endfunction()
192203

204+
193205
# Add flash using NXP's LinkServer (redserver)
194206
# https://www.nxp.com/design/software/development-software/mcuxpresso-software-and-tools-/linkserver-for-microcontrollers:LINKERSERVER
195207
function(family_flash_nxplink TARGET)
196-
if (NOT DEFINED LINKSERVER)
197-
set(LINKSERVER LinkServer)
198-
endif ()
208+
if (NOT DEFINED LINKSERVER)
209+
set(LINKSERVER LinkServer)
210+
endif ()
199211

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)
212+
# LinkServer has a bug that can only execute with full path otherwise it throws:
213+
# realpath error: No such file or directory
214+
execute_process(COMMAND which ${LINKSERVER} OUTPUT_VARIABLE LINKSERVER_PATH OUTPUT_STRIP_TRAILING_WHITESPACE)
203215

204-
add_custom_target(${TARGET}-nxplink
205-
DEPENDS ${TARGET}
206-
COMMAND ${LINKSERVER_PATH} flash ${NXPLINK_DEVICE} load $<TARGET_FILE:${TARGET}>
207-
)
216+
add_custom_target(${TARGET}-nxplink
217+
DEPENDS ${TARGET}
218+
COMMAND ${LINKSERVER_PATH} flash ${NXPLINK_DEVICE} load $<TARGET_FILE:${TARGET}>
219+
)
208220
endfunction()
209221

222+
210223
# configure an executable target to link to tinyusb in device mode, and add the board implementation
211224
function(family_configure_device_example TARGET)
212-
# default implementation is empty, the function should be redefined in the FAMILY/family.cmake
225+
# default implementation is empty, the function should be redefined in the FAMILY/family.cmake
213226
endfunction()
214227

228+
215229
# configure an executable target to link to tinyusb in host mode, and add the board implementation
216230
function(family_configure_host_example TARGET)
217-
# default implementation is empty, the function should be redefined in the FAMILY/family.cmake
231+
# default implementation is empty, the function should be redefined in the FAMILY/family.cmake
218232
endfunction()
219233

234+
220235
# Add freeRTOS support to example, can be overridden by FAMILY/family.cmake
221236
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()
237+
# freeros config
238+
if (NOT TARGET freertos_config)
239+
add_library(freertos_config INTERFACE)
240+
target_include_directories(freertos_config SYSTEM INTERFACE
241+
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/${FAMILY}/FreeRTOSConfig
242+
)
243+
endif()
229244

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 ()
245+
# freertos kernel should be generic as freertos_config however, CMAKE complains with missing variable
246+
# such as CMAKE_C_COMPILE_OBJECT
247+
if (NOT TARGET freertos_kernel)
248+
add_subdirectory(${TOP}/lib/FreeRTOS-Kernel ${CMAKE_BINARY_DIR}/lib/freertos_kernel)
249+
endif ()
235250

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-
)
251+
# Add FreeRTOS option to tinyusb_config
252+
target_compile_definitions(${TARGET}-tinyusb_config INTERFACE
253+
CFG_TUSB_OS=OPT_OS_FREERTOS
254+
)
255+
# link tinyusb with freeRTOS kernel
256+
target_link_libraries(${TARGET}-tinyusb PUBLIC
257+
freertos_kernel
258+
)
259+
target_link_libraries(${TARGET} PUBLIC
260+
freertos_kernel
261+
)
247262
endfunction()
248263

264+
249265
include(${CMAKE_CURRENT_LIST_DIR}/${FAMILY}/family.cmake)
250266

251267
if (NOT FAMILY_MCUS)
252-
set(FAMILY_MCUS ${FAMILY})
268+
set(FAMILY_MCUS ${FAMILY})
253269
endif()
254270

255271
# save it in case of re-inclusion

0 commit comments

Comments
 (0)