Symbolic links CMake

前端 未结 6 437
抹茶落季
抹茶落季 2020-12-17 10:34

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 b

相关标签:
6条回答
  • 2020-12-17 11:23

    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.

    0 讨论(0)
  • 2020-12-17 11:32

    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)
    
    0 讨论(0)
  • 2020-12-17 11:34

    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 guide.

    Available on UNIX only:

    create_symlink old new - create a symbolic link new -> old

    0 讨论(0)
  • 2020-12-17 11:36

    I've struggled with this a few different ways with the above responses in order to install '.so.#' files that refer to other '.so.#.#' files. I've had success by not introducing a link to the file, but by installing the '.so.#.#' file as the '.so.#' file.

    I.e. instead of

    install(
        FILES
            .../libmpi.so.12.0
        DESTINATION lib
    )
    
    install(CODE 
        "EXECUTE_PROCESS( ${CMAKE_COMMAND} -E create_symlink lib/libmpi.so.12.0 lib/libmpi.so.12)")
    

    Which didn't quite work for me even. I have instead had success by doing this.

    install(FILES 
            .../libmpi.so.12.0
        RENAME libmpi.so.12
        DESTINATION lib
    )
    

    Not 'exactly' the same, but sufficient. Don't defeat the problem, solve the problem.

    0 讨论(0)
  • 2020-12-17 11:38

    I've added the check to @ulidtko's approach, so symlink doesn't overridden on every rebuild unconditionally:

    install(CODE "if (NOT EXISTS ${link})
                      execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink \
                                                                    ${target} \
                                                                    ${link})
                  endif()" )
    
    0 讨论(0)
  • 2020-12-17 11:40

    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 "${CMAKE_CURRENT_SOURCE_DIR}/target" REALPATH)
        string(REPLACE "/" "\\" target_path "${real_path}")
        execute_process(
            COMMAND cmd /C mklink /J link_name "${target_path}"
            WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
            )
      else()
        execute_process(COMMAND "ln -s ${CMAKE_CURRENT_SOURCE_DIR}/input ${CMAKE_CURRENT_BINARY_DIR}/input")
      endif()
    
    0 讨论(0)
提交回复
热议问题