Skip to content

Commit aab2edf

Browse files
committed
add PHP_C_STANDARD_LIBRARY function to detect glibc/musl more accurately
fix "cross-compilation" with musl-libc on glibc systems
1 parent 73b1ebf commit aab2edf

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

build/php.m4

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2488,3 +2488,43 @@ AC_DEFUN([PHP_REMOVE_OPTIMIZATION_FLAGS], [
24882488
CFLAGS=$(echo "$CFLAGS" | $SED -e "$sed_script")
24892489
CXXFLAGS=$(echo "$CXXFLAGS" | $SED -e "$sed_script")
24902490
])
2491+
2492+
dnl
2493+
dnl PHP_C_STANDARD_LIBRARY
2494+
dnl
2495+
dnl Determine the C standard library used for the build. The uclibc is checked
2496+
dnl first because it also defines the __GLIBC__ and could otherwise be detected
2497+
dnl as glibc. Musl C library is determined heuristically.
2498+
dnl
2499+
AC_DEFUN([PHP_C_STANDARD_LIBRARY],
2500+
[AC_CACHE_CHECK([C standard library implementation],
2501+
[php_cv_c_standard_library],
2502+
[php_cv_c_standard_library=unknown
2503+
dnl Check if C standard library is uclibc.
2504+
AS_VAR_IF([php_cv_c_standard_library], [unknown],
2505+
[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include <features.h>],
2506+
[#ifndef __UCLIBC__
2507+
(void) __UCLIBC__;
2508+
#endif])],
2509+
[php_cv_c_standard_library=uclibc],
2510+
[php_cv_c_standard_library=unknown])])
2511+
dnl Check if C standard library is GNU C.
2512+
AS_VAR_IF([php_cv_c_standard_library], [unknown],
2513+
[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include <features.h>],
2514+
[#ifndef __GLIBC__
2515+
(void) __GLIBC__;
2516+
#endif])],
2517+
[php_cv_c_standard_library=glibc],
2518+
[php_cv_c_standard_library=unknown])])
2519+
dnl Check if C standard library is musl libc.
2520+
AS_VAR_IF([php_cv_c_standard_library], [unknown],
2521+
[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include <stdarg.h>],
2522+
[#ifndef __DEFINED_va_list
2523+
(void) __DEFINED_va_list;
2524+
#endif])],
2525+
[php_cv_c_standard_library=musl],
2526+
[AS_IF([command -v ldd >/dev/null && ldd --version 2>&1 | grep ^musl >/dev/null 2>&1],
2527+
[php_cv_c_standard_library=musl],
2528+
[php_cv_c_standard_library=unknown])])])
2529+
])
2530+
])

configure.ac

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,10 @@ case $host_alias in
234234
;;
235235
esac
236236

237-
dnl Detect musl libc
238-
AC_MSG_CHECKING([whether we are using musl libc])
239-
if command -v ldd >/dev/null && ldd --version 2>&1 | grep ^musl >/dev/null 2>&1
240-
then
241-
AC_MSG_RESULT([yes])
242-
AC_DEFINE([__MUSL__], [1], [Define to 1 when using musl libc.])
243-
else
244-
AC_MSG_RESULT([no])
245-
fi
237+
dnl Detect C library.
238+
PHP_C_STANDARD_LIBRARY
239+
AS_VAR_IF([php_cv_c_standard_library], [musl],
240+
[AC_DEFINE([__MUSL__], [1], [Define when using musl libc.])])
246241

247242
dnl Add _GNU_SOURCE compile definition because the php_config.h with definitions
248243
dnl by AC_USE_SYSTEM_EXTENSIONS might be included after the system headers which

ext/posix/config.m4

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ if test "$PHP_POSIX" = "yes"; then
4444

4545
dnl Skip pathconf and fpathconf check on musl libc due to limited implementation
4646
dnl (first argument is not validated and has different error).
47-
AS_IF([command -v ldd >/dev/null && ldd --version 2>&1 | grep ^musl >/dev/null 2>&1],
47+
PHP_C_STANDARD_LIBRARY
48+
AS_VAR_IF([php_cv_c_standard_library], [musl],
4849
[],
4950
[AC_CHECK_FUNCS([pathconf fpathconf])])
5051

0 commit comments

Comments
 (0)