From 643fc46bdc13dd57584d5d77eee30a99308ba896 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 1 Dec 2021 12:36:07 -0500 Subject: file(RPATH): Restore tolerance of unknown formats if new RPATH is empty Since commit 2e1149874d (cmSystemTools: Support multiple binary formats, 2021-06-14, v3.22.0-rc1~575^2) the `file(RPATH_...)` operations fail on files that are not ELF or XCOFF format. Previously the RPATH operations tolerated files of unknown format if the goal was to produce a file with an empty RPATH. Restore this tolerance in order to support setting an empty RPATH on GNU ld scripts. Fixes: #22963 --- Source/cmSystemTools.cxx | 23 +++++++++++++++++++++-- Tests/RunCMake/file-RPATH/RunCMakeTest.cmake | 11 +++++++++++ Tests/RunCMake/file-RPATH/TextChange-result.txt | 1 + Tests/RunCMake/file-RPATH/TextChange-stderr.txt | 12 ++++++++++++ Tests/RunCMake/file-RPATH/TextChange.cmake | 3 +++ Tests/RunCMake/file-RPATH/TextChangeEmpty.cmake | 3 +++ Tests/RunCMake/file-RPATH/TextCheck.cmake | 6 ++++++ Tests/RunCMake/file-RPATH/TextCheckEmpty.cmake | 6 ++++++ Tests/RunCMake/file-RPATH/TextRemove.cmake | 3 +++ Tests/RunCMake/file-RPATH/TextSet-result.txt | 1 + Tests/RunCMake/file-RPATH/TextSet-stderr.txt | 12 ++++++++++++ Tests/RunCMake/file-RPATH/TextSet.cmake | 3 +++ Tests/RunCMake/file-RPATH/TextSetEmpty.cmake | 3 +++ 13 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 Tests/RunCMake/file-RPATH/TextChange-result.txt create mode 100644 Tests/RunCMake/file-RPATH/TextChange-stderr.txt create mode 100644 Tests/RunCMake/file-RPATH/TextChange.cmake create mode 100644 Tests/RunCMake/file-RPATH/TextChangeEmpty.cmake create mode 100644 Tests/RunCMake/file-RPATH/TextCheck.cmake create mode 100644 Tests/RunCMake/file-RPATH/TextCheckEmpty.cmake create mode 100644 Tests/RunCMake/file-RPATH/TextRemove.cmake create mode 100644 Tests/RunCMake/file-RPATH/TextSet-result.txt create mode 100644 Tests/RunCMake/file-RPATH/TextSet-stderr.txt create mode 100644 Tests/RunCMake/file-RPATH/TextSet.cmake create mode 100644 Tests/RunCMake/file-RPATH/TextSetEmpty.cmake diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 6320be8..1934393 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -2869,6 +2869,14 @@ bool cmSystemTools::ChangeRPath(std::string const& file, file, oldRPath, newRPath, removeEnvironmentRPath, emsg, changed)) { return result.value(); } + // The file format is not recognized. Assume it has no RPATH. + if (newRPath.empty()) { + // The caller wanted no RPATH anyway. + return true; + } + if (emsg) { + *emsg = "The file format is not recognized."; + } return false; } @@ -2883,6 +2891,14 @@ bool cmSystemTools::SetRPath(std::string const& file, SetRPathXCOFF(file, newRPath, emsg, changed)) { return result.value(); } + // The file format is not recognized. Assume it has no RPATH. + if (newRPath.empty()) { + // The caller wanted no RPATH anyway. + return true; + } + if (emsg) { + *emsg = "The file format is not recognized."; + } return false; } @@ -3212,7 +3228,8 @@ bool cmSystemTools::RemoveRPath(std::string const& file, std::string* emsg, if (cm::optional result = RemoveRPathXCOFF(file, emsg, removed)) { return result.value(); } - return false; + // The file format is not recognized. Assume it has no RPATH. + return true; } bool cmSystemTools::CheckRPath(std::string const& file, @@ -3252,7 +3269,9 @@ bool cmSystemTools::CheckRPath(std::string const& file, return false; } #endif - return false; + // The file format is not recognized. Assume it has no RPATH. + // Therefore we succeed if the new rpath is empty anyway. + return newRPath.empty(); } bool cmSystemTools::RepeatedRemoveDirectory(const std::string& dir) diff --git a/Tests/RunCMake/file-RPATH/RunCMakeTest.cmake b/Tests/RunCMake/file-RPATH/RunCMakeTest.cmake index 1ca2e75..525df09 100644 --- a/Tests/RunCMake/file-RPATH/RunCMakeTest.cmake +++ b/Tests/RunCMake/file-RPATH/RunCMakeTest.cmake @@ -5,3 +5,14 @@ run_cmake_command(ELF ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR}/ELF.cmake) if(CMAKE_SYSTEM_NAME STREQUAL "AIX") run_cmake_command(XCOFF ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR}/XCOFF.cmake) endif() + +run_cmake_command(TextCheck ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR}/TextCheck.cmake) +run_cmake_command(TextCheckEmpty ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR}/TextCheckEmpty.cmake) + +run_cmake_command(TextChange ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR}/TextChange.cmake) +run_cmake_command(TextChangeEmpty ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR}/TextChangeEmpty.cmake) + +run_cmake_command(TextSet ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR}/TextSet.cmake) +run_cmake_command(TextSetEmpty ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR}/TextSetEmpty.cmake) + +run_cmake_command(TextRemove ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR}/TextRemove.cmake) diff --git a/Tests/RunCMake/file-RPATH/TextChange-result.txt b/Tests/RunCMake/file-RPATH/TextChange-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/file-RPATH/TextChange-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/file-RPATH/TextChange-stderr.txt b/Tests/RunCMake/file-RPATH/TextChange-stderr.txt new file mode 100644 index 0000000..48a1bf5 --- /dev/null +++ b/Tests/RunCMake/file-RPATH/TextChange-stderr.txt @@ -0,0 +1,12 @@ +^CMake Error at [^ +]*/Tests/RunCMake/file-RPATH/TextChange.cmake:[0-9]+ \(file\): + file RPATH_CHANGE could not write new RPATH: + + /new/rpath + + to the file: + + [^ +]*/Tests/RunCMake/file-RPATH/TextChange-build/not_a_binary.txt + + The file format is not recognized\.$ diff --git a/Tests/RunCMake/file-RPATH/TextChange.cmake b/Tests/RunCMake/file-RPATH/TextChange.cmake new file mode 100644 index 0000000..5c599ec --- /dev/null +++ b/Tests/RunCMake/file-RPATH/TextChange.cmake @@ -0,0 +1,3 @@ +set(f "${CMAKE_CURRENT_BINARY_DIR}/not_a_binary.txt") +file(WRITE "${f}" "Not a binary.\n") +file(RPATH_CHANGE FILE "${f}" OLD_RPATH "/old/rpath" NEW_RPATH "/new/rpath") diff --git a/Tests/RunCMake/file-RPATH/TextChangeEmpty.cmake b/Tests/RunCMake/file-RPATH/TextChangeEmpty.cmake new file mode 100644 index 0000000..8f855d9 --- /dev/null +++ b/Tests/RunCMake/file-RPATH/TextChangeEmpty.cmake @@ -0,0 +1,3 @@ +set(f "${CMAKE_CURRENT_BINARY_DIR}/not_a_binary.txt") +file(WRITE "${f}" "Not a binary.\n") +file(RPATH_CHANGE FILE "${f}" OLD_RPATH "/old/rpath" NEW_RPATH "") diff --git a/Tests/RunCMake/file-RPATH/TextCheck.cmake b/Tests/RunCMake/file-RPATH/TextCheck.cmake new file mode 100644 index 0000000..1f21ba7 --- /dev/null +++ b/Tests/RunCMake/file-RPATH/TextCheck.cmake @@ -0,0 +1,6 @@ +set(f "${CMAKE_CURRENT_BINARY_DIR}/not_a_binary.txt") +file(WRITE "${f}" "Not a binary.\n") +file(RPATH_CHECK FILE "${f}" RPATH "/some/rpath") +if(EXISTS "${f}") + message(FATAL_ERROR "RPATH_CHECK did not remove\n ${f}\nfor non-empty RPATH") +endif() diff --git a/Tests/RunCMake/file-RPATH/TextCheckEmpty.cmake b/Tests/RunCMake/file-RPATH/TextCheckEmpty.cmake new file mode 100644 index 0000000..806c73d --- /dev/null +++ b/Tests/RunCMake/file-RPATH/TextCheckEmpty.cmake @@ -0,0 +1,6 @@ +set(f "${CMAKE_CURRENT_BINARY_DIR}/not_a_binary.txt") +file(WRITE "${f}" "Not a binary.\n") +file(RPATH_CHECK FILE "${f}" RPATH "") +if(NOT EXISTS "${f}") + message(FATAL_ERROR "RPATH_CHECK removed\n ${f}\nfor empty RPATH") +endif() diff --git a/Tests/RunCMake/file-RPATH/TextRemove.cmake b/Tests/RunCMake/file-RPATH/TextRemove.cmake new file mode 100644 index 0000000..aecaf6f --- /dev/null +++ b/Tests/RunCMake/file-RPATH/TextRemove.cmake @@ -0,0 +1,3 @@ +set(f "${CMAKE_CURRENT_BINARY_DIR}/not_a_binary.txt") +file(WRITE "${f}" "Not a binary.\n") +file(RPATH_REMOVE FILE "${f}") diff --git a/Tests/RunCMake/file-RPATH/TextSet-result.txt b/Tests/RunCMake/file-RPATH/TextSet-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/file-RPATH/TextSet-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/file-RPATH/TextSet-stderr.txt b/Tests/RunCMake/file-RPATH/TextSet-stderr.txt new file mode 100644 index 0000000..811c9a5 --- /dev/null +++ b/Tests/RunCMake/file-RPATH/TextSet-stderr.txt @@ -0,0 +1,12 @@ +^CMake Error at [^ +]*/Tests/RunCMake/file-RPATH/TextSet.cmake:[0-9]+ \(file\): + file RPATH_SET could not write new RPATH: + + /new/rpath + + to the file: + + [^ +]*/Tests/RunCMake/file-RPATH/TextSet-build/not_a_binary.txt + + The file format is not recognized\.$ diff --git a/Tests/RunCMake/file-RPATH/TextSet.cmake b/Tests/RunCMake/file-RPATH/TextSet.cmake new file mode 100644 index 0000000..f4b09d2 --- /dev/null +++ b/Tests/RunCMake/file-RPATH/TextSet.cmake @@ -0,0 +1,3 @@ +set(f "${CMAKE_CURRENT_BINARY_DIR}/not_a_binary.txt") +file(WRITE "${f}" "Not a binary.\n") +file(RPATH_SET FILE "${f}" NEW_RPATH "/new/rpath") diff --git a/Tests/RunCMake/file-RPATH/TextSetEmpty.cmake b/Tests/RunCMake/file-RPATH/TextSetEmpty.cmake new file mode 100644 index 0000000..d9517e0 --- /dev/null +++ b/Tests/RunCMake/file-RPATH/TextSetEmpty.cmake @@ -0,0 +1,3 @@ +set(f "${CMAKE_CURRENT_BINARY_DIR}/not_a_binary.txt") +file(WRITE "${f}" "Not a binary.\n") +file(RPATH_SET FILE "${f}" NEW_RPATH "") -- cgit v0.12