5

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?

4 Answers 4

4

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()
3

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
    )
3
  • 1
    When I try specifying 'all' as the target using this method, I get an error saying "No TARGET 'all' has been created in this directory."
    – HunterZ
    May 29, 2019 at 23:54
  • I get the same tried "ALL", "all", "<all>", and "<ALL>" -- no matter what I get "No TARGET 'all' has been created in this directory." Sep 24, 2020 at 20:59
  • 1
    This retriggers a build, which recursively calls the post_build, which retriggers build, ad infinitum. Was this tested? I've converted the solution to an answer.
    – scx
    Oct 29, 2020 at 17:51
0

I've solved this problem by using the following macro to generate data dependency rules for the specified target. These rules require only a make and not a make install.

The macro maps an arbitrary file from your source tree into a "staging" tree, where each tree structure may be different, and you may optionally rename the file. The staged data is kept up-to-date, so if you change it in your source tree then it'll be updated the next time you make.

# Macro used to create dependencies for staging data files (e.g. config files, assets) and keeping them up-to-date.
# The given "source" file is copied (and possibly renamed) to "staged" for the given "target".
#
# It works by creating a rule that creates "staged" by copying "source", then creating a target that depends upon "staged",
# then making the given "target" depend upon the new target. Or in makefile speak:
#
#   staged:  source
#       cp source staged
#
#   targetData1:  staged
#
#   target:  <existing dependencies...> targetData1
#       <existing commands...>
#
# The intermediate rule is used for parallel build robustness.  For details, see:
# http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:add_custom_command
#
# Example:
#   target = myExeTarget
#   source = "${CMAKE_CURRENT_SOURCE_DIR}/../../data/images/bush1.png"
#   staged = "${STAGING_DATA_DIR}/images/bush1.png"
macro(add_data_dependency target source staged)
    add_custom_command(
        OUTPUT "${staged}"
        COMMAND ${CMAKE_COMMAND} -E copy_if_different "${source}" "${staged}"    # Dir(s) will be created if needed.
        DEPENDS "${source}"
    )

    if (NOT DEFINED add_data_dependency_counter)
        #message(status "Setting tmp counter......")
        set(add_data_dependency_counter "0")
    endif()

    math(EXPR add_data_dependency_counter "${add_data_dependency_counter} + 1")
    #message(status "tmp counter is ${add_data_dependency_counter}......")

    set(customTarget "${target}Data${add_data_dependency_counter}")

    add_custom_target(${customTarget} DEPENDS "${staged}")
    add_dependencies(${target} ${customTarget})
endmacro()

In your case, usage would be something like:

add_data_dependency(myTarget "${CMAKE_CURRENT_SOURCE_DIR}/assets/file1.ext" "${CMAKE_CURRENT_SOURCE_DIR}/dest/file1.ext")
add_data_dependency(myTarget "${CMAKE_CURRENT_SOURCE_DIR}/assets/file2.ext" "${CMAKE_CURRENT_SOURCE_DIR}/dest/file2.ext")
add_data_dependency(myTarget "${CMAKE_CURRENT_SOURCE_DIR}/assets/rename_me.ext" "${CMAKE_CURRENT_SOURCE_DIR}/dest/renamed.ext")
:
:

Then whenever you make, the files will be copied if missing or out-of-date (assuming that myTarget is part of all).

-1

The normal way to use CMake is to create a build directory outside your project and all compiled binaries are put there. When you are finished developing and want to install some binaries into your system then you call make install. In this way you keep your project source folder free from all compiler generated stuff.

Example file directory structure:

my_project/
my_project_build/

from my_project_build you call cmake ../my_project to generate build files. Call make to build it and all binaries will be in my_project_build.

1
  • All right, I could adopt this approach too, it doesn't make different in this case. The question is: how to copy the asset files from the assets/ directory to a destination directory (which I called dest/, but it could be my_project_build too, whatever) using only make? I know how to do that with make install (it is just a matter of using install commands), but I want to copy those files without issuing make install: a simple make should do that. Is it clear now?
    – thiagowfx
    Mar 15, 2015 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.