Skip to content

Commit

Permalink
add support for SOURCE_DIR argument (#97)
Browse files Browse the repository at this point in the history
* add support for SOURCE_DIR argument

* simplify code

* add test

* update comment
  • Loading branch information
TheLartians authored Feb 25, 2020
1 parent 775a235 commit 01b69aa
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 17 deletions.
34 changes: 17 additions & 17 deletions cmake/CPM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

set(CURRENT_CPM_VERSION 0.16)
set(CURRENT_CPM_VERSION 0.17)

if(CPM_DIRECTORY)
if(NOT ${CPM_DIRECTORY} MATCHES ${CMAKE_CURRENT_LIST_DIR})
Expand Down Expand Up @@ -208,22 +208,22 @@ function(CPMAddPackage)

if (DEFINED CPM_ARGS_DOWNLOAD_COMMAND)
set(FETCH_CONTENT_DECLARE_EXTRA_OPTS DOWNLOAD_COMMAND ${CPM_ARGS_DOWNLOAD_COMMAND})
else()
if (CPM_SOURCE_CACHE AND NOT DEFINED CPM_ARGS_SOURCE_DIR)
string(TOLOWER ${CPM_ARGS_NAME} lower_case_name)
set(origin_parameters ${CPM_ARGS_UNPARSED_ARGUMENTS})
list(SORT origin_parameters)
string(SHA1 origin_hash "${origin_parameters}")
set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash})
list(APPEND FETCH_CONTENT_DECLARE_EXTRA_OPTS SOURCE_DIR ${download_directory})
if (EXISTS ${download_directory})
list(APPEND FETCH_CONTENT_DECLARE_EXTRA_OPTS DOWNLOAD_COMMAND ":")
set(PACKAGE_INFO "${download_directory}")
else()
# remove timestamps so CMake will re-download the dependency
file(REMOVE_RECURSE ${CMAKE_BINARY_DIR}/_deps/${lower_case_name}-subbuild)
set(PACKAGE_INFO "${PACKAGE_INFO} -> ${download_directory}")
endif()
elseif(DEFINED CPM_ARGS_SOURCE_DIR)
set(FETCH_CONTENT_DECLARE_EXTRA_OPTS SOURCE_DIR ${CPM_ARGS_SOURCE_DIR})
elseif (CPM_SOURCE_CACHE)
string(TOLOWER ${CPM_ARGS_NAME} lower_case_name)
set(origin_parameters ${CPM_ARGS_UNPARSED_ARGUMENTS})
list(SORT origin_parameters)
string(SHA1 origin_hash "${origin_parameters}")
set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash})
list(APPEND FETCH_CONTENT_DECLARE_EXTRA_OPTS SOURCE_DIR ${download_directory})
if (EXISTS ${download_directory})
list(APPEND FETCH_CONTENT_DECLARE_EXTRA_OPTS DOWNLOAD_COMMAND ":")
set(PACKAGE_INFO "${download_directory}")
else()
# remove timestamps so CMake will re-download the dependency
file(REMOVE_RECURSE ${CMAKE_BINARY_DIR}/_deps/${lower_case_name}-subbuild)
set(PACKAGE_INFO "${PACKAGE_INFO} -> ${download_directory}")
endif()
endif()

Expand Down
19 changes: 19 additions & 0 deletions test/unit/source_dir.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

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

set(TEST_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/source_dir)

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

execute_process(
COMMAND
${CMAKE_COMMAND} "-H${CMAKE_CURRENT_LIST_DIR}/source_dir" "-B${TEST_BUILD_DIR}"
RESULT_VARIABLE ret
)

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

project(CPMExampleCatch2)

# ---- Options ----

option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF)

# ---- Dependencies ----

include(@CPM_PATH@/CPM.cmake)

CPMAddPackage(
NAME Dependency
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/dependency
)

# ---- Call dependency method to validate correct addition of directory ----

dependency_function()
4 changes: 4 additions & 0 deletions test/unit/source_dir/dependency/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

function(dependency_function)
message("called external method")
endfunction()

0 comments on commit 01b69aa

Please sign in to comment.