diff --git a/README.md b/README.md index b7027fd..8f28255 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,7 @@ After calling `CPMAddPackage`, the following variables are defined in the local - `_SOURCE_DIR` is the path to the source of the dependency. - `_BINARY_DIR` is the path to the build directory of the dependency. - `_ADDED` is set to `YES` if the dependency has not been added before, otherwise it is set to `NO`. +- `CPM_LAST_PACKAGE_NAME` is set to the determined name of the last added dependency (equivalent to ``). For using CPM.cmake projects with external package managers, such as conan or vcpkg, setting the variable [`CPM_USE_LOCAL_PACKAGES`](#options) will make CPM.cmake try to add a package through `find_package` first, and add it from source if it doesn't succeed. diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index 4423619..80a13c0 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -808,6 +808,10 @@ macro(cpm_export_variables name) "${${name}_ADDED}" PARENT_SCOPE ) + set(CPM_LAST_PACKAGE_NAME + "${name}" + PARENT_SCOPE + ) endmacro() # declares a package, so that any call to CPMAddPackage for the package name will use these diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 4bc734c..35496f8 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -14,3 +14,5 @@ foreach(test ${tests}) endforeach() add_custom_target(test-verbose COMMAND ${CMAKE_CTEST_COMMAND} -C Debug --verbose) + +add_subdirectory(style) diff --git a/test/unit/dependency_properties.cmake b/test/unit/dependency_properties.cmake index 69f26a7..b94c539 100644 --- a/test/unit/dependency_properties.cmake +++ b/test/unit/dependency_properties.cmake @@ -6,6 +6,7 @@ include(${CPM_PATH}/testing.cmake) set(CPM_DRY_RUN ON) CPMAddPackage(NAME A GIT_TAG 1.2.3) +assert_equal("${CPM_LAST_PACKAGE_NAME}" "A") CPMAddPackage(NAME A VERSION 1.2.3) @@ -17,6 +18,8 @@ CPMAddPackage(NAME B VERSION 2.4.1) CPMAddPackage(NAME B GIT_TAG v2.3.1) CPMGetPackageVersion(B VERSION) +assert_equal("${CPM_LAST_PACKAGE_NAME}" "B") + assert_equal(${VERSION} "2.4.1") CPMAddPackage( @@ -24,6 +27,10 @@ CPMAddPackage( GIT_TAG v3.1.2-a VERSION 3.1.2 ) +assert_equal("${CPM_LAST_PACKAGE_NAME}" "C") CPMGetPackageVersion(C VERSION) assert_equal(${VERSION} "3.1.2") + +CPMAddPackage("gh:dry-run/D") +assert_equal("${CPM_LAST_PACKAGE_NAME}" "D") diff --git a/test/unit/local_dependency/ModuleCMakeLists.txt.in b/test/unit/local_dependency/ModuleCMakeLists.txt.in index 7f71827..8c87ec1 100644 --- a/test/unit/local_dependency/ModuleCMakeLists.txt.in +++ b/test/unit/local_dependency/ModuleCMakeLists.txt.in @@ -32,3 +32,4 @@ include(@CPM_PATH@/testing.cmake) ASSERT_TRUTHY(@TEST_DEPENDENCY_NAME@_ADDED) ASSERT_DEFINED(@TEST_DEPENDENCY_NAME@_SOURCE_DIR) ASSERT_DEFINED(@TEST_DEPENDENCY_NAME@_BINARY_DIR) +ASSERT_EQUAL("${CPM_LAST_PACKAGE_NAME}" "@TEST_DEPENDENCY_NAME@") diff --git a/test/unit/local_dependency/OverrideCMakeLists.txt.in b/test/unit/local_dependency/OverrideCMakeLists.txt.in index 3925bc7..8987a16 100644 --- a/test/unit/local_dependency/OverrideCMakeLists.txt.in +++ b/test/unit/local_dependency/OverrideCMakeLists.txt.in @@ -25,3 +25,5 @@ include(@CPM_PATH@/testing.cmake) ASSERT_TRUTHY(Dependency_ADDED) ASSERT_DEFINED(Dependency_SOURCE_DIR) ASSERT_DEFINED(Dependency_BINARY_DIR) +ASSERT_EQUAL("${CPM_LAST_PACKAGE_NAME}" "Dependency") + diff --git a/test/unit/remote_dependency/CMakeLists.txt.in b/test/unit/remote_dependency/CMakeLists.txt.in index a4f5338..c312aa6 100644 --- a/test/unit/remote_dependency/CMakeLists.txt.in +++ b/test/unit/remote_dependency/CMakeLists.txt.in @@ -30,3 +30,4 @@ include(@CPM_PATH@/testing.cmake) ASSERT_TRUTHY(fibonacci_ADDED) ASSERT_DEFINED(fibonacci_SOURCE_DIR) ASSERT_DEFINED(fibonacci_BINARY_DIR) +ASSERT_EQUAL("${CPM_LAST_PACKAGE_NAME}" "fibonacci")