CMakeLists.txt 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. cmake_minimum_required(VERSION 2.8)
  2. project(target_link_libraries)
  3. file(WRITE
  4. "${CMAKE_CURRENT_BINARY_DIR}/main.cxx"
  5. "int main() { return 0; }
  6. "
  7. )
  8. add_executable(
  9. target_link_libraries
  10. "${CMAKE_CURRENT_BINARY_DIR}/main.cxx"
  11. )
  12. macro(ASSERT_PROPERTY _target _property _value)
  13. get_target_property(_out ${_target} ${_property})
  14. if (NOT _out)
  15. set(_out "")
  16. endif()
  17. if (NOT "${_out}" STREQUAL "${_value}")
  18. message(SEND_ERROR "Target ${_target} does not have property ${_property} with value ${_value}. Actual value: ${_out}")
  19. endif()
  20. endmacro()
  21. include(GenerateExportHeader)
  22. set(CMAKE_INCLUDE_CURRENT_DIR ON)
  23. add_library(depA SHARED depA.cpp)
  24. generate_export_header(depA)
  25. add_library(depB SHARED depB.cpp)
  26. generate_export_header(depB)
  27. target_link_libraries(depB LINK_PRIVATE depA)
  28. add_library(libgenex SHARED libgenex.cpp)
  29. generate_export_header(libgenex)
  30. set_property(TARGET depB APPEND PROPERTY
  31. LINK_LIBRARIES $<1:libgenex>
  32. )
  33. add_library(depC SHARED depC.cpp)
  34. generate_export_header(depC)
  35. target_link_libraries(depC LINK_PUBLIC depA)
  36. assert_property(depA LINK_INTERFACE_LIBRARIES "")
  37. assert_property(depB LINK_INTERFACE_LIBRARIES "")
  38. assert_property(depC LINK_INTERFACE_LIBRARIES "depA")
  39. add_executable(targetA targetA.cpp)
  40. target_link_libraries(targetA LINK_INTERFACE_LIBRARIES depA depB)
  41. assert_property(targetA LINK_INTERFACE_LIBRARIES "depA;depB")
  42. set_target_properties(targetA PROPERTIES LINK_INTERFACE_LIBRARIES "")
  43. assert_property(targetA LINK_INTERFACE_LIBRARIES "")
  44. add_subdirectory(subdir)
  45. target_link_libraries(targetA subdirlib)
  46. target_link_libraries(targetA depB depC)
  47. assert_property(targetA LINK_INTERFACE_LIBRARIES "")
  48. # Exclude depIfaceOnly from ALL so that it will only be built if something
  49. # depends on it. As it is in the link interface of depB, targetA
  50. # will depend on it. That dependency is what is being tested here.
  51. add_library(depIfaceOnly SHARED EXCLUDE_FROM_ALL depIfaceOnly.cpp)
  52. generate_export_header(depIfaceOnly)
  53. set_property(TARGET depB APPEND PROPERTY LINK_INTERFACE_LIBRARIES depIfaceOnly)
  54. add_library(depD SHARED depD.cpp)
  55. generate_export_header(depD)
  56. set_property(TARGET depD APPEND PROPERTY
  57. LINK_INTERFACE_LIBRARIES
  58. $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:depA>
  59. )
  60. add_executable(targetB targetB.cpp)
  61. target_link_libraries(targetB depD)
  62. macro(create_header _name)
  63. file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${_name}")
  64. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${_name}/${_name}.h" "//${_name}.h\n")
  65. endmacro()
  66. create_header(foo)
  67. create_header(bar)
  68. add_library(depG SHARED depG.cpp)
  69. generate_export_header(depG)
  70. target_include_directories(depG INTERFACE
  71. "${CMAKE_CURRENT_BINARY_DIR}/foo"
  72. "${CMAKE_CURRENT_BINARY_DIR}/bar"
  73. )
  74. target_compile_definitions(depG INTERFACE
  75. TEST_DEF
  76. )
  77. add_executable(targetC targetC.cpp)
  78. if(NOT BORLAND AND NOT WATCOM)
  79. # Linking to a target containing a + should be non-fatal, though it does
  80. # not work at all on Borland or watcom
  81. add_library(wrapc++ empty.cpp)
  82. target_link_libraries(targetC wrapc++)
  83. endif()
  84. # The TARGET_PROPERTY expression is duplicated below to test that there is no
  85. # shortcutting of the evaluation by returning an empty string.
  86. set(_exe_test $<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>)
  87. target_link_libraries(targetC $<$<AND:${_exe_test},${_exe_test}>:depG>)
  88. add_library(libConsumer empty.cpp)
  89. # This line causes $<$<CONFIG:Debug>:depA> to be used when
  90. # determining the include directories for libConsumer based on the
  91. # interface properties of its LINK_LIBRARIES. Because the above expression
  92. # evaluates to the empty string in non-Debug cases, ensure that that causes
  93. # no problems.
  94. target_link_libraries(libConsumer debug depA)
  95. add_subdirectory(cmp0022)