add_custom_command: default working_directory seems depends on OS
We have post-build steps in our projects. In case it matters, here is an example:
add_custom_command (TARGET our_target POST_BUILD VERBATIM
COMMAND "${CMAKE_COMMAND}" -E echo "Installing our_target..."
COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_CONFIG_NAME=$<CONFIG> -DCMAKE_INSTALL_COMPONENT=${PROJECT_NAME} -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
It works fine on Linux and Windows. But when we tried to build for Android (on Ubuntu 16 build box) it failed.
Investigation showed that current working directory for post-build step on Windows and Linux is root binary directory (CMAKE_BINARY_DIR
) but on Android it's current binary directory (CMAKE_CURRENT_BINARY_DIR
).
So we fixed it by specifying WORKING_DIRECTORY
:
add_custom_command (TARGET our_target POST_BUILD VERBATIM
COMMAND "${CMAKE_COMMAND}" -E echo "Installing our_target..."
COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_CONFIG_NAME=$<CONFIG> -DCMAKE_INSTALL_COMPONENT=${PROJECT_NAME} -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
Default working directory is not described in the documentation:
WORKING_DIRECTORY
Execute the command with the given current working directory.
If it is a relative path it will be interpreted relative to the build tree directory
corresponding to the current source directory.
I would mention in the doc that default current directory is undefined, or better made it deterministic and documented.