Skip to content

Commit 03705fc

Browse files
Added export of CPM_LAST_PACKAGE_NAME (#403)
1 parent f00ccd8 commit 03705fc

File tree

7 files changed

+18
-0
lines changed

7 files changed

+18
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ After calling `CPMAddPackage`, the following variables are defined in the local
9797
- `<dependency>_SOURCE_DIR` is the path to the source of the dependency.
9898
- `<dependency>_BINARY_DIR` is the path to the build directory of the dependency.
9999
- `<dependency>_ADDED` is set to `YES` if the dependency has not been added before, otherwise it is set to `NO`.
100+
- `CPM_LAST_PACKAGE_NAME` is set to the determined name of the last added dependency (equivalent to `<dependency>`).
100101

101102
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.
102103

cmake/CPM.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,10 @@ macro(cpm_export_variables name)
808808
"${${name}_ADDED}"
809809
PARENT_SCOPE
810810
)
811+
set(CPM_LAST_PACKAGE_NAME
812+
"${name}"
813+
PARENT_SCOPE
814+
)
811815
endmacro()
812816

813817
# declares a package, so that any call to CPMAddPackage for the package name will use these

test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ foreach(test ${tests})
1414
endforeach()
1515

1616
add_custom_target(test-verbose COMMAND ${CMAKE_CTEST_COMMAND} -C Debug --verbose)
17+
18+
add_subdirectory(style)

test/unit/dependency_properties.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ include(${CPM_PATH}/testing.cmake)
66
set(CPM_DRY_RUN ON)
77

88
CPMAddPackage(NAME A GIT_TAG 1.2.3)
9+
assert_equal("${CPM_LAST_PACKAGE_NAME}" "A")
910

1011
CPMAddPackage(NAME A VERSION 1.2.3)
1112

@@ -17,13 +18,19 @@ CPMAddPackage(NAME B VERSION 2.4.1)
1718
CPMAddPackage(NAME B GIT_TAG v2.3.1)
1819

1920
CPMGetPackageVersion(B VERSION)
21+
assert_equal("${CPM_LAST_PACKAGE_NAME}" "B")
22+
2023
assert_equal(${VERSION} "2.4.1")
2124

2225
CPMAddPackage(
2326
NAME C
2427
GIT_TAG v3.1.2-a
2528
VERSION 3.1.2
2629
)
30+
assert_equal("${CPM_LAST_PACKAGE_NAME}" "C")
2731

2832
CPMGetPackageVersion(C VERSION)
2933
assert_equal(${VERSION} "3.1.2")
34+
35+
CPMAddPackage("gh:dry-run/D")
36+
assert_equal("${CPM_LAST_PACKAGE_NAME}" "D")

test/unit/local_dependency/ModuleCMakeLists.txt.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ include(@CPM_PATH@/testing.cmake)
3232
ASSERT_TRUTHY(@TEST_DEPENDENCY_NAME@_ADDED)
3333
ASSERT_DEFINED(@TEST_DEPENDENCY_NAME@_SOURCE_DIR)
3434
ASSERT_DEFINED(@TEST_DEPENDENCY_NAME@_BINARY_DIR)
35+
ASSERT_EQUAL("${CPM_LAST_PACKAGE_NAME}" "@TEST_DEPENDENCY_NAME@")

test/unit/local_dependency/OverrideCMakeLists.txt.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ include(@CPM_PATH@/testing.cmake)
2525
ASSERT_TRUTHY(Dependency_ADDED)
2626
ASSERT_DEFINED(Dependency_SOURCE_DIR)
2727
ASSERT_DEFINED(Dependency_BINARY_DIR)
28+
ASSERT_EQUAL("${CPM_LAST_PACKAGE_NAME}" "Dependency")
29+

test/unit/remote_dependency/CMakeLists.txt.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ include(@CPM_PATH@/testing.cmake)
3030
ASSERT_TRUTHY(fibonacci_ADDED)
3131
ASSERT_DEFINED(fibonacci_SOURCE_DIR)
3232
ASSERT_DEFINED(fibonacci_BINARY_DIR)
33+
ASSERT_EQUAL("${CPM_LAST_PACKAGE_NAME}" "fibonacci")

0 commit comments

Comments
 (0)