This document explains how to add custom commands to the evo-crypter
project's CMake build system.
Custom commands are useful for automating various tasks in your build process, such as:
The primary CMake command for adding custom commands is add_custom_command
. It has two main forms:
1. Adding a custom command to generate an output file:
2. Adding a custom command to a target (e.g., executable or library):
Commonly Used Options:
OUTPUT output_file
:** Specifies the output file(s) generated by the command.COMMAND command1 [ARGS] [arguments1...]
:** Defines the command to execute along with its arguments. You can have multiple COMMAND
clauses, which will be executed in order.DEPENDS [depends...]
:** Lists the dependencies of the custom command. The command will be re-run if any of the dependencies change.WORKING_DIRECTORY dir
:** Sets the working directory for the command.COMMENT comment
:** Adds a comment that will be displayed during the build process.VERBATIM
:** Ensures that all arguments are passed to the command exactly as specified, without any special interpretation by CMake.TARGET target
:** Specifies the target (executable or library) to which the custom command is attached.PRE_BUILD
:** Runs the command before the target is built.PRE_LINK
:** Runs the command before the target is linked.POST_BUILD
:** Runs the command after the target is built.Let's add a custom command that executes the scripts/update_version.sh
script to update the project's version before the build starts.
Edit CMakeLists.txt
:
To update the version before building, you can run:
bash cmake -DNEW_VERSION=0.2.0 .. cmake --build .
Let's add a custom command that executes the scripts/lint.sh
script to run the linter after the evo
executable is built.
Edit CMakeLists.txt
:
evo
executable is built.DEPENDS
to ensure that the commands are re-run when necessary.OUTPUT
so that CMake can track them for dependency management.COMMENT
to make your custom commands more understandable.Custom commands are a powerful tool for extending the CMake build system. They allow you to automate a wide range of tasks and customize your build process to meet the specific needs of your project. By understanding the different options and best practices, you can effectively use add_custom_command
to enhance your development workflow.