Mohamed Kandeel
Mohamed Kandeel

Reputation: 147

How to add a custom command to targets that depend on a specific target

I have a target that produces a shared library call it A and it depends on another shared library call it B.

I have some targets that depend on A call them C, D and E.

I want to copy A and B next to where each C, D and E lie on disk.

Formally speaking i want to copy the shared library A and any other shared library that A depends on, to the locations of any target that depends on A.

How can i achieve this in cmake ? is this even possible ?

Upvotes: 0

Views: 648

Answers (1)

fabian
fabian

Reputation: 82451

Assuming you've got full control of all the CMakeLists.txt files producing all 5 targets, you can set the location .dll or .so files are build to. You can separately specify the install location assuming you want to specify the location after the install step.

Commands refering to a target need to be placed after the target is created using add_library/add_executable.

set_target_properies(A PROPERTIES 
     ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" # import libraries
     LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" # .so files
     RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" # .dll files/programs
)

# similar as above, but specifying the path relative to CMAKE_INSTALL_PREFIX
install(TARGETS A
    ARCHIVE DESTINATION lib
    LIBRARY DESTINATION bin
    RUNTIME DESTINATION bin
)

Depending on your needs you only need one of the commands and for both it would be possible to replace target A with a list of targets. This way you could avoid repeating the command(s) 5 times. I do recommend however doing this in the CMakeLists.txt file where you create the target.

Even though the output dirs are relative to the current directory in the above we worked around this by using the absolute path to the build dir (CMAKE_BINARY_DIR) as part of the directory name.

Upvotes: 1

Related Questions