What is the best way to do additional stuff for all (binary) targets?
Examples:
- I want to check that each library name follows a pattern.
- I want to sign each executable.
I dont what the C/C++ developers to use nonstandard commands (like add_library2
). I want them to use and learn the official CMake functions, but have them do additonal, project specific, stuff.
The built-in CMake functions
add_library
andadd_executable
can be overidden by defining CMake functions of the same name. E.g., to automatically sign all added executables add the following code:The original built-in
add_executable
can be invoked by prefixing it with an underscore character. The same pattern can be applied toadd_library
to check the name of the library.You can overwrite any CMake command/function to extend its functionality, but please
Call Things by their Names
I strongly advocate to call the things by their names and not doing things implicitly. It will be easier for everybody using/maintaining/debugging your CMake based project.
If you want to sign your executable - and that's probably even platform specific - you create a function like
add_post_build_step_sign_executable()
which would add the appropriate post build steps:And if you have to repeat yourself too often put that code snippet into a function like
my_project_add_signed_executable()
itself. It could still have the same parameter syntax as CMake'sadd_executable()
command.Runtime Checks vs. Static Code Analysis
Regarding library naming checks, I see this more like checking against your project's CMake script coding styles and would not use runtime checks for this.
For example you could use something like
cmake-lint
or your own external script to check for conformity.References