Make Linker Script Available To An Upstream Dependency

I have a linker script (link1.ld) in depL that I need to use in depA.

Please could someone explain to me how make depL make the link1.ld script available to depA so that the following works:

target_link_options(depA PRIVATE -T#[[Valid reference to]]link1.ld)

The full directory structure and file content for my example is below.

Directory Structure

.
│   CMakeLists.txt
│
├───depA
│   │   CMakeLists.txt
│   │
│   └───src
│           depA.c
│
└───depL
    │   CMakeLists.txt
    │
    ├───src
    │       link1.ld
    │
    └───include
            mem_map.h

Top-Level CMakeLists.txt

cmake_minimum_required(VERSION 3.29)
project(proj_cmake1 C)

add_subdirectory(depA)
add_subdirectory(depL)

depL CMakeLists.txt

cmake_minimum_required(VERSION 3.29)
project(proj_depL C)

add_library(depL INTERFACE)
target_sources(depL INTERFACE src/link1.ld)
target_include_directories(depL INTERFACE include)

depA CMakeLists.txt

cmake_minimum_required(VERSION 3.29)
project(proj_depA C)

add_executable(depA src/depA.c)
target_link_libraries (depA depL)
target_link_options(depA PRIVATE -T#[[Valid reference to]]link1.ld)

A possible solution is to specify link option as part of depL target.
depL CMakeLists.txt:

add_library(depL INTERFACE)
target_link_options(depL INTERFACE -T${CMAKE_CURRENT_SOURCE_DIR}/src/link1.ld)
target_include_directories(depL INTERFACE include)

And linking depA with depL will be enough:

add_executable(depA src/depA.c)
target_link_libraries (depA PRIVATE depL)
1 Like

Thank you Marc, that works perfectly and results in a very elegant solution.