Skip to content

Commit 894587f

Browse files
committed
cmake: Pass OPTIMIZATION_FLAG to linker too
The compiler may want to know the desired optimization level during linking, as when the compiler multilib configuration includes -Os as a selector. Do this by adding a new (optional) linker function, toolchain_linker_add_compiler_options, which maps compiler options to equivalent linker options, discarding any that aren't applicable. Linker configurations which use the compiler driver (ld, lld, and xt-ld) apply all provided compiler options. Linkers which don't provide this function fall back to an implementation which simply discards all options. Signed-off-by: Keith Packard <[email protected]>
1 parent d453fb9 commit 894587f

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ endif()
242242
# Apply the final optimization flag(s)
243243
zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:${OPTIMIZATION_FLAG}>)
244244
zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:${OPTIMIZATION_FLAG}>)
245+
toolchain_linker_add_compiler_options(${OPTIMIZATION_FLAG})
245246

246247
if(CONFIG_LTO)
247248
zephyr_compile_options($<TARGET_PROPERTY:compiler,optimization_lto>)

cmake/linker/ld/target.cmake

+8
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,14 @@ macro(toolchain_linker_finalize)
168168
set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> ${cpp_link}")
169169
endmacro()
170170

171+
# Function to map compiler flags into suitable linker flags
172+
# When using the compiler driver to run the linker, just pass
173+
# them all through
174+
175+
function(toolchain_linker_add_compiler_options)
176+
add_link_options(${ARGV})
177+
endfunction()
178+
171179
# Load toolchain_ld-family macros
172180
include(${ZEPHYR_BASE}/cmake/linker/${LINKER}/target_relocation.cmake)
173181
include(${ZEPHYR_BASE}/cmake/linker/${LINKER}/target_configure.cmake)

cmake/linker/lld/target.cmake

+8
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ macro(toolchain_linker_finalize)
128128
set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> ${common_link}")
129129
endmacro()
130130

131+
# Function to map compiler flags into suitable linker flags
132+
# When using the compiler driver to run the linker, just pass
133+
# them all through
134+
135+
function(toolchain_linker_add_compiler_options)
136+
add_link_options(${ARGV})
137+
endfunction()
138+
131139
# Load toolchain_ld-family macros
132140
include(${ZEPHYR_BASE}/cmake/linker/ld/target_relocation.cmake)
133141
include(${ZEPHYR_BASE}/cmake/linker/ld/target_configure.cmake)

cmake/linker/target_template.cmake

+11
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,14 @@ if(NOT COMMAND toolchain_linker_finalize)
1111
macro(toolchain_linker_finalize)
1212
endmacro()
1313
endif()
14+
15+
if(NOT COMMAND toolchain_linker_add_compiler_options)
16+
17+
# If the linker doesn't provide a method for mapping compiler options
18+
# to linker options, then assume we can't. This matters when the linker
19+
# is using additional compiler flags (like OPTIMIZATION_FLAG) when
20+
# computing toolchain library paths.
21+
22+
function(toolchain_linker_add_compiler_options)
23+
endfunction()
24+
endif()

cmake/linker/xt-ld/target.cmake

+8
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ macro(toolchain_linker_finalize)
156156
set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> ${common_link}")
157157
endmacro()
158158

159+
# Function to map compiler flags into suitable linker flags
160+
# When using the compiler driver to run the linker, just pass
161+
# them all through
162+
163+
function(toolchain_linker_add_compiler_options)
164+
add_link_options(${ARGV})
165+
endfunction()
166+
159167
# xt-ld is Xtensa's own version of binutils' ld.
160168
# So we can reuse most of the ld configurations.
161169
include(${ZEPHYR_BASE}/cmake/linker/ld/target_relocation.cmake)

0 commit comments

Comments
 (0)