Skip to content

Commit ede6045

Browse files
authored
CPMAddPackage fails if the SOURCE_DIR directory is deleted. (#370)
* Fixed: Deleted SOURCE_DIR directory would abort just after git stash save --quiet;--include-untracked * Fixed: Review comments * Added: Integration test for deleted SOURCE_DIR with FetchContent * Fixed: Review comments * Fixed: Review comments
1 parent 5961f9f commit ede6045

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

cmake/CPM.cmake

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,20 @@ function(CPMAddPackage)
650650
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS DOWNLOAD_COMMAND ${CPM_ARGS_DOWNLOAD_COMMAND})
651651
elseif(DEFINED CPM_ARGS_SOURCE_DIR)
652652
list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS SOURCE_DIR ${CPM_ARGS_SOURCE_DIR})
653+
if(NOT IS_ABSOLUTE ${CPM_ARGS_SOURCE_DIR})
654+
# Expand `CPM_ARGS_SOURCE_DIR` relative path. This is important because EXISTS doesn't work
655+
# for relative paths.
656+
get_filename_component(
657+
source_directory ${CPM_ARGS_SOURCE_DIR} REALPATH BASE_DIR ${CMAKE_CURRENT_BINARY_DIR}
658+
)
659+
else()
660+
set(source_directory ${CPM_ARGS_SOURCE_DIR})
661+
endif()
662+
if(NOT EXISTS ${source_directory})
663+
string(TOLOWER ${CPM_ARGS_NAME} lower_case_name)
664+
# remove timestamps so CMake will re-download the dependency
665+
file(REMOVE_RECURSE "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-subbuild")
666+
endif()
653667
elseif(CPM_SOURCE_CACHE AND NOT CPM_ARGS_NO_CACHE)
654668
string(TOLOWER ${CPM_ARGS_NAME} lower_case_name)
655669
set(origin_parameters ${CPM_ARGS_UNPARSED_ARGUMENTS})
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require_relative './lib'
2+
3+
class RemoveSourceDir < IntegrationTest
4+
def test_remove_source_dir
5+
prj = make_project 'using-adder'
6+
7+
prj.create_lists_from_default_template package: <<~PACK
8+
CPMAddPackage(
9+
NAME testpack-adder
10+
GITHUB_REPOSITORY cpm-cmake/testpack-adder
11+
VERSION 1.0.0
12+
OPTIONS "ADDER_BUILD_TESTS OFF"
13+
SOURCE_DIR testpack-adder
14+
)
15+
PACK
16+
17+
# configure and build
18+
assert_success prj.configure
19+
assert_success prj.build
20+
21+
# source_dir is populated
22+
assert_true File.exist?(File.join(prj.bin_dir, 'testpack-adder'))
23+
24+
# source_dir is deleted by user
25+
FileUtils.remove_dir(File.join(prj.bin_dir, 'testpack-adder'), true)
26+
assert_false File.exist?(File.join(prj.bin_dir, 'testpack-adder'))
27+
28+
# configure and build with missing source_dir to fetch new content
29+
assert_success prj.configure
30+
assert_success prj.build
31+
32+
# source_dir is populated
33+
assert_true File.exist?(File.join(prj.bin_dir, 'testpack-adder'))
34+
end
35+
36+
end

0 commit comments

Comments
 (0)