CMakeLists.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. cmake_minimum_required (VERSION 2.7.20090711)
  2. project(Export C CXX)
  3. # Pretend that RelWithDebInfo should link to debug libraries to test
  4. # the DEBUG_CONFIGURATIONS property.
  5. set_property(GLOBAL PROPERTY DEBUG_CONFIGURATIONS Debug RelWithDebInfo)
  6. add_library(testExe1lib STATIC testExe1lib.c) # not exported
  7. add_executable(testExe1 testExe1.c)
  8. target_link_libraries(testExe1 testExe1lib)
  9. set_property(TARGET testExe1 PROPERTY VERSION 4)
  10. add_library(testExe2libImp SHARED testExe2libImp.c)
  11. set_property(TARGET testExe2libImp PROPERTY LIBRARY_OUTPUT_DIRECTORY impl)
  12. add_library(testExe2lib SHARED testExe2lib.c)
  13. target_link_libraries(testExe2lib testExe2libImp)
  14. set_property(TARGET testExe2lib PROPERTY LINK_INTERFACE_LIBRARIES "")
  15. add_executable(testExe2 testExe2.c)
  16. set_property(TARGET testExe2 PROPERTY ENABLE_EXPORTS 1)
  17. set_property(TARGET testExe2 PROPERTY LINK_INTERFACE_LIBRARIES testExe2lib)
  18. add_library(testLib1 STATIC testLib1.c)
  19. add_library(testLib2 STATIC testLib2.c)
  20. target_link_libraries(testLib2 testLib1)
  21. # Test library with empty link interface. Link it to an implementation
  22. # dependency that itself links to dependencies publicly.
  23. add_library(testLib3ImpDep SHARED testLib3ImpDep.c)
  24. set_property(TARGET testLib3ImpDep PROPERTY LIBRARY_OUTPUT_DIRECTORY impl/dep)
  25. add_library(testLib3Imp SHARED testLib3Imp.c)
  26. set_property(TARGET testLib3Imp PROPERTY LIBRARY_OUTPUT_DIRECTORY impl)
  27. target_link_libraries(testLib3Imp testLib3ImpDep)
  28. add_library(testLib3 SHARED testLib3.c)
  29. target_link_libraries(testLib3 testLib3Imp)
  30. set_property(TARGET testLib3 PROPERTY LINK_INTERFACE_LIBRARIES "")
  31. set_property(TARGET testLib3 PROPERTY VERSION 1.2)
  32. set_property(TARGET testLib3 PROPERTY SOVERSION 3)
  33. # Test <ARCHIVE|LIBRARY|RUNTIME>_OUTPUT_NAME[_<CONFIG>] properties.
  34. set_property(TARGET testLib3 PROPERTY RUNTIME_OUTPUT_NAME_DEBUG testLib3dll-d)
  35. set_property(TARGET testLib3 PROPERTY RUNTIME_OUTPUT_NAME_RELEASE testLib3dll-r)
  36. set_property(TARGET testLib3 PROPERTY RUNTIME_OUTPUT_NAME testLib3dll)
  37. set_property(TARGET testLib3 PROPERTY LIBRARY_OUTPUT_NAME_DEBUG testLib3lib-d)
  38. set_property(TARGET testLib3 PROPERTY LIBRARY_OUTPUT_NAME_RELEASE testLib3lib-r)
  39. set_property(TARGET testLib3 PROPERTY LIBRARY_OUTPUT_NAME testLib3lib)
  40. set_property(TARGET testLib3 PROPERTY ARCHIVE_OUTPUT_NAME testLib3import)
  41. add_library(testLib4 SHARED testLib4.c)
  42. set_property(TARGET testLib4 PROPERTY FRAMEWORK 1)
  43. add_library(testLib5 SHARED testLib5.c)
  44. add_library(testLib6 STATIC testLib6.cxx testLib6c.c)
  45. # Work-around: Visual Studio 6 does not support per-target object files.
  46. set(VS6)
  47. if("${CMAKE_GENERATOR}" MATCHES "Visual Studio 6")
  48. set(VS6 1)
  49. endif()
  50. # Test using the target_link_libraries command to set the
  51. # LINK_INTERFACE_LIBRARIES* properties. We construct two libraries
  52. # providing the same two symbols. In each library one of the symbols
  53. # will work and the other one will fail to link. The import part of
  54. # this test will try to use the symbol corresponding to the
  55. # configuration in which it is built. If the proper library is not
  56. # used via the link interface the import test will fail to link.
  57. add_library(testLib4lib STATIC testLib4lib.c)
  58. add_library(testLib4libdbg STATIC testLib4libopt.c testLib4libdbg${VS6}.c)
  59. add_library(testLib4libopt STATIC testLib4libdbg.c testLib4libopt${VS6}.c)
  60. set_property(TARGET testLib4libdbg PROPERTY COMPILE_DEFINITIONS LIB_DBG)
  61. set_property(TARGET testLib4libopt PROPERTY COMPILE_DEFINITIONS LIB_OPT)
  62. target_link_libraries(testLib4
  63. LINK_INTERFACE_LIBRARIES
  64. testLib4lib debug testLib4libdbg optimized testLib4libopt
  65. )
  66. add_executable(testExe3 testExe3.c)
  67. set_property(TARGET testExe3 PROPERTY MACOSX_BUNDLE 1)
  68. # Test cyclic dependencies.
  69. add_library(testLibCycleA STATIC
  70. testLibCycleA1.c testLibCycleA2.c testLibCycleA3.c)
  71. add_library(testLibCycleB STATIC
  72. testLibCycleB1.c testLibCycleB2.c testLibCycleB3.c)
  73. target_link_libraries(testLibCycleA testLibCycleB)
  74. target_link_libraries(testLibCycleB testLibCycleA)
  75. set_property(TARGET testLibCycleA PROPERTY LINK_INTERFACE_MULTIPLICITY 3)
  76. # Test exporting dependent libraries into different exports
  77. add_library(testLibRequired testLibRequired.c)
  78. add_library(testLibDepends testLibDepends.c)
  79. target_link_libraries(testLibDepends LINK_PUBLIC testLibRequired)
  80. macro(add_include_lib _libName)
  81. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${_libName}.c" "// no content\n")
  82. add_library(${_libName} "${CMAKE_CURRENT_BINARY_DIR}/${_libName}.c")
  83. file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${_libName}")
  84. set_property(TARGET ${_libName} APPEND PROPERTY
  85. INTERFACE_INCLUDE_DIRECTORIES
  86. "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/${_libName}>"
  87. "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include/${_libName}>"
  88. )
  89. if (NOT "${ARGV1}" STREQUAL "NO_HEADER")
  90. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${_libName}/${_libName}.h" "// no content\n")
  91. install(FILES
  92. "${CMAKE_CURRENT_BINARY_DIR}/${_libName}/${_libName}.h"
  93. DESTINATION include/${_libName}
  94. )
  95. endif()
  96. endmacro()
  97. add_include_lib(testLibIncludeRequired1)
  98. add_include_lib(testLibIncludeRequired2)
  99. add_include_lib(testLibIncludeRequired3 NO_HEADER)
  100. # Generate testLibIncludeRequired4 in the testLibIncludeRequired3 directory
  101. # with an error. If the includes from testLibIncludeRequired3 appear first,
  102. # the error will be hit.
  103. # Below, the '3' library appears before the '4' library
  104. # but we are testing that the INSTALL_INTERFACE causes it not to be used
  105. # at build time.
  106. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/testLibIncludeRequired3/testLibIncludeRequired4.h" "#error Should not be included\n")
  107. install(FILES
  108. "${CMAKE_CURRENT_BINARY_DIR}/testLibIncludeRequired3/testLibIncludeRequired4.h"
  109. DESTINATION include/testLibIncludeRequired3
  110. )
  111. add_include_lib(testLibIncludeRequired4)
  112. add_include_lib(testLibIncludeRequired5 NO_HEADER)
  113. # Generate testLibIncludeRequired6 in the testLibIncludeRequired5 directory
  114. # with an error. If the includes from testLibIncludeRequired5 appear first,
  115. # the error will be hit.
  116. # Below, the '5' library appears before the '6' library
  117. # but we are testing that when the installed IMPORTED target is used, from
  118. # the Import side of this unit test, the '6' include from the '5' directory
  119. # will not be used because it is in the BUILD_INTERFACE only.
  120. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/testLibIncludeRequired5/testLibIncludeRequired6.h" "#error Should not be included\n")
  121. install(FILES
  122. "${CMAKE_CURRENT_BINARY_DIR}/testLibIncludeRequired5/testLibIncludeRequired6.h"
  123. DESTINATION include/testLibIncludeRequired5
  124. )
  125. add_include_lib(testLibIncludeRequired6)
  126. set_property(TARGET testLibRequired APPEND PROPERTY
  127. INTERFACE_INCLUDE_DIRECTORIES
  128. $<TARGET_PROPERTY:testLibIncludeRequired1,INTERFACE_INCLUDE_DIRECTORIES>
  129. $<TARGET_PROPERTY:$<1:$<TARGET_NAME:testLibIncludeRequired2>>,INTERFACE_INCLUDE_DIRECTORIES>
  130. $<INSTALL_INTERFACE:$<TARGET_PROPERTY:testLibIncludeRequired3,INTERFACE_INCLUDE_DIRECTORIES>>
  131. $<BUILD_INTERFACE:$<TARGET_PROPERTY:testLibIncludeRequired4,INTERFACE_INCLUDE_DIRECTORIES>>
  132. $<BUILD_INTERFACE:$<TARGET_PROPERTY:testLibIncludeRequired5,INTERFACE_INCLUDE_DIRECTORIES>>
  133. $<INSTALL_INTERFACE:$<TARGET_PROPERTY:testLibIncludeRequired6,INTERFACE_INCLUDE_DIRECTORIES>>
  134. # The BUILD_INTERFACE entry from above is duplicated below. This is to test that
  135. # the INSTALL_INTERFACE entry bound by a BUILD_INTERFACE entry on either side is
  136. # preprocessed correctly on install(EXPORT).
  137. $<BUILD_INTERFACE:$<TARGET_PROPERTY:testLibIncludeRequired5,INTERFACE_INCLUDE_DIRECTORIES>>
  138. # Test that the below is non-fatal
  139. $<$<STREQUAL:one,two>:$<TARGET_PROPERTY:not_a_target,INTERFACE_INCLUDE_DIRECTORIES>>
  140. )
  141. set_property(TARGET testLibRequired APPEND PROPERTY
  142. INTERFACE_COMPILE_DEFINITIONS
  143. testLibRequired_IFACE_DEFINE
  144. $<BUILD_INTERFACE:BuildOnly_DEFINE>
  145. $<INSTALL_INTERFACE:InstallOnly_DEFINE>
  146. )
  147. include(GenerateExportHeader)
  148. add_subdirectory(renamed)
  149. add_library(testSharedLibRequired SHARED testSharedLibRequired.cpp)
  150. generate_export_header(testSharedLibRequired)
  151. set_property(TARGET testSharedLibRequired
  152. PROPERTY
  153. INTERFACE_POSITION_INDEPENDENT_CODE ON
  154. )
  155. set_property(TARGET testSharedLibRequired APPEND PROPERTY
  156. INCLUDE_DIRECTORIES "${CMAKE_CURRENT_BINARY_DIR}"
  157. )
  158. install(FILES
  159. "${CMAKE_CURRENT_SOURCE_DIR}/testSharedLibRequired.h"
  160. "${CMAKE_CURRENT_BINARY_DIR}/testsharedlibrequired_export.h"
  161. DESTINATION include/testSharedLibRequired
  162. )
  163. set_property(TARGET testSharedLibRequired APPEND PROPERTY
  164. INTERFACE_INCLUDE_DIRECTORIES "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include/testSharedLibRequired>"
  165. "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR};${CMAKE_CURRENT_SOURCE_DIR}>"
  166. )
  167. set_property(TARGET testSharedLibRequired
  168. APPEND PROPERTY
  169. COMPATIBLE_INTERFACE_BOOL CUSTOM_PROP
  170. )
  171. set_property(TARGET testSharedLibRequired
  172. PROPERTY
  173. INTERFACE_CUSTOM_PROP ON
  174. )
  175. set_property(TARGET testSharedLibRequired
  176. APPEND PROPERTY
  177. COMPATIBLE_INTERFACE_STRING CUSTOM_STRING
  178. )
  179. set_property(TARGET testSharedLibRequired
  180. PROPERTY
  181. INTERFACE_CUSTOM_STRING testcontent
  182. )
  183. set_property(TARGET testSharedLibRequired APPEND PROPERTY
  184. INTERFACE_COMPILE_OPTIONS
  185. $<$<CXX_COMPILER_ID:GNU>:-DCUSTOM_COMPILE_OPTION>
  186. )
  187. add_library(testSharedLibDepends SHARED testSharedLibDepends.cpp)
  188. set_property(TARGET testSharedLibDepends APPEND PROPERTY
  189. INCLUDE_DIRECTORIES "${CMAKE_CURRENT_BINARY_DIR}"
  190. )
  191. generate_export_header(testSharedLibDepends)
  192. set_property(TARGET testSharedLibDepends APPEND PROPERTY
  193. INTERFACE_INCLUDE_DIRECTORIES
  194. $<TARGET_PROPERTY:testSharedLibRequired,INTERFACE_INCLUDE_DIRECTORIES>
  195. )
  196. install(FILES
  197. "${CMAKE_CURRENT_SOURCE_DIR}/testSharedLibDepends.h"
  198. "${CMAKE_CURRENT_BINARY_DIR}/testsharedlibdepends_export.h"
  199. DESTINATION include/testSharedLibDepends
  200. )
  201. set_property(TARGET testSharedLibDepends APPEND PROPERTY
  202. INTERFACE_INCLUDE_DIRECTORIES "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include/testSharedLibDepends>"
  203. "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR};${CMAKE_CURRENT_SOURCE_DIR}>"
  204. )
  205. # LINK_PRIVATE because the LINK_INTERFACE_LIBRARIES is specified above.
  206. target_link_libraries(testSharedLibDepends LINK_PRIVATE testSharedLibRequired)
  207. target_link_libraries(testSharedLibDepends LINK_PUBLIC renamed_on_export)
  208. target_link_libraries(testSharedLibDepends LINK_INTERFACE_LIBRARIES
  209. $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:$<TARGET_NAME:testSharedLibRequired>>)
  210. install(TARGETS testLibRequired
  211. testLibIncludeRequired1
  212. testLibIncludeRequired2
  213. testLibIncludeRequired3
  214. testLibIncludeRequired4
  215. testLibIncludeRequired5
  216. testLibIncludeRequired6
  217. testSharedLibRequired
  218. EXPORT RequiredExp DESTINATION lib )
  219. install(EXPORT RequiredExp NAMESPACE Req:: FILE testLibRequiredTargets.cmake DESTINATION lib/cmake/testLibRequired)
  220. install(TARGETS testLibDepends testSharedLibDepends EXPORT DependsExp DESTINATION lib )
  221. install(EXPORT DependsExp FILE testLibDependsTargets.cmake DESTINATION lib/cmake/testLibDepends)
  222. file(WRITE
  223. "${CMAKE_CURRENT_BINARY_DIR}/testLibRequiredConfig.cmake"
  224. "
  225. if(\${CMAKE_FIND_PACKAGE_NAME}_FIND_VERSION VERSION_LESS 2.3 AND NOT \${CMAKE_FIND_PACKAGE_NAME}_INTERFACES)
  226. set(\${CMAKE_FIND_PACKAGE_NAME}_NO_INTERFACES 1)
  227. endif()
  228. include(\"\${CMAKE_CURRENT_LIST_DIR}/testLibRequiredTargets.cmake\")
  229. set(\${CMAKE_FIND_PACKAGE_NAME}_INCLUDE_DIRS \"${CMAKE_CURRENT_BINARY_DIR}\" \"${CMAKE_CURRENT_SOURCE_DIR}\" )
  230. "
  231. )
  232. include(CMakePackageConfigHelpers)
  233. write_basic_package_version_file( testLibRequiredConfigVersion.cmake VERSION 2.5 COMPATIBILITY AnyNewerVersion)
  234. install(FILES
  235. "${CMAKE_CURRENT_BINARY_DIR}/testLibRequiredConfig.cmake"
  236. "${CMAKE_CURRENT_BINARY_DIR}/testLibRequiredConfigVersion.cmake"
  237. DESTINATION lib/cmake/testLibRequired
  238. )
  239. # Install and export from install tree.
  240. install(
  241. TARGETS
  242. testExe1 testLib1 testLib2 testExe2 testLib3 testLib4 testExe3
  243. testExe2lib testLib4lib testLib4libdbg testLib4libopt
  244. testLib6
  245. testLibCycleA testLibCycleB
  246. EXPORT exp
  247. RUNTIME DESTINATION bin
  248. LIBRARY DESTINATION lib NAMELINK_SKIP
  249. ARCHIVE DESTINATION lib
  250. FRAMEWORK DESTINATION Frameworks
  251. BUNDLE DESTINATION Applications
  252. )
  253. install(
  254. TARGETS
  255. testExe2libImp testLib3Imp
  256. EXPORT exp
  257. RUNTIME DESTINATION bin
  258. LIBRARY DESTINATION lib/impl
  259. ARCHIVE DESTINATION lib/impl
  260. )
  261. install(
  262. TARGETS
  263. testLib3ImpDep
  264. EXPORT exp
  265. RUNTIME DESTINATION bin
  266. LIBRARY DESTINATION lib/impl/dep
  267. ARCHIVE DESTINATION lib/impl/dep
  268. )
  269. install(
  270. TARGETS testLib5
  271. EXPORT exp
  272. # Leave out RUNTIME DESTINATION to test implib-only export.
  273. LIBRARY DESTINATION lib
  274. ARCHIVE DESTINATION lib
  275. )
  276. install(EXPORT exp NAMESPACE exp_ DESTINATION lib/exp)
  277. # Install testLib5.dll outside the export.
  278. if(WIN32)
  279. install(TARGETS testLib5 RUNTIME DESTINATION bin)
  280. endif()
  281. add_subdirectory(sublib) # For CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE test.
  282. # Export from build tree.
  283. export(TARGETS testExe1 testLib1 testLib2 testLib3
  284. testExe2libImp testLib3Imp testLib3ImpDep subdirlib
  285. testSharedLibRequired testSharedLibDepends renamed_on_export
  286. NAMESPACE bld_
  287. FILE ExportBuildTree.cmake
  288. )
  289. export(TARGETS testExe2 testLib4 testLib5 testLib6 testExe3 testExe2lib
  290. testLib4lib testLib4libdbg testLib4libopt
  291. testLibCycleA testLibCycleB
  292. NAMESPACE bld_
  293. APPEND FILE ExportBuildTree.cmake
  294. )