Pārlūkot izejas kodu

file(RPATH_CHANGE ...): no-op for static binary

Setting `CMAKE_INSTALL_RPATH` on a project that contains statically
linked executables breaks the install step, since rpaths cannot be
changed.

This commit makes rpath adjustments not an error on static libraries.

Fixes #26078
Harmen Stoppels 1 gadu atpakaļ
vecāks
revīzija
02f3e5be6a

+ 9 - 1
Source/cmELF.cxx

@@ -112,6 +112,9 @@ public:
   virtual bool IsMips() const = 0;
   virtual bool IsMips() const = 0;
   virtual void PrintInfo(std::ostream& os) const = 0;
   virtual void PrintInfo(std::ostream& os) const = 0;
 
 
+  /** Returns true if the ELF file has a dynamic section **/
+  bool HasDynamicSection() const { return this->DynamicSectionIndex >= 0; }
+
   // Lookup the SONAME in the DYNAMIC section.
   // Lookup the SONAME in the DYNAMIC section.
   StringEntry const* GetSOName()
   StringEntry const* GetSOName()
   {
   {
@@ -461,7 +464,7 @@ template <class Types>
 bool cmELFInternalImpl<Types>::LoadDynamicSection()
 bool cmELFInternalImpl<Types>::LoadDynamicSection()
 {
 {
   // If there is no dynamic section we are done.
   // If there is no dynamic section we are done.
-  if (this->DynamicSectionIndex < 0) {
+  if (!this->HasDynamicSection()) {
     return false;
     return false;
   }
   }
 
 
@@ -772,6 +775,11 @@ std::vector<char> cmELF::EncodeDynamicEntries(
   return std::vector<char>();
   return std::vector<char>();
 }
 }
 
 
+bool cmELF::HasDynamicSection() const
+{
+  return this->Valid() && this->Internal->HasDynamicSection();
+}
+
 bool cmELF::GetSOName(std::string& soname)
 bool cmELF::GetSOName(std::string& soname)
 {
 {
   if (StringEntry const* se = this->GetSOName()) {
   if (StringEntry const* se = this->GetSOName()) {

+ 3 - 0
Source/cmELF.h

@@ -88,6 +88,9 @@ public:
   std::vector<char> EncodeDynamicEntries(
   std::vector<char> EncodeDynamicEntries(
     const DynamicEntryList& entries) const;
     const DynamicEntryList& entries) const;
 
 
+  /** Returns true if the ELF file has a dynamic section **/
+  bool HasDynamicSection() const;
+
   /** Get the SONAME field if any.  */
   /** Get the SONAME field if any.  */
   bool GetSOName(std::string& soname);
   bool GetSOName(std::string& soname);
   StringEntry const* GetSOName();
   StringEntry const* GetSOName();

+ 4 - 0
Source/cmSystemTools.cxx

@@ -2817,6 +2817,10 @@ cm::optional<bool> AdjustRPathELF(std::string const& file,
       return cm::nullopt; // Not a valid ELF file.
       return cm::nullopt; // Not a valid ELF file.
     }
     }
 
 
+    if (!elf.HasDynamicSection()) {
+      return true; // No dynamic section to update.
+    }
+
     // Get the RPATH and RUNPATH entries from it.
     // Get the RPATH and RUNPATH entries from it.
     int se_count = 0;
     int se_count = 0;
     cmELF::StringEntry const* se[2] = { nullptr, nullptr };
     cmELF::StringEntry const* se[2] = { nullptr, nullptr };

+ 3 - 0
Tests/RunCMake/file-RPATH/ELF.cmake

@@ -4,6 +4,9 @@ set(dynamic
   elf64lsb.bin
   elf64lsb.bin
   elf64msb.bin
   elf64msb.bin
   )
   )
+set(static
+  elf64lsb-static.bin
+  )
 set(format ELF)
 set(format ELF)
 
 
 include(${CMAKE_CURRENT_LIST_DIR}/Common.cmake)
 include(${CMAKE_CURRENT_LIST_DIR}/Common.cmake)

BIN
Tests/RunCMake/file-RPATH/ELF/elf64lsb-static.bin