add_custom_command/add_custom_target fails to detect generator expression in WORKING_DIRECTORY as absolute path
Hi, I came across an issue with using just a generator expression in WORKING_DIRECTORY
of either add_custom_command or add_custom_target. E.g. $<TARGET_FILE_DIR:sometarget>
.
In this case cmake interprets it as a relative path tries to prepend ${CMAKE_CURRENT_BINARY_DIR}
infront of the generator expression which is already the absolute path. A workaround is to put a / infront of the generator expression like so: /$<TARGET_FILE_DIR:sometarget>
.
This is isn't a huge issue since there is a simple workaround but it definitely stumped me for a few minutes figuring out what was wrong.
What I am using:
- CMake 3.13-rc2
- Ubuntu 16.04
- Reproduced on Unix Makefile and Ninja generators (I don't have any others)
Here's a test project I tested with:
cmake_minimum_required(VERSION 3.13)
project(abc)
add_executable(a a.cpp)
# Removing the / in any of the WORKING_DIRECTORY arguments will cause the project to fail to build
add_custom_command(OUTPUT y
COMMAND ${CMAKE_COMMAND} -E touch $<TARGET_FILE:a>
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:a> y
WORKING_DIRECTORY /$<TARGET_FILE_DIR:a>
)
add_custom_target(z ALL ${CMAKE_COMMAND} -E copy y z
DEPENDS y
WORKING_DIRECTORY /$<TARGET_FILE_DIR:a>
)
add_custom_command(TARGET z
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy z b
WORKING_DIRECTORY /$<TARGET_FILE_DIR:a>
)
Heres the output for this project with the Unix Makefile generator after I removed the / from the WORKING_DIRECTORY
of add_custom_target(z ...)
$ cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/cmaketest/build
$ make
Scanning dependencies of target a
[ 33%] Building CXX object CMakeFiles/a.dir/a.cpp.o
[ 66%] Linking CXX executable a
[ 66%] Built target a
Scanning dependencies of target z
[100%] Generating y
/bin/sh: line 0: cd: /tmp/cmaketest/build//tmp/cmaketest/build: No such file or directory
CMakeFiles/z.dir/build.make:57: recipe for target 'CMakeFiles/z' failed
make[2]: *** [CMakeFiles/z] Error 1
CMakeFiles/Makefile2:109: recipe for target 'CMakeFiles/z.dir/all' failed
make[1]: *** [CMakeFiles/z.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2