CMakeLists.txt 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. set(CMAKE_EXPERIMENTAL_EXPORT_PACKAGE_DEPENDENCIES "1942b4fa-b2c5-4546-9385-83f254070067")
  2. cmake_minimum_required(VERSION 3.10)
  3. if(POLICY CMP0129)
  4. cmake_policy(SET CMP0129 NEW)
  5. endif()
  6. project(Export C CXX)
  7. set(CMAKE_AIX_SHARED_LIBRARY_ARCHIVE 1)
  8. find_package(Foo REQUIRED CONFIG NO_DEFAULT_PATH)
  9. # Pretend that RelWithDebInfo should link to debug libraries to test
  10. # the DEBUG_CONFIGURATIONS property.
  11. set_property(GLOBAL PROPERTY DEBUG_CONFIGURATIONS Debug RelWithDebInfo)
  12. add_library(testExe1lib STATIC testExe1lib.c) # not exported
  13. add_executable(testExe1 testExe1.c)
  14. target_link_libraries(testExe1 testExe1lib)
  15. set_property(TARGET testExe1 PROPERTY VERSION 4)
  16. add_library(testExe2libImp SHARED testExe2libImp.c)
  17. set_property(TARGET testExe2libImp PROPERTY LIBRARY_OUTPUT_DIRECTORY impl)
  18. add_library(testExe2lib SHARED testExe2lib.c)
  19. target_link_libraries(testExe2lib testExe2libImp)
  20. add_executable(testExe2 testExe2.c)
  21. set_property(TARGET testExe2 PROPERTY ENABLE_EXPORTS 1)
  22. set_property(TARGET testExe2 PROPERTY INTERFACE_LINK_LIBRARIES testExe2lib)
  23. add_library(compileOnly INTERFACE)
  24. target_compile_definitions(compileOnly INTERFACE FROM_compileOnly)
  25. target_link_options(compileOnly INTERFACE -fthis-flag-does-not-exist)
  26. add_library(noUses INTERFACE)
  27. target_link_libraries(noUses INTERFACE this::target_does_not_exist)
  28. add_library(testLib1 STATIC testLib1.c)
  29. add_library(testLib2 STATIC testLib2.c)
  30. target_link_libraries(testLib2
  31. PRIVATE
  32. testLib1
  33. "$<COMPILE_ONLY:compileOnly>")
  34. # Test install(FILES) with generator expressions referencing testLib1.
  35. add_custom_command(TARGET testLib1 POST_BUILD
  36. COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:testLib1>
  37. $<TARGET_FILE:testLib1>.genex
  38. )
  39. install(FILES $<TARGET_FILE:testLib1>.genex
  40. DESTINATION $<1:lib>$<0:/wrong>
  41. )
  42. set_property(TARGET testLib1 PROPERTY MY_FILES
  43. ${CMAKE_CURRENT_SOURCE_DIR}/testLib1file1.txt
  44. ${CMAKE_CURRENT_SOURCE_DIR}/testLib1file2.txt
  45. )
  46. install(FILES $<TARGET_PROPERTY:testLib1,MY_FILES>
  47. DESTINATION $<1:doc>$<0:/wrong>
  48. )
  49. # Test library with empty link interface. Link it to an implementation
  50. # dependency that itself links to dependencies publicly.
  51. add_library(testLib3ImpDep SHARED testLib3ImpDep.c)
  52. set_property(TARGET testLib3ImpDep PROPERTY LIBRARY_OUTPUT_DIRECTORY impl/dep)
  53. add_library(testLib3Imp SHARED testLib3Imp.c)
  54. set_property(TARGET testLib3Imp PROPERTY LIBRARY_OUTPUT_DIRECTORY impl)
  55. target_link_libraries(testLib3Imp testLib3ImpDep)
  56. add_library(testLib3 SHARED testLib3.c)
  57. target_link_libraries(testLib3 testLib3Imp)
  58. set_property(TARGET testLib3 PROPERTY VERSION 1.2)
  59. set_property(TARGET testLib3 PROPERTY SOVERSION 3)
  60. # Test <ARCHIVE|LIBRARY|RUNTIME>_OUTPUT_NAME[_<CONFIG>] properties.
  61. set_property(TARGET testLib3 PROPERTY RUNTIME_OUTPUT_NAME_DEBUG testLib3dll-d)
  62. set_property(TARGET testLib3 PROPERTY RUNTIME_OUTPUT_NAME_RELEASE testLib3dll-r)
  63. set_property(TARGET testLib3 PROPERTY RUNTIME_OUTPUT_NAME testLib3dll)
  64. set_property(TARGET testLib3 PROPERTY LIBRARY_OUTPUT_NAME_DEBUG testLib3lib-d)
  65. set_property(TARGET testLib3 PROPERTY LIBRARY_OUTPUT_NAME_RELEASE testLib3lib-r)
  66. set_property(TARGET testLib3 PROPERTY LIBRARY_OUTPUT_NAME testLib3lib)
  67. set_property(TARGET testLib3 PROPERTY ARCHIVE_OUTPUT_NAME testLib3import)
  68. add_library(testLib4 SHARED testLib4.c)
  69. set_property(TARGET testLib4 PROPERTY FRAMEWORK 1)
  70. add_library(testLib5 SHARED testLib5.c)
  71. add_library(testLib6 STATIC testLib6.cxx testLib6c.c)
  72. add_library(testLibPerConfigDest STATIC testLibPerConfigDest.c)
  73. install(TARGETS testLibPerConfigDest EXPORT exp
  74. DESTINATION lib/$<$<BOOL:$<CONFIG>>:$<CONFIG>>$<$<NOT:$<BOOL:$<CONFIG>>>:NoConfig>
  75. )
  76. # Test OUTPUT_NAME properties with generator expressions
  77. add_library(testLib7 STATIC testLib7.c)
  78. set_property(TARGET testLib7 PROPERTY OUTPUT_NAME_DEBUG testLib7D-$<CONFIG>)
  79. set_property(TARGET testLib7 PROPERTY OUTPUT_NAME_RELEASE testLib7R-$<CONFIG>)
  80. set_property(TARGET testLib7 PROPERTY OUTPUT_NAME testLib7-$<CONFIG>)
  81. # Test exporting OBJECT targets
  82. add_library(testLib8 OBJECT testLib8A.c testLib8B.c sub/testLib8C.c)
  83. if(NOT CMAKE_GENERATOR STREQUAL "Xcode" OR NOT CMAKE_OSX_ARCHITECTURES MATCHES "[;$]")
  84. set(maybe_OBJECTS_DESTINATION OBJECTS DESTINATION $<1:lib>)
  85. else()
  86. set(maybe_OBJECTS_DESTINATION "")
  87. endif()
  88. add_library(testLib9ObjPub OBJECT testLib9ObjPub.c)
  89. target_compile_definitions(testLib9ObjPub INTERFACE testLib9ObjPub_USED)
  90. add_library(testLib9ObjPriv OBJECT testLib9ObjPriv.c)
  91. target_compile_definitions(testLib9ObjPriv INTERFACE testLib9ObjPriv_USED)
  92. add_library(testLib9ObjIface OBJECT testLib9ObjIface.c)
  93. target_compile_definitions(testLib9ObjIface INTERFACE testLib9ObjIface_USED)
  94. add_library(testLib9 STATIC testLib9.c)
  95. target_link_libraries(testLib9 INTERFACE testLib9ObjIface PUBLIC testLib9ObjPub PRIVATE testLib9ObjPriv)
  96. target_link_libraries(testLib9 PUBLIC Foo::Foo)
  97. add_library(testLib10 STATIC testLib10.c)
  98. set_target_properties(testLib10 PROPERTIES
  99. TRANSITIVE_COMPILE_PROPERTIES "CUSTOM_C"
  100. TRANSITIVE_LINK_PROPERTIES "CUSTOM_L"
  101. INTERFACE_CUSTOM_C "TESTLIB10_INTERFACE_CUSTOM_C"
  102. INTERFACE_CUSTOM_L "TESTLIB10_INTERFACE_CUSTOM_L"
  103. )
  104. target_compile_definitions(testLib10 INTERFACE
  105. "$<TARGET_PROPERTY:CUSTOM_C>"
  106. "$<TARGET_PROPERTY:CUSTOM_L>"
  107. )
  108. add_library(testLib11 STATIC testLib11.c)
  109. target_link_libraries(testLib11 PRIVATE testLib10)
  110. set_target_properties(testLib11 PROPERTIES
  111. INTERFACE_CUSTOM_C "TESTLIB11_INTERFACE_CUSTOM_C"
  112. INTERFACE_CUSTOM_L "TESTLIB11_INTERFACE_CUSTOM_L"
  113. TRANSITIVE_COMPILE_PROPERTIES "CUSTOM_D"
  114. TRANSITIVE_LINK_PROPERTIES "CUSTOM_M"
  115. INTERFACE_CUSTOM_D "TESTLIB11_INTERFACE_CUSTOM_D"
  116. INTERFACE_CUSTOM_M "TESTLIB11_INTERFACE_CUSTOM_M"
  117. )
  118. target_compile_definitions(testLib11 INTERFACE
  119. "$<TARGET_PROPERTY:CUSTOM_C>"
  120. "$<TARGET_PROPERTY:CUSTOM_D>"
  121. "$<TARGET_PROPERTY:CUSTOM_L>"
  122. "$<TARGET_PROPERTY:CUSTOM_M>"
  123. )
  124. # Test using the target_link_libraries command to set the
  125. # INTERFACE_LINK_LIBRARIES properties. We construct two libraries
  126. # providing the same two symbols. In each library one of the symbols
  127. # will work and the other one will fail to link. The import part of
  128. # this test will try to use the symbol corresponding to the
  129. # configuration in which it is built. If the proper library is not
  130. # used via the link interface the import test will fail to link.
  131. add_library(testLib4lib STATIC testLib4lib.c)
  132. add_library(testLib4libdbg STATIC testLib4libopt.c testLib4libdbg.c)
  133. add_library(testLib4libopt STATIC testLib4libdbg.c testLib4libopt.c)
  134. set_property(TARGET testLib4libdbg PROPERTY COMPILE_DEFINITIONS LIB_DBG)
  135. set_property(TARGET testLib4libopt PROPERTY COMPILE_DEFINITIONS LIB_OPT)
  136. target_link_libraries(testLib4
  137. LINK_INTERFACE_LIBRARIES
  138. testLib4lib debug $<TARGET_NAME:testLib4libdbg> optimized $<TARGET_NAME:testLib4libopt>
  139. )
  140. add_executable(testExe3 testExe3.c)
  141. set_property(TARGET testExe3 PROPERTY MACOSX_BUNDLE 1)
  142. # Test <ARCHIVE|LIBRARY|RUNTIME>_OUTPUT_DIRECTORY[_<CONFIG>] properties with generator expressions
  143. add_executable(testExe4 testExe4.c)
  144. target_link_libraries(testExe4 testExe1lib)
  145. set_property(TARGET testLib7 PROPERTY ARCHIVE_OUTPUT_DIRECTORY_DEBUG testLib7D-$<CONFIG>)
  146. set_property(TARGET testLib7 PROPERTY ARCHIVE_OUTPUT_DIRECTORY_RELEASE testLib7R-$<CONFIG>)
  147. set_property(TARGET testLib7 PROPERTY ARCHIVE_OUTPUT_DIRECTORY testLib7-$<CONFIG>)
  148. set_property(TARGET testLib5 PROPERTY LIBRARY_OUTPUT_DIRECTORY_DEBUG testLib5D-$<CONFIG>)
  149. set_property(TARGET testLib5 PROPERTY LIBRARY_OUTPUT_DIRECTORY_RELEASE testLib5R-$<CONFIG>)
  150. set_property(TARGET testLib5 PROPERTY LIBRARY_OUTPUT_DIRECTORY testLib5-$<CONFIG>)
  151. set_property(TARGET testExe4 PROPERTY RUNTIME_OUTPUT_DIRECTORY_DEBUG testExe4D-$<CONFIG>)
  152. set_property(TARGET testExe4 PROPERTY RUNTIME_OUTPUT_DIRECTORY_RELEASE testExe4R-$<CONFIG>)
  153. set_property(TARGET testExe4 PROPERTY RUNTIME_OUTPUT_DIRECTORY testExe4-$<CONFIG>)
  154. # Test cyclic dependencies.
  155. add_library(testLibCycleA STATIC
  156. testLibCycleA1.c testLibCycleA2.c testLibCycleA3.c)
  157. add_library(testLibCycleB STATIC
  158. testLibCycleB1.c testLibCycleB2.c testLibCycleB3.c)
  159. target_link_libraries(testLibCycleA testLibCycleB)
  160. target_link_libraries(testLibCycleB testLibCycleA)
  161. set_property(TARGET testLibCycleA PROPERTY LINK_INTERFACE_MULTIPLICITY 3)
  162. add_library(testLibNoSONAME SHARED testLibNoSONAME.c)
  163. set_property(TARGET testLibNoSONAME PROPERTY NO_SONAME 1)
  164. add_library(testInterfaceIncludeUser INTERFACE)
  165. target_include_directories(testInterfaceIncludeUser
  166. INTERFACE
  167. "$<INSTALL_INTERFACE:include/testInterfaceIncludeUser>"
  168. "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/testInterfaceIncludeUser>"
  169. )
  170. set_property(TARGET testInterfaceIncludeUser PROPERTY IMPORTED_NO_SYSTEM 1)
  171. add_library(testInterfaceIncludeUser2 INTERFACE)
  172. target_include_directories(testInterfaceIncludeUser2
  173. INTERFACE
  174. "$<INSTALL_INTERFACE:include/testInterfaceIncludeUser>"
  175. "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/testInterfaceIncludeUser>"
  176. )
  177. set_property(TARGET testInterfaceIncludeUser2 PROPERTY EXPORT_NO_SYSTEM 1)
  178. install(
  179. FILES
  180. "${CMAKE_CURRENT_SOURCE_DIR}/include/testInterfaceIncludeUser/testInterfaceInclude.h"
  181. DESTINATION include/testInterfaceIncludeUser
  182. )
  183. # Test control over direct linking.
  184. include(../../InterfaceLinkLibrariesDirect/testStaticLibPlugin.cmake)
  185. include(../../InterfaceLinkLibrariesDirect/testSharedLibWithHelper.cmake)
  186. include(../../InterfaceLinkLibrariesDirect/testExeWithPluginHelper.cmake)
  187. if(NOT maybe_OBJECTS_DESTINATION)
  188. target_compile_definitions(testSharedLibHelperObj INTERFACE testSharedLibHelperObj_NO_OBJECT)
  189. target_compile_definitions(testExePluginHelperObj INTERFACE testExePluginHelperObj_NO_OBJECT)
  190. endif()
  191. # Test exporting dependent libraries into different exports
  192. add_library(testLibRequired testLibRequired.c)
  193. add_library(testLibDepends testLibDepends.c)
  194. target_link_libraries(testLibDepends LINK_PUBLIC testLibRequired)
  195. add_library(testStaticLibRequiredPrivate testStaticLibRequiredPrivate.c)
  196. target_link_libraries(testLibDepends PRIVATE testStaticLibRequiredPrivate)
  197. cmake_policy(PUSH)
  198. cmake_policy(SET CMP0079 NEW)
  199. add_library(TopDirLib STATIC testTopDirLib.c)
  200. add_subdirectory(SubDirLinkA)
  201. add_subdirectory(SubDirLinkB)
  202. cmake_policy(POP)
  203. macro(add_include_lib _libName)
  204. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${_libName}.c" "/* no content */\n")
  205. add_library(${_libName} "${CMAKE_CURRENT_BINARY_DIR}/${_libName}.c")
  206. file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${_libName}")
  207. set_property(TARGET ${_libName} APPEND PROPERTY
  208. INTERFACE_INCLUDE_DIRECTORIES
  209. "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/${_libName}>"
  210. )
  211. if (NOT "${ARGV1}" STREQUAL "NO_HEADER")
  212. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${_libName}/${_libName}.h" "/* no content */\n")
  213. install(FILES
  214. "${CMAKE_CURRENT_BINARY_DIR}/${_libName}/${_libName}.h"
  215. DESTINATION include/${_libName}
  216. )
  217. endif()
  218. endmacro()
  219. add_include_lib(testLibIncludeRequired1)
  220. add_include_lib(testLibIncludeRequired2)
  221. add_include_lib(testLibIncludeRequired3 NO_HEADER)
  222. # Generate testLibIncludeRequired4 in the testLibIncludeRequired3 directory
  223. # with an error. If the includes from testLibIncludeRequired3 appear first,
  224. # the error will be hit.
  225. # Below, the '3' library appears before the '4' library
  226. # but we are testing that the INSTALL_INTERFACE causes it not to be used
  227. # at build time.
  228. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/testLibIncludeRequired3/testLibIncludeRequired4.h" "#error Should not be included\n")
  229. install(FILES
  230. "${CMAKE_CURRENT_BINARY_DIR}/testLibIncludeRequired3/testLibIncludeRequired4.h"
  231. DESTINATION include/testLibIncludeRequired3
  232. )
  233. add_include_lib(testLibIncludeRequired4)
  234. add_include_lib(testLibIncludeRequired5 NO_HEADER)
  235. # Generate testLibIncludeRequired6 in the testLibIncludeRequired5 directory
  236. # with an error. If the includes from testLibIncludeRequired5 appear first,
  237. # the error will be hit.
  238. # Below, the '5' library appears before the '6' library
  239. # but we are testing that when the installed IMPORTED target is used, from
  240. # the Import side of this unit test, the '6' include from the '5' directory
  241. # will not be used because it is in the BUILD_INTERFACE only.
  242. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/testLibIncludeRequired5/testLibIncludeRequired6.h" "#error Should not be included\n")
  243. install(FILES
  244. "${CMAKE_CURRENT_BINARY_DIR}/testLibIncludeRequired5/testLibIncludeRequired6.h"
  245. DESTINATION include/testLibIncludeRequired5
  246. )
  247. add_include_lib(testLibIncludeRequired6)
  248. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/testLibIncludeRequired7/testLibIncludeRequired7.h" "/* No content */\n")
  249. install(FILES
  250. "${CMAKE_CURRENT_BINARY_DIR}/testLibIncludeRequired7/testLibIncludeRequired7.h"
  251. DESTINATION include/testLibIncludeRequired7
  252. )
  253. set_property(TARGET testLibRequired APPEND PROPERTY
  254. INTERFACE_INCLUDE_DIRECTORIES
  255. $<TARGET_PROPERTY:testLibIncludeRequired1,INTERFACE_INCLUDE_DIRECTORIES>
  256. $<TARGET_PROPERTY:$<1:$<TARGET_NAME:testLibIncludeRequired2>>,INTERFACE_INCLUDE_DIRECTORIES>
  257. $<INSTALL_INTERFACE:$<TARGET_PROPERTY:testLibIncludeRequired3,INTERFACE_INCLUDE_DIRECTORIES>>
  258. $<BUILD_INTERFACE:$<TARGET_PROPERTY:testLibIncludeRequired4,INTERFACE_INCLUDE_DIRECTORIES>>
  259. $<BUILD_INTERFACE:$<TARGET_PROPERTY:testLibIncludeRequired5,INTERFACE_INCLUDE_DIRECTORIES>>
  260. $<INSTALL_INTERFACE:$<TARGET_PROPERTY:testLibIncludeRequired6,INTERFACE_INCLUDE_DIRECTORIES>>
  261. # The BUILD_INTERFACE entry from above is duplicated below. This is to test that
  262. # the INSTALL_INTERFACE entry bound by a BUILD_INTERFACE entry on either side is
  263. # preprocessed correctly on install(EXPORT).
  264. $<BUILD_INTERFACE:$<TARGET_PROPERTY:testLibIncludeRequired5,INTERFACE_INCLUDE_DIRECTORIES>>
  265. # Test that the below is non-fatal
  266. $<$<STREQUAL:one,two>:$<TARGET_PROPERTY:not_a_target,INTERFACE_INCLUDE_DIRECTORIES>>
  267. $<INSTALL_INTERFACE:include/testLibIncludeRequired7;include/testLibIncludeRequired4>
  268. )
  269. set_property(TARGET testLibRequired APPEND PROPERTY
  270. INTERFACE_COMPILE_DEFINITIONS
  271. testLibRequired_IFACE_DEFINE
  272. $<BUILD_INTERFACE:BuildOnly_DEFINE>
  273. $<INSTALL_INTERFACE:InstallOnly_DEFINE>
  274. )
  275. include(GenerateExportHeader)
  276. # Test deprecation of imported library
  277. add_library(testLibDeprecation STATIC testLib1.c)
  278. set_property(TARGET testLibDeprecation PROPERTY DEPRECATION "Deprecated version. Please use latest version")
  279. add_subdirectory(renamed)
  280. add_library(testSharedLibRequired SHARED testSharedLibRequired.cpp)
  281. generate_export_header(testSharedLibRequired)
  282. set_property(TARGET testSharedLibRequired
  283. PROPERTY
  284. INTERFACE_POSITION_INDEPENDENT_CODE ON
  285. )
  286. set_property(TARGET testSharedLibRequired APPEND PROPERTY
  287. INCLUDE_DIRECTORIES "${CMAKE_CURRENT_BINARY_DIR}"
  288. )
  289. install(FILES
  290. "${CMAKE_CURRENT_SOURCE_DIR}/testSharedLibRequired.h"
  291. "${CMAKE_CURRENT_BINARY_DIR}/testsharedlibrequired_export.h"
  292. DESTINATION include/testSharedLibRequired
  293. )
  294. set_property(TARGET testSharedLibRequired APPEND PROPERTY
  295. INTERFACE_INCLUDE_DIRECTORIES "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR};${CMAKE_CURRENT_SOURCE_DIR}>"
  296. )
  297. set_property(TARGET testSharedLibRequired APPEND PROPERTY
  298. INTERFACE_COMPILE_DEFINITIONS USING_TESTSHAREDLIBREQUIRED
  299. )
  300. set_property(TARGET testSharedLibRequired
  301. APPEND PROPERTY
  302. COMPATIBLE_INTERFACE_BOOL CUSTOM_PROP
  303. )
  304. set_property(TARGET testSharedLibRequired
  305. PROPERTY
  306. INTERFACE_CUSTOM_PROP ON
  307. )
  308. set_property(TARGET testSharedLibRequired
  309. APPEND PROPERTY
  310. COMPATIBLE_INTERFACE_STRING CUSTOM_STRING
  311. )
  312. set_property(TARGET testSharedLibRequired
  313. PROPERTY
  314. INTERFACE_CUSTOM_STRING testcontent
  315. )
  316. set_property(TARGET testSharedLibRequired APPEND PROPERTY
  317. INTERFACE_COMPILE_OPTIONS
  318. $<$<CXX_COMPILER_ID:GNU>:-DCUSTOM_COMPILE_OPTION>
  319. $<$<CXX_COMPILER_ID:LCC>:-DCUSTOM_COMPILE_OPTION>
  320. )
  321. add_library(testSharedLibRequiredUser SHARED testSharedLibRequiredUser.cpp)
  322. generate_export_header(testSharedLibRequiredUser)
  323. # LINK_PRIVATE so that it appears in the LINK_DEPENDENT_LIBRARIES, but not
  324. # the INTERFACE_LINK_LIBRARIES.
  325. set_property(TARGET testSharedLibRequiredUser APPEND PROPERTY
  326. INTERFACE_INCLUDE_DIRECTORIES "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR};${CMAKE_CURRENT_SOURCE_DIR}>"
  327. )
  328. target_link_libraries(testSharedLibRequiredUser LINK_PRIVATE testSharedLibRequired)
  329. install(FILES
  330. "${CMAKE_CURRENT_SOURCE_DIR}/testSharedLibRequiredUser.h"
  331. "${CMAKE_CURRENT_BINARY_DIR}/testsharedlibrequireduser_export.h"
  332. DESTINATION include/testSharedLibRequiredUser
  333. )
  334. add_library(testSharedLibRequiredUser2 SHARED testSharedLibRequiredUser2.cpp)
  335. generate_export_header(testSharedLibRequiredUser2)
  336. set_property(TARGET testSharedLibRequiredUser2 APPEND PROPERTY
  337. INTERFACE_INCLUDE_DIRECTORIES "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR};${CMAKE_CURRENT_SOURCE_DIR}>"
  338. )
  339. set_property(TARGET testSharedLibRequiredUser2 PROPERTY LINK_LIBRARIES testSharedLibRequired)
  340. set_property(TARGET testSharedLibRequiredUser2 PROPERTY INTERFACE_LINK_LIBRARIES testSharedLibRequired)
  341. install(FILES
  342. "${CMAKE_CURRENT_SOURCE_DIR}/testSharedLibRequiredUser2.h"
  343. "${CMAKE_CURRENT_BINARY_DIR}/testsharedlibrequireduser2_export.h"
  344. DESTINATION include/testSharedLibRequiredUser2
  345. )
  346. cmake_policy(PUSH)
  347. cmake_policy(SET CMP0041 NEW)
  348. add_library(testSharedLibDepends SHARED testSharedLibDepends.cpp)
  349. set_property(TARGET testSharedLibDepends APPEND PROPERTY
  350. INCLUDE_DIRECTORIES "${CMAKE_CURRENT_BINARY_DIR}"
  351. )
  352. generate_export_header(testSharedLibDepends)
  353. set_property(TARGET testSharedLibDepends APPEND PROPERTY
  354. INTERFACE_INCLUDE_DIRECTORIES
  355. $<TARGET_PROPERTY:testSharedLibRequired,INTERFACE_INCLUDE_DIRECTORIES>
  356. )
  357. install(FILES
  358. "${CMAKE_CURRENT_SOURCE_DIR}/testSharedLibDepends.h"
  359. "${CMAKE_CURRENT_BINARY_DIR}/testsharedlibdepends_export.h"
  360. DESTINATION include/testSharedLibDepends
  361. )
  362. set_property(TARGET testSharedLibDepends APPEND PROPERTY
  363. INTERFACE_INCLUDE_DIRECTORIES "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/$<1:include>/testSharedLibDepends>"
  364. "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR};${CMAKE_CURRENT_SOURCE_DIR}>"
  365. )
  366. cmake_policy(POP)
  367. target_link_libraries(testSharedLibDepends LINK_PRIVATE testSharedLibRequired)
  368. target_link_libraries(testSharedLibDepends LINK_PUBLIC renamed_on_export)
  369. target_link_libraries(testSharedLibDepends INTERFACE
  370. $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:$<TARGET_NAME:testSharedLibRequired>>)
  371. add_library(cmp0022NEW SHARED cmp0022_vs6_1.cpp)
  372. generate_export_header(cmp0022NEW BASE_NAME cmp0022)
  373. target_include_directories(cmp0022NEW PUBLIC
  374. "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR};${CMAKE_CURRENT_SOURCE_DIR}>"
  375. "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include/cmp0022>"
  376. )
  377. install(FILES
  378. "${CMAKE_CURRENT_SOURCE_DIR}/cmp0022.h"
  379. "${CMAKE_CURRENT_BINARY_DIR}/cmp0022_export.h"
  380. DESTINATION include/cmp0022
  381. )
  382. set_property(TARGET testLib2 APPEND PROPERTY INTERFACE_COMPILE_DEFINITIONS USING_TESTLIB2)
  383. set_property(TARGET testLib3 APPEND PROPERTY INTERFACE_COMPILE_DEFINITIONS USING_TESTLIB3)
  384. set_target_properties(testLib3 PROPERTIES
  385. EXPORT_PROPERTIES "EXPORTED_PROPERTY1"
  386. EXPORTED_PROPERTY1 "EXPORTING_TESTLIB3")
  387. set_target_properties(testLib4 PROPERTIES
  388. EXPORTED_PROPERTY2 "EXPORTING_TESTLIB4_1"
  389. EXPORTED_PROPERTY3 "EXPORTING_TESTLIB4_2")
  390. set_property(TARGET testLib4 PROPERTY
  391. EXPORT_PROPERTIES EXPORTED_PROPERTY2 EXPORTED_PROPERTY3)
  392. set_property(TARGET cmp0022NEW APPEND PROPERTY INTERFACE_LINK_LIBRARIES testLib2)
  393. add_library(noIncludesInterface empty.cpp)
  394. add_library(systemlib SHARED systemlib.cpp)
  395. install(FILES systemlib.h DESTINATION include/systemlib)
  396. target_include_directories(systemlib
  397. INTERFACE
  398. $<INSTALL_INTERFACE:include/systemlib>
  399. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
  400. )
  401. add_library(testMod1 MODULE empty.cpp)
  402. add_library(testMod2 MODULE empty.cpp)
  403. set_property(TARGET testMod2 PROPERTY BUNDLE 1)
  404. add_library(testSharedObjectAIX SHARED testSharedObjectAIX.c)
  405. set_property(TARGET testSharedObjectAIX PROPERTY AIX_SHARED_LIBRARY_ARCHIVE 0)
  406. install(TARGETS testLibRequired
  407. EXPORT RequiredExp DESTINATION lib
  408. INCLUDES DESTINATION
  409. installIncludesTest
  410. $<INSTALL_PREFIX>/installIncludesTest2
  411. installIncludesTest3/$<TARGET_PROPERTY:NAME>
  412. $<TARGET_PROPERTY:NAME>/installIncludesTest4
  413. $<INSTALL_INTERFACE:installIncludesTest5$<0:>>
  414. $<INSTALL_INTERFACE:$<0:>installIncludesTest6>
  415. $<INSTALL_INTERFACE:$<INSTALL_PREFIX>/installIncludesTest7>
  416. )
  417. target_include_directories(testLibRequired INTERFACE
  418. # These can't be in the above install(INCLUDES DESTINATION call because
  419. # that is only for installed interfaces. These directories are prefixes
  420. # in the build dir, which is an error for the installed interface.
  421. # We add them here so that we don't have to add conditions in the Import
  422. # component of the test.
  423. $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest5$<0:>>
  424. $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/$<0:>installIncludesTest6>
  425. $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest7>
  426. $<INSTALL_INTERFACE:installIncludesTest8/$<0:>>
  427. $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest8$<0:>>
  428. )
  429. install(TARGETS
  430. testLibIncludeRequired1
  431. testLibIncludeRequired2
  432. testLibIncludeRequired3
  433. testLibIncludeRequired4
  434. testLibIncludeRequired5
  435. testLibIncludeRequired6
  436. testSharedLibRequired
  437. testSharedLibRequiredUser
  438. testSharedLibRequiredUser2
  439. noIncludesInterface
  440. EXPORT RequiredExp DESTINATION lib
  441. INCLUDES DESTINATION
  442. $<INSTALL_PREFIX>/include/$<TARGET_PROPERTY:NAME>
  443. )
  444. install(TARGETS
  445. testStaticLibRequiredPrivate
  446. EXPORT RequiredExp DESTINATION lib
  447. )
  448. install(EXPORT RequiredExp NAMESPACE Req:: FILE testLibRequiredTargets.cmake DESTINATION lib/cmake/testLibRequired)
  449. file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest")
  450. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest/installIncludesTest.h" "/* No content */\n")
  451. file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest2")
  452. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest2/installIncludesTest2.h" "/* No content */\n")
  453. file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest3/testLibRequired")
  454. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest3/testLibRequired/installIncludesTest3.h" "/* No content */\n")
  455. file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/testLibRequired/installIncludesTest4")
  456. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/testLibRequired/installIncludesTest4/installIncludesTest4.h" "/* No content */\n")
  457. file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest5")
  458. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest5/installIncludesTest5.h" "/* No content */\n")
  459. file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest6")
  460. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest6/installIncludesTest6.h" "/* No content */\n")
  461. file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest7")
  462. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest7/installIncludesTest7.h" "/* No content */\n")
  463. file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest8")
  464. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest8/installIncludesTest8.h" "/* No content */\n")
  465. install(FILES
  466. "${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest/installIncludesTest.h"
  467. DESTINATION installIncludesTest
  468. )
  469. install(FILES
  470. "${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest2/installIncludesTest2.h"
  471. DESTINATION installIncludesTest2
  472. )
  473. install(FILES
  474. "${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest3/testLibRequired/installIncludesTest3.h"
  475. DESTINATION installIncludesTest3/testLibRequired
  476. )
  477. install(FILES
  478. "${CMAKE_CURRENT_BINARY_DIR}/testLibRequired/installIncludesTest4/installIncludesTest4.h"
  479. DESTINATION testLibRequired/installIncludesTest4
  480. )
  481. install(FILES
  482. "${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest5/installIncludesTest5.h"
  483. DESTINATION installIncludesTest5
  484. )
  485. install(FILES
  486. "${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest6/installIncludesTest6.h"
  487. DESTINATION installIncludesTest6
  488. )
  489. install(FILES
  490. "${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest7/installIncludesTest7.h"
  491. DESTINATION installIncludesTest7
  492. )
  493. install(FILES
  494. "${CMAKE_CURRENT_BINARY_DIR}/installIncludesTest8/installIncludesTest8.h"
  495. DESTINATION installIncludesTest8
  496. )
  497. install(TARGETS testLibDepends testSharedLibDepends EXPORT DependsExp DESTINATION lib )
  498. install(EXPORT DependsExp FILE testLibDependsTargets.cmake DESTINATION lib/cmake/testLibDepends)
  499. file(WRITE
  500. "${CMAKE_CURRENT_BINARY_DIR}/testLibRequiredConfig.cmake"
  501. "
  502. if(\${CMAKE_FIND_PACKAGE_NAME}_FIND_VERSION VERSION_LESS 2.3 AND NOT \${CMAKE_FIND_PACKAGE_NAME}_INTERFACES)
  503. set(\${CMAKE_FIND_PACKAGE_NAME}_NO_INTERFACES 1)
  504. endif()
  505. include(\"\${CMAKE_CURRENT_LIST_DIR}/testLibRequiredTargets.cmake\")
  506. set(\${CMAKE_FIND_PACKAGE_NAME}_INCLUDE_DIRS \"${CMAKE_CURRENT_BINARY_DIR}\" \"${CMAKE_CURRENT_SOURCE_DIR}\" )
  507. "
  508. )
  509. include(CMakePackageConfigHelpers)
  510. write_basic_package_version_file( testLibRequiredConfigVersion.cmake VERSION 2.5 COMPATIBILITY AnyNewerVersion)
  511. install(FILES
  512. "${CMAKE_CURRENT_BINARY_DIR}/testLibRequiredConfig.cmake"
  513. "${CMAKE_CURRENT_BINARY_DIR}/testLibRequiredConfigVersion.cmake"
  514. DESTINATION lib/cmake/testLibRequired
  515. )
  516. # Install and export from install tree.
  517. install(
  518. TARGETS
  519. compileOnly
  520. noUses
  521. testExe1 testLib1 testLib2 testExe2 testLib3 testLib4 testExe3 testExe4
  522. testExe2lib testLib4lib testLib4libdbg testLib4libopt
  523. testLib6 testLib7 testLib8
  524. testLib9
  525. testLib10 testLib11
  526. testLibDeprecation
  527. testLibCycleA testLibCycleB
  528. testLibNoSONAME
  529. testStaticLibWithPlugin testStaticLibPluginExtra testStaticLibPlugin
  530. testSharedLibWithHelper testSharedLibHelperObj
  531. testExeWithPluginHelper testExePluginHelperObj
  532. testMod1 testMod2
  533. cmp0022NEW
  534. TopDirLib SubDirLinkA
  535. systemlib
  536. testInterfaceIncludeUser
  537. testInterfaceIncludeUser2
  538. testSharedObjectAIX
  539. EXPORT exp
  540. RUNTIME DESTINATION $<1:bin>$<0:/wrong>
  541. LIBRARY DESTINATION $<1:lib>$<0:/wrong> NAMELINK_SKIP
  542. ARCHIVE DESTINATION $<1:lib>$<0:/wrong>
  543. ${maybe_OBJECTS_DESTINATION}
  544. FRAMEWORK DESTINATION Frameworks
  545. BUNDLE DESTINATION Applications
  546. )
  547. install(
  548. TARGETS
  549. testLib9ObjPub testLib9ObjPriv testLib9ObjIface
  550. EXPORT exp
  551. )
  552. if (APPLE)
  553. file(COPY testLib4.h DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/testLib4.framework/Headers)
  554. file(COPY testLib4.h DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/Debug/testLib4.framework/Headers)
  555. file(COPY testLib4.h DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/Release/testLib4.framework/Headers)
  556. install(FILES testLib4.h DESTINATION Frameworks/testLib4.framework/Headers)
  557. endif()
  558. install(
  559. TARGETS
  560. testExe2libImp testLib3Imp
  561. EXPORT exp
  562. RUNTIME DESTINATION bin
  563. LIBRARY DESTINATION lib/impl
  564. ARCHIVE DESTINATION lib/impl
  565. )
  566. install(
  567. TARGETS
  568. testLib3ImpDep
  569. EXPORT exp
  570. RUNTIME DESTINATION bin
  571. LIBRARY DESTINATION lib/impl/dep
  572. ARCHIVE DESTINATION lib/impl/dep
  573. )
  574. install(
  575. TARGETS testLib5
  576. EXPORT exp
  577. # Leave out RUNTIME DESTINATION to test implib-only export.
  578. LIBRARY DESTINATION lib
  579. ARCHIVE DESTINATION lib
  580. )
  581. install(EXPORT exp NAMESPACE exp_ DESTINATION lib/exp EXPORT_PACKAGE_DEPENDENCIES)
  582. # Install testLib5.dll outside the export.
  583. if(WIN32)
  584. install(TARGETS testLib5 RUNTIME DESTINATION bin)
  585. endif()
  586. add_subdirectory(sublib) # For CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE test.
  587. # Export from build tree.
  588. export(TARGETS testExe1 testLib1 testLib2 testLib3
  589. testExe2libImp testLib3Imp testLib3ImpDep subdirlib
  590. testSharedLibRequired testSharedLibRequiredUser testSharedLibRequiredUser2
  591. testSharedLibDepends renamed_on_export
  592. cmp0022NEW
  593. TopDirLib SubDirLinkA
  594. systemlib noUses
  595. testInterfaceIncludeUser
  596. testInterfaceIncludeUser2
  597. NAMESPACE bld_
  598. FILE ExportBuildTree.cmake
  599. )
  600. export(TARGETS testExe2 testLib4 testLib5 testLib6 testLib7 testExe3 testExe4 testExe2lib
  601. testLib8
  602. testLib9 testLib9ObjPub testLib9ObjPriv testLib9ObjIface
  603. testLib10 testLib11
  604. testLibDeprecation
  605. testLib4lib testLib4libdbg testLib4libopt
  606. testLibCycleA testLibCycleB
  607. testLibNoSONAME
  608. testStaticLibWithPlugin testStaticLibPluginExtra testStaticLibPlugin
  609. testSharedLibWithHelper testSharedLibHelperObj
  610. testExeWithPluginHelper testExePluginHelperObj
  611. testMod1 testMod2
  612. testLibPerConfigDest
  613. testSharedObjectAIX
  614. NAMESPACE bld_
  615. APPEND FILE ExportBuildTree.cmake
  616. )
  617. add_subdirectory(Interface)
  618. #-----------------------------------------------------------------------------
  619. # Install export with absolute destination but relative pieces.
  620. add_library(testLibAbs1 STATIC testLibAbs1.c)
  621. target_include_directories(testLibAbs1 INTERFACE
  622. "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include/abs/1a;include/abs/1b>"
  623. )
  624. install(
  625. TARGETS testLibAbs1
  626. EXPORT expAbs
  627. ARCHIVE DESTINATION lib
  628. INCLUDES DESTINATION include/abs
  629. )
  630. install(DIRECTORY $<1:include/abs>$<0:/wrong> DESTINATION $<1:include>$<0:/wrong>)
  631. install(EXPORT expAbs NAMESPACE expAbs_ DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/expAbs)
  632. #------------------------------------------------------------------------------
  633. # test export of INTERFACE_LINK_OPTIONS
  634. add_library(testLinkOptions INTERFACE)
  635. target_link_options(testLinkOptions INTERFACE INTERFACE_FLAG)
  636. install(TARGETS testLinkOptions
  637. EXPORT RequiredExp DESTINATION lib)
  638. export(TARGETS testLinkOptions NAMESPACE bld_ APPEND FILE ExportBuildTree.cmake)
  639. #------------------------------------------------------------------------------
  640. # test export of INTERFACE_LINK_DIRECTORIES
  641. add_library(testLinkDirectories INTERFACE)
  642. target_link_directories(testLinkDirectories INTERFACE
  643. $<BUILD_INTERFACE:/interface/build>
  644. $<INSTALL_INTERFACE:interface/install>)
  645. install(TARGETS testLinkDirectories
  646. EXPORT RequiredExp DESTINATION lib)
  647. export(TARGETS testLinkDirectories NAMESPACE bld_ APPEND FILE ExportBuildTree.cmake)
  648. #------------------------------------------------------------------------------
  649. # test export of INTERFACE_LINK_DEPENDS
  650. if(CMAKE_GENERATOR MATCHES "Make|Ninja")
  651. add_library(testLinkDepends INTERFACE)
  652. set_property(TARGET testLinkDepends PROPERTY INTERFACE_LINK_DEPENDS
  653. $<BUILD_INTERFACE:BUILD_LINK_DEPENDS>
  654. $<INSTALL_INTERFACE:INSTALL_LINK_DEPENDS>)
  655. install(TARGETS testLinkDepends
  656. EXPORT RequiredExp DESTINATION lib)
  657. export(TARGETS testLinkDepends NAMESPACE bld_ APPEND FILE ExportBuildTree.cmake)
  658. endif()
  659. #------------------------------------------------------------------------------
  660. # test export of CUDA language
  661. if(CMake_TEST_CUDA)
  662. enable_language(CUDA)
  663. add_library(cudaInterfaceLib INTERFACE)
  664. target_compile_features(cudaInterfaceLib INTERFACE $<BUILD_INTERFACE:cuda_std_11> $<INSTALL_INTERFACE:cuda_std_14>)
  665. install(TARGETS cudaInterfaceLib
  666. EXPORT RequiredExp DESTINATION lib)
  667. export(TARGETS cudaInterfaceLib NAMESPACE bld_ APPEND FILE ExportBuildTree.cmake)
  668. endif()
  669. # Test the presence of targets named the same as languages.
  670. # IMPORTED_LINK_INTERFACE_LANGUAGES entries should not be targets.
  671. add_library(C INTERFACE)
  672. add_library(CXX INTERFACE)
  673. #------------------------------------------------------------------------------
  674. # test export of targets built from sources named using $<CONFIG> generator-expression
  675. # FIXME: Enable test on Xcode generator when it supports per-config sources.
  676. if(NOT XCODE)
  677. file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/Generated_$<CONFIG>.cpp" CONTENT "// content")
  678. add_library(testStaticFromGeneratedSource STATIC)
  679. target_sources(testStaticFromGeneratedSource PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/Generated_$<CONFIG>.cpp")
  680. add_library(testLibFromGeneratedSource SHARED empty.cpp)
  681. target_link_libraries(testLibFromGeneratedSource PRIVATE testStaticFromGeneratedSource)
  682. install(TARGETS testLibFromGeneratedSource EXPORT testLibFromGeneratedSource_Export)
  683. install(EXPORT testLibFromGeneratedSource_Export DESTINATION lib)
  684. endif()
  685. add_subdirectory(install-RUNTIME_DEPENDENCY_SET)