Hossein
Hossein

Reputation: 25924

Is it possible to get the target filename in cmake?

I'm trying to copy several targets into a specific build directory using the add_custom_command which is as follows:

get_filename_component(buildDirRelFilePath "libDetector_dynamic.so"
                       REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
add_custom_command(
        TARGET Detector_dynamic POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy
                ${buildDirRelFilePath}
                ${CMAKE_INSTALL_PREFIX}/lib/libDetector_dynamic.so)

It's very desirable to simply remove all the hardcoded values that refer to the target being built and instead use a CMake's variable that changes automatically for each target.

Upvotes: 2

Views: 1852

Answers (1)

KamilCuk
KamilCuk

Reputation: 141020

So use generator expressions.

add_library(Detector_dynamic ...)
add_custom_command(
    TARGET Detector_dynamic POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy
            $<TARGET_FILE:Detector_dynamic>
            ${CMAKE_INSTALL_PREFIX}/lib/)

I do not think there's ever need to reference the filename by the name. For me it looks like you should use CMAKE_LIBRARY_OUTPUT_DIRECTORY or just maybe just write install scripts.

Upvotes: 2

Related Questions