CMakeLists.txt 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # Import targets from the exported build tree.
  2. include(${Import_BINARY_DIR}/../Export/ExportBuildTree.cmake)
  3. # Import targets from the exported install tree.
  4. include(${CMAKE_INSTALL_PREFIX}/lib/exp/exp.cmake)
  5. # Import two exports, where the Depends one depends on an exported target from the Required one:
  6. include(${CMAKE_INSTALL_PREFIX}/lib/cmake/testLibRequired/testLibRequiredConfig.cmake)
  7. include(${CMAKE_INSTALL_PREFIX}/lib/cmake/testLibDepends/testLibDependsConfig.cmake)
  8. # Try referencing an executable imported from the install tree.
  9. add_custom_command(
  10. OUTPUT ${Import_BINARY_DIR}/exp_generated.c
  11. COMMAND exp_testExe1 ${Import_BINARY_DIR}/exp_generated.c
  12. DEPENDS exp_testExe1
  13. )
  14. add_custom_command(
  15. OUTPUT ${Import_BINARY_DIR}/exp_generated3.c
  16. COMMAND exp_testExe3 ${Import_BINARY_DIR}/exp_generated3.c
  17. DEPENDS exp_testExe3
  18. )
  19. add_executable(imp_testExe1
  20. imp_testExe1.c
  21. ${Import_BINARY_DIR}/exp_generated.c
  22. ${Import_BINARY_DIR}/exp_generated3.c
  23. )
  24. # Try linking to a library imported from the install tree.
  25. target_link_libraries(imp_testExe1
  26. exp_testLib2
  27. exp_testLib3
  28. exp_testLib4
  29. exp_testLib5
  30. exp_testLib6
  31. exp_testLibCycleA
  32. )
  33. # Try building a plugin to an executable imported from the install tree.
  34. add_library(imp_mod1 MODULE imp_mod1.c)
  35. target_link_libraries(imp_mod1 exp_testExe2)
  36. # Try referencing an executable imported from the build tree.
  37. add_custom_command(
  38. OUTPUT ${Import_BINARY_DIR}/bld_generated.c
  39. COMMAND bld_testExe1 ${Import_BINARY_DIR}/bld_generated.c
  40. DEPENDS bld_testExe1
  41. )
  42. add_custom_command(
  43. OUTPUT ${Import_BINARY_DIR}/bld_generated3.c
  44. COMMAND bld_testExe3 ${Import_BINARY_DIR}/bld_generated3.c
  45. DEPENDS bld_testExe3
  46. )
  47. add_executable(imp_testExe1b
  48. imp_testExe1.c
  49. ${Import_BINARY_DIR}/bld_generated.c
  50. ${Import_BINARY_DIR}/bld_generated3.c
  51. )
  52. # Try linking to a library imported from the build tree.
  53. target_link_libraries(imp_testExe1b
  54. bld_testLib2
  55. bld_testLib3
  56. bld_testLib4
  57. bld_testLib5
  58. bld_testLib6
  59. bld_testLibCycleA
  60. )
  61. # Try building a plugin to an executable imported from the build tree.
  62. add_library(imp_mod1b MODULE imp_mod1.c)
  63. target_link_libraries(imp_mod1b bld_testExe2)
  64. # Export/CMakeLists.txt pretends the RelWithDebInfo (as well as Debug)
  65. # configuration should link to debug libs.
  66. foreach(c DEBUG RELWITHDEBINFO)
  67. set_property(TARGET imp_testExe1 PROPERTY COMPILE_DEFINITIONS_${c} EXE_DBG)
  68. set_property(TARGET imp_testExe1b PROPERTY COMPILE_DEFINITIONS_${c} EXE_DBG)
  69. endforeach()
  70. #-----------------------------------------------------------------------------
  71. # Create a custom target to generate a header for the libraries below.
  72. # Drive the header generation through an indirect chain of imported
  73. # target dependencies.
  74. # testLib2tmp1.h
  75. add_custom_command(
  76. OUTPUT testLib2tmp1.h
  77. VERBATIM COMMAND
  78. ${CMAKE_COMMAND} -E echo "extern int testLib2(void);" > testLib2tmp1.h
  79. )
  80. # hdr_testLib2tmp1 needs testLib2tmp1.h
  81. add_custom_target(hdr_testLib2tmp1 DEPENDS testLib2tmp1.h)
  82. # exp_testExe2 needs hdr_testLib2tmp1
  83. add_dependencies(exp_testExe2 hdr_testLib2tmp1)
  84. # testLib2tmp.h needs exp_testExe2
  85. add_custom_command(
  86. OUTPUT testLib2tmp.h
  87. VERBATIM COMMAND exp_testExe2
  88. COMMAND ${CMAKE_COMMAND} -E copy testLib2tmp1.h testLib2tmp.h
  89. )
  90. # hdr_testLib2tmp needs testLib2tmp.h
  91. add_custom_target(hdr_testLib2tmp DEPENDS testLib2tmp.h)
  92. add_library(dep_testLib2tmp UNKNOWN IMPORTED)
  93. set_property(TARGET dep_testLib2tmp PROPERTY
  94. IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/testLib2tmp.h)
  95. # dep_testLib2tmp needs hdr_testLib2tmp
  96. add_dependencies(dep_testLib2tmp hdr_testLib2tmp)
  97. # testLib2.h needs dep_testLib2tmp
  98. add_custom_command(
  99. OUTPUT testLib2.h
  100. VERBATIM COMMAND ${CMAKE_COMMAND} -E copy testLib2tmp.h testLib2.h
  101. DEPENDS dep_testLib2tmp
  102. )
  103. # hdr_testLib2 needs testLib2.h
  104. add_custom_target(hdr_testLib2 DEPENDS testLib2.h)
  105. add_library(dep_testLib2 UNKNOWN IMPORTED)
  106. # dep_testLib2 needs hdr_testLib2
  107. add_dependencies(dep_testLib2 hdr_testLib2)
  108. # exp_testLib2 and bld_testLib2 both need dep_testLib2
  109. add_dependencies(bld_testLib2 dep_testLib2)
  110. add_dependencies(exp_testLib2 dep_testLib2)
  111. #-----------------------------------------------------------------------------
  112. # Create a library to be linked by another directory in this project
  113. # to test transitive linking to otherwise invisible imported targets.
  114. include_directories(${CMAKE_CURRENT_BINARY_DIR})
  115. add_library(imp_lib1 STATIC imp_lib1.c)
  116. target_link_libraries(imp_lib1 exp_testLib2)
  117. add_library(imp_lib1b STATIC imp_lib1.c)
  118. target_link_libraries(imp_lib1b bld_testLib2)
  119. #-----------------------------------------------------------------------------
  120. # Test that handling imported targets, including transitive dependencies,
  121. # works in CheckFunctionExists (...and hopefully all other try_compile() checks
  122. include(CheckFunctionExists)
  123. unset(HAVE_TESTLIB1_FUNCTION CACHE)
  124. set(CMAKE_REQUIRED_LIBRARIES exp_testLib2)
  125. check_function_exists(testLib1 HAVE_TESTLIB1_FUNCTION)
  126. if (NOT HAVE_TESTLIB1_FUNCTION)
  127. message(SEND_ERROR "Using imported target testLib2 in check_function_exists() failed !")
  128. endif()
  129. #-----------------------------------------------------------------------------
  130. # Test that dependent imported targets have usable
  131. # INTERFACE_COMPILE_DEFINITIONS and INTERFACE_INCLUDE_DIRECTORIES
  132. add_library(deps_iface deps_iface.cpp)
  133. target_link_libraries(deps_iface testLibsDepends)
  134. set_property(TARGET deps_iface APPEND PROPERTY
  135. COMPILE_DEFINITIONS
  136. $<TARGET_PROPERTY:testLibDepends,INTERFACE_COMPILE_DEFINITIONS>
  137. )
  138. set_property(TARGET deps_iface APPEND PROPERTY
  139. INCLUDE_DIRECTORIES
  140. $<TARGET_PROPERTY:testLibDepends,INTERFACE_INCLUDE_DIRECTORIES>
  141. )