diff --git a/Kconfig.zephyr b/Kconfig.zephyr index 383e91c10efe3a..4f92cd07c568c6 100644 --- a/Kconfig.zephyr +++ b/Kconfig.zephyr @@ -132,8 +132,36 @@ config ROM_START_OFFSET alignment requirements on most ARM targets, although some targets may require smaller or larger values. +config LD_LINKER_SCRIPT_SUPPORTED + bool + default y + +choice LINKER_SCRIPT + prompt "Linker script" + default LD_LINKER_TEMPLATE if LD_LINKER_SCRIPT_SUPPORTED + +config LD_LINKER_TEMPLATE + bool "LD template" + depends on LD_LINKER_SCRIPT_SUPPORTED + help + Select this option to use the LD linker script templates. + The templates are pre-processed by the C pre-processor to create the + final LD linker script. + +config CMAKE_LINKER_GENERATOR + bool "CMake generator" + depends on ARM + help + Select this option to use the Zephyr CMake linker script generator. + The linker configuration is written in CMake and the final linker + script will be generated by the toolchain specific linker generator. + For LD based linkers, this will be the ld generator, for ARMClang / + armlink based linkers it will be the scatter generator. + +endchoice + config HAVE_CUSTOM_LINKER_SCRIPT - bool "Custom linker scripts provided" + bool "Custom linker script provided" help Set this option if you have a custom linker script which needed to be define in CUSTOM_LINKER_SCRIPT. diff --git a/cmake/linker/ld/target.cmake b/cmake/linker/ld/target.cmake index 2bef796673f01c..b83dc5bb041518 100644 --- a/cmake/linker/ld/target.cmake +++ b/cmake/linker/ld/target.cmake @@ -31,51 +31,74 @@ endif() macro(configure_linker_script linker_script_gen linker_pass_define) set(extra_dependencies ${ARGN}) - # Different generators deal with depfiles differently. - if(CMAKE_GENERATOR STREQUAL "Unix Makefiles") - # Note that the IMPLICIT_DEPENDS option is currently supported only - # for Makefile generators and will be ignored by other generators. - set(linker_script_dep IMPLICIT_DEPENDS C ${LINKER_SCRIPT}) - elseif(CMAKE_GENERATOR STREQUAL "Ninja") - # Using DEPFILE with other generators than Ninja is an error. - set(linker_script_dep DEPFILE ${PROJECT_BINARY_DIR}/${linker_script_gen}.dep) + if(CONFIG_CMAKE_LINKER_GENERATOR) + if("${linker_pass_define}" STREQUAL "-DLINKER_ZEPHYR_PREBUILT") + set(PASS 1) + elseif("${linker_pass_define}" STREQUAL "-DLINKER_ZEPHYR_FINAL;-DLINKER_PASS2") + set(PASS 2) + endif() + + add_custom_command( + OUTPUT ${linker_script_gen} + COMMAND ${CMAKE_COMMAND} + -DPASS=${PASS} + -DFORMAT="$" + -DENTRY="$" + -DMEMORY_REGIONS="$" + -DGROUPS="$" + -DSECTIONS="$" + -DSECTION_SETTINGS="$" + -DSYMBOLS="$" + -DOUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/${linker_script_gen} + -P ${ZEPHYR_BASE}/cmake/linker/ld/ld_script.cmake + ) else() - # TODO: How would the linker script dependencies work for non-linker - # script generators. - message(STATUS "Warning; this generator is not well supported. The - Linker script may not be regenerated when it should.") - set(linker_script_dep "") - endif() + # Different generators deal with depfiles differently. + if(CMAKE_GENERATOR STREQUAL "Unix Makefiles") + # Note that the IMPLICIT_DEPENDS option is currently supported only + # for Makefile generators and will be ignored by other generators. + set(linker_script_dep IMPLICIT_DEPENDS C ${LINKER_SCRIPT}) + elseif(CMAKE_GENERATOR STREQUAL "Ninja") + # Using DEPFILE with other generators than Ninja is an error. + set(linker_script_dep DEPFILE ${PROJECT_BINARY_DIR}/${linker_script_gen}.dep) + else() + # TODO: How would the linker script dependencies work for non-linker + # script generators. + message(STATUS "Warning; this generator is not well supported. The + Linker script may not be regenerated when it should.") + set(linker_script_dep "") + endif() - zephyr_get_include_directories_for_lang(C current_includes) - get_filename_component(base_name ${CMAKE_CURRENT_BINARY_DIR} NAME) - get_property(current_defines GLOBAL PROPERTY PROPERTY_LINKER_SCRIPT_DEFINES) - - add_custom_command( - OUTPUT ${linker_script_gen} - DEPENDS - ${LINKER_SCRIPT} - ${AUTOCONF_H} - ${extra_dependencies} - # NB: 'linker_script_dep' will use a keyword that ends 'DEPENDS' - ${linker_script_dep} - COMMAND ${CMAKE_C_COMPILER} - -x assembler-with-cpp - ${NOSYSDEF_CFLAG} - -MD -MF ${linker_script_gen}.dep -MT ${base_name}/${linker_script_gen} - -D_LINKER - -D_ASMLANGUAGE - -imacros ${AUTOCONF_H} - ${current_includes} - ${current_defines} - ${linker_pass_define} - -E ${LINKER_SCRIPT} - -P # Prevent generation of debug `#line' directives. - -o ${linker_script_gen} - VERBATIM - WORKING_DIRECTORY ${PROJECT_BINARY_DIR} - COMMAND_EXPAND_LISTS - ) + zephyr_get_include_directories_for_lang(C current_includes) + get_filename_component(base_name ${CMAKE_CURRENT_BINARY_DIR} NAME) + get_property(current_defines GLOBAL PROPERTY PROPERTY_LINKER_SCRIPT_DEFINES) + + add_custom_command( + OUTPUT ${linker_script_gen} + DEPENDS + ${LINKER_SCRIPT} + ${AUTOCONF_H} + ${extra_dependencies} + # NB: 'linker_script_dep' will use a keyword that ends 'DEPENDS' + ${linker_script_dep} + COMMAND ${CMAKE_C_COMPILER} + -x assembler-with-cpp + ${NOSYSDEF_CFLAG} + -MD -MF ${linker_script_gen}.dep -MT ${base_name}/${linker_script_gen} + -D_LINKER + -D_ASMLANGUAGE + -imacros ${AUTOCONF_H} + ${current_includes} + ${current_defines} + ${linker_pass_define} + -E ${LINKER_SCRIPT} + -P # Prevent generation of debug `#line' directives. + -o ${linker_script_gen} + VERBATIM + WORKING_DIRECTORY ${PROJECT_BINARY_DIR} + COMMAND_EXPAND_LISTS + ) + endif() endmacro() # Force symbols to be entered in the output file as undefined symbols