CMake: how to get target location for install rule

后端 未结 2 1903
慢半拍i
慢半拍i 2021-01-18 02:57

I have a CMakeLists.txt that does this:

get_target_property(myloc mytarget LOCATION)

It used to work fine, but CMake 3.0 deprecated using <

相关标签:
2条回答
  • 2021-01-18 03:11

    You can try to use/re-enable the < v3.0 behaviour.

    cmake_policy(SET CMP0026 OLD)
    

    See cmake-policies

    0 讨论(0)
  • 2021-01-18 03:30

    Ideally, it would be done via generator expressions:

    install(CODE "execute_process(COMMAND strip $<TARGET_FILE:mytarget>)")
    

    Unfortunately, support for generator expressions in CODE and SCRIPT modes of install command has been added only in CMake 3.14 (see documentation of install command in that version).

    Before CMake 3.14 you may work only with single-configuration generators (e.g. with Makefile but not with Visual Studio).

    In such conditions, you may either disable CMP0026 warning, as suggested in the Th.Thielemann's answer and read LOCATION property.

    Or you may use install(SCRIPT) flow of the command, where the script is prepared with file(GENERATE) command which is like configure_file but works with generator expressions:

    file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/mytarget_strip.cmake
         CONTENT "execute_process(COMMAND strip $<TARGET_FILE:mytarget>)")
    
    install(SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/mytarget_strip.cmake)
    

    Note, that the approach with file(GENERATE) still doesn't work with multi-configuration generators: CMake requires filename for OUTPUT clause to be unique between the configurations. One may "fix" that with

    file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/mytarget_strip.cmake_$<CONFIG>
         CONTENT "execute_process(COMMAND strip $<TARGET_FILE:mytarget>)")
    
    install(SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/mytarget_strip.cmake_${CMAKE_BUILD_TYPE})
    

    but this still won't work: in multi-configuration generators CMAKE_BUILD_TYPE is evaluated to empty string.

    (Replacing ${CMAKE_BUILD_TYPE} with $<CONFIG> in install(SCRIPT) command would work only in CMake 3.14 and after, but in these versions the whole file(GENERATE) is not needed and one may just use the very first snippet.)

    0 讨论(0)
提交回复
热议问题