eudoxos
eudoxos

Reputation: 19065

Execute process at install stage, every time

I would like to run program to perform do extra installation tasks from CMake. My attempted solution, based on INSTALL(CODE ...) is (this is a real MWE):

macro(MY_EXTRA_STUFF ARG)
    execute_process(...)
endmacro()
install(CODE "MY_EXTRA_STUFF(${SOME_ARG})")

but CMake complains when I run ninja install (or make install, depending on generator in use):

[0/1] Install the project...
-- Install configuration: ""
CMake Error at cmake_install.cmake:41 (MY_EXTRA_STUFF):
  Unknown CMake command "MY_EXTRA_STUFF".


FAILED: CMakeFiles/install.util 
cd /tmp && /usr/bin/cmake -P cmake_install.cmake
ninja: build stopped: subcommand failed.

Is there a way to smuggle my own code into the install stage? The code is too long to fit inside install(CODE "...") nicely. A bonus to do it without an external file. Thanks!

Upvotes: 2

Views: 745

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 65870

The code passed to install(CODE) is executed as standalone CMake code, thus it shouldn't use definitions (functions,macros, variables) from the rest of CMakeLists.txt.

That is, install(CODE) behaves similar as install(SCRIPT) with a standalone script containing given code.

The thing is that configuration stage (when you call cmake to configure your project) and installation stage, which, as you can see, calls /usr/bin/cmake -P cmake_install.cmake, are separate cmake invocations. These invocations parse different files, so they unaware about context of each other.

Upvotes: 2

Related Questions