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
This commit is contained in:
Brad King
2021-12-01 12:36:07 -05:00
parent 5596cba7dc
commit 643fc46bdc
13 changed files with 85 additions and 2 deletions

View File

@@ -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<bool> 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)

View File

@@ -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)

View File

@@ -0,0 +1 @@
1

View File

@@ -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\.$

View File

@@ -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")

View File

@@ -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 "")

View File

@@ -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()

View File

@@ -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()

View File

@@ -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}")

View File

@@ -0,0 +1 @@
1

View File

@@ -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\.$

View File

@@ -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")

View File

@@ -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 "")