Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add detect_gnu_libc() and detect_musl_libc() functions #15683

Merged
merged 7 commits into from
Feb 19, 2024

Conversation

jwillikers
Copy link
Contributor

@jwillikers jwillikers commented Feb 15, 2024

Changelog: Feature: Add detect_libc to the detect_api to get the name and version of the C library.
Docs: conan-io/docs#3590

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.

  • Refer to the issue that supports this Pull Request.
  • If the issue has missing info, explain the purpose/use case/pain/need that covers this Pull Request.
  • I've read the Contributing guide.
  • I've followed the PEP8 style guides for Python code.
  • I've opened another PR in the Conan docs repo to the develop branch, documenting this one.

@CLAassistant
Copy link

CLAassistant commented Feb 15, 2024

CLA assistant check
All committers have signed the CLA.

jwillikers added a commit to jwillikers/docs that referenced this pull request Feb 15, 2024
@jwillikers
Copy link
Contributor Author

Not sure the best way to add some automated unit tests for these functions, particularly since they are only applicable under Linux and need to run on a Linux system, unless I mock out ldd, which would probably be fine for unit tests.

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 conan-io#3972, at least in part.
Copy link
Member

@memsharded memsharded left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much for your contribution @jwillikers

It is true that testing these is typically not easy.

I would test it very similarly to TestProfileDetectAPI in "functional" tests, without a strict validation of the specific return version, just checking that it doesn't crash and return a version is fine. For the musl that will not be in CI, checking that it returns None without crashing would be enough, as long as you test it locally and works for you.

return None


def detect_musl_libc(ldd="/usr/bin/ldd"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functionally looks good, but it would be necessary to understand the usage patterns, which can be made explicit in a test.

Maybe it is better just a single function that returns libc, version = detect_api.detect_libc(), so libc gets the value glibc or musl?

Copy link
Contributor Author

@jwillikers jwillikers Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's definitely possible and seems useful. I think it might imply the scope applies to more than just Linux, however, since macOS, BSD*, and Windows are all going to have their own libc implementation. I guess the functionality could always be expanded in the future, but it is quite nice to be able to pass ldd as an argument, too. I'll work on getting the tests sorted out tomorrow.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly, I think it can be expanded in the future in different forms like detect_api.detect_osx_libc(), via a new argument, or just by adding functionality if we are sure it will not break.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I've added a detect_libc function to be used instead of ones for the individual C libraries which returns a tuple of the name, i.e. gnu or musl, and the version.

I broke out the actual parsing logic into separate functions and added unit tests for this logic using the output of the ldd command.

I added a functional test for the detect_libc function.

Copy link
Member

@memsharded memsharded left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just a minor detail about returning None instead of None, None

conans/test/unittests/util/detect_libc_test.py Outdated Show resolved Hide resolved
conan/internal/api/detect_api.py Outdated Show resolved Hide resolved
Copy link
Member

@memsharded memsharded left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, many thanks!
Merging this for next release 2.2.

I'd recommend documenting this as "experimental", we might discover cases we didn't think about it when other users use it.

@memsharded memsharded added this to the 2.2.0 milestone Feb 19, 2024
@memsharded memsharded merged commit 524eb0a into conan-io:develop2 Feb 19, 2024
2 checks passed
@jwillikers jwillikers deleted the detect-glibc branch February 19, 2024 17:38
czoido pushed a commit to conan-io/docs that referenced this pull request Mar 19, 2024
* Document detect_glib_libc() and detect_musl_libc() functions

This documents the functions added in conan-io/conan#15683.

* Document the detect_libc() function

* Revert "Document detect_glib_libc() and detect_musl_libc() functions"

This reverts commit b4f58e8.

* Mark detect_libc as experimental
AbrilRBS added a commit to AbrilRBS/conan that referenced this pull request Jun 27, 2024
AbrilRBS added a commit that referenced this pull request Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants