Missing link flags for targets when using set_target_properties and LINK_FLAGS

948 views Asked by At

We have three targets. One is a static library, one is a shared library and one is a program. We are trying to add target specific linker flags to the shared library and program.

When we try to append the flags we need:

if (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro")
    CRYPTOPP_XARCH_OPTIONS = "-xarch=ssse3 -xarch=sse4_1 ..."
endif()
...

if (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro")
    set_target_properties(cryptest-program PROPERTIES LINK_FLAGS APPEND "-M cryptest.mapfile")
    file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/cryptest.mapfile DESTINATION ${PROJECT_BINARY_DIR})
    set_target_properties(cryptest-program PROPERTIES LINK_FLAGS APPEND ${CRYPTOPP_XARCH_OPTIONS})
endif ()

It results in a failure:

CMake Error at CMakeLists.txt:631 (set_target_properties):
 set_target_properties called with incorrect number of arguments.

When we attempt to use X = ${X} Y syntax due to a missing += operator, then the properties are silently dropped:

if (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro")
    set_target_properties(cryptest-program PROPERTIES LINK_FLAGS ${LINK_FLAGS} "-M cryptest.mapfile")
    file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/cryptest.mapfile DESTINATION ${PROJECT_BINARY_DIR})
    set_target_properties(cryptest-program PROPERTIES LINK_FLAGS ${LINK_FLAGS} ${CRYPTOPP_XARCH_OPTIONS})
endif ()

For example, the mapfile was lost so testing under Solaris on early Xeon's results in:

$ ./cryptest.exe v
ld.so.1: cryptest.exe: fatal: cryptest.exe: hardware capability (CA_SUNW_HW_1) unsupported: 0x4800000  [ AES SSE4.1 ]
Killed

Here are the docs for set_property,set_target_properties and LINK_FLAGS. They don't provide the necessary details and fail to provide examples. The doc for set_target_properties is especially charming.

How do we add target specific link flags?

1

There are 1 answers

0
utopia On

It is recommended to use target_link_libraries() for linker flags.

set(CRYPTOPP_XARCH_OPTIONS 
  -xarch=ssse3 
  -xarch=sse4_1 
  ...)
target_link_libraries(cryptest-program
  -M cryptest.mapfile
  ${CRYPTOPP_XARCH_OPTIONS})

Note that in all cases you're dealing with lists, not strings like you would have to manage with the LINK_FLAGS property. The somewhat unnecessary indentation above is to emphasize that fact.