CMakeLists.txt 4.7 KB

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