How to add_custom_target that depends on “make ins

2019-02-16 13:24发布

问题:

I'd like to add a custom target named "package" which depends on install target. When I run make package it should cause first running make install and after that, running my custom command to create a package.

I have tried the following DEPENDS install but it does not work.

I get error message: No rule to make target CMakeFiles/install.dir/all, needed by CMakeFiles/package.dir/all

install(FILES
        "module/module.pexe"
        "module/module.nmf"
        DESTINATION "./extension")

add_custom_target(package
    COMMAND "chromium-browser" "--pack-extension=./extension"
    DEPENDS install)    

EDIT: I tried DEPENDS install keyword and add_dependencies(package install) but neither of them works.

According to http://public.kitware.com/Bug/view.php?id=8438 it is not possible to add dependencies to built-in targets like install or test

回答1:

You can create custom target which will run install and some other script after.

CMake script

For instance if you have a CMake script MyScript.cmake:

add_custom_target(
    MyInstall
    COMMAND
    "${CMAKE_COMMAND}" --build . --target install
    COMMAND
    "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_LIST_DIR}/MyScript.cmake"
    WORKING_DIRECTORY
    "${CMAKE_BINARY_DIR}"
)

You can run it by building target MyInstall:

cmake --build /path/to/build/directory --target MyInstall

Python script

Of course you can use any scripting language. Just remember to be polite to other platforms (so probably it's a bad idea to write bash script, it will not work on windows).

For example python script MyScript.py:

find_package(PythonInterp 3.2 REQUIRED)

add_custom_target(
    MyInstall
    COMMAND
    "${CMAKE_COMMAND}" --build . --target install
    COMMAND
    "${PYTHON_EXECUTABLE}" "${CMAKE_CURRENT_LIST_DIR}/MyScript.py"
    WORKING_DIRECTORY
    "${CMAKE_BINARY_DIR}"
)


标签: cmake