Skip to content

Instantly share code, notes, and snippets.

@cardsigner
Forked from baiwfg2/CMakeLists.txt
Created April 16, 2020 17:29
Show Gist options
  • Save cardsigner/dcaad0dcbeeb6bcbf5c488354053dcaf to your computer and use it in GitHub Desktop.
Save cardsigner/dcaad0dcbeeb6bcbf5c488354053dcaf to your computer and use it in GitHub Desktop.
How to use add_custom_target and add_custom_command correctly in cmake
# References:
# https://cmake.org/cmake/help/latest/command/add_custom_target.html
# https://samthursfield.wordpress.com/2015/11/21/cmake-dependencies-between-targets-and-files-and-custom-commands/
# https://gist.github.com/socantre/7ee63133a0a3a08f3990
# https://stackoverflow.com/questions/24163778/how-to-add-custom-target-that-depends-on-make-install
# https://stackoverflow.com/questions/30719275/add-custom-command-is-not-generating-a-target
# https://stackoverflow.com/questions/26024235/how-to-call-a-cmake-function-from-add-custom-target-command
# https://blog.csdn.net/gubenpeiyuan/article/details/51096777
cmake_minimum_required(VERSION 3.10)
project(foo)
set(TEST_FILE "log.txt")
# add_custom_command does not create a new target. You have to define targets explicitly
# by add_executable, add_library or add_custom_target in order to make them visible to make
add_custom_command(OUTPUT ${TEST_FILE}
COMMAND touch ${TEST_FILE}
# Display the given message before the commands are executed at build time
COMMENT "Creating ${TEST_FILE}"
)
# target zoo is always built
add_custom_target(zoo ALL
COMMAND echo "This is ALL target 'zoo', and it depends on ${TEST_FILE}"
# If the file exists, then commands related to that file won't be executed
# DONOT let other target depends on the same OUTPUT as current target,
# or it may be bad when doing parallel make
DEPENDS ${TEST_FILE}
# to make quotes printable,for example
VERBATIM
)
# target bar is only build when `make bar` is issued
add_custom_target(bar
# cmake -E support copy/env/echo and so on. use cmake -E to see
# COMMAND/COMMENT must be upper case
COMMAND ${CMAKE_COMMAND} -E echo bar:hello
#COMMAND ${CMAKE_COMMAND} -E environment
COMMENT "testing add_custom_target 'bar'..."
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
#DEPENDS zoo
)
# It seems to be same as `DEPENDS zoo` above
add_dependencies(bar zoo)
# This is the second signature of add_custom_command, which adds a custom command to a target such as a library or executable. This is useful for performing an operation before or after building the target. The command becomes part of the target and will only execute when the target itself is built. If the target is already built, the command will not execute
add_custom_command(TARGET bar
# On Visual Studio Generators, run before any other rules are executed within the target. On other generators, run just before PRE_LINK commands
PRE_BUILD
COMMAND echo -e "\texecuting a PRE_BUILD command"
COMMENT "This command will be executed before building bar"
VERBATIM # to support \t for example
)
add_custom_command(TARGET bar
# Run after sources have been compiled but before linking the binary or running the librarian or archiver tool of a static library. This is not defined for targets created by the add_custom_target() command
PRE_LINK
COMMAND echo -e "\texecuting a PRE_LINK command"
COMMENT "This command will be executed after building bar"
VERBATIM
)
add_custom_command(TARGET bar
# Run after all other rules within the target have been executed
POST_BUILD
COMMAND echo -e "\texecuting a POST_BUILD command"
COMMENT "This command will be executed after building bar"
VERBATIM
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment