Skip to content

Instantly share code, notes, and snippets.

@baiwfg2
Created September 29, 2018 12:42
Show Gist options
  • Save baiwfg2/39881ba703e9c74e95366ed422641609 to your computer and use it in GitHub Desktop.
Save baiwfg2/39881ba703e9c74e95366ed422641609 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
)
@mapingshuo
Copy link

A very good example, thx.

@Inkln
Copy link

Inkln commented Oct 3, 2019

Thanks!

@e8035669
Copy link

These examples are better than documents.

@gcc-tan
Copy link

gcc-tan commented Nov 14, 2019

the best example for add_custom_command, thanks

@cardsigner
Copy link

el magnefico!

@CairoLee
Copy link

Thanks you. It's a very good example!

@stdrc
Copy link

stdrc commented Feb 23, 2021

This is realy helpful!

@TadeasKucera
Copy link

A very good example!

@Steve-Hawk
Copy link

An illuminating example!

@ozdang
Copy link

ozdang commented Jul 14, 2021

Thanks!

@EnricoGallus
Copy link

Amazing example. Really really helpful

@tangbom
Copy link

tangbom commented Oct 13, 2022

nice job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment