From 93d6a45a78bc11668b1b3133459c925fe08ab3b2 Mon Sep 17 00:00:00 2001 From: DubbleClick Date: Sat, 5 Jul 2025 12:01:52 +0700 Subject: [PATCH] *full* shared extension build linked against musl libc dynamically works! --- src/SPC/builder/linux/LinuxBuilder.php | 12 ++++++------ src/SPC/store/SourcePatcher.php | 19 ++++++++++++++----- src/globals/test-extensions.php | 4 ++-- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/SPC/builder/linux/LinuxBuilder.php b/src/SPC/builder/linux/LinuxBuilder.php index 258365d1..76e6a71d 100644 --- a/src/SPC/builder/linux/LinuxBuilder.php +++ b/src/SPC/builder/linux/LinuxBuilder.php @@ -192,12 +192,12 @@ class LinuxBuilder extends UnixBuilderBase ->exec('sed -i "s|//lib|/lib|g" Makefile') ->exec("{$SPC_CMD_PREFIX_PHP_MAKE} {$vars} cli"); + if (!$this->getOption('no-strip', false)) { + shell()->cd(SOURCE_PATH . '/php-src/sapi/cli')->exec('strip --strip-all php'); + } if ($this->getOption('with-upx-pack')) { shell()->cd(SOURCE_PATH . '/php-src/sapi/cli') - ->exec('strip --strip-all php') ->exec(getenv('UPX_EXEC') . ' --best php'); - } elseif (!$this->getOption('no-strip', false)) { - shell()->cd(SOURCE_PATH . '/php-src/sapi/cli')->exec('strip --strip-all php'); } $this->deployBinary(BUILD_TARGET_CLI); @@ -255,12 +255,12 @@ class LinuxBuilder extends UnixBuilderBase ->exec('sed -i "s|//lib|/lib|g" Makefile') ->exec("{$SPC_CMD_PREFIX_PHP_MAKE} {$vars} fpm"); + if (!$this->getOption('no-strip', false)) { + shell()->cd(SOURCE_PATH . '/php-src/sapi/fpm')->exec('strip --strip-all php-fpm'); + } if ($this->getOption('with-upx-pack')) { shell()->cd(SOURCE_PATH . '/php-src/sapi/fpm') - ->exec('strip --strip-all php-fpm') ->exec(getenv('UPX_EXEC') . ' --best php-fpm'); - } elseif (!$this->getOption('no-strip', false)) { - shell()->cd(SOURCE_PATH . '/php-src/sapi/fpm')->exec('strip --strip-all php-fpm'); } $this->deployBinary(BUILD_TARGET_FPM); } diff --git a/src/SPC/store/SourcePatcher.php b/src/SPC/store/SourcePatcher.php index 08ebdaef..89c2dc0d 100644 --- a/src/SPC/store/SourcePatcher.php +++ b/src/SPC/store/SourcePatcher.php @@ -256,14 +256,23 @@ class SourcePatcher */ public static function patchBeforeMake(BuilderBase $builder): void { - // Try to fix debian environment build lack HAVE_STRLCAT problem - if ($builder instanceof LinuxBuilder) { - FileSystem::replaceFileRegex(SOURCE_PATH . '/php-src/main/php_config.h', '/^#define HAVE_STRLCPY 1$/m', ''); - FileSystem::replaceFileRegex(SOURCE_PATH . '/php-src/main/php_config.h', '/^#define HAVE_STRLCAT 1$/m', ''); - } if ($builder instanceof UnixBuilderBase) { FileSystem::replaceFileStr(SOURCE_PATH . '/php-src/Makefile', 'install-micro', ''); } + if (!SPCTarget::isStatic() && SPCTarget::getLibc() === 'musl') { + // we need to patch the symbol to global visibility, otherwise extensions with `initial-exec` TLS model will fail to load + FileSystem::replaceFileStr( + SOURCE_PATH . '/php-src/TSRM/TSRM.h', + '#define TSRMLS_MAIN_CACHE_DEFINE() TSRM_TLS void *TSRMLS_CACHE TSRM_TLS_MODEL_ATTR = NULL;', + '#define TSRMLS_MAIN_CACHE_DEFINE() TSRM_TLS __attribute__((visibility("default"))) void *TSRMLS_CACHE TSRM_TLS_MODEL_ATTR = NULL;', + ); + } else { + FileSystem::replaceFileStr( + SOURCE_PATH . '/php-src/TSRM/TSRM.h', + '#define TSRMLS_MAIN_CACHE_DEFINE() TSRM_TLS __attribute__((visibility("default"))) void *TSRMLS_CACHE TSRM_TLS_MODEL_ATTR = NULL;', + '#define TSRMLS_MAIN_CACHE_DEFINE() TSRM_TLS void *TSRMLS_CACHE TSRM_TLS_MODEL_ATTR = NULL;', + ); + } // no asan // if (strpos(file_get_contents(SOURCE_PATH . '/php-src/Makefile'), 'CFLAGS_CLEAN = -g') === false) { diff --git a/src/globals/test-extensions.php b/src/globals/test-extensions.php index 73752c00..23d3f15d 100644 --- a/src/globals/test-extensions.php +++ b/src/globals/test-extensions.php @@ -158,8 +158,8 @@ if ($shared_extensions) { break; case 'ubuntu-24.04': case 'ubuntu-24.04-arm': - putenv('SPC_TARGET=native-native-gnu'); - if (getenv('SPC_TARGET') && !str_contains((string) getenv('SPC_TARGET'), '-musl')) { + putenv('SPC_TARGET=native-native-musl -dynamic'); + if (getenv('SPC_TARGET') && !str_contains(getenv('SPC_TARGET'), '-musl') || str_contains(getenv('SPC_TARGET'), '-dynamic')) { $shared_cmd = ' --build-shared=' . quote2($shared_extensions) . ' '; } break;