CMakeLists.txt 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. # Prepare imported targets that the exported project itself imported.
  2. add_library(SubDirLinkAImported IMPORTED INTERFACE)
  3. target_compile_definitions(SubDirLinkAImported INTERFACE DEF_SubDirLinkAImportedForImport)
  4. add_library(SubDirLinkBImported IMPORTED INTERFACE)
  5. target_compile_definitions(SubDirLinkBImported INTERFACE DEF_SubDirLinkBImportedForImport)
  6. # Import targets from the exported build tree.
  7. include(${Import_BINARY_DIR}/../Export/ExportBuildTree.cmake)
  8. # Import targets from the exported install tree.
  9. include(${CMAKE_INSTALL_PREFIX}/lib/exp/exp.cmake)
  10. # Import two exports, where the Depends one depends on an exported target from the Required one:
  11. include(${CMAKE_INSTALL_PREFIX}/lib/cmake/testLibRequired/testLibRequiredTargets.cmake)
  12. include(${CMAKE_INSTALL_PREFIX}/lib/cmake/testLibDepends/testLibDependsTargets.cmake)
  13. # Try referencing an executable imported from the install tree.
  14. add_custom_command(
  15. OUTPUT ${Import_BINARY_DIR}/exp_generated.c
  16. COMMAND exp_testExe1 ${Import_BINARY_DIR}/exp_generated.c
  17. DEPENDS exp_testExe1
  18. )
  19. add_custom_command(
  20. OUTPUT ${Import_BINARY_DIR}/exp_generated3.c
  21. COMMAND exp_testExe3 ${Import_BINARY_DIR}/exp_generated3.c
  22. DEPENDS exp_testExe3
  23. )
  24. add_custom_command(
  25. OUTPUT ${Import_BINARY_DIR}/exp_generated4.c
  26. COMMAND exp_testExe4 ${Import_BINARY_DIR}/exp_generated4.c
  27. DEPENDS exp_testExe4
  28. )
  29. add_executable(imp_testExe1
  30. imp_testExe1.c
  31. ${Import_BINARY_DIR}/exp_generated.c
  32. ${Import_BINARY_DIR}/exp_generated3.c
  33. ${Import_BINARY_DIR}/exp_generated4.c
  34. )
  35. function(checkForProperty _TARGET _PROP _EXPECTED)
  36. get_target_property(EXPORTED_PROPERTY ${_TARGET} "${_PROP}")
  37. if (NOT EXPORTED_PROPERTY STREQUAL "${_EXPECTED}")
  38. message(SEND_ERROR "${_TARGET} was expected to export \"${_PROP}\" with value \"${_EXPECTED}\" but got \"${EXPORTED_PROPERTY}\"")
  39. endif()
  40. endfunction()
  41. checkForProperty(bld_testLib3 "EXPORTED_PROPERTY1" "EXPORTING_TESTLIB3")
  42. checkForProperty(exp_testLib3 "EXPORTED_PROPERTY1" "EXPORTING_TESTLIB3")
  43. checkForProperty(bld_testLib4 "EXPORTED_PROPERTY2" "EXPORTING_TESTLIB4_1")
  44. checkForProperty(exp_testLib4 "EXPORTED_PROPERTY2" "EXPORTING_TESTLIB4_1")
  45. checkForProperty(bld_testLib4 "EXPORTED_PROPERTY3" "EXPORTING_TESTLIB4_2")
  46. checkForProperty(exp_testLib4 "EXPORTED_PROPERTY3" "EXPORTING_TESTLIB4_2")
  47. checkForProperty(bld_testLibDeprecation "DEPRECATION" "Deprecated version. Please use latest version")
  48. checkForProperty(exp_testLibDeprecation "DEPRECATION" "Deprecated version. Please use latest version")
  49. # Try linking to a deprecated library
  50. target_link_libraries(imp_testExe1 exp_testLibDeprecation)
  51. # Try linking to a library imported from the install tree.
  52. target_link_libraries(imp_testExe1
  53. exp_testLib2
  54. exp_testLib3
  55. exp_testLib4
  56. exp_testLib5
  57. exp_testLib6
  58. exp_testLib7
  59. exp_testLibCycleA
  60. exp_testLibPerConfigDest
  61. exp_testSharedObjectAIX
  62. exp_testStaticLibWithPlugin
  63. )
  64. add_library(imp_testInterfaceInclude1 STATIC imp_testInterfaceInclude1.c)
  65. target_include_directories(imp_testInterfaceInclude1 SYSTEM PRIVATE testInterfaceIncludeSystem)
  66. target_link_libraries(imp_testInterfaceInclude1 PRIVATE exp_testInterfaceIncludeUser)
  67. add_library(imp_testInterfaceInclude1b STATIC imp_testInterfaceInclude1.c)
  68. target_include_directories(imp_testInterfaceInclude1b SYSTEM PRIVATE testInterfaceIncludeSystem)
  69. target_link_libraries(imp_testInterfaceInclude1b PRIVATE exp_testInterfaceIncludeUser2)
  70. add_executable(imp_UseSharedLibWithHelper1 ../../../InterfaceLinkLibrariesDirect/UseSharedLibWithHelper.c)
  71. target_link_libraries(imp_UseSharedLibWithHelper1 PRIVATE exp_testSharedLibWithHelper testSharedLibHelperExclude)
  72. # Try building a plugin to an executable imported from the install tree.
  73. add_library(imp_mod1 MODULE imp_mod1.c)
  74. target_link_libraries(imp_mod1 exp_testExe2)
  75. add_library(imp_ExePlugin1 MODULE ../../../InterfaceLinkLibrariesDirect/ExePlugin.c)
  76. target_link_libraries(imp_ExePlugin1 PRIVATE exp_testExeWithPluginHelper testExePluginHelperExclude)
  77. # Try referencing an executable imported from the build tree.
  78. add_custom_command(
  79. OUTPUT ${Import_BINARY_DIR}/bld_generated.c
  80. COMMAND bld_testExe1 ${Import_BINARY_DIR}/bld_generated.c
  81. DEPENDS bld_testExe1
  82. )
  83. add_custom_command(
  84. OUTPUT ${Import_BINARY_DIR}/bld_generated3.c
  85. COMMAND bld_testExe3 ${Import_BINARY_DIR}/bld_generated3.c
  86. DEPENDS bld_testExe3
  87. )
  88. add_custom_command(
  89. OUTPUT ${Import_BINARY_DIR}/bld_generated4.c
  90. COMMAND bld_testExe4 ${Import_BINARY_DIR}/bld_generated4.c
  91. DEPENDS bld_testExe4
  92. )
  93. add_executable(imp_testExe1b
  94. imp_testExe1.c
  95. ${Import_BINARY_DIR}/bld_generated.c
  96. ${Import_BINARY_DIR}/bld_generated3.c
  97. ${Import_BINARY_DIR}/bld_generated4.c
  98. )
  99. # Try linking to a library imported from the build tree.
  100. target_link_libraries(imp_testExe1b
  101. bld_testLib2
  102. bld_testLib3
  103. bld_testLib4
  104. bld_testLib5
  105. bld_testLib6
  106. bld_testLib7
  107. bld_testLibCycleA
  108. bld_testLibPerConfigDest
  109. bld_testSharedObjectAIX
  110. bld_testStaticLibWithPlugin
  111. )
  112. add_library(imp_testInterfaceInclude1c STATIC imp_testInterfaceInclude1.c)
  113. target_include_directories(imp_testInterfaceInclude1c SYSTEM PRIVATE testInterfaceIncludeSystem)
  114. target_link_libraries(imp_testInterfaceInclude1c PRIVATE bld_testInterfaceIncludeUser)
  115. add_library(imp_testInterfaceInclude1d STATIC imp_testInterfaceInclude1.c)
  116. target_include_directories(imp_testInterfaceInclude1d SYSTEM PRIVATE testInterfaceIncludeSystem)
  117. target_link_libraries(imp_testInterfaceInclude1d PRIVATE bld_testInterfaceIncludeUser2)
  118. add_custom_target(check_testLib1_genex ALL
  119. COMMAND ${CMAKE_COMMAND} -DtestLib1=$<TARGET_FILE:exp_testLib1>
  120. -Dprefix=${CMAKE_INSTALL_PREFIX}
  121. -P ${CMAKE_CURRENT_SOURCE_DIR}/check_testLib1_genex.cmake
  122. )
  123. if(CMAKE_SHARED_LIBRARY_SONAME_C_FLAG AND
  124. "${CMAKE_C_CREATE_SHARED_MODULE}" MATCHES "SONAME_FLAG")
  125. foreach(ns exp bld)
  126. get_property(configs TARGET ${ns}_testLib5 PROPERTY IMPORTED_CONFIGURATIONS)
  127. foreach(c ${configs})
  128. string(TOUPPER "${c}" CONFIG)
  129. get_property(soname TARGET ${ns}_testLib5 PROPERTY IMPORTED_NO_SONAME_${CONFIG})
  130. if(soname)
  131. message(SEND_ERROR "${ns}_testLib5 has IMPORTED_NO_SONAME_${CONFIG} but should:\n ${soname}")
  132. else()
  133. message(STATUS "${ns}_testLib5 does not have IMPORTED_NO_SONAME_${CONFIG} as expected")
  134. endif()
  135. endforeach()
  136. get_property(configs TARGET ${ns}_testLibNoSONAME PROPERTY IMPORTED_CONFIGURATIONS)
  137. foreach(c ${configs})
  138. string(TOUPPER "${c}" CONFIG)
  139. get_property(soname TARGET ${ns}_testLibNoSONAME PROPERTY IMPORTED_NO_SONAME_${CONFIG})
  140. if(soname)
  141. message(STATUS "${ns}_testLibNoSONAME has IMPORTED_NO_SONAME_${CONFIG} as expected")
  142. else()
  143. message(SEND_ERROR "${ns}_testLibNoSONAME does not have IMPORTED_NO_SONAME_${CONFIG} but should")
  144. endif()
  145. endforeach()
  146. # Parse the binary to check for SONAME if possible.
  147. if("${CMAKE_EXECUTABLE_FORMAT}" MATCHES "ELF")
  148. find_program(READELF_EXE readelf)
  149. if(READELF_EXE)
  150. add_custom_target(check_${ns}_testLib5_soname ALL COMMAND
  151. ${CMAKE_COMMAND} -Dreadelf=${READELF_EXE}
  152. -Dlib=$<TARGET_FILE:${ns}_testLib5>
  153. -P ${CMAKE_CURRENT_SOURCE_DIR}/check_lib_soname.cmake
  154. )
  155. add_custom_target(check_${ns}_testLibNoSONAME_soname ALL COMMAND
  156. ${CMAKE_COMMAND} -Dreadelf=${READELF_EXE}
  157. -Dlib=$<TARGET_FILE:${ns}_testLibNoSONAME>
  158. -P ${CMAKE_CURRENT_SOURCE_DIR}/check_lib_nosoname.cmake
  159. )
  160. endif()
  161. endif()
  162. endforeach()
  163. endif()
  164. add_executable(cmp0022NEW_test cmp0022NEW_test_vs6_1.cpp)
  165. target_link_libraries(cmp0022NEW_test bld_cmp0022NEW)
  166. add_executable(cmp0022NEW_exp_test cmp0022NEW_test_vs6_2.cpp)
  167. target_link_libraries(cmp0022NEW_exp_test exp_cmp0022NEW)
  168. add_executable(SubDirLink_bld SubDirLink.c)
  169. target_link_libraries(SubDirLink_bld PRIVATE bld_TopDirLib bld_SubDirLinkA)
  170. add_executable(SubDirLink_exp SubDirLink.c)
  171. target_link_libraries(SubDirLink_exp PRIVATE exp_TopDirLib exp_SubDirLinkA)
  172. add_executable(imp_UseSharedLibWithHelper1b ../../../InterfaceLinkLibrariesDirect/UseSharedLibWithHelper.c)
  173. target_link_libraries(imp_UseSharedLibWithHelper1b PRIVATE bld_testSharedLibWithHelper testSharedLibHelperExclude)
  174. # Try building a plugin to an executable imported from the build tree.
  175. add_library(imp_mod1b MODULE imp_mod1.c)
  176. target_link_libraries(imp_mod1b bld_testExe2)
  177. add_library(imp_ExePlugin1b MODULE ../../../InterfaceLinkLibrariesDirect/ExePlugin.c)
  178. target_link_libraries(imp_ExePlugin1b PRIVATE bld_testExeWithPluginHelper testExePluginHelperExclude)
  179. # Export/CMakeLists.txt pretends the RelWithDebInfo (as well as Debug)
  180. # configuration should link to debug libs.
  181. foreach(c DEBUG RELWITHDEBINFO)
  182. set_property(TARGET imp_testExe1 PROPERTY COMPILE_DEFINITIONS_${c} EXE_DBG)
  183. set_property(TARGET imp_testExe1b PROPERTY COMPILE_DEFINITIONS_${c} EXE_DBG)
  184. endforeach()
  185. #-----------------------------------------------------------------------------
  186. include(${CMAKE_INSTALL_PREFIX}/lib/expAbs/expAbs.cmake)
  187. add_executable(imp_testExeAbs1
  188. imp_testExeAbs1.c
  189. )
  190. target_link_libraries(imp_testExeAbs1
  191. expAbs_testLibAbs1
  192. )
  193. #-----------------------------------------------------------------------------
  194. # Create a custom target to generate a header for the libraries below.
  195. # Drive the header generation through an indirect chain of imported
  196. # target dependencies.
  197. # testLib2tmp1.h
  198. add_custom_command(
  199. OUTPUT testLib2tmp1.h
  200. VERBATIM COMMAND
  201. ${CMAKE_COMMAND} -E echo "extern int testLib2(void);" > testLib2tmp1.h
  202. )
  203. # hdr_testLib2tmp1 needs testLib2tmp1.h
  204. add_custom_target(hdr_testLib2tmp1 DEPENDS testLib2tmp1.h)
  205. # exp_testExe2 needs hdr_testLib2tmp1
  206. add_dependencies(exp_testExe2 hdr_testLib2tmp1)
  207. # testLib2tmp.h needs exp_testExe2
  208. add_custom_command(
  209. OUTPUT testLib2tmp.h
  210. VERBATIM COMMAND exp_testExe2
  211. COMMAND ${CMAKE_COMMAND} -E copy testLib2tmp1.h testLib2tmp.h
  212. )
  213. # hdr_testLib2tmp needs testLib2tmp.h
  214. add_custom_target(hdr_testLib2tmp DEPENDS testLib2tmp.h)
  215. add_library(dep_testLib2tmp UNKNOWN IMPORTED)
  216. set_property(TARGET dep_testLib2tmp PROPERTY
  217. IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/testLib2tmp.h)
  218. # dep_testLib2tmp needs hdr_testLib2tmp
  219. add_dependencies(dep_testLib2tmp hdr_testLib2tmp)
  220. # testLib2.h needs dep_testLib2tmp
  221. add_custom_command(
  222. OUTPUT testLib2.h
  223. VERBATIM COMMAND ${CMAKE_COMMAND} -E copy testLib2tmp.h testLib2.h
  224. DEPENDS dep_testLib2tmp
  225. )
  226. # hdr_testLib2 needs testLib2.h
  227. add_custom_target(hdr_testLib2 DEPENDS testLib2.h)
  228. add_library(dep_testLib2 UNKNOWN IMPORTED)
  229. # dep_testLib2 needs hdr_testLib2
  230. add_dependencies(dep_testLib2 hdr_testLib2)
  231. # exp_testLib2 and bld_testLib2 both need dep_testLib2
  232. add_dependencies(bld_testLib2 dep_testLib2)
  233. add_dependencies(exp_testLib2 dep_testLib2)
  234. #-----------------------------------------------------------------------------
  235. # Create a library to be linked by another directory in this project
  236. # to test transitive linking to otherwise invisible imported targets.
  237. include_directories(${CMAKE_CURRENT_BINARY_DIR})
  238. add_library(imp_lib1 STATIC imp_lib1.c)
  239. target_link_libraries(imp_lib1 exp_testLib2)
  240. add_library(imp_lib1b STATIC imp_lib1.c)
  241. target_link_libraries(imp_lib1b bld_testLib2)
  242. if(NOT CMAKE_GENERATOR STREQUAL "Xcode" OR NOT CMAKE_OSX_ARCHITECTURES MATCHES "[;$]")
  243. set(bld_objlib_type OBJECT_LIBRARY)
  244. # Create executables using objects imported from the install tree
  245. add_executable(imp_testLib8_src imp_testLib8.c $<TARGET_OBJECTS:exp_testLib8>)
  246. add_executable(imp_testLib8_link imp_testLib8.c)
  247. target_link_libraries(imp_testLib8_link exp_testLib8)
  248. if(NOT CMAKE_GENERATOR STREQUAL "Xcode" OR NOT XCODE_VERSION VERSION_LESS 5)
  249. # Create executables using objects imported from the build tree
  250. add_executable(imp_testLib8b_src imp_testLib8.c $<TARGET_OBJECTS:bld_testLib8>)
  251. add_executable(imp_testLib8b_link imp_testLib8.c)
  252. target_link_libraries(imp_testLib8b_link bld_testLib8)
  253. endif()
  254. else()
  255. set(bld_objlib_type INTERFACE_LIBRARY)
  256. endif()
  257. # Create an executable that uses a library imported from the install tree
  258. # that itself was built using an object library. Verify we get the usage
  259. # requirements.
  260. add_executable(imp_testLib9 imp_testLib9.c)
  261. target_link_libraries(imp_testLib9 exp_testLib9)
  262. # Similarly for importing from the build tree.
  263. add_executable(imp_testLib9b imp_testLib9.c)
  264. target_link_libraries(imp_testLib9b bld_testLib9)
  265. # Check that object libraries were transformed on export as expected.
  266. foreach(vis Pub Priv Iface)
  267. get_property(type TARGET exp_testLib9Obj${vis} PROPERTY TYPE)
  268. if(NOT type STREQUAL INTERFACE_LIBRARY)
  269. message(FATAL_ERROR "exp_testLib9Obj${vis} type is '${type}', not 'INTERFACE_LIBRARY'")
  270. endif()
  271. get_property(type TARGET bld_testLib9Obj${vis} PROPERTY TYPE)
  272. if(NOT type STREQUAL "${bld_objlib_type}")
  273. message(FATAL_ERROR "bld_testLib9Obj${vis} type is '${type}', not '${bld_objlib_type}'")
  274. endif()
  275. endforeach()
  276. # Create executables to verify custom transitive properties.
  277. add_executable(imp_testLib10 imp_testLib10.c)
  278. target_link_libraries(imp_testLib10 PRIVATE exp_testLib10)
  279. add_executable(imp_testLib10b imp_testLib10.c)
  280. target_link_libraries(imp_testLib10b PRIVATE bld_testLib10)
  281. add_executable(imp_testLib11 imp_testLib11.c)
  282. target_link_libraries(imp_testLib11 PRIVATE exp_testLib11)
  283. add_executable(imp_testLib11b imp_testLib11.c)
  284. target_link_libraries(imp_testLib11b PRIVATE bld_testLib11)
  285. #-----------------------------------------------------------------------------
  286. # Test that handling imported targets, including transitive dependencies,
  287. # works in CheckFunctionExists (...and hopefully all other try_compile() checks
  288. include(CheckFunctionExists)
  289. unset(HAVE_TESTLIB1_FUNCTION CACHE)
  290. set(CMAKE_REQUIRED_LIBRARIES exp_testLib2)
  291. check_function_exists(testLib1 HAVE_TESTLIB1_FUNCTION)
  292. if (NOT HAVE_TESTLIB1_FUNCTION)
  293. message(SEND_ERROR "Using imported target testLib2 in check_function_exists() failed !")
  294. endif()
  295. #-----------------------------------------------------------------------------
  296. # Test that dependent imported targets have usable
  297. # INTERFACE_COMPILE_DEFINITIONS and INTERFACE_INCLUDE_DIRECTORIES
  298. add_executable(deps_iface deps_iface.c)
  299. target_link_libraries(deps_iface testLibDepends)
  300. add_executable(deps_shared_iface deps_shared_iface.cpp)
  301. target_link_libraries(deps_shared_iface testSharedLibDepends)
  302. target_compile_definitions(deps_shared_iface
  303. PRIVATE
  304. $<$<BOOL:$<TARGET_PROPERTY:POSITION_INDEPENDENT_CODE>>:PIC_PROPERTY_IS_ON>
  305. $<$<BOOL:$<TARGET_PROPERTY:CUSTOM_PROP>>:CUSTOM_PROPERTY_IS_ON>
  306. $<$<STREQUAL:$<TARGET_PROPERTY:CUSTOM_STRING>,testcontent>:CUSTOM_STRING_IS_MATCH>
  307. )
  308. if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "LCC")
  309. target_compile_definitions(deps_shared_iface
  310. PRIVATE
  311. "DO_GNU_TESTS"
  312. )
  313. endif()
  314. if (APPLE OR CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "LCC")
  315. include(CheckCXXCompilerFlag)
  316. check_cxx_compiler_flag(-fPIE run_pic_test)
  317. else()
  318. if (CMAKE_CXX_COMPILER_ID MATCHES "PGI"
  319. OR CMAKE_CXX_COMPILER_ID MATCHES "PathScale"
  320. OR CMAKE_CXX_COMPILER_ID MATCHES "Intel")
  321. set(run_pic_test 0)
  322. else()
  323. set(run_pic_test 1)
  324. endif()
  325. endif()
  326. add_executable(exp_renamed_test renamed_test.cpp)
  327. target_link_libraries(exp_renamed_test exp_renamed)
  328. add_executable(bld_renamed_test renamed_test.cpp)
  329. target_link_libraries(bld_renamed_test bld_renamed)
  330. if (run_pic_test)
  331. target_compile_definitions(deps_shared_iface PRIVATE CHECK_PIC_WORKS)
  332. endif()
  333. if(APPLE)
  334. add_subdirectory(framework_interface)
  335. endif()
  336. #-----------------------------------------------------------------------------
  337. # Test that targets imported from the build tree have their dependencies
  338. # evaluated correctly. The above already tests the same for the install tree.
  339. add_executable(deps_shared_iface2 deps_shared_iface.cpp)
  340. target_link_libraries(deps_shared_iface2 bld_testSharedLibDepends bld_subdirlib)
  341. if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "LCC")
  342. target_compile_definitions(deps_shared_iface2
  343. PRIVATE
  344. "DO_GNU_TESTS"
  345. )
  346. endif()
  347. target_compile_definitions(deps_shared_iface2
  348. PRIVATE TEST_SUBDIR_LIB
  349. $<$<BOOL:$<TARGET_PROPERTY:POSITION_INDEPENDENT_CODE>>:PIC_PROPERTY_IS_ON>
  350. $<$<BOOL:$<TARGET_PROPERTY:CUSTOM_PROP>>:CUSTOM_PROPERTY_IS_ON>
  351. $<$<STREQUAL:$<TARGET_PROPERTY:CUSTOM_STRING>,testcontent>:CUSTOM_STRING_IS_MATCH>
  352. )
  353. add_subdirectory(excludedFromAll)
  354. add_executable(iface_test_bld iface_test.cpp)
  355. target_link_libraries(iface_test_bld bld_testSharedLibDepends)
  356. set_property(TARGET iface_test_bld PROPERTY NO_SYSTEM_FROM_IMPORTED 1)
  357. set_property(TARGET bld_testSharedLibRequired APPEND PROPERTY
  358. INTERFACE_LINK_LIBRARIES
  359. excludedFromAll
  360. )
  361. add_executable(iface_test_exp iface_test.cpp)
  362. target_link_libraries(iface_test_exp testSharedLibDepends)
  363. set_property(TARGET testSharedLibDepends APPEND PROPERTY
  364. INTERFACE_LINK_LIBRARIES
  365. excludedFromAll
  366. )
  367. if (((CMAKE_C_COMPILER_ID STREQUAL GNU AND CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.4)
  368. OR CMAKE_C_COMPILER_ID MATCHES "LCC"
  369. OR (CMAKE_C_COMPILER_ID STREQUAL Clang AND NOT "x${CMAKE_CXX_SIMULATE_ID}" STREQUAL "xMSVC"))
  370. AND (CMAKE_GENERATOR STREQUAL "Unix Makefiles" OR CMAKE_GENERATOR STREQUAL "Ninja"))
  371. include(CheckCXXCompilerFlag)
  372. check_cxx_compiler_flag(-Wunused-variable run_sys_includes_test)
  373. if(run_sys_includes_test)
  374. # The Bullseye wrapper appears to break the -isystem effect.
  375. execute_process(COMMAND ${CMAKE_CXX_COMPILER} --version OUTPUT_VARIABLE out ERROR_VARIABLE out)
  376. if("x${out}" MATCHES "Bullseye")
  377. set(run_sys_includes_test 0)
  378. endif()
  379. endif()
  380. if (run_sys_includes_test)
  381. add_executable(test_system_exp test_system.cpp)
  382. target_link_libraries(test_system_exp exp_systemlib)
  383. target_compile_options(test_system_exp PRIVATE -Wunused-variable -Werror=unused-variable)
  384. unset(EXP_ERROR_VARIABLE CACHE)
  385. try_compile(EXP_ERROR_VARIABLE
  386. "${CMAKE_CURRENT_BINARY_DIR}/test_system"
  387. "${CMAKE_CURRENT_SOURCE_DIR}/test_system.cpp"
  388. COMPILE_DEFINITIONS "-Wunused-variable -Werror=unused-variable"
  389. LINK_LIBRARIES exp_systemlib
  390. OUTPUT_VARIABLE OUTPUT
  391. )
  392. if(NOT EXP_ERROR_VARIABLE)
  393. message(SEND_ERROR "EXP_ERROR_VARIABLE try_compile failed, but it was expected to succeed ${OUTPUT}.")
  394. endif()
  395. if(NOT CMAKE_CROSSCOMPILING)
  396. unset(EXP_RUN_VAR CACHE)
  397. unset(EXP_COMPILE_VAR CACHE)
  398. try_run(EXP_RUN_VAR EXP_COMPILE_VAR
  399. "${CMAKE_CURRENT_BINARY_DIR}/test_system"
  400. "${CMAKE_CURRENT_SOURCE_DIR}/test_system.cpp"
  401. COMPILE_DEFINITIONS "-Wunused-variable -Werror=unused-variable"
  402. LINK_LIBRARIES exp_systemlib
  403. OUTPUT_VARIABLE OUTPUT
  404. )
  405. if(NOT EXP_COMPILE_VAR)
  406. message(SEND_ERROR "try_run compile failed, but it was expected to succeed ${OUTPUT}.")
  407. endif()
  408. endif()
  409. add_executable(test_system_bld test_system.cpp)
  410. target_link_libraries(test_system_bld bld_systemlib)
  411. target_compile_options(test_system_bld PRIVATE -Wunused-variable -Werror=unused-variable)
  412. unset(BLD_ERROR_VARIABLE CACHE)
  413. try_compile(BLD_ERROR_VARIABLE
  414. "${CMAKE_CURRENT_BINARY_DIR}/test_system"
  415. "${CMAKE_CURRENT_SOURCE_DIR}/test_system.cpp"
  416. COMPILE_DEFINITIONS "-Wunused-variable -Werror=unused-variable"
  417. LINK_LIBRARIES bld_systemlib
  418. OUTPUT_VARIABLE OUTPUT
  419. )
  420. if(NOT BLD_ERROR_VARIABLE)
  421. message(SEND_ERROR "BLD_ERROR_VARIABLE try_compile failed, but it was expected to succeed. ${OUTPUT}")
  422. endif()
  423. if(NOT CMAKE_CROSSCOMPILING)
  424. unset(BLD_RUN_VAR CACHE)
  425. unset(BLD_COMPILE_VAR CACHE)
  426. try_run(BLD_RUN_VAR BLD_COMPILE_VAR
  427. "${CMAKE_CURRENT_BINARY_DIR}/test_system"
  428. "${CMAKE_CURRENT_SOURCE_DIR}/test_system.cpp"
  429. COMPILE_DEFINITIONS "-Wunused-variable -Werror=unused-variable"
  430. LINK_LIBRARIES bld_systemlib
  431. OUTPUT_VARIABLE OUTPUT
  432. )
  433. if(NOT BLD_COMPILE_VAR)
  434. message(SEND_ERROR "try_run compile failed, but it was expected to succeed ${OUTPUT}.")
  435. endif()
  436. endif()
  437. endif()
  438. # Testing try_compile with ALIAS targets.
  439. # These assume that previous test were successful, or at least the failures will be at the linking stage
  440. # with symbol not found errors
  441. # First make sure that if the test run without appropriate alias targets, they should error out
  442. try_compile(FAILING_LIBRARY_ALIAS_ERROR_VARIABLE
  443. "${CMAKE_CURRENT_BINARY_DIR}/test_failing_library_alias"
  444. "${CMAKE_CURRENT_SOURCE_DIR}/test_system.cpp"
  445. LINK_LIBRARIES not_existing_library
  446. OUTPUT_VARIABLE OUTPUT
  447. NO_CACHE
  448. )
  449. if(FAILING_LIBRARY_ALIAS_ERROR_VARIABLE)
  450. message(SEND_ERROR "FAILING_LIBRARY_ALIAS_ERROR_VARIABLE try_compile succeeded, but it was expected to fail ${OUTPUT}.")
  451. endif()
  452. # FIXME: CMAKE_TRY_COMPILE_TARGET_TYPE=MODULE is needed to properly link and test targets linked to an executable
  453. # set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
  454. # try_compile(FAILING_EXE_ALIAS_ERROR_VARIABLE
  455. # "${CMAKE_CURRENT_BINARY_DIR}/test_failing_exe_alias"
  456. # "${CMAKE_CURRENT_SOURCE_DIR}/imp_mod1.c"
  457. # LINK_LIBRARIES not_existing_executable
  458. # OUTPUT_VARIABLE OUTPUT
  459. # NO_CACHE
  460. # )
  461. # unset(CMAKE_TRY_COMPILE_TARGET_TYPE)
  462. # if(FAILING_EXE_ALIAS_ERROR_VARIABLE)
  463. # message(SEND_ERROR "FAILING_EXE_ALIAS_ERROR_VARIABLE try_compile succeeded, but it was expected to fail ${OUTPUT}.")
  464. # endif()
  465. # Do the actual try_compile tests for ALIAS targets
  466. add_library(exp_systemlib_alias ALIAS exp_systemlib)
  467. try_compile(EXP_LIBRARY_ALIAS_ERROR_VARIABLE
  468. "${CMAKE_CURRENT_BINARY_DIR}/test_library_alias"
  469. "${CMAKE_CURRENT_SOURCE_DIR}/test_system.cpp"
  470. LINK_LIBRARIES exp_systemlib_alias
  471. OUTPUT_VARIABLE OUTPUT
  472. NO_CACHE
  473. )
  474. if(NOT EXP_LIBRARY_ALIAS_ERROR_VARIABLE)
  475. message(SEND_ERROR "EXP_LIBRARY_ALIAS_ERROR_VARIABLE try_compile failed with library aliased target, but it was expected to succeed ${OUTPUT}.")
  476. endif()
  477. # FIXME: CMAKE_TRY_COMPILE_TARGET_TYPE=MODULE is needed to properly link and test targets linked to an executable
  478. # set(CMAKE_TRY_COMPILE_TARGET_TYPE MODULE)
  479. # add_executable(exp_exe_alias ALIAS exp_testExe2)
  480. # try_compile(EXP_EXE_ALIAS_ERROR_VARIABLE
  481. # "${CMAKE_CURRENT_BINARY_DIR}/test_exe_alias"
  482. # "${CMAKE_CURRENT_SOURCE_DIR}/imp_mod1.c"
  483. # LINK_LIBRARIES exp_exe_alias
  484. # OUTPUT_VARIABLE OUTPUT
  485. # NO_CACHE
  486. # )
  487. # unset(CMAKE_TRY_COMPILE_TARGET_TYPE)
  488. # if(NOT EXP_EXE_ALIAS_ERROR_VARIABLE)
  489. # message(SEND_ERROR "EXP_EXE_ALIAS_ERROR_VARIABLE try_compile failed with executable aliased target, but it was expected to succeed ${OUTPUT}.")
  490. # endif()
  491. add_library(bld_systemlib_alias ALIAS bld_systemlib)
  492. try_compile(BLD_LIBRARY_ALIAS_ERROR_VARIABLE
  493. "${CMAKE_CURRENT_BINARY_DIR}/test_library_alias"
  494. "${CMAKE_CURRENT_SOURCE_DIR}/test_system.cpp"
  495. LINK_LIBRARIES bld_systemlib_alias
  496. OUTPUT_VARIABLE OUTPUT
  497. NO_CACHE
  498. )
  499. if(NOT BLD_LIBRARY_ALIAS_ERROR_VARIABLE)
  500. message(SEND_ERROR "BLD_LIBRARY_ALIAS_ERROR_VARIABLE try_compile failed with library aliased target, but it was expected to succeed. ${OUTPUT}")
  501. endif()
  502. # FIXME: CMAKE_TRY_COMPILE_TARGET_TYPE=MODULE is needed to properly link and test targets linked to an executable
  503. # set(CMAKE_TRY_COMPILE_TARGET_TYPE MODULE)
  504. # add_executable(bld_exe_alias ALIAS bld_testExe2)
  505. # try_compile(BLD_EXE_ALIAS_ERROR_VARIABLE
  506. # "${CMAKE_CURRENT_BINARY_DIR}/test_exe_alias"
  507. # "${CMAKE_CURRENT_SOURCE_DIR}/imp_mod1.c"
  508. # LINK_LIBRARIES bld_exe_alias
  509. # OUTPUT_VARIABLE OUTPUT
  510. # NO_CACHE
  511. # )
  512. # unset(CMAKE_TRY_COMPILE_TARGET_TYPE)
  513. # if(NOT BLD_EXE_ALIAS_ERROR_VARIABLE)
  514. # message(SEND_ERROR "BLD_EXE_ALIAS_ERROR_VARIABLE try_compile failed with executable aliased target, but it was expected to succeed. ${OUTPUT}")
  515. # endif()
  516. endif()
  517. #---------------------------------------------------------------------------------
  518. # check that imported libraries have the expected INTERFACE_LINK_OPTIONS property
  519. checkForProperty(bld_testLinkOptions "INTERFACE_LINK_OPTIONS" "INTERFACE_FLAG")
  520. checkForProperty(Req::testLinkOptions "INTERFACE_LINK_OPTIONS" "INTERFACE_FLAG")
  521. #---------------------------------------------------------------------------------
  522. # check that imported libraries have the expected INTERFACE_LINK_DIRECTORIES property
  523. checkForProperty(bld_testLinkDirectories "INTERFACE_LINK_DIRECTORIES" "/interface/build")
  524. checkForProperty(Req::testLinkDirectories "INTERFACE_LINK_DIRECTORIES" "${CMAKE_INSTALL_PREFIX}/interface/install")
  525. #---------------------------------------------------------------------------------
  526. # check that imported libraries have the expected INTERFACE_LINK_DEPENDS property
  527. if(CMAKE_GENERATOR MATCHES "Make|Ninja")
  528. checkForProperty(bld_testLinkDepends "INTERFACE_LINK_DEPENDS" "BUILD_LINK_DEPENDS")
  529. checkForProperty(Req::testLinkDepends "INTERFACE_LINK_DEPENDS" "${CMAKE_INSTALL_PREFIX}/INSTALL_LINK_DEPENDS")
  530. endif()
  531. #------------------------------------------------------------------------------
  532. # test import of CUDA language level
  533. if(CMake_TEST_CUDA)
  534. checkForProperty(bld_cudaInterfaceLib "INTERFACE_COMPILE_FEATURES" "cuda_std_11")
  535. checkForProperty(Req::cudaInterfaceLib "INTERFACE_COMPILE_FEATURES" "cuda_std_14")
  536. endif()