CMakeLists.txt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # Using 2.8 will trigger a deprecation warning. In this case it's explicitly
  2. # intentional since the tests checks various policy implementations prior to
  3. # 3.5
  4. cmake_minimum_required(VERSION 2.8)
  5. if(POLICY CMP0129)
  6. cmake_policy(SET CMP0129 NEW)
  7. endif()
  8. project(target_link_libraries)
  9. file(WRITE
  10. "${CMAKE_CURRENT_BINARY_DIR}/main.cxx"
  11. "int main() { return 0; }
  12. "
  13. )
  14. add_executable(
  15. target_link_libraries
  16. "${CMAKE_CURRENT_BINARY_DIR}/main.cxx"
  17. )
  18. macro(ASSERT_PROPERTY _target _property _value)
  19. get_target_property(_out ${_target} ${_property})
  20. if (NOT _out)
  21. set(_out "")
  22. endif()
  23. if (NOT "${_out}" STREQUAL "${_value}")
  24. message(SEND_ERROR "Target ${_target} does not have property ${_property} with value ${_value}. Actual value: ${_out}")
  25. endif()
  26. endmacro()
  27. include(GenerateExportHeader)
  28. set(CMAKE_INCLUDE_CURRENT_DIR ON)
  29. add_library(depA SHARED depA.cpp)
  30. generate_export_header(depA)
  31. add_library(depB SHARED depB.cpp)
  32. generate_export_header(depB)
  33. target_link_libraries(depB LINK_PRIVATE depA LINK_PRIVATE depA)
  34. add_library(libgenex SHARED libgenex.cpp)
  35. generate_export_header(libgenex)
  36. set_property(TARGET depB APPEND PROPERTY
  37. LINK_LIBRARIES $<1:libgenex>
  38. )
  39. add_library(depC SHARED depC.cpp)
  40. generate_export_header(depC)
  41. target_link_libraries(depC LINK_PUBLIC depA LINK_PUBLIC depA)
  42. assert_property(depA LINK_INTERFACE_LIBRARIES "")
  43. assert_property(depB LINK_INTERFACE_LIBRARIES "")
  44. assert_property(depC LINK_INTERFACE_LIBRARIES "depA;depA")
  45. add_executable(targetA targetA.cpp)
  46. target_link_libraries(targetA LINK_INTERFACE_LIBRARIES depA depB)
  47. assert_property(targetA LINK_INTERFACE_LIBRARIES "depA;depB")
  48. set_target_properties(targetA PROPERTIES LINK_INTERFACE_LIBRARIES "")
  49. assert_property(targetA LINK_INTERFACE_LIBRARIES "")
  50. add_subdirectory(subdir)
  51. target_link_libraries(targetA subdirlib)
  52. target_link_libraries(targetA depB depC)
  53. assert_property(targetA LINK_INTERFACE_LIBRARIES "")
  54. # Exclude depIfaceOnly from ALL so that it will only be built if something
  55. # depends on it. As it is in the link interface of depB, targetA
  56. # will depend on it. That dependency is what is being tested here.
  57. add_library(depIfaceOnly SHARED EXCLUDE_FROM_ALL depIfaceOnly.cpp)
  58. generate_export_header(depIfaceOnly)
  59. set_property(TARGET depB APPEND PROPERTY LINK_INTERFACE_LIBRARIES depIfaceOnly)
  60. add_library(depD SHARED depD.cpp)
  61. generate_export_header(depD)
  62. set_property(TARGET depD APPEND PROPERTY
  63. LINK_INTERFACE_LIBRARIES
  64. $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:depA>
  65. )
  66. add_executable(targetB targetB.cpp)
  67. target_link_libraries(targetB depD)
  68. macro(create_header _name)
  69. file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${_name}")
  70. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${_name}/${_name}.h" "//${_name}.h\n")
  71. endmacro()
  72. create_header(foo)
  73. create_header(bar)
  74. add_library(depG SHARED depG.cpp)
  75. generate_export_header(depG)
  76. target_include_directories(depG INTERFACE
  77. "${CMAKE_CURRENT_BINARY_DIR}/foo"
  78. "${CMAKE_CURRENT_BINARY_DIR}/bar"
  79. )
  80. target_compile_definitions(depG INTERFACE
  81. TEST_DEF
  82. )
  83. add_executable(targetC targetC.cpp)
  84. if(NOT BORLAND AND NOT WATCOM)
  85. # Linking to a target containing a + should be non-fatal, though it does
  86. # not work at all on Borland or watcom
  87. add_library(wrapc++ empty.cpp)
  88. target_link_libraries(targetC wrapc++)
  89. endif()
  90. # The TARGET_PROPERTY expression is duplicated below to test that there is no
  91. # shortcutting of the evaluation by returning an empty string.
  92. set(_exe_test $<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>)
  93. target_link_libraries(targetC $<$<AND:${_exe_test},${_exe_test}>:depG>)
  94. add_library(libConsumer empty.cpp)
  95. # This line causes $<$<CONFIG:Debug>:depA> to be used when
  96. # determining the include directories for libConsumer based on the
  97. # interface properties of its LINK_LIBRARIES. Because the above expression
  98. # evaluates to the empty string in non-Debug cases, ensure that that causes
  99. # no problems.
  100. target_link_libraries(libConsumer debug depA)
  101. add_subdirectory(cmp0022)
  102. add_executable(newsignature1 newsignature1.cpp)
  103. target_link_libraries(newsignature1 PRIVATE depC INTERFACE depD PUBLIC depB PRIVATE subdirlib INTERFACE INTERFACE PUBLIC)
  104. assert_property(newsignature1 INTERFACE_LINK_LIBRARIES "depD;depB")
  105. assert_property(newsignature1 LINK_LIBRARIES "depC;depB;subdirlib")
  106. #----------------------------------------------------------------------------
  107. # Test cross-directory linking.
  108. cmake_policy(PUSH)
  109. cmake_policy(SET CMP0022 NEW)
  110. cmake_policy(SET CMP0079 NEW)
  111. add_executable(TopDir TopDir.c)
  112. add_library(TopDirInterface INTERFACE)
  113. target_link_libraries(TopDir PRIVATE TopDirInterface)
  114. add_subdirectory(SubDirA)
  115. add_subdirectory(SubDirB)
  116. target_link_libraries(SubDirB TopDirImported)
  117. add_subdirectory(SubDirC)
  118. target_link_libraries(SubDirC PRIVATE SubDirC2)
  119. target_link_libraries(TopDir SubDirC)
  120. add_library(TopDirImported IMPORTED INTERFACE)
  121. target_compile_definitions(TopDirImported INTERFACE DEF_TopDirImported)
  122. cmake_policy(POP)
  123. #----------------------------------------------------------------------------
  124. # Test $<COMPILE_ONLY:> genex.
  125. cmake_policy(SET CMP0099 NEW)
  126. add_library(dont_link_too SHARED compile_only.cpp)
  127. target_compile_definitions(dont_link_too PUBLIC USE_EXAMPLE)
  128. target_link_options(dont_link_too INTERFACE invalid_link_option)
  129. target_link_libraries(dont_link_too INTERFACE invalid_link_library)
  130. add_library(uses_compile_only_genex SHARED compile_only.cpp)
  131. target_link_libraries(uses_compile_only_genex PUBLIC $<COMPILE_ONLY:dont_link_too>)
  132. add_library(uses_compile_only_genex_static STATIC compile_only.cpp)
  133. target_link_libraries(uses_compile_only_genex_static PRIVATE $<COMPILE_ONLY:dont_link_too>)
  134. add_executable(uses_via_static_linking main.cxx)
  135. target_link_libraries(uses_via_static_linking PRIVATE uses_compile_only_genex_static)