Skip to content

Commit 761eada

Browse files
tejlmandnashif
authored andcommitted
kconfig: cmake: CMake linker script generator symbol added
A "Linker script" choice group has been added to the linker options menu. The existing HAVE_CUSTOM_LINKER_SCRIPT config has been relocated inside the linker script menu. Two new entries has been created: - LD template, for using the old ld template files together with the C pre-processor to create the final linker script. - CMake linker generator, used for ARMClang / armlink toolchain. The CMake linker generator is also supported for LD linker scripts for the ARM architecture. In CMake, the file cmake/linker/ld/target.cmake has been updated to support the CMake LD linker generator. Signed-off-by: Torsten Rasmussen <[email protected]>
1 parent e9e47a4 commit 761eada

File tree

2 files changed

+95
-44
lines changed

2 files changed

+95
-44
lines changed

Kconfig.zephyr

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,36 @@ config ROM_START_OFFSET
132132
alignment requirements on most ARM targets, although some targets
133133
may require smaller or larger values.
134134

135+
config LD_LINKER_SCRIPT_SUPPORTED
136+
bool
137+
default y
138+
139+
choice LINKER_SCRIPT
140+
prompt "Linker script"
141+
default LD_LINKER_TEMPLATE if LD_LINKER_SCRIPT_SUPPORTED
142+
143+
config LD_LINKER_TEMPLATE
144+
bool "LD template"
145+
depends on LD_LINKER_SCRIPT_SUPPORTED
146+
help
147+
Select this option to use the LD linker script templates.
148+
The templates are pre-processed by the C pre-processor to create the
149+
final LD linker script.
150+
151+
config CMAKE_LINKER_GENERATOR
152+
bool "CMake generator"
153+
depends on ARM
154+
help
155+
Select this option to use the Zephyr CMake linker script generator.
156+
The linker configuration is written in CMake and the final linker
157+
script will be generated by the toolchain specific linker generator.
158+
For LD based linkers, this will be the ld generator, for ARMClang /
159+
armlink based linkers it will be the scatter generator.
160+
161+
endchoice
162+
135163
config HAVE_CUSTOM_LINKER_SCRIPT
136-
bool "Custom linker scripts provided"
164+
bool "Custom linker script provided"
137165
help
138166
Set this option if you have a custom linker script which needed to
139167
be define in CUSTOM_LINKER_SCRIPT.

cmake/linker/ld/target.cmake

Lines changed: 66 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -31,51 +31,74 @@ endif()
3131
macro(configure_linker_script linker_script_gen linker_pass_define)
3232
set(extra_dependencies ${ARGN})
3333

34-
# Different generators deal with depfiles differently.
35-
if(CMAKE_GENERATOR STREQUAL "Unix Makefiles")
36-
# Note that the IMPLICIT_DEPENDS option is currently supported only
37-
# for Makefile generators and will be ignored by other generators.
38-
set(linker_script_dep IMPLICIT_DEPENDS C ${LINKER_SCRIPT})
39-
elseif(CMAKE_GENERATOR STREQUAL "Ninja")
40-
# Using DEPFILE with other generators than Ninja is an error.
41-
set(linker_script_dep DEPFILE ${PROJECT_BINARY_DIR}/${linker_script_gen}.dep)
34+
if(CONFIG_CMAKE_LINKER_GENERATOR)
35+
if("${linker_pass_define}" STREQUAL "-DLINKER_ZEPHYR_PREBUILT")
36+
set(PASS 1)
37+
elseif("${linker_pass_define}" STREQUAL "-DLINKER_ZEPHYR_FINAL;-DLINKER_PASS2")
38+
set(PASS 2)
39+
endif()
40+
41+
add_custom_command(
42+
OUTPUT ${linker_script_gen}
43+
COMMAND ${CMAKE_COMMAND}
44+
-DPASS=${PASS}
45+
-DFORMAT="$<TARGET_PROPERTY:linker,FORMAT>"
46+
-DENTRY="$<TARGET_PROPERTY:linker,ENTRY>"
47+
-DMEMORY_REGIONS="$<TARGET_PROPERTY:linker,MEMORY_REGIONS>"
48+
-DGROUPS="$<TARGET_PROPERTY:linker,GROUPS>"
49+
-DSECTIONS="$<TARGET_PROPERTY:linker,SECTIONS>"
50+
-DSECTION_SETTINGS="$<TARGET_PROPERTY:linker,SECTION_SETTINGS>"
51+
-DSYMBOLS="$<TARGET_PROPERTY:linker,SYMBOLS>"
52+
-DOUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/${linker_script_gen}
53+
-P ${ZEPHYR_BASE}/cmake/linker/ld/ld_script.cmake
54+
)
4255
else()
43-
# TODO: How would the linker script dependencies work for non-linker
44-
# script generators.
45-
message(STATUS "Warning; this generator is not well supported. The
46-
Linker script may not be regenerated when it should.")
47-
set(linker_script_dep "")
48-
endif()
56+
# Different generators deal with depfiles differently.
57+
if(CMAKE_GENERATOR STREQUAL "Unix Makefiles")
58+
# Note that the IMPLICIT_DEPENDS option is currently supported only
59+
# for Makefile generators and will be ignored by other generators.
60+
set(linker_script_dep IMPLICIT_DEPENDS C ${LINKER_SCRIPT})
61+
elseif(CMAKE_GENERATOR STREQUAL "Ninja")
62+
# Using DEPFILE with other generators than Ninja is an error.
63+
set(linker_script_dep DEPFILE ${PROJECT_BINARY_DIR}/${linker_script_gen}.dep)
64+
else()
65+
# TODO: How would the linker script dependencies work for non-linker
66+
# script generators.
67+
message(STATUS "Warning; this generator is not well supported. The
68+
Linker script may not be regenerated when it should.")
69+
set(linker_script_dep "")
70+
endif()
4971

50-
zephyr_get_include_directories_for_lang(C current_includes)
51-
get_filename_component(base_name ${CMAKE_CURRENT_BINARY_DIR} NAME)
52-
get_property(current_defines GLOBAL PROPERTY PROPERTY_LINKER_SCRIPT_DEFINES)
53-
54-
add_custom_command(
55-
OUTPUT ${linker_script_gen}
56-
DEPENDS
57-
${LINKER_SCRIPT}
58-
${AUTOCONF_H}
59-
${extra_dependencies}
60-
# NB: 'linker_script_dep' will use a keyword that ends 'DEPENDS'
61-
${linker_script_dep}
62-
COMMAND ${CMAKE_C_COMPILER}
63-
-x assembler-with-cpp
64-
${NOSYSDEF_CFLAG}
65-
-MD -MF ${linker_script_gen}.dep -MT ${base_name}/${linker_script_gen}
66-
-D_LINKER
67-
-D_ASMLANGUAGE
68-
-imacros ${AUTOCONF_H}
69-
${current_includes}
70-
${current_defines}
71-
${linker_pass_define}
72-
-E ${LINKER_SCRIPT}
73-
-P # Prevent generation of debug `#line' directives.
74-
-o ${linker_script_gen}
75-
VERBATIM
76-
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
77-
COMMAND_EXPAND_LISTS
78-
)
72+
zephyr_get_include_directories_for_lang(C current_includes)
73+
get_filename_component(base_name ${CMAKE_CURRENT_BINARY_DIR} NAME)
74+
get_property(current_defines GLOBAL PROPERTY PROPERTY_LINKER_SCRIPT_DEFINES)
75+
76+
add_custom_command(
77+
OUTPUT ${linker_script_gen}
78+
DEPENDS
79+
${LINKER_SCRIPT}
80+
${AUTOCONF_H}
81+
${extra_dependencies}
82+
# NB: 'linker_script_dep' will use a keyword that ends 'DEPENDS'
83+
${linker_script_dep}
84+
COMMAND ${CMAKE_C_COMPILER}
85+
-x assembler-with-cpp
86+
${NOSYSDEF_CFLAG}
87+
-MD -MF ${linker_script_gen}.dep -MT ${base_name}/${linker_script_gen}
88+
-D_LINKER
89+
-D_ASMLANGUAGE
90+
-imacros ${AUTOCONF_H}
91+
${current_includes}
92+
${current_defines}
93+
${linker_pass_define}
94+
-E ${LINKER_SCRIPT}
95+
-P # Prevent generation of debug `#line' directives.
96+
-o ${linker_script_gen}
97+
VERBATIM
98+
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
99+
COMMAND_EXPAND_LISTS
100+
)
101+
endif()
79102
endmacro()
80103

81104
# Force symbols to be entered in the output file as undefined symbols

0 commit comments

Comments
 (0)