Copy target file to another location in a post build step in CMake
Asked Answered
S

3

51

I have a dynamic library that gets a different name depending on configuration, specified in the CMake scripts by:

set_target_properties(${name} PROPERTIES OUTPUT_NAME ${outputName}64)
set_target_properties(${name} PROPERTIES DEBUG_OUTPUT_NAME ${outputName}64_d)

The end result is that I get a different name on release and debug builds. I would like to copy the resulting library to a different directory as a post-build step, but the gift(?) of CMake-Fu did not smile upon yours truly.

I have tried doing this:

GET_TARGET_PROPERTY(origfile mylibrary LOCATION)
STRING(REGEX REPLACE "/" "\\\\" origfile ${origfile})

set(targetfile my_target_path\\${CMAKE_CFG_INTDIR}\\)
STRING(REGEX REPLACE "/" "\\\\" targetfile ${targetfile})

add_custom_command(TARGET mylibrary POST_BUILD
    COMMAND copy ${origfile} ${targetfile}
)

This works fine for release builds, but for debug the source does not include the _d that I would have expected. How do I get the output path for the target so that I can copy the file?

Note: As can be seen from the above snippet, this is currently for Windows/Visual Studio, but I would like this to work on OS X / Xcode / make as well.

Note: I need the library to be placed in an extra directory that serves as the output directory for several other projects that depend on this library, so that these projects are able to load the library at runtime. An alternative solution that would be acceptable would be to be able to create a custom target that does the copying, so that the other projects can depend on this project, which in turn depends on the library.

Schizogenesis answered 3/4, 2012 at 13:14 Comment(0)
D
77

Rather than using the obsolete LOCATION property, prefer using generator expressions:

add_custom_command(TARGET mylibrary POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:mylibrary> ${targetfile}
)

You could also just generate the exe in the target directory directly by setting the target property RUNTIME_OUTPUT_DIRECTORY instead of copying it. This has per-configuration options (e.g. RUNTIME_OUTPUT_DIRECTORY_DEBUG).

set_target_properties(mylibrary PROPERTIES
                      RUNTIME_OUTPUT_DIRECTORY_DEBUG <debug path>
                      RUNTIME_OUTPUT_DIRECTORY_RELEASE <release path>
)

For further details run:

cmake --help-property "RUNTIME_OUTPUT_DIRECTORY"
cmake --help-property "RUNTIME_OUTPUT_DIRECTORY_<CONFIG>"

Also, you should be able to use forward slashes throughout for path separators, even on Windows.

Dirtcheap answered 3/4, 2012 at 14:3 Comment(4)
If you are targeting Xcode or Visual Studio, you may want to use RUNTIME_OUTPUT_DIRECTORY instead and let the IDE take care of appending the correct configuration. (as you would discover if you ran the help cmd @Dirtcheap added)Culex
Warning: the question is specifically about dynamic libraries, and as cmake --help-property "RUNTIME_OUTPUT_DIRECTORY" points out, these work fundamentally different on Windows. On Windows, you get a .LIB alongside a .DLL, and the .DLL is treated as a RUNTIME component (so the DLL follows RUNTIME_OUTPUT_DIRECTORY). On Linux, the .so file combines both roles (used by linker and loader, ld and ld.so.) so it it NOT copied to RUNTIME_OUTPUT_DIRECTORYLiguria
Poor answer. Here is way better and clear explanation: thomas.trocha.com/blog/cmake--copy-files-after-buildSesquicarbonate
Is it possible to do it with imported library, for example using include_external_msproject ? When I try to set_target_properties to my library and/or add_custom_command, required libraries don't copy to output dir ?Mitochondrion
S
17

Use generator expressions in the POST_BUILD command instead of manually computing the output path. These are configuration aware. Example:

add_custom_command(TARGET mylibrary POST_BUILD 
  COMMAND "${CMAKE_COMMAND}" -E copy 
     "$<TARGET_FILE:mylibrary>"
     "my_target_path/$<CONFIGURATION>/$<TARGET_FILE_NAME:mylibrary>" 
  COMMENT "Copying to output directory")
Shaeshaef answered 3/4, 2012 at 14:12 Comment(0)
C
5

The other answers weren't 100% clear to me...

Say you're building an executable test_base.exe, the following will build the executable then copy the .exe to the base 'build' directory:

add_executable(test_base "")
target_sources(test_base
    PRIVATE
        catch_main.cpp
        catch_tests.cpp
        sc_main.cpp
)
target_link_libraries(test_base PRIVATE Catch2 systemc)

add_custom_command(TARGET test_base POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:test_base> ${PROJECT_BINARY_DIR}/test_base.exe
    COMMENT "Created ${PROJECT_BINARY_DIR}/test_base.exe"
)

So, after this runs your project will have:

<project dir>/build/test_base.exe

Crotty answered 11/2, 2020 at 14:15 Comment(3)
would you please explain what is $<TARGET_FILE:test_base>? is is cmake syntax or what?!Juttajutty
Yes, that is standard CMake syntax. It is called a 'generator expression', cmake.org/cmake/help/latest/manual/…Crotty
It can also be $<TARGET_FILE:${PROJECT_NAME}> or $<TARGET_FILE_NAME:${PROJECT_NAME}> where the former contains the full path.Pappas

© 2022 - 2024 — McMap. All rights reserved.