Skip to content

Commit

Permalink
Treat relative paths as relative to parent project's remote
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Wright authored and inkychris committed Nov 21, 2023
1 parent a9c8c6f commit 8bc2bfd
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Dependencies using CPM will automatically use the updated script of the outermos
- **Some CMake policies set to `NEW`** Including CPM.cmake will lead to several CMake policies being set to `NEW`. Users which need the old behavior will need to manually modify their CMake code to ensure they're set to `OLD` at the appropriate places. The policies are:
- [CMP0077](https://cmake.org/cmake/help/latest/policy/CMP0077.html) and [CMP0126](https://cmake.org/cmake/help/latest/policy/CMP0126.html). They make setting package options from `CMPAddPackage` possible.
- [CMP0135](https://cmake.org/cmake/help/latest/policy/CMP0135.html) It allows for proper package rebuilds of packages which are archives, source cache is not used, and the package URL is changed to an older version.
- [CMP0150](https://cmake.org/cmake/help/latest/policy/CMP0150.html) Relative paths provided to `GIT_REPOSITORY` are treated as relative to the parent project's remote.

For projects with more complex needs and where an extra setup step doesn't matter, it may be worth to check out an external C++ package manager such as [vcpkg](https://github.com/microsoft/vcpkg), [conan](https://conan.io) or [hunter](https://github.com/ruslo/hunter).
Dependencies added with `CPMFindPackage` should work with external package managers.
Expand Down
6 changes: 6 additions & 0 deletions cmake/CPM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ macro(cpm_set_policies)
cmake_policy(SET CMP0135 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0135 NEW)
endif()

# treat relative git repository paths as being relative to the parent project's remote
if(POLICY CMP0150)
cmake_policy(SET CMP0150 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0150 NEW)
endif()
endmacro()
cpm_set_policies()

Expand Down
30 changes: 30 additions & 0 deletions test/unit/relative_remote_dependency.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
if(NOT POLICY CMP0150)
return()
endif()

cmake_minimum_required(VERSION 3.27 FATAL_ERROR)

include(${CPM_PATH}/testing.cmake)
include(CMakePackageConfigHelpers)

set(CPM_SOURCE_CACHE_DIR "${CMAKE_CURRENT_BINARY_DIR}/CPM")
set(TEST_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/relative_remote_dependency)

message(STATUS "clearing CPM cache")
file(REMOVE_RECURSE ${CPM_SOURCE_CACHE_DIR})
assert_not_exists("${CPM_SOURCE_CACHE_DIR}")

file(REMOVE_RECURSE ${TEST_BUILD_DIR})

configure_package_config_file(
"${CMAKE_CURRENT_LIST_DIR}/relative_remote_dependency/CMakeLists.txt.in"
"${CMAKE_CURRENT_LIST_DIR}/relative_remote_dependency/CMakeLists.txt"
INSTALL_DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/junk
)

execute_process(
COMMAND ${CMAKE_COMMAND} "-S${CMAKE_CURRENT_LIST_DIR}/relative_remote_dependency"
"-B${TEST_BUILD_DIR}" "-DCPM_SOURCE_CACHE=${CPM_SOURCE_CACHE_DIR}" RESULT_VARIABLE ret
)

assert_equal(${ret} "0")
1 change: 1 addition & 0 deletions test/unit/relative_remote_dependency/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/CMakeLists.txt
18 changes: 18 additions & 0 deletions test/unit/relative_remote_dependency/CMakeLists.txt.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.27 FATAL_ERROR)

project(CPMRelativeRemoteDependency)

include(@CPM_PATH@/CPM.cmake)

CPMAddPackage(
NAME adder
GIT_REPOSITORY ../testpack-adder.git
GIT_TAG 5d6da7b
)

include(@CPM_PATH@/testing.cmake)

ASSERT_TRUTHY(adder_ADDED)
ASSERT_DEFINED(adder_SOURCE_DIR)
ASSERT_DEFINED(adder_BINARY_DIR)
ASSERT_EQUAL("${CPM_LAST_PACKAGE_NAME}" "adder")

0 comments on commit 8bc2bfd

Please sign in to comment.