[compiler-rt] 0c11478 - [compiler-rt] Stop using system ldd to detect libc version

Alex Brachet via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 26 06:41:27 PDT 2023


Author: Alex Brachet
Date: 2023-06-26T13:41:19Z
New Revision: 0c11478b791a8f5845c5c819317d5a77b139f8c7

URL: https://github.com/llvm/llvm-project/commit/0c11478b791a8f5845c5c819317d5a77b139f8c7
DIFF: https://github.com/llvm/llvm-project/commit/0c11478b791a8f5845c5c819317d5a77b139f8c7.diff

LOG: [compiler-rt] Stop using system ldd to detect libc version

The system libc may be different from the libc passed in
CMAKE_SYSROOT. Instead of using the ldd in PATH to detect
glibc version, use the features.h header file.

Differential Revision: https://reviews.llvm.org/D151678

Added: 
    

Modified: 
    compiler-rt/test/lit.common.cfg.py

Removed: 
    


################################################################################
diff  --git a/compiler-rt/test/lit.common.cfg.py b/compiler-rt/test/lit.common.cfg.py
index aeda48f5e8132..24473f38b9a4c 100644
--- a/compiler-rt/test/lit.common.cfg.py
+++ b/compiler-rt/test/lit.common.cfg.py
@@ -584,20 +584,13 @@ def get_macos_aligned_version(macos_vers):
     config.substitutions.append(("%adb_shell", "echo "))
 
 if config.host_os == "Linux":
-    # detect whether we are using glibc, and which version
-    # NB: 'ldd' is just one of the tools commonly installed as part of glibc/musl
-    ldd_ver_cmd = subprocess.Popen(
-        ["ldd", "--version"],
-        stdout=subprocess.PIPE,
-        stderr=subprocess.DEVNULL,
-        env={"LANG": "C"},
-    )
-    sout, _ = ldd_ver_cmd.communicate()
-    ver_lines = sout.splitlines()
-    if not config.android and len(ver_lines) and ver_lines[0].startswith(b"ldd "):
+    def add_glibc_versions(ver_string):
+        if config.android:
+            return
+
         from distutils.version import LooseVersion
 
-        ver = LooseVersion(ver_lines[0].split()[-1].decode())
+        ver = LooseVersion(ver_string)
         any_glibc = False
         for required in ["2.19", "2.27", "2.30", "2.34", "2.37"]:
             if ver >= LooseVersion(required):
@@ -606,6 +599,31 @@ def get_macos_aligned_version(macos_vers):
             if any_glibc:
                 config.available_features.add("glibc")
 
+    # detect whether we are using glibc, and which version
+    cmd_args = [
+        config.clang.strip(),
+        f"--target={config.target_triple}",
+        "-xc",
+        "-",
+        "-o",
+        "-",
+        "-dM",
+        "-E",
+    ] + shlex.split(config.target_cflags)
+    cmd = subprocess.Popen(
+        cmd_args,
+        stdout=subprocess.PIPE,
+        stdin=subprocess.PIPE,
+        stderr=subprocess.DEVNULL,
+        env={"LANG": "C"},
+    )
+    try:
+        sout, _ = cmd.communicate(b"#include <features.h>")
+        m = dict(re.findall(r"#define (__GLIBC__|__GLIBC_MINOR__) (\d+)", str(sout)))
+        add_glibc_versions(f"{m['__GLIBC__']}.{m['__GLIBC_MINOR__']}")
+    except:
+        pass
+
 sancovcc_path = os.path.join(config.llvm_tools_dir, "sancov")
 if os.path.exists(sancovcc_path):
     config.available_features.add("has_sancovcc")


        


More information about the llvm-commits mailing list