From 5799d0e78879ee60a4efa3ecbeae1076d2bef15f Mon Sep 17 00:00:00 2001 From: scivision Date: Mon, 13 Nov 2023 11:48:03 -0500 Subject: cmake: Improve error message if CMAKE_BINARY_DIR not writable Fixes: #24407 --- Source/cmake.cxx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 942c59b..002a020 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -2397,8 +2397,15 @@ int cmake::ActualConfigure() cmSystemTools::RemoveADirectory(redirectsDir); if (!cmSystemTools::MakeDirectory(redirectsDir)) { cmSystemTools::Error( - "Unable to (re)create the private pkgRedirects directory:\n" + - redirectsDir); + cmStrCat("Unable to (re)create the private pkgRedirects directory:\n ", + redirectsDir, + "\n" + "This may be caused by not having read/write access to " + "the build directory.\n" + "Try specifying a location with read/write access like:\n" + " cmake -B build\n" + "If using a CMake presets file, ensure that preset parameter\n" + "'binaryDir' expands to a writable directory.\n")); return -1; } this->AddCacheEntry("CMAKE_FIND_PACKAGE_REDIRECTS_DIR", redirectsDir, -- cgit v0.12 From d91c02e40ff515fa2f5765bc2bea55b3c2da713c Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 30 Nov 2023 17:04:37 -0500 Subject: Tests: Factor out RunCMake helper to get UNIX user id --- Tests/RunCMake/RunCMake.cmake | 16 ++++++++++++++++ Tests/RunCMake/if/RunCMakeTest.cmake | 15 +++------------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/Tests/RunCMake/RunCMake.cmake b/Tests/RunCMake/RunCMake.cmake index fcf904e..179c80e 100644 --- a/Tests/RunCMake/RunCMake.cmake +++ b/Tests/RunCMake/RunCMake.cmake @@ -323,5 +323,21 @@ function(ensure_files_match expected_file actual_file) endif() endfunction() +# Get the user id on unix if possible. +function(get_unix_uid var) + set("${var}" "" PARENT_SCOPE) + if(UNIX) + set(ID "id") + if(CMAKE_SYSTEM_NAME STREQUAL "SunOS" AND EXISTS "/usr/xpg4/bin/id") + set (ID "/usr/xpg4/bin/id") + endif() + execute_process(COMMAND ${ID} -u $ENV{USER} OUTPUT_VARIABLE uid ERROR_QUIET + RESULT_VARIABLE status OUTPUT_STRIP_TRAILING_WHITESPACE) + if(status EQUAL 0) + set("${var}" "${uid}" PARENT_SCOPE) + endif() + endif() +endfunction() + # Protect RunCMake tests from calling environment. unset(ENV{MAKEFLAGS}) diff --git a/Tests/RunCMake/if/RunCMakeTest.cmake b/Tests/RunCMake/if/RunCMakeTest.cmake index 0bfff90..43dfd3c 100644 --- a/Tests/RunCMake/if/RunCMakeTest.cmake +++ b/Tests/RunCMake/if/RunCMakeTest.cmake @@ -4,18 +4,9 @@ run_cmake(InvalidArgument1) run_cmake(exists) if(NOT MSYS) # permissions and symbolic links are broken on MSYS - unset(uid) - unset(status) - if(UNIX) - set(ID "id") - if (CMAKE_SYSTEM_NAME STREQUAL "SunOS" AND EXISTS "/usr/xpg4/bin/id") - set (ID "/usr/xpg4/bin/id") - endif() - # if real user is root, tests are irrelevant - execute_process(COMMAND ${ID} -u $ENV{USER} OUTPUT_VARIABLE uid ERROR_QUIET - RESULT_VARIABLE status OUTPUT_STRIP_TRAILING_WHITESPACE) - endif() - if(NOT status AND NOT uid STREQUAL "0") + # if real user is root, tests are irrelevant + get_unix_uid(uid) + if(NOT uid STREQUAL "0") run_cmake(FilePermissions) endif() endif() -- cgit v0.12 From 0fd64345fe445797f5b2d89c1371e421d915dc9b Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 30 Nov 2023 14:20:32 -0500 Subject: Tests: Add case covering non-writable CMAKE_BINARY_DIR Issue: #24407 --- Tests/RunCMake/Configure/ReadOnly-result.txt | 1 + Tests/RunCMake/Configure/ReadOnly-stderr.txt | 8 ++++++++ Tests/RunCMake/Configure/ReadOnly.cmake | 1 + Tests/RunCMake/Configure/RunCMakeTest.cmake | 16 ++++++++++++++++ 4 files changed, 26 insertions(+) create mode 100644 Tests/RunCMake/Configure/ReadOnly-result.txt create mode 100644 Tests/RunCMake/Configure/ReadOnly-stderr.txt create mode 100644 Tests/RunCMake/Configure/ReadOnly.cmake diff --git a/Tests/RunCMake/Configure/ReadOnly-result.txt b/Tests/RunCMake/Configure/ReadOnly-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/Configure/ReadOnly-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/Configure/ReadOnly-stderr.txt b/Tests/RunCMake/Configure/ReadOnly-stderr.txt new file mode 100644 index 0000000..fb00ae9 --- /dev/null +++ b/Tests/RunCMake/Configure/ReadOnly-stderr.txt @@ -0,0 +1,8 @@ +CMake Error: Unable to \(re\)create the private pkgRedirects directory: + [^ +]*/Tests/RunCMake/Configure/ReadOnly-build/CMakeFiles/pkgRedirects +This may be caused by not having read/write access to the build directory. +Try specifying a location with read/write access like: + cmake -B build +If using a CMake presets file, ensure that preset parameter +'binaryDir' expands to a writable directory. diff --git a/Tests/RunCMake/Configure/ReadOnly.cmake b/Tests/RunCMake/Configure/ReadOnly.cmake new file mode 100644 index 0000000..2fc38e5 --- /dev/null +++ b/Tests/RunCMake/Configure/ReadOnly.cmake @@ -0,0 +1 @@ +message(FATAL_ERROR "This should not be reached!") diff --git a/Tests/RunCMake/Configure/RunCMakeTest.cmake b/Tests/RunCMake/Configure/RunCMakeTest.cmake index df6849e..842a005 100644 --- a/Tests/RunCMake/Configure/RunCMakeTest.cmake +++ b/Tests/RunCMake/Configure/RunCMakeTest.cmake @@ -55,3 +55,19 @@ if(NOT RunCMake_GENERATOR MATCHES "^Ninja Multi-Config$") run_cmake(NoCMAKE_DEFAULT_BUILD_TYPE) run_cmake(NoCMAKE_DEFAULT_CONFIGS) endif() + +if(NOT CMAKE_HOST_WIN32) + block() + # Test a non-writable build directory. + # Exclude when running as root because directories are always writable. + get_unix_uid(uid) + if(NOT uid STREQUAL "0") + set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/ReadOnly-build) + file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}") + file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}") + file(CHMOD "${RunCMake_TEST_BINARY_DIR}" PERMISSIONS OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) + set(RunCMake_TEST_NO_CLEAN 1) + run_cmake(ReadOnly) + endif() + endblock() +endif() -- cgit v0.12