[bugfix 8.5] update musl libc test to fix "cross" compilation on glibc system#19352
Closed
henderkes wants to merge 2 commits intophp:PHP-8.5from
Closed
[bugfix 8.5] update musl libc test to fix "cross" compilation on glibc system#19352henderkes wants to merge 2 commits intophp:PHP-8.5from
henderkes wants to merge 2 commits intophp:PHP-8.5from
Conversation
20 tasks
devnexen
reviewed
Aug 3, 2025
Member
|
Hello. Let's then make a new Autoconf macro to determine the C standard library - diff --git a/build/php.m4 b/build/php.m4
index e82e856667e..0a2169f6f51 100644
--- a/build/php.m4
+++ b/build/php.m4
@@ -2502,3 +2502,43 @@ AC_DEFUN([PHP_REMOVE_OPTIMIZATION_FLAGS], [
CFLAGS=$(echo "$CFLAGS" | $SED -e "$sed_script")
CXXFLAGS=$(echo "$CXXFLAGS" | $SED -e "$sed_script")
])
+
+dnl
+dnl PHP_C_STANDARD_LIBRARY
+dnl
+dnl Determine the C standard library used for the build. The uclibc is checked
+dnl first because it also defines the __GLIBC__ and could otherwise be detected
+dnl as glibc. Musl C library is determined heuristically.
+dnl
+AC_DEFUN([PHP_C_STANDARD_LIBRARY],
+[AC_CACHE_CHECK([C standard library implementation],
+[php_cv_c_standard_library],
+[php_cv_c_standard_library=unknown
+dnl Check if C standard library is uclibc.
+AS_VAR_IF([php_cv_c_standard_library], [unknown],
+[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include <features.h>],
+[#ifndef __UCLIBC__
+ (void) __UCLIBC__;
+#endif])],
+[php_cv_c_standard_library=uclibc],
+[php_cv_c_standard_library=unknown])])
+dnl Check if C standard library is GNU C.
+AS_VAR_IF([php_cv_c_standard_library], [unknown],
+[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include <features.h>],
+[#ifndef __GLIBC__
+ (void) __GLIBC__;
+#endif])],
+[php_cv_c_standard_library=glibc],
+[php_cv_c_standard_library=unknown])])
+dnl Check if C standard library is musl libc.
+AS_VAR_IF([php_cv_c_standard_library], [unknown],
+[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include <stdarg.h>],
+[#ifndef __DEFINED_va_list
+ (void) __DEFINED_va_list;
+#endif])],
+[php_cv_c_standard_library=musl],
+[AS_IF([command -v ldd >/dev/null && ldd --version 2>&1 | grep ^musl >/dev/null 2>&1],
+[php_cv_c_standard_library=musl],
+[php_cv_c_standard_library=unknown])])])
+])
+])
diff --git a/configure.ac b/configure.ac
index 54f6157d96a..86b5b071639 100644
--- a/configure.ac
+++ b/configure.ac
@@ -250,15 +250,10 @@ case $host_alias in
;;
esac
-dnl Detect musl libc
-AC_MSG_CHECKING([whether we are using musl libc])
-if command -v ldd >/dev/null && ldd --version 2>&1 | grep ^musl >/dev/null 2>&1
-then
- AC_MSG_RESULT([yes])
- AC_DEFINE([__MUSL__], [1], [Define to 1 when using musl libc.])
-else
- AC_MSG_RESULT([no])
-fi
+dnl Detect C library.
+PHP_C_STANDARD_LIBRARY
+AS_VAR_IF([php_cv_c_standard_library], [musl],
+ [AC_DEFINE([__MUSL__], [1], [Define when using musl libc.])])
dnl Add _GNU_SOURCE compile definition because the php_config.h with definitions
dnl by AC_USE_SYSTEM_EXTENSIONS might be included after the system headers which
diff --git a/ext/posix/config.m4 b/ext/posix/config.m4
index 8960979065f..86554635695 100644
--- a/ext/posix/config.m4
+++ b/ext/posix/config.m4
@@ -44,7 +44,8 @@ if test "$PHP_POSIX" = "yes"; then
dnl Skip pathconf and fpathconf check on musl libc due to limited implementation
dnl (first argument is not validated and has different error).
- AS_IF([command -v ldd >/dev/null && ldd --version 2>&1 | grep ^musl >/dev/null 2>&1],
+ PHP_C_STANDARD_LIBRARY
+ AS_VAR_IF([php_cv_c_standard_library], [musl],
[],
[AC_CHECK_FUNCS([pathconf fpathconf])]) |
Contributor
Author
|
Yes, thank you for your input. Would you like me to apply the changes to this PR, or open one yourself? |
Member
You can add it in this PR. No problem. |
fix "cross-compilation" with musl-libc on glibc systems
petk
reviewed
Oct 2, 2025
Co-authored-by: Peter Kokot <peterkokot@gmail.com>
Member
|
Merged to PHP-8.5 branch. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The ldd check for musl libc does not correctly detect musl libc in case one is compiling for musl libc on a glibc system. This PR fixes that.
This works because <features.h> will automatically define
__USE_GNUunder glibc to enable GNU extensions, which musl explicitly does not support.