diff options
author | Kyle Edwards <kyle.edwards@kitware.com> | 2023-11-07 16:44:00 (GMT) |
---|---|---|
committer | Kyle Edwards <kyle.edwards@kitware.com> | 2023-11-13 16:07:52 (GMT) |
commit | c6e6861e631ecfdc96c79f562a66021cb8971fcc (patch) | |
tree | 6cb0de03a6db5d0f997f5d351ac2a03bb060bff7 /Tests | |
parent | 2837f592ab7642e6a769949b4ddc1ffb37e21b9c (diff) | |
download | CMake-c6e6861e631ecfdc96c79f562a66021cb8971fcc.zip CMake-c6e6861e631ecfdc96c79f562a66021cb8971fcc.tar.gz CMake-c6e6861e631ecfdc96c79f562a66021cb8971fcc.tar.bz2 |
install(EXPORT): Export find_dependency() calls
Issue: #20511
Co-Authored-by: Brad King <brad.king@kitware.com>
Co-Authored-by: Robert Maynard <rmaynard@nvidia.com>
Diffstat (limited to 'Tests')
25 files changed, 230 insertions, 1 deletions
diff --git a/Tests/ExportImport/Export/CMakeLists.txt b/Tests/ExportImport/Export/CMakeLists.txt index 67f2fcb..56b8aaa 100644 --- a/Tests/ExportImport/Export/CMakeLists.txt +++ b/Tests/ExportImport/Export/CMakeLists.txt @@ -4,6 +4,8 @@ if(POLICY CMP0129) endif() project(Export C CXX) +find_package(Foo REQUIRED CONFIG NO_DEFAULT_PATH) + # Pretend that RelWithDebInfo should link to debug libraries to test # the DEBUG_CONFIGURATIONS property. set_property(GLOBAL PROPERTY DEBUG_CONFIGURATIONS Debug RelWithDebInfo) @@ -110,6 +112,7 @@ add_library(testLib9ObjIface OBJECT testLib9ObjIface.c) target_compile_definitions(testLib9ObjIface INTERFACE testLib9ObjIface_USED) add_library(testLib9 STATIC testLib9.c) target_link_libraries(testLib9 INTERFACE testLib9ObjIface PUBLIC testLib9ObjPub PRIVATE testLib9ObjPriv) +target_link_libraries(testLib9 PUBLIC Foo::Foo) cmake_policy(POP) # Test using the target_link_libraries command to set the @@ -624,7 +627,7 @@ install( LIBRARY DESTINATION lib ARCHIVE DESTINATION lib ) -install(EXPORT exp NAMESPACE exp_ DESTINATION lib/exp) +install(EXPORT exp NAMESPACE exp_ DESTINATION lib/exp EXPORT_PACKAGE_DEPENDENCIES) # Install testLib5.dll outside the export. if(WIN32) diff --git a/Tests/ExportImport/External/FooConfig.cmake b/Tests/ExportImport/External/FooConfig.cmake new file mode 100644 index 0000000..48b6289 --- /dev/null +++ b/Tests/ExportImport/External/FooConfig.cmake @@ -0,0 +1,3 @@ +if(NOT TARGET Foo::Foo) + add_library(Foo::Foo INTERFACE IMPORTED) +endif() diff --git a/Tests/ExportImport/Import/CMakeLists.txt b/Tests/ExportImport/Import/CMakeLists.txt index e6dcd65..83c87a8 100644 --- a/Tests/ExportImport/Import/CMakeLists.txt +++ b/Tests/ExportImport/Import/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required (VERSION 2.7.20090711) cmake_policy(SET CMP0025 NEW) +cmake_policy(SET CMP0028 NEW) if(POLICY CMP0129) cmake_policy(SET CMP0129 NEW) endif() diff --git a/Tests/ExportImport/InitialCache.cmake.in b/Tests/ExportImport/InitialCache.cmake.in index 44cd179..55aef86 100644 --- a/Tests/ExportImport/InitialCache.cmake.in +++ b/Tests/ExportImport/InitialCache.cmake.in @@ -15,3 +15,4 @@ set(CMAKE_INSTALL_PREFIX "@ExportImport_BINARY_DIR@/Root" CACHE STRING "Installa set(CMAKE_SKIP_RPATH ON CACHE BOOL "No RPATH") set(CMAKE_GNUtoMS "@ExportImport_GNUtoMS@" CACHE BOOL "CMAKE_GNUtoMS") set(CMake_TEST_CUDA "@CMake_TEST_CUDA@" CACHE BOOL "CMake_TEST_CUDA") +set(Foo_DIR "@CMAKE_CURRENT_SOURCE_DIR@/External" CACHE PATH "Foo cmake package directory") diff --git a/Tests/RunCMake/export/CMake/FindHasDeps.cmake b/Tests/RunCMake/export/CMake/FindHasDeps.cmake new file mode 100644 index 0000000..86b2abe --- /dev/null +++ b/Tests/RunCMake/export/CMake/FindHasDeps.cmake @@ -0,0 +1,17 @@ +find_package(Threads REQUIRED) +find_package(P4 REQUIRED) + +add_library(HasDeps::interface IMPORTED INTERFACE) +target_link_libraries(HasDeps::interface INTERFACE Threads::Threads l4) + +add_library(HasDeps::A IMPORTED UNKNOWN) +target_link_libraries(HasDeps::A INTERFACE HasDeps::interface) +file(TOUCH "${CMAKE_CURRENT_BINARY_DIR}/a.so") +set_property(TARGET HasDeps::A PROPERTY IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/a.so") + +add_library(HasDeps::B IMPORTED UNKNOWN) +target_link_libraries(HasDeps::B INTERFACE HasDeps::interface) +file(TOUCH "${CMAKE_CURRENT_BINARY_DIR}/b.so") +set_property(TARGET HasDeps::B PROPERTY IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/b.so") + +set(HASDEPS_FOUND TRUE) diff --git a/Tests/RunCMake/export/CMake/FindP1.cmake b/Tests/RunCMake/export/CMake/FindP1.cmake new file mode 100644 index 0000000..e33c3d6 --- /dev/null +++ b/Tests/RunCMake/export/CMake/FindP1.cmake @@ -0,0 +1,2 @@ +add_library(l1 IMPORTED INTERFACE) +set(P1_FOUND TRUE) diff --git a/Tests/RunCMake/export/CMake/FindP2.cmake b/Tests/RunCMake/export/CMake/FindP2.cmake new file mode 100644 index 0000000..6a360ce --- /dev/null +++ b/Tests/RunCMake/export/CMake/FindP2.cmake @@ -0,0 +1,2 @@ +add_library(l2 IMPORTED INTERFACE) +set(P2_FOUND TRUE) diff --git a/Tests/RunCMake/export/CMake/FindP3.cmake b/Tests/RunCMake/export/CMake/FindP3.cmake new file mode 100644 index 0000000..72bd829 --- /dev/null +++ b/Tests/RunCMake/export/CMake/FindP3.cmake @@ -0,0 +1,2 @@ +add_library(l3 IMPORTED INTERFACE) +set(P3_FOUND TRUE) diff --git a/Tests/RunCMake/export/CMake/FindP4.cmake b/Tests/RunCMake/export/CMake/FindP4.cmake new file mode 100644 index 0000000..b62a040 --- /dev/null +++ b/Tests/RunCMake/export/CMake/FindP4.cmake @@ -0,0 +1,2 @@ +add_library(l4 IMPORTED INTERFACE) +set(P4_FOUND TRUE) diff --git a/Tests/RunCMake/export/CMake/FindP9.cmake b/Tests/RunCMake/export/CMake/FindP9.cmake new file mode 100644 index 0000000..201b86a --- /dev/null +++ b/Tests/RunCMake/export/CMake/FindP9.cmake @@ -0,0 +1,2 @@ +add_library(l9 IMPORTED INTERFACE) +set(P9_FOUND TRUE) diff --git a/Tests/RunCMake/export/FindDependencyExport-check.cmake b/Tests/RunCMake/export/FindDependencyExport-check.cmake new file mode 100644 index 0000000..0cbb195 --- /dev/null +++ b/Tests/RunCMake/export/FindDependencyExport-check.cmake @@ -0,0 +1,35 @@ +file(READ "${RunCMake_TEST_BINARY_DIR}/mytargets.cmake" mytargets) +if("${mytargets}" MATCHES "find_dependency\\(P1") + string(APPEND RunCMake_TEST_FAILED "P1 dependency should not be exported but it is\n") +endif() +if(NOT "${mytargets}" MATCHES "find_dependency\\(P2 REQUIRED \"VERSION\" \"1\\.0\"\\)") + string(APPEND RunCMake_TEST_FAILED "P2 dependency should be exported but it is not\n") +endif() +if(NOT "${mytargets}" MATCHES "find_dependency\\(P3 REQUIRED\\)") + string(APPEND RunCMake_TEST_FAILED "P3 dependency should be exported but it is not\n") +endif() +if(NOT "${mytargets}" MATCHES "find_dependency\\(P4 REQUIRED\\)") + string(APPEND RunCMake_TEST_FAILED "P4 dependency should be exported but it is not\n") +endif() +if("${mytargets}" MATCHES "find_dependency\\(P5") + string(APPEND RunCMake_TEST_FAILED "P5 dependency should not be exported but it is\n") +endif() +if(NOT "${mytargets}" MATCHES "find_dependency\\(P6 REQUIRED\\)") + string(APPEND RunCMake_TEST_FAILED "P6 dependency should be exported but it is not\n") +endif() +if("${mytargets}" MATCHES "find_dependency\\(P7") + string(APPEND RunCMake_TEST_FAILED "P7 dependency should not be exported but it is\n") +endif() +if(NOT "${mytargets}" MATCHES "find_dependency\\(P3[^ +]*\\) +find_dependency\\(P2[^ +]*\\) +find_dependency\\(P8[^ +]*\\) +find_dependency\\(P6[^ +]*\\) +find_dependency\\(P9[^ +]*\\) +find_dependency\\(P4") + string(APPEND RunCMake_TEST_FAILED "Dependencies are not in the correct order\n") +endif() diff --git a/Tests/RunCMake/export/FindDependencyExport.cmake b/Tests/RunCMake/export/FindDependencyExport.cmake new file mode 100644 index 0000000..59dd4cc --- /dev/null +++ b/Tests/RunCMake/export/FindDependencyExport.cmake @@ -0,0 +1,30 @@ +set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMake) + +find_package(P1) +find_package(P2) +find_package(P9) +find_package(P4) +find_package(P3) + +add_library(mylib INTERFACE) +target_link_libraries(mylib INTERFACE l1 l2 l3 l4 l9) + +install(TARGETS mylib EXPORT mytargets) +export(SETUP mytargets + PACKAGE_DEPENDENCY P1 + ENABLED OFF + PACKAGE_DEPENDENCY P3 + ENABLED AUTO + PACKAGE_DEPENDENCY P2 + ENABLED ON + EXTRA_ARGS VERSION 1.0 + PACKAGE_DEPENDENCY P5 + ENABLED FALSE + PACKAGE_DEPENDENCY P8 + ENABLED TRUE + PACKAGE_DEPENDENCY P6 + ENABLED 1 + PACKAGE_DEPENDENCY P7 + ENABLED AUTO + ) +export(EXPORT mytargets EXPORT_PACKAGE_DEPENDENCIES FILE mytargets.cmake) diff --git a/Tests/RunCMake/export/FindDependencyExportFetchContent-check.cmake b/Tests/RunCMake/export/FindDependencyExportFetchContent-check.cmake new file mode 100644 index 0000000..353bb08 --- /dev/null +++ b/Tests/RunCMake/export/FindDependencyExportFetchContent-check.cmake @@ -0,0 +1,14 @@ +file(READ "${RunCMake_TEST_BINARY_DIR}/my_private_targets.cmake" my_private_targets) +if(NOT "${my_private_targets}" MATCHES "find_dependency\\(HasDeps") + string(APPEND RunCMake_TEST_FAILED "HasDeps dependency should be exported but it is not\n") +endif() + +file(READ "${RunCMake_TEST_BINARY_DIR}/my_static_targets.cmake" my_static_targets) +if(NOT "${my_static_targets}" MATCHES "find_dependency\\(MyPrivate") + string(APPEND RunCMake_TEST_FAILED "HasDeps dependency should be exported but it is not\n") +endif() + +file(READ "${RunCMake_TEST_BINARY_DIR}/my_shared_targets.cmake" my_shared_targets) +if(NOT "${my_shared_targets}" MATCHES "find_dependency\\(MyPrivate") + string(APPEND RunCMake_TEST_FAILED "MyStatic dependency should be exported but it is not\n") +endif() diff --git a/Tests/RunCMake/export/FindDependencyExportFetchContent.cmake b/Tests/RunCMake/export/FindDependencyExportFetchContent.cmake new file mode 100644 index 0000000..9e6c565 --- /dev/null +++ b/Tests/RunCMake/export/FindDependencyExportFetchContent.cmake @@ -0,0 +1,37 @@ +set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMake) + +enable_language(CXX) + +find_package(HasDeps) + +# replicates FetchContent where a dependency is brought +# in via source. In these cases we need to extend the `install` +# `export` commands to allow markup on what `Find<Project>` will +# map to the export set +add_library(my_private_lib STATIC empty.cpp) +target_link_libraries(my_private_lib PUBLIC HasDeps::A) +set_target_properties(my_private_lib PROPERTIES EXPORT_FIND_PACKAGE_NAME "MyPrivate") + +install(TARGETS my_private_lib EXPORT my_private_targets) +install(EXPORT my_private_targets + FILE my_private.cmake + DESTINATION lib) +export(EXPORT my_private_targets EXPORT_PACKAGE_DEPENDENCIES FILE my_private_targets.cmake) + +add_library(my_static_lib STATIC empty.cpp) +target_link_libraries(my_static_lib PRIVATE my_private_lib) + +install(TARGETS my_static_lib EXPORT my_static_targets) +install(EXPORT my_static_targets + FILE my_static.cmake + DESTINATION lib) +export(EXPORT my_static_targets EXPORT_PACKAGE_DEPENDENCIES FILE my_static_targets.cmake) + +add_library(my_shared_lib SHARED empty.cpp) +target_link_libraries(my_shared_lib PUBLIC my_private_lib) + +install(TARGETS my_shared_lib EXPORT my_shared_targets) +install(EXPORT my_shared_targets + FILE my_shared.cmake + DESTINATION lib) +export(EXPORT my_shared_targets EXPORT_PACKAGE_DEPENDENCIES FILE my_shared_targets.cmake) diff --git a/Tests/RunCMake/export/FindDependencyExportShared-check.cmake b/Tests/RunCMake/export/FindDependencyExportShared-check.cmake new file mode 100644 index 0000000..d7a32d1 --- /dev/null +++ b/Tests/RunCMake/export/FindDependencyExportShared-check.cmake @@ -0,0 +1,4 @@ +file(READ "${RunCMake_TEST_BINARY_DIR}/mytargets.cmake" mytargets) +if("${mytargets}" MATCHES "find_dependency") + string(APPEND RunCMake_TEST_FAILED "No dependencies should not be exported\n") +endif() diff --git a/Tests/RunCMake/export/FindDependencyExportShared.cmake b/Tests/RunCMake/export/FindDependencyExportShared.cmake new file mode 100644 index 0000000..6e662a1 --- /dev/null +++ b/Tests/RunCMake/export/FindDependencyExportShared.cmake @@ -0,0 +1,14 @@ +set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMake) + +enable_language(CXX) + +find_package(P1) +find_package(P2) +find_package(P3) +find_package(P4) + +add_library(mylib SHARED empty.cpp) +target_link_libraries(mylib PRIVATE l1 l2 l3 l4) + +install(TARGETS mylib EXPORT mytargets) +export(EXPORT mytargets EXPORT_PACKAGE_DEPENDENCIES FILE mytargets.cmake) diff --git a/Tests/RunCMake/export/FindDependencyExportStatic-check.cmake b/Tests/RunCMake/export/FindDependencyExportStatic-check.cmake new file mode 100644 index 0000000..b78bccb --- /dev/null +++ b/Tests/RunCMake/export/FindDependencyExportStatic-check.cmake @@ -0,0 +1,13 @@ +file(READ "${RunCMake_TEST_BINARY_DIR}/mytargets.cmake" mytargets) +if(NOT "${mytargets}" MATCHES "find_dependency\\(P1") + string(APPEND RunCMake_TEST_FAILED "P1 dependency should be exported but it is not\n") +endif() +if(NOT "${mytargets}" MATCHES "find_dependency\\(P2") + string(APPEND RunCMake_TEST_FAILED "P2 dependency should be exported but it is not\n") +endif() +if(NOT "${mytargets}" MATCHES "find_dependency\\(P3") + string(APPEND RunCMake_TEST_FAILED "P3 dependency should be exported but it is not\n") +endif() +if(NOT "${mytargets}" MATCHES "find_dependency\\(P4") + string(APPEND RunCMake_TEST_FAILED "P4 dependency should be exported but it is not\n") +endif() diff --git a/Tests/RunCMake/export/FindDependencyExportStatic.cmake b/Tests/RunCMake/export/FindDependencyExportStatic.cmake new file mode 100644 index 0000000..2d32eb2 --- /dev/null +++ b/Tests/RunCMake/export/FindDependencyExportStatic.cmake @@ -0,0 +1,14 @@ +set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMake) + +enable_language(CXX) + +find_package(P1) +find_package(P2) +find_package(P3) +find_package(P4) + +add_library(mylib STATIC empty.cpp) +target_link_libraries(mylib PRIVATE l1 l2 l3 l4) + +install(TARGETS mylib EXPORT mytargets) +export(EXPORT mytargets EXPORT_PACKAGE_DEPENDENCIES FILE mytargets.cmake) diff --git a/Tests/RunCMake/export/RunCMakeTest.cmake b/Tests/RunCMake/export/RunCMakeTest.cmake index ee00b27..3e1b7d6 100644 --- a/Tests/RunCMake/export/RunCMakeTest.cmake +++ b/Tests/RunCMake/export/RunCMakeTest.cmake @@ -19,3 +19,7 @@ run_cmake(UnknownExport) run_cmake(NamelinkOnlyExport) run_cmake(SeparateNamelinkExport) run_cmake(TryCompileExport) +run_cmake(FindDependencyExport) +run_cmake(FindDependencyExportStatic) +run_cmake(FindDependencyExportShared) +run_cmake(FindDependencyExportFetchContent) diff --git a/Tests/RunCMake/install/EXPORT-FindDependencyExport-all-check.cmake b/Tests/RunCMake/install/EXPORT-FindDependencyExport-all-check.cmake new file mode 100644 index 0000000..6a8b2e5 --- /dev/null +++ b/Tests/RunCMake/install/EXPORT-FindDependencyExport-all-check.cmake @@ -0,0 +1,4 @@ +file(READ "${RunCMake_TEST_BINARY_DIR}/root-all/lib/cmake/mylib/mylib-targets.cmake" contents) +if(NOT contents MATCHES "include\\(CMakeFindDependencyMacro\\)\nfind_dependency\\(P2 REQUIRED\\)\nfind_dependency\\(P1 REQUIRED\\)\n") + set(RunCMake_TEST_FAILED "Dependencies were not properly exported") +endif() diff --git a/Tests/RunCMake/install/EXPORT-FindDependencyExport.cmake b/Tests/RunCMake/install/EXPORT-FindDependencyExport.cmake new file mode 100644 index 0000000..071414b --- /dev/null +++ b/Tests/RunCMake/install/EXPORT-FindDependencyExport.cmake @@ -0,0 +1,18 @@ +enable_language(C) + +set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) + +find_package(P1 REQUIRED) +find_package(P2 REQUIRED) +find_package(P3 REQUIRED) + +add_library(mylib INTERFACE) +target_link_libraries(mylib INTERFACE lib1 lib2 lib3) +install(TARGETS mylib EXPORT mylib-targets) +export(SETUP mylib-targets + PACKAGE_DEPENDENCY P2 + ENABLED AUTO + PACKAGE_DEPENDENCY P3 + ENABLED OFF + ) +install(EXPORT mylib-targets EXPORT_PACKAGE_DEPENDENCIES FILE mylib-targets.cmake DESTINATION lib/cmake/mylib) diff --git a/Tests/RunCMake/install/RunCMakeTest.cmake b/Tests/RunCMake/install/RunCMakeTest.cmake index efafdd1..6f63550 100644 --- a/Tests/RunCMake/install/RunCMakeTest.cmake +++ b/Tests/RunCMake/install/RunCMakeTest.cmake @@ -176,6 +176,7 @@ run_install_test(TARGETS-Parts) run_install_test(FILES-PERMISSIONS) run_install_test(TARGETS-RPATH) run_install_test(InstallRequiredSystemLibraries) +run_install_test(EXPORT-FindDependencyExport) set(RunCMake_TEST_OPTIONS "-DCMAKE_POLICY_DEFAULT_CMP0087:STRING=NEW") run_install_test(SCRIPT) diff --git a/Tests/RunCMake/install/cmake/FindP1.cmake b/Tests/RunCMake/install/cmake/FindP1.cmake new file mode 100644 index 0000000..c772836 --- /dev/null +++ b/Tests/RunCMake/install/cmake/FindP1.cmake @@ -0,0 +1,2 @@ +add_library(lib1 IMPORTED INTERFACE) +set(P1_FOUND TRUE) diff --git a/Tests/RunCMake/install/cmake/FindP2.cmake b/Tests/RunCMake/install/cmake/FindP2.cmake new file mode 100644 index 0000000..d81b35d --- /dev/null +++ b/Tests/RunCMake/install/cmake/FindP2.cmake @@ -0,0 +1,2 @@ +add_library(lib2 IMPORTED INTERFACE) +set(P2_FOUND TRUE) diff --git a/Tests/RunCMake/install/cmake/FindP3.cmake b/Tests/RunCMake/install/cmake/FindP3.cmake new file mode 100644 index 0000000..2818c3b --- /dev/null +++ b/Tests/RunCMake/install/cmake/FindP3.cmake @@ -0,0 +1,2 @@ +add_library(lib3 IMPORTED INTERFACE) +set(P3_FOUND TRUE) |