Makefile: target with LINK_DEPENDS on custom command output says "no rule to make target"
Tested on CMake 3.22.1 Minimal example:
cmake_minimum_required(VERSION 3.22)
project("example" LANGUAGES C)
add_executable(example main.c)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/linkerscript.ld
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/linkerscript.in
COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/linkerscript.in ${CMAKE_CURRENT_BINARY_DIR}/linkerscript.ld
COMMENT Generates linkerscript with custom sections
)
# Notice that LINK_DEPENDS of example is set to the generated linkerscript
set_target_properties(example PROPERTIES LINK_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/linkerscript.ld)
target_link_options(example PRIVATE -T${CMAKE_CURRENT_BINARY_DIR}/linkerscript.ld)
when building with Makefiles, get the following output:
[ 50%] Building C object CMakeFiles/example.dir/main.c.o
gmake[2]: *** No rule to make target 'linkerscript.ld', needed by 'example'. Stop.
gmake[1]: *** [CMakeFiles/Makefile2:84: CMakeFiles/example.dir/all] Error 2
gmake: *** [Makefile:91: all] Error
When building with -GNinja
, linkerscript.ld
is generated successfully and used for linking.
main.c
- doesn't matter, example:
#include <stdio.h>
int main() {
printf("Hello world!\n");
}
linkerscript.in
- doesn't matter, can be an empty file.
If I add a custom target and get example
to depend on it then this works with Makefile as well:
add_custom_target(linkerscript DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/linkerscript.ld)
add_dependencies(example linkerscript)
Edited by Tal Risin