How to copy soft link with target name in CMake

293 Views Asked by At

I'm using cmake to copy a soft link 'libbssl.so' (which has target libssl.so.3) to a build subdirectory.

COMMAND ${CMAKE_COMMAND} -E copy ${OPENSSL_SSL_LIBRARY} ${CMAKE_CURRENT_BINARY_DIR}/lib

The copy command works as expected and copies the file, however with libssl.so as the filename and not libssl.so.3. How do I get cmake to save the filename as libssl.so.3 keeping in mind that I don't necessarily know this name in advance, i.e., I don't want to hard code it. I'm using find_package(OpenSSL).

1

There are 1 best solutions below

0
On

I don't know about a convenient way to use the cmake command linke tool for this directly, but starting CMake 3.15, cmake provides the FOLLOW_SYMLINK_CHAIN for file(COPY) for copying the library file including the full symlink chain to the target directory.

You could create a cmake script to execute as command.

copy_symlink_chain.cmake

#[===[
Params:
    LIBRARY     : the name of the symlink to the lib
    DESTINATION : the directory to copy the files to
]===]

file(COPY "${LIBRARY}" DESTINATION "${DESTINATION}" FOLLOW_SYMLINK_CHAIN)
...
   COMMAND ${CMAKE_COMMAND} -D "LIBRARY=${OPENSSL_SSL_LIBRARY}" -D "DESTINATION=${CMAKE_CURRENT_BINARY_DIR}/lib" -P ${CMAKE_CURRENT_SOURCE_DIR}/copy_symlink_chain.cmake
...