Skip to content

Commit

Permalink
Add detect_gnu_libc() and detect_musl_libc() functions
Browse files Browse the repository at this point in the history
This PR adds a simple function to detect the version of glibc to the detect_api.
This functionality is only intended for Linux systems using glibc.
The function is detect_gnu_libc().
It simply runs ldd --version to detect the version of glibc.
It is also possible to detect the version of the libc library by running the libc.so file.
The location of this library varies much more than the path to ldd, so ldd --version is used instead.
The path to ldd is passed as an argument, /usr/bin/ldd by default.
This can modified to suite alternate sysroots.
It can even be used in cross-compilation scenarios where emulation is available to execute ldd.

The detect_gnu_libc function does a simple sanity check to verify that glibc is indeed being used.
This avoids confusion on systems using an alternative libc implementation, i.e. musl.

I also added a function to detect the version of the musl libc library as well.
This function is detect_musl_libc() and works the same way.

Addresses #3972, at least in part.
  • Loading branch information
jwillikers committed Feb 15, 2024
1 parent 953035a commit f0f11d1
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion conan/internal/api/detect_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,46 @@ def _get_e2k_architecture():
}.get(platform.processor())


def detect_gnu_libc(ldd="/usr/bin/ldd"):
if platform.system() != "Linux":
ConanOutput(scope="detect_api").warning("detect_gnu_libc() only works on Linux")
return None
try:
first_line = check_output_runner(f"{ldd} --version").partition("\n")[0]
if "GNU libc" not in first_line:
ConanOutput(scope="detect_api").warning(
f"detect_gnu_libc() did not detect glibc in the first line of output from '{ldd} --version': '{first_line}'"
)
return None
return first_line.split()[-1].strip()
except Exception as e:
ConanOutput(scope="detect_api").warning(
f"Couldn't get the glibc version from the '{ldd} --version' command {e}"
)
return None


def detect_musl_libc(ldd="/usr/bin/ldd"):
if platform.system() != "Linux":
ConanOutput(scope="detect_api").warning(
"detect_musl_libc() only works on Linux"
)
return None
try:
lines = check_output_runner(f"{ldd} --version").partition("\n")
if "musl libc" not in lines[0]:
ConanOutput(scope="detect_api").warning(
f"detect_musl_libc() did not detect musl libc in the first line of output from '{ldd} --version': '{lines[0]}'"
)
return None
return lines[1].split()[-1].strip()
except Exception as e:
ConanOutput(scope="detect_api").warning(
f"Couldn't get the musl libc version from the '{ldd} --version' command {e}"
)
return None


def detect_libcxx(compiler, version, compiler_exe=None):
assert isinstance(version, Version)

Expand Down Expand Up @@ -186,7 +226,7 @@ def _detect_gcc_libcxx(version_, executable):
elif compiler == "mcst-lcc":
return "libstdc++"
elif compiler == "intel-cc":
return "libstdc++11"
return "libstdc++11"


def default_msvc_runtime(compiler):
Expand Down

0 comments on commit f0f11d1

Please sign in to comment.