MantisBT - CMake
View Issue Details
0013846CMake(No Category)public2013-01-11 20:232016-06-10 14:31
szx 
Kitware Robot 
normalminorhave not tried
closedmoved 
 
 
0013846: Custom commands not working when comamnd output used in another directory
At least for the Unix Makefile generator (haven't tried others) custom commands are not called when their output file is specified as a source file for a target defined in the parent directory. As soon as I move both add_custom_command() and the target to the same file, everything works fine.
============ Case 1 =============

1. Create CMakeLists.txt with the following contents

cmake_minimum_required(VERSION 2.8.9)
project(test)

add_subdirectory("src")
set_source_files_properties("src/test.c" PROPERTIES GENERATED TRUE)
add_executable(test "src/test.c")

2. Create a directory called "src" and another CMakeLists.txt in it:

add_custom_command(
    OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/test.c"
    COMMAND echo "int main\\(\\) \\{\\}" > "${CMAKE_CURRENT_SOURCE_DIR}/test.c"
    WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)

3. You will get the following error from make:

test1-build$ make
make[2]: *** No rule to make target `src/test.c', needed by `CMakeFiles/test.dir/src/test.c.o'. Stop.
make[1]: *** [CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2

============ Case 2 =============

1. Create a CMakeLists.txt with the following contents:

cmake_minimum_required(VERSION 2.8.9)
project(test)

file(MAKE_DIRECTORY "src")
add_custom_command(
    OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/src/test.c"
    COMMAND echo "int main\\(\\) \\{\\}" > "${CMAKE_CURRENT_SOURCE_DIR}/src/test.c"
    WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
set_source_files_properties("src/test.c" PROPERTIES GENERATED TRUE)
add_executable(test "src/test.c")

2. make will complete without errors:

test2-build$ make
[ 50%] Generating /home/asd/Desktop/fuck-cmake/test2/src/test.c
Scanning dependencies of target test
[100%] Building C object CMakeFiles/test.dir/src/test.c.o
Linking C executable test
[100%] Built target test
No tags attached.
zip test.zip (1,390) 2013-01-11 20:23
https://public.kitware.com/Bug/file/4614/test.zip
Issue History
2013-01-11 20:23szxNew Issue
2013-01-11 20:23szxFile Added: test.zip
2016-06-10 14:28Kitware RobotNote Added: 0042199
2016-06-10 14:28Kitware RobotStatusnew => resolved
2016-06-10 14:28Kitware RobotResolutionopen => moved
2016-06-10 14:28Kitware RobotAssigned To => Kitware Robot
2016-06-10 14:31Kitware RobotStatusresolved => closed

Notes
(0042199)
Kitware Robot   
2016-06-10 14:28   
Resolving issue as `moved`.

This issue tracker is no longer used. Further discussion of this issue may take place in the current CMake Issues page linked in the banner at the top of this page.