CMakeLists.txt 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. target_compile_definitions(imp_testExe1 PRIVATE "$<$<CONFIG:Debug,RelWithDebInfo>:EXE_DBG>")
  182. target_compile_definitions(imp_testExe1b PRIVATE "$<$<CONFIG:Debug,RelWithDebInfo>:EXE_DBG>")
  183. #-----------------------------------------------------------------------------
  184. include(${CMAKE_INSTALL_PREFIX}/lib/expAbs/expAbs.cmake)
  185. add_executable(imp_testExeAbs1
  186. imp_testExeAbs1.c
  187. )
  188. target_link_libraries(imp_testExeAbs1
  189. expAbs_testLibAbs1
  190. )
  191. #-----------------------------------------------------------------------------
  192. # Create a custom target to generate a header for the libraries below.
  193. # Drive the header generation through an indirect chain of imported
  194. # target dependencies.
  195. # testLib2tmp1.h
  196. add_custom_command(
  197. OUTPUT testLib2tmp1.h
  198. VERBATIM COMMAND
  199. ${CMAKE_COMMAND} -E echo "extern int testLib2(void);" > testLib2tmp1.h
  200. )
  201. # hdr_testLib2tmp1 needs testLib2tmp1.h
  202. add_custom_target(hdr_testLib2tmp1 DEPENDS testLib2tmp1.h)
  203. # exp_testExe2 needs hdr_testLib2tmp1
  204. add_dependencies(exp_testExe2 hdr_testLib2tmp1)
  205. # testLib2tmp.h needs exp_testExe2
  206. add_custom_command(
  207. OUTPUT testLib2tmp.h
  208. VERBATIM COMMAND exp_testExe2
  209. COMMAND ${CMAKE_COMMAND} -E copy testLib2tmp1.h testLib2tmp.h
  210. )
  211. # hdr_testLib2tmp needs testLib2tmp.h
  212. add_custom_target(hdr_testLib2tmp DEPENDS testLib2tmp.h)
  213. add_library(dep_testLib2tmp UNKNOWN IMPORTED)
  214. set_property(TARGET dep_testLib2tmp PROPERTY
  215. IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/testLib2tmp.h)
  216. # dep_testLib2tmp needs hdr_testLib2tmp
  217. add_dependencies(dep_testLib2tmp hdr_testLib2tmp)
  218. # testLib2.h needs dep_testLib2tmp
  219. add_custom_command(
  220. OUTPUT testLib2.h
  221. VERBATIM COMMAND ${CMAKE_COMMAND} -E copy testLib2tmp.h testLib2.h
  222. DEPENDS dep_testLib2tmp
  223. )
  224. # hdr_testLib2 needs testLib2.h
  225. add_custom_target(hdr_testLib2 DEPENDS testLib2.h)
  226. add_library(dep_testLib2 UNKNOWN IMPORTED)
  227. # dep_testLib2 needs hdr_testLib2
  228. add_dependencies(dep_testLib2 hdr_testLib2)
  229. # exp_testLib2 and bld_testLib2 both need dep_testLib2
  230. add_dependencies(bld_testLib2 dep_testLib2)
  231. add_dependencies(exp_testLib2 dep_testLib2)
  232. #-----------------------------------------------------------------------------
  233. # Create a library to be linked by another directory in this project
  234. # to test transitive linking to otherwise invisible imported targets.
  235. include_directories(${CMAKE_CURRENT_BINARY_DIR})
  236. add_library(imp_lib1 STATIC imp_lib1.c)
  237. target_link_libraries(imp_lib1 exp_testLib2)
  238. add_library(imp_lib1b STATIC imp_lib1.c)
  239. target_link_libraries(imp_lib1b bld_testLib2)
  240. if(NOT CMAKE_GENERATOR STREQUAL "Xcode" OR NOT CMAKE_OSX_ARCHITECTURES MATCHES "[;$]")
  241. set(bld_objlib_type OBJECT_LIBRARY)
  242. # Create executables using objects imported from the install tree
  243. add_executable(imp_testLib8_src imp_testLib8.c $<TARGET_OBJECTS:exp_testLib8>)
  244. add_executable(imp_testLib8_link imp_testLib8.c)
  245. target_link_libraries(imp_testLib8_link exp_testLib8)
  246. if(NOT CMAKE_GENERATOR STREQUAL "Xcode" OR NOT XCODE_VERSION VERSION_LESS 5)
  247. # Create executables using objects imported from the build tree
  248. add_executable(imp_testLib8b_src imp_testLib8.c $<TARGET_OBJECTS:bld_testLib8>)
  249. add_executable(imp_testLib8b_link imp_testLib8.c)
  250. target_link_libraries(imp_testLib8b_link bld_testLib8)
  251. endif()
  252. else()
  253. set(bld_objlib_type INTERFACE_LIBRARY)
  254. endif()
  255. # Create an executable that uses a library imported from the install tree
  256. # that itself was built using an object library. Verify we get the usage
  257. # requirements.
  258. add_executable(imp_testLib9 imp_testLib9.c)
  259. target_link_libraries(imp_testLib9 exp_testLib9)
  260. # Similarly for importing from the build tree.
  261. add_executable(imp_testLib9b imp_testLib9.c)
  262. target_link_libraries(imp_testLib9b bld_testLib9)
  263. # Check that object libraries were transformed on export as expected.
  264. foreach(vis Pub Priv Iface)
  265. get_property(type TARGET exp_testLib9Obj${vis} PROPERTY TYPE)
  266. if(NOT type STREQUAL INTERFACE_LIBRARY)
  267. message(FATAL_ERROR "exp_testLib9Obj${vis} type is '${type}', not 'INTERFACE_LIBRARY'")
  268. endif()
  269. get_property(type TARGET bld_testLib9Obj${vis} PROPERTY TYPE)
  270. if(NOT type STREQUAL "${bld_objlib_type}")
  271. message(FATAL_ERROR "bld_testLib9Obj${vis} type is '${type}', not '${bld_objlib_type}'")
  272. endif()
  273. endforeach()
  274. # Create executables to verify custom transitive properties.
  275. add_executable(imp_testLib10 imp_testLib10.c)
  276. target_link_libraries(imp_testLib10 PRIVATE exp_testLib10)
  277. add_executable(imp_testLib10b imp_testLib10.c)
  278. target_link_libraries(imp_testLib10b PRIVATE bld_testLib10)
  279. add_executable(imp_testLib11 imp_testLib11.c)
  280. target_link_libraries(imp_testLib11 PRIVATE exp_testLib11)
  281. add_executable(imp_testLib11b imp_testLib11.c)
  282. target_link_libraries(imp_testLib11b PRIVATE bld_testLib11)
  283. #-----------------------------------------------------------------------------
  284. # Test that handling imported targets, including transitive dependencies,
  285. # works in CheckFunctionExists (...and hopefully all other try_compile() checks
  286. include(CheckFunctionExists)
  287. unset(HAVE_TESTLIB1_FUNCTION CACHE)
  288. set(CMAKE_REQUIRED_LIBRARIES exp_testLib2)
  289. check_function_exists(testLib1 HAVE_TESTLIB1_FUNCTION)
  290. if (NOT HAVE_TESTLIB1_FUNCTION)
  291. message(SEND_ERROR "Using imported target testLib2 in check_function_exists() failed !")
  292. endif()
  293. #-----------------------------------------------------------------------------
  294. # Test that dependent imported targets have usable
  295. # INTERFACE_COMPILE_DEFINITIONS and INTERFACE_INCLUDE_DIRECTORIES
  296. add_executable(deps_iface deps_iface.c)
  297. target_link_libraries(deps_iface testLibDepends)
  298. add_executable(deps_shared_iface deps_shared_iface.cpp)
  299. target_link_libraries(deps_shared_iface testSharedLibDepends)
  300. target_compile_definitions(deps_shared_iface
  301. PRIVATE
  302. $<$<BOOL:$<TARGET_PROPERTY:POSITION_INDEPENDENT_CODE>>:PIC_PROPERTY_IS_ON>
  303. $<$<BOOL:$<TARGET_PROPERTY:CUSTOM_PROP>>:CUSTOM_PROPERTY_IS_ON>
  304. $<$<STREQUAL:$<TARGET_PROPERTY:CUSTOM_STRING>,testcontent>:CUSTOM_STRING_IS_MATCH>
  305. )
  306. if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "LCC")
  307. target_compile_definitions(deps_shared_iface
  308. PRIVATE
  309. "DO_GNU_TESTS"
  310. )
  311. endif()
  312. if (APPLE OR CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "LCC")
  313. include(CheckCXXCompilerFlag)
  314. check_cxx_compiler_flag(-fPIE run_pic_test)
  315. else()
  316. if (CMAKE_CXX_COMPILER_ID MATCHES "PGI"
  317. OR CMAKE_CXX_COMPILER_ID MATCHES "PathScale"
  318. OR CMAKE_CXX_COMPILER_ID MATCHES "Intel")
  319. set(run_pic_test 0)
  320. else()
  321. set(run_pic_test 1)
  322. endif()
  323. endif()
  324. add_executable(exp_renamed_test renamed_test.cpp)
  325. target_link_libraries(exp_renamed_test exp_renamed)
  326. add_executable(bld_renamed_test renamed_test.cpp)
  327. target_link_libraries(bld_renamed_test bld_renamed)
  328. if (run_pic_test)
  329. target_compile_definitions(deps_shared_iface PRIVATE CHECK_PIC_WORKS)
  330. endif()
  331. if(APPLE)
  332. add_subdirectory(framework_interface)
  333. endif()
  334. #-----------------------------------------------------------------------------
  335. # Test that targets imported from the build tree have their dependencies
  336. # evaluated correctly. The above already tests the same for the install tree.
  337. add_executable(deps_shared_iface2 deps_shared_iface.cpp)
  338. target_link_libraries(deps_shared_iface2 bld_testSharedLibDepends bld_subdirlib)
  339. if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "LCC")
  340. target_compile_definitions(deps_shared_iface2
  341. PRIVATE
  342. "DO_GNU_TESTS"
  343. )
  344. endif()
  345. target_compile_definitions(deps_shared_iface2
  346. PRIVATE TEST_SUBDIR_LIB
  347. $<$<BOOL:$<TARGET_PROPERTY:POSITION_INDEPENDENT_CODE>>:PIC_PROPERTY_IS_ON>
  348. $<$<BOOL:$<TARGET_PROPERTY:CUSTOM_PROP>>:CUSTOM_PROPERTY_IS_ON>
  349. $<$<STREQUAL:$<TARGET_PROPERTY:CUSTOM_STRING>,testcontent>:CUSTOM_STRING_IS_MATCH>
  350. )
  351. add_subdirectory(excludedFromAll)
  352. add_executable(iface_test_bld iface_test.cpp)
  353. target_link_libraries(iface_test_bld bld_testSharedLibDepends)
  354. set_property(TARGET iface_test_bld PROPERTY NO_SYSTEM_FROM_IMPORTED 1)
  355. set_property(TARGET bld_testSharedLibRequired APPEND PROPERTY
  356. INTERFACE_LINK_LIBRARIES
  357. excludedFromAll
  358. )
  359. add_executable(iface_test_exp iface_test.cpp)
  360. target_link_libraries(iface_test_exp testSharedLibDepends)
  361. set_property(TARGET testSharedLibDepends APPEND PROPERTY
  362. INTERFACE_LINK_LIBRARIES
  363. excludedFromAll
  364. )
  365. if (((CMAKE_C_COMPILER_ID STREQUAL GNU AND CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.4)
  366. OR CMAKE_C_COMPILER_ID MATCHES "LCC"
  367. OR (CMAKE_C_COMPILER_ID STREQUAL Clang AND NOT "x${CMAKE_CXX_SIMULATE_ID}" STREQUAL "xMSVC"))
  368. AND (CMAKE_GENERATOR STREQUAL "Unix Makefiles" OR CMAKE_GENERATOR STREQUAL "Ninja" OR CMAKE_GENERATOR STREQUAL "FASTBuild"))
  369. include(CheckCXXCompilerFlag)
  370. check_cxx_compiler_flag(-Wunused-variable run_sys_includes_test)
  371. if(run_sys_includes_test)
  372. # The Bullseye wrapper appears to break the -isystem effect.
  373. execute_process(COMMAND ${CMAKE_CXX_COMPILER} --version OUTPUT_VARIABLE out ERROR_VARIABLE out)
  374. if("x${out}" MATCHES "Bullseye")
  375. set(run_sys_includes_test 0)
  376. endif()
  377. endif()
  378. if (run_sys_includes_test)
  379. add_executable(test_system_exp test_system.cpp)
  380. target_link_libraries(test_system_exp exp_systemlib)
  381. target_compile_options(test_system_exp PRIVATE -Wunused-variable -Werror=unused-variable)
  382. unset(EXP_ERROR_VARIABLE CACHE)
  383. try_compile(EXP_ERROR_VARIABLE
  384. "${CMAKE_CURRENT_BINARY_DIR}/test_system"
  385. "${CMAKE_CURRENT_SOURCE_DIR}/test_system.cpp"
  386. COMPILE_DEFINITIONS "-Wunused-variable -Werror=unused-variable"
  387. LINK_LIBRARIES exp_systemlib
  388. OUTPUT_VARIABLE OUTPUT
  389. )
  390. if(NOT EXP_ERROR_VARIABLE)
  391. message(SEND_ERROR "EXP_ERROR_VARIABLE try_compile failed, but it was expected to succeed ${OUTPUT}.")
  392. endif()
  393. if(NOT CMAKE_CROSSCOMPILING)
  394. unset(EXP_RUN_VAR CACHE)
  395. unset(EXP_COMPILE_VAR CACHE)
  396. try_run(EXP_RUN_VAR EXP_COMPILE_VAR
  397. "${CMAKE_CURRENT_BINARY_DIR}/test_system"
  398. "${CMAKE_CURRENT_SOURCE_DIR}/test_system.cpp"
  399. COMPILE_DEFINITIONS "-Wunused-variable -Werror=unused-variable"
  400. LINK_LIBRARIES exp_systemlib
  401. OUTPUT_VARIABLE OUTPUT
  402. )
  403. if(NOT EXP_COMPILE_VAR)
  404. message(SEND_ERROR "try_run compile failed, but it was expected to succeed ${OUTPUT}.")
  405. endif()
  406. endif()
  407. add_executable(test_system_bld test_system.cpp)
  408. target_link_libraries(test_system_bld bld_systemlib)
  409. target_compile_options(test_system_bld PRIVATE -Wunused-variable -Werror=unused-variable)
  410. unset(BLD_ERROR_VARIABLE CACHE)
  411. try_compile(BLD_ERROR_VARIABLE
  412. "${CMAKE_CURRENT_BINARY_DIR}/test_system"
  413. "${CMAKE_CURRENT_SOURCE_DIR}/test_system.cpp"
  414. COMPILE_DEFINITIONS "-Wunused-variable -Werror=unused-variable"
  415. LINK_LIBRARIES bld_systemlib
  416. OUTPUT_VARIABLE OUTPUT
  417. )
  418. if(NOT BLD_ERROR_VARIABLE)
  419. message(SEND_ERROR "BLD_ERROR_VARIABLE try_compile failed, but it was expected to succeed. ${OUTPUT}")
  420. endif()
  421. if(NOT CMAKE_CROSSCOMPILING)
  422. unset(BLD_RUN_VAR CACHE)
  423. unset(BLD_COMPILE_VAR CACHE)
  424. try_run(BLD_RUN_VAR BLD_COMPILE_VAR
  425. "${CMAKE_CURRENT_BINARY_DIR}/test_system"
  426. "${CMAKE_CURRENT_SOURCE_DIR}/test_system.cpp"
  427. COMPILE_DEFINITIONS "-Wunused-variable -Werror=unused-variable"
  428. LINK_LIBRARIES bld_systemlib
  429. OUTPUT_VARIABLE OUTPUT
  430. )
  431. if(NOT BLD_COMPILE_VAR)
  432. message(SEND_ERROR "try_run compile failed, but it was expected to succeed ${OUTPUT}.")
  433. endif()
  434. endif()
  435. endif()
  436. # Testing try_compile with ALIAS targets.
  437. # These assume that previous test were successful, or at least the failures will be at the linking stage
  438. # with symbol not found errors
  439. # First make sure that if the test run without appropriate alias targets, they should error out
  440. try_compile(FAILING_LIBRARY_ALIAS_ERROR_VARIABLE
  441. "${CMAKE_CURRENT_BINARY_DIR}/test_failing_library_alias"
  442. "${CMAKE_CURRENT_SOURCE_DIR}/test_system.cpp"
  443. LINK_LIBRARIES not_existing_library
  444. OUTPUT_VARIABLE OUTPUT
  445. NO_CACHE
  446. )
  447. if(FAILING_LIBRARY_ALIAS_ERROR_VARIABLE)
  448. message(SEND_ERROR "FAILING_LIBRARY_ALIAS_ERROR_VARIABLE try_compile succeeded, but it was expected to fail ${OUTPUT}.")
  449. endif()
  450. # FIXME: CMAKE_TRY_COMPILE_TARGET_TYPE=MODULE is needed to properly link and test targets linked to an executable
  451. # set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
  452. # try_compile(FAILING_EXE_ALIAS_ERROR_VARIABLE
  453. # "${CMAKE_CURRENT_BINARY_DIR}/test_failing_exe_alias"
  454. # "${CMAKE_CURRENT_SOURCE_DIR}/imp_mod1.c"
  455. # LINK_LIBRARIES not_existing_executable
  456. # OUTPUT_VARIABLE OUTPUT
  457. # NO_CACHE
  458. # )
  459. # unset(CMAKE_TRY_COMPILE_TARGET_TYPE)
  460. # if(FAILING_EXE_ALIAS_ERROR_VARIABLE)
  461. # message(SEND_ERROR "FAILING_EXE_ALIAS_ERROR_VARIABLE try_compile succeeded, but it was expected to fail ${OUTPUT}.")
  462. # endif()
  463. # Do the actual try_compile tests for ALIAS targets
  464. add_library(exp_systemlib_alias ALIAS exp_systemlib)
  465. try_compile(EXP_LIBRARY_ALIAS_ERROR_VARIABLE
  466. "${CMAKE_CURRENT_BINARY_DIR}/test_library_alias"
  467. "${CMAKE_CURRENT_SOURCE_DIR}/test_system.cpp"
  468. LINK_LIBRARIES exp_systemlib_alias
  469. OUTPUT_VARIABLE OUTPUT
  470. NO_CACHE
  471. )
  472. if(NOT EXP_LIBRARY_ALIAS_ERROR_VARIABLE)
  473. message(SEND_ERROR "EXP_LIBRARY_ALIAS_ERROR_VARIABLE try_compile failed with library aliased target, but it was expected to succeed ${OUTPUT}.")
  474. endif()
  475. # FIXME: CMAKE_TRY_COMPILE_TARGET_TYPE=MODULE is needed to properly link and test targets linked to an executable
  476. # set(CMAKE_TRY_COMPILE_TARGET_TYPE MODULE)
  477. # add_executable(exp_exe_alias ALIAS exp_testExe2)
  478. # try_compile(EXP_EXE_ALIAS_ERROR_VARIABLE
  479. # "${CMAKE_CURRENT_BINARY_DIR}/test_exe_alias"
  480. # "${CMAKE_CURRENT_SOURCE_DIR}/imp_mod1.c"
  481. # LINK_LIBRARIES exp_exe_alias
  482. # OUTPUT_VARIABLE OUTPUT
  483. # NO_CACHE
  484. # )
  485. # unset(CMAKE_TRY_COMPILE_TARGET_TYPE)
  486. # if(NOT EXP_EXE_ALIAS_ERROR_VARIABLE)
  487. # message(SEND_ERROR "EXP_EXE_ALIAS_ERROR_VARIABLE try_compile failed with executable aliased target, but it was expected to succeed ${OUTPUT}.")
  488. # endif()
  489. add_library(bld_systemlib_alias ALIAS bld_systemlib)
  490. try_compile(BLD_LIBRARY_ALIAS_ERROR_VARIABLE
  491. "${CMAKE_CURRENT_BINARY_DIR}/test_library_alias"
  492. "${CMAKE_CURRENT_SOURCE_DIR}/test_system.cpp"
  493. LINK_LIBRARIES bld_systemlib_alias
  494. OUTPUT_VARIABLE OUTPUT
  495. NO_CACHE
  496. )
  497. if(NOT BLD_LIBRARY_ALIAS_ERROR_VARIABLE)
  498. message(SEND_ERROR "BLD_LIBRARY_ALIAS_ERROR_VARIABLE try_compile failed with library aliased target, but it was expected to succeed. ${OUTPUT}")
  499. endif()
  500. # FIXME: CMAKE_TRY_COMPILE_TARGET_TYPE=MODULE is needed to properly link and test targets linked to an executable
  501. # set(CMAKE_TRY_COMPILE_TARGET_TYPE MODULE)
  502. # add_executable(bld_exe_alias ALIAS bld_testExe2)
  503. # try_compile(BLD_EXE_ALIAS_ERROR_VARIABLE
  504. # "${CMAKE_CURRENT_BINARY_DIR}/test_exe_alias"
  505. # "${CMAKE_CURRENT_SOURCE_DIR}/imp_mod1.c"
  506. # LINK_LIBRARIES bld_exe_alias
  507. # OUTPUT_VARIABLE OUTPUT
  508. # NO_CACHE
  509. # )
  510. # unset(CMAKE_TRY_COMPILE_TARGET_TYPE)
  511. # if(NOT BLD_EXE_ALIAS_ERROR_VARIABLE)
  512. # message(SEND_ERROR "BLD_EXE_ALIAS_ERROR_VARIABLE try_compile failed with executable aliased target, but it was expected to succeed. ${OUTPUT}")
  513. # endif()
  514. endif()
  515. #---------------------------------------------------------------------------------
  516. # check that imported libraries have the expected INTERFACE_LINK_OPTIONS property
  517. checkForProperty(bld_testLinkOptions "INTERFACE_LINK_OPTIONS" "INTERFACE_FLAG")
  518. checkForProperty(Req::testLinkOptions "INTERFACE_LINK_OPTIONS" "INTERFACE_FLAG")
  519. #---------------------------------------------------------------------------------
  520. # check that imported libraries have the expected INTERFACE_LINK_DIRECTORIES property
  521. checkForProperty(bld_testLinkDirectories "INTERFACE_LINK_DIRECTORIES" "/interface/build")
  522. checkForProperty(Req::testLinkDirectories "INTERFACE_LINK_DIRECTORIES" "${CMAKE_INSTALL_PREFIX}/interface/install")
  523. #---------------------------------------------------------------------------------
  524. # check that imported libraries have the expected INTERFACE_LINK_DEPENDS property
  525. if(CMAKE_GENERATOR MATCHES "Make|Ninja|FASTBuild")
  526. checkForProperty(bld_testLinkDepends "INTERFACE_LINK_DEPENDS" "BUILD_LINK_DEPENDS")
  527. checkForProperty(Req::testLinkDepends "INTERFACE_LINK_DEPENDS" "${CMAKE_INSTALL_PREFIX}/INSTALL_LINK_DEPENDS")
  528. endif()
  529. #------------------------------------------------------------------------------
  530. # test import of CUDA language level
  531. if(CMake_TEST_CUDA)
  532. checkForProperty(bld_cudaInterfaceLib "INTERFACE_COMPILE_FEATURES" "cuda_std_11")
  533. checkForProperty(Req::cudaInterfaceLib "INTERFACE_COMPILE_FEATURES" "cuda_std_14")
  534. endif()