Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symbolic links CMake

Tags:

cmake

I want to rename certain executables in CMakeLists.txt but also want symbolic links from the older names to new files for backward compatibility. How can this be accomplished on systems that support symbolic links?

Also what are the alternatives for system that does not support symbolic links?

like image 381
stillanoob Avatar asked Mar 03 '16 06:03

stillanoob


4 Answers

Another way to do it:

INSTALL(CODE "execute_process( \
    COMMAND ${CMAKE_COMMAND} -E create_symlink \
    ${target} \
    ${link}   \
    )"
)

This way the symlinking will be done during make install only.

like image 174
ulidtko Avatar answered Nov 20 '22 04:11

ulidtko


You can create a custom target and use CMake to create symlinks

ADD_CUSTOM_TARGET(link_target ALL
                  COMMAND ${CMAKE_COMMAND} -E create_symlink ${target} ${link})

This will only work on systems that support symlinks, see the documentation.

Before CMake v3.14, this did not work on Windows. In v3.13, support for Windows was added.

like image 41
Emil Avatar answered Nov 20 '22 03:11

Emil


Another method that is a bit more verbose and only runs on install:

macro(install_symlink filepath sympath)
    install(CODE "execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${filepath} ${sympath})")
    install(CODE "message(\"-- Created symlink: ${sympath} -> ${filepath}\")")
endmacro(install_symlink)

Use it like this (similar to ln -s):

install_symlink(filepath sympath)
like image 19
Rian Quinn Avatar answered Nov 20 '22 03:11

Rian Quinn


Lets say you need to create a link in binary dir to a target located in source directory.

You can try file CREATE_LINK since version 3.14

${CMAKE_COMMAND} -E create_symlink is accessible at Windows since 3.17

You can use execute_process since early cmake versions:

if(WIN32)
  get_filename_component(real_path "${dirname}" REALPATH)
  string(REPLACE "/" "\\" target_path "${real_path}")
  execute_process(
    COMMAND cmd /C mklink /J ${dirname} "${target_path}"
    WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
    )
else()
  execute_process(COMMAND "ln -s ${CMAKE_CURRENT_SOURCE_DIR}/${dirname} ${CMAKE_CURRENT_BINARY_DIR}/${dirname}")
endif()
like image 4
Sergei Krivonos Avatar answered Nov 20 '22 02:11

Sergei Krivonos