CMake generates something like the following for the install rule:
# Special rule for the target install
install: preinstall
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Install the project..."
/usr/local/bin/cmake -P cmake_install.cmake
.PHONY : install
What I want to do is have some custom commands executed after cmake_install.cmake is invoked so it looks something like:
# Special rule for the target install
install: preinstall
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Install the project..."
/usr/local/bin/cmake -P cmake_install.cmake
post_install_command_1
...
post_install_command_n
.PHONY : install
I can do what I want using add_custom_command(TARGET ... POST_BUILD ...) for things that we have written (6 – 10 macros to update). However, there are a bunch of third-party things that get installed and I really don’t want to add POST_BUILD custom commands for all of them (currently 19 projects with more to come and it can be tough to identify what needs to be processed after building instead of after installing). I think it would be much easier to maintain if the custom commands are only used in one place (i.e. as last part of install handling) and where I know they will do everything that is necessary.
Is it possible to get CMake to add commands to the top-level Makefile’s install rule?
You can use the
SCRIPTorCODEvariant of the install command. If you put the required commands in a scriptPostInstall.cmakein the project root directory, add the following call to your outermostCMakeLists.txt:installcommands are added to thecmake_install.cmakescript in order, thus the call should be added to the end of theCMakeLists.txtto have it run after all other installations have completed.