STib
STib

Reputation: 1

CMAKE: How to add a dependency to the link phase of the add_executable() command?

I would like to build an executable with a specific linker script file (my.linkcmds). And when I update my linker script, I would like to trigger the linker part of the add_executable().

I use the following CMakeLists.txt:

cmake_minimum_required (VERSION 3.22)
project (HELLO)

set(LNK_SCRIPT ${CMAKE_SOURCE_DIR}/my.linkcmds)
add_custom_target(hello_linkcmd DEPENDS ${LNK_SCRIPT})

add_executable       (hello main.c)
target_link_libraries(hello -T ${LNK_SCRIPT})
add_dependencies     (hello hello_linkcmd)

Problem: When I update my.linkcmds, it just updates the main target but not redo the link phase.

$ touch ../my.linkcmds
$ make
[  0%] Built target hello_linkcmd
[100%] Built target hello

My current workaround is to set the dependency to the source file to trigger the main.o rebuild and force the hello relink, but it is not elegant because source recompilation is not needed in this case!

set_source_files_properties(main.c OBJECT_DEPENDS ${LNK_SCRIPT_TEMPLATE})

What is the best way to add a dependency to the link phase of the add_executable() command ?

Upvotes: 0

Views: 132

Answers (1)

STib
STib

Reputation: 1

Ok, the solution was already on stackoverflow! (Thanks KamilCuk)

cmake_minimum_required (VERSION 3.22)
project (HELLO)

set(LNK_SCRIPT ${CMAKE_SOURCE_DIR}/my.linkcmds)

add_executable       (hello main.c)
target_link_libraries(hello -T ${LNK_SCRIPT})
set_target_properties(hello PROPERTIES LINK_DEPENDS ${LNK_SCRIPT})

Works well!

Upvotes: 0

Related Questions