From ede60451a91bec12e889338a13d2a336ae138648 Mon Sep 17 00:00:00 2001 From: Thomas Mosegaard Pedersen Date: Tue, 26 Jul 2022 09:15:11 +0200 Subject: [PATCH] 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 --- cmake/CPM.cmake | 14 +++++++++ test/integration/test_remove_source_dir.rb | 36 ++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 test/integration/test_remove_source_dir.rb diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index 9b47e70..6271991 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -650,6 +650,20 @@ function(CPMAddPackage) list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS DOWNLOAD_COMMAND ${CPM_ARGS_DOWNLOAD_COMMAND}) elseif(DEFINED CPM_ARGS_SOURCE_DIR) list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS SOURCE_DIR ${CPM_ARGS_SOURCE_DIR}) + if(NOT IS_ABSOLUTE ${CPM_ARGS_SOURCE_DIR}) + # Expand `CPM_ARGS_SOURCE_DIR` relative path. This is important because EXISTS doesn't work + # for relative paths. + get_filename_component( + source_directory ${CPM_ARGS_SOURCE_DIR} REALPATH BASE_DIR ${CMAKE_CURRENT_BINARY_DIR} + ) + else() + set(source_directory ${CPM_ARGS_SOURCE_DIR}) + endif() + if(NOT EXISTS ${source_directory}) + string(TOLOWER ${CPM_ARGS_NAME} lower_case_name) + # remove timestamps so CMake will re-download the dependency + file(REMOVE_RECURSE "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-subbuild") + endif() elseif(CPM_SOURCE_CACHE AND NOT CPM_ARGS_NO_CACHE) string(TOLOWER ${CPM_ARGS_NAME} lower_case_name) set(origin_parameters ${CPM_ARGS_UNPARSED_ARGUMENTS}) diff --git a/test/integration/test_remove_source_dir.rb b/test/integration/test_remove_source_dir.rb new file mode 100644 index 0000000..852c12d --- /dev/null +++ b/test/integration/test_remove_source_dir.rb @@ -0,0 +1,36 @@ +require_relative './lib' + +class RemoveSourceDir < IntegrationTest + def test_remove_source_dir + prj = make_project 'using-adder' + + prj.create_lists_from_default_template package: <<~PACK + CPMAddPackage( + NAME testpack-adder + GITHUB_REPOSITORY cpm-cmake/testpack-adder + VERSION 1.0.0 + OPTIONS "ADDER_BUILD_TESTS OFF" + SOURCE_DIR testpack-adder + ) + PACK + + # configure and build + assert_success prj.configure + assert_success prj.build + + # source_dir is populated + assert_true File.exist?(File.join(prj.bin_dir, 'testpack-adder')) + + # source_dir is deleted by user + FileUtils.remove_dir(File.join(prj.bin_dir, 'testpack-adder'), true) + assert_false File.exist?(File.join(prj.bin_dir, 'testpack-adder')) + + # configure and build with missing source_dir to fetch new content + assert_success prj.configure + assert_success prj.build + + # source_dir is populated + assert_true File.exist?(File.join(prj.bin_dir, 'testpack-adder')) + end + +end