|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +find_program(CMAKE_LINKER ld.lld ) |
| 4 | + |
| 5 | +set_ifndef(LINKERFLAGPREFIX -Wl) |
| 6 | + |
| 7 | +# Run $LINKER_SCRIPT file through the C preprocessor, producing ${linker_script_gen} |
| 8 | +# NOTE: ${linker_script_gen} will be produced at build-time; not at configure-time |
| 9 | +macro(configure_linker_script linker_script_gen linker_pass_define) |
| 10 | + set(extra_dependencies ${ARGN}) |
| 11 | + |
| 12 | + # Different generators deal with depfiles differently. |
| 13 | + if(CMAKE_GENERATOR STREQUAL "Unix Makefiles") |
| 14 | + # Note that the IMPLICIT_DEPENDS option is currently supported only |
| 15 | + # for Makefile generators and will be ignored by other generators. |
| 16 | + set(linker_script_dep IMPLICIT_DEPENDS C ${LINKER_SCRIPT}) |
| 17 | + elseif(CMAKE_GENERATOR STREQUAL "Ninja") |
| 18 | + # Using DEPFILE with other generators than Ninja is an error. |
| 19 | + set(linker_script_dep DEPFILE ${PROJECT_BINARY_DIR}/${linker_script_gen}.dep) |
| 20 | + else() |
| 21 | + # TODO: How would the linker script dependencies work for non-linker |
| 22 | + # script generators. |
| 23 | + message(STATUS "Warning; this generator is not well supported. The |
| 24 | + Linker script may not be regenerated when it should.") |
| 25 | + set(linker_script_dep "") |
| 26 | + endif() |
| 27 | + |
| 28 | + zephyr_get_include_directories_for_lang(C current_includes) |
| 29 | + get_filename_component(base_name ${CMAKE_CURRENT_BINARY_DIR} NAME) |
| 30 | + get_property(current_defines GLOBAL PROPERTY PROPERTY_LINKER_SCRIPT_DEFINES) |
| 31 | + |
| 32 | + add_custom_command( |
| 33 | + OUTPUT ${linker_script_gen} |
| 34 | + DEPENDS |
| 35 | + ${LINKER_SCRIPT} |
| 36 | + ${extra_dependencies} |
| 37 | + # NB: 'linker_script_dep' will use a keyword that ends 'DEPENDS' |
| 38 | + ${linker_script_dep} |
| 39 | + COMMAND ${CMAKE_C_COMPILER} |
| 40 | + -x assembler-with-cpp |
| 41 | + ${NOSYSDEF_CFLAG} |
| 42 | + -MD -MF ${linker_script_gen}.dep -MT ${base_name}/${linker_script_gen} |
| 43 | + -D_LINKER |
| 44 | + -D_ASMLANGUAGE |
| 45 | + ${current_includes} |
| 46 | + ${current_defines} |
| 47 | + ${linker_pass_define} |
| 48 | + -E ${LINKER_SCRIPT} |
| 49 | + -P # Prevent generation of debug `#line' directives. |
| 50 | + -o ${linker_script_gen} |
| 51 | + VERBATIM |
| 52 | + WORKING_DIRECTORY ${PROJECT_BINARY_DIR} |
| 53 | + COMMAND_EXPAND_LISTS |
| 54 | + ) |
| 55 | +endmacro() |
| 56 | + |
| 57 | +# Force symbols to be entered in the output file as undefined symbols |
| 58 | +function(toolchain_ld_force_undefined_symbols) |
| 59 | + foreach(symbol ${ARGN}) |
| 60 | + zephyr_link_libraries(${LINKERFLAGPREFIX},-u,${symbol}) |
| 61 | + endforeach() |
| 62 | +endfunction() |
| 63 | + |
| 64 | +# Link a target to given libraries with toolchain-specific argument order |
| 65 | +# |
| 66 | +# Usage: |
| 67 | +# toolchain_ld_link_elf( |
| 68 | +# TARGET_ELF <target_elf> |
| 69 | +# OUTPUT_MAP <output_map_file_of_target> |
| 70 | +# LIBRARIES_PRE_SCRIPT [libraries_pre_script] |
| 71 | +# LINKER_SCRIPT <linker_script> |
| 72 | +# LIBRARIES_POST_SCRIPT [libraries_post_script] |
| 73 | +# DEPENDENCIES [dependencies] |
| 74 | +# ) |
| 75 | +function(toolchain_ld_link_elf) |
| 76 | + cmake_parse_arguments( |
| 77 | + TOOLCHAIN_LD_LINK_ELF # prefix of output variables |
| 78 | + "" # list of names of the boolean arguments |
| 79 | + "TARGET_ELF;OUTPUT_MAP;LINKER_SCRIPT" # list of names of scalar arguments |
| 80 | + "LIBRARIES_PRE_SCRIPT;LIBRARIES_POST_SCRIPT;DEPENDENCIES" # list of names of list arguments |
| 81 | + ${ARGN} # input args to parse |
| 82 | + ) |
| 83 | + |
| 84 | + target_link_libraries( |
| 85 | + ${TOOLCHAIN_LD_LINK_ELF_TARGET_ELF} |
| 86 | + ${TOOLCHAIN_LD_LINK_ELF_LIBRARIES_PRE_SCRIPT} |
| 87 | + ${TOPT} |
| 88 | + ${TOOLCHAIN_LD_LINK_ELF_LINKER_SCRIPT} |
| 89 | + ${TOOLCHAIN_LD_LINK_ELF_LIBRARIES_POST_SCRIPT} |
| 90 | + |
| 91 | + ${LINKERFLAGPREFIX},-Map=${TOOLCHAIN_LD_LINK_ELF_OUTPUT_MAP} |
| 92 | + ${LINKERFLAGPREFIX},--whole-archive |
| 93 | + ${ZEPHYR_LIBS_PROPERTY} |
| 94 | + ${LINKERFLAGPREFIX},--no-whole-archive |
| 95 | + kernel |
| 96 | + $<TARGET_OBJECTS:${OFFSETS_LIB}> |
| 97 | + ${LIB_INCLUDE_DIR} |
| 98 | + -L${PROJECT_BINARY_DIR} |
| 99 | + ${TOOLCHAIN_LIBS} |
| 100 | + |
| 101 | + ${TOOLCHAIN_LD_LINK_ELF_DEPENDENCIES} |
| 102 | + ) |
| 103 | +endfunction(toolchain_ld_link_elf) |
| 104 | + |
| 105 | + |
| 106 | +# Load toolchain_ld-family macros |
| 107 | +include(${ZEPHYR_BASE}/cmake/linker/ld/target_base.cmake) |
| 108 | +include(${ZEPHYR_BASE}/cmake/linker/${LINKER}/target_baremetal.cmake) |
| 109 | +include(${ZEPHYR_BASE}/cmake/linker/ld/target_cpp.cmake) |
| 110 | +include(${ZEPHYR_BASE}/cmake/linker/ld/target_relocation.cmake) |
| 111 | +include(${ZEPHYR_BASE}/cmake/linker/ld/target_configure.cmake) |
0 commit comments