do post processing after make install in cmake

2019-03-03 23:09发布

问题:

I am trying to make a copy of my executable at the end of my"make install"

I need to do something like:

cp bin/prog bin/prog1

I have added the following as the last line in my CMakelists.txt

install (CODE  "execute_process(COMMAND /src/copyExe.sh ${BIN_DIR})")

copyExe.sh is a bash script that does the copying. In order to have the desired affect, I need to run "make install" twice. The first time it complains that prog doesn't exist, and then copies the file to bin. The second time it finds prog and is able to make the copy.

Is there a way to ensure that my copyExe script runs AFTER the files get copied to bin?

Directory structure

site
    bin
    src
        CMakeLists.txt ( contains add_dir(foo) and install(CODE....))
    foo
        CMakeLists.txt ( contains install( TARGET..... ))

回答1:

While CMake documentation for install command says (about installation logic):

The order across directories is not defined.

it looks like it tends to process installation logic in different subdirectories in the same order as add_subdirectory() calls.

However, it processes installation logic of install() call in the current directory before ones in subdirectories.

You may move install(CODE) into some subdirectory (say, fix_binaries), and add this subdirectory at the end of CMakeLists.txt in src:

src/fix_binaries/CMakeLists.txt:

install(CODE ...)

src/CMakeLists.txt:

...
add_subdirectory(foo)
...
# After all add_subdirectory() calls
add_subdirectory(fix_binaries)

Such approach works for one of the project I have involved (related code).



标签: cmake