Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add 'install' target to 'all' in CMake

Tags:

makefile

cmake

I have a CMake project with the following directory tree:

build/
assets/
dest/
<other files>

dest is a directory where all installed files should go:

  1. The executable, which goes to dest/ with a simple make, this is controlled with CMAKE_RUNTIME_OUTPUT_DIRECTORY

  2. The assets, located on assets/, which go to dest/ after a make install.

But I don't want to issue make install to copy all files do the dest/ dir: I want a simple make to do this.

In this sense, how do I add the install target to the default one (all)? Or, is there a better way to solve this?

like image 202
thiagowfx Avatar asked Mar 15 '15 17:03

thiagowfx


People also ask

Can CMake install dependencies?

Any complex software will have its dependencies – be it system API calls or other libraries calls either statically or dynamically linked to it. As a build system generator CMake will help you manage these dependencies in the most natural way possible.

What is install command in CMake?

CMake provides the install command to specify how a project is to be installed. This command is invoked by a project in the CMakeLists file and tells CMake how to generate installation scripts. The scripts are executed at install time to perform the actual installation of files.

What is Cmake_install CMake file?

As previous answer tells, the cmake_install. cmake contains the commands generated by install command from your CMakeLists. txt . You can execute it by cmake -P cmake_install. cmake and it performs the installation of your project even on windows.


2 Answers

Using the following wont cause recursion. Requires CMake >= 3.15.

add_custom_command(
    TARGET ${MY_TARGET} POST_BUILD
    COMMAND ${CMAKE_COMMAND} --install ${CMAKE_BINARY_DIR} --config $<CONFIG>
)

Extra : You may want to provide a default (local) install path so this doesn't fail on Windows.

if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
    set(CMAKE_INSTALL_PREFIX "install" CACHE PATH "Default install path." FORCE)
endif()
like image 77
scx Avatar answered Sep 20 '22 22:09

scx


This will execute the install target after building <target_name> (which could be all):

add_custom_command(
    TARGET <target_name>
    POST_BUILD
    COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target install
    )
like image 27
thiagowfx Avatar answered Sep 19 '22 22:09

thiagowfx