diff --git a/.github/workflows/build-unix.yml b/.github/workflows/build-unix.yml index 9a40960e5..083b464c9 100644 --- a/.github/workflows/build-unix.yml +++ b/.github/workflows/build-unix.yml @@ -29,6 +29,9 @@ on: description: Extensions to build (comma separated) required: true type: string + shared-extensions: + description: Shared extensions to build (optional, comma separated) + type: string extra-libs: description: Extra libraries to build (optional, comma separated) type: string @@ -42,6 +45,14 @@ on: build-fpm: description: Build fpm binary type: boolean + build-frankenphp: + description: Build frankenphp binary (requires ZTS) + type: boolean + default: false + enable-zts: + description: Enable ZTS + type: boolean + default: false prefer-pre-built: description: Prefer pre-built binaries (reduce build time) type: boolean @@ -73,6 +84,9 @@ on: description: Extensions to build (comma separated) required: true type: string + shared-extensions: + description: Shared extensions to build (optional, comma separated) + type: string extra-libs: description: Extra libraries to build (optional, comma separated) type: string @@ -86,6 +100,14 @@ on: build-fpm: description: Build fpm binary type: boolean + build-frankenphp: + description: Build frankenphp binary (requires ZTS) + type: boolean + default: false + enable-zts: + description: Enable ZTS + type: boolean + default: false prefer-pre-built: description: Prefer pre-built binaries (reduce build time) type: boolean @@ -152,8 +174,19 @@ jobs: RUNS_ON="macos-15" ;; esac - DOWN_CMD="$DOWN_CMD --with-php=${{ inputs.php-version }} --for-extensions=${{ inputs.extensions }} --ignore-cache-sources=php-src" - BUILD_CMD="$BUILD_CMD ${{ inputs.extensions }}" + STATIC_EXTS="${{ inputs.extensions }}" + SHARED_EXTS="${{ inputs['shared-extensions'] }}" + BUILD_FRANKENPHP="${{ inputs['build-frankenphp'] }}" + ENABLE_ZTS="${{ inputs['enable-zts'] }}" + ALL_EXTS="$STATIC_EXTS" + if [ -n "$SHARED_EXTS" ]; then + ALL_EXTS="$ALL_EXTS,$SHARED_EXTS" + fi + DOWN_CMD="$DOWN_CMD --with-php=${{ inputs.php-version }} --for-extensions=$ALL_EXTS --ignore-cache-sources=php-src" + BUILD_CMD="$BUILD_CMD $STATIC_EXTS" + if [ -n "$SHARED_EXTS" ]; then + BUILD_CMD="$BUILD_CMD --build-shared=$SHARED_EXTS" + fi if [ -n "${{ inputs.extra-libs }}" ]; then DOWN_CMD="$DOWN_CMD --for-libs=${{ inputs.extra-libs }}" BUILD_CMD="$BUILD_CMD --with-libs=${{ inputs.extra-libs }}" @@ -177,6 +210,12 @@ jobs: if [ ${{ inputs.build-fpm }} == true ]; then BUILD_CMD="$BUILD_CMD --build-fpm" fi + if [ "$BUILD_FRANKENPHP" = "true" ]; then + BUILD_CMD="$BUILD_CMD --build-frankenphp" + fi + if [ "$ENABLE_ZTS" = "true" ]; then + BUILD_CMD="$BUILD_CMD --enable-zts" + fi echo 'download='"$DOWN_CMD" >> "$GITHUB_OUTPUT" echo 'build='"$BUILD_CMD" >> "$GITHUB_OUTPUT" echo 'run='"$RUNS_ON" >> "$GITHUB_OUTPUT" @@ -199,6 +238,27 @@ jobs: env: phpts: nts + - if: ${{ inputs['build-frankenphp'] == true }} + name: "Install go-xcaddy for FrankenPHP" + run: | + case "${{ inputs.os }}" in + linux-x86_64|linux-aarch64) + ./bin/spc-alpine-docker install-pkg go-xcaddy + ;; + linux-x86_64-glibc|linux-aarch64-glibc) + ./bin/spc-gnu-docker install-pkg go-xcaddy + ;; + macos-x86_64|macos-aarch64) + composer update --no-dev --classmap-authoritative + ./bin/spc doctor --auto-fix + ./bin/spc install-pkg go-xcaddy + ;; + *) + echo "Unsupported OS for go-xcaddy install: ${{ inputs.os }}" + exit 1 + ;; + esac + # Cache downloaded source - id: cache-download uses: actions/cache@v4 @@ -245,7 +305,22 @@ jobs: name: php-fpm-${{ inputs.php-version }}-${{ inputs.os }} path: buildroot/bin/php-fpm + # Upload frankenphp executable + - if: ${{ inputs['build-frankenphp'] == true }} + name: "Upload FrankenPHP SAPI" + uses: actions/upload-artifact@v4 + with: + name: php-frankenphp-${{ inputs.php-version }}-${{ inputs.os }} + path: buildroot/bin/frankenphp + # Upload extensions metadata + - if: ${{ inputs['shared-extensions'] != '' }} + name: "Upload shared extensions" + uses: actions/upload-artifact@v4 + with: + name: php-shared-ext-${{ inputs.php-version }}-${{ inputs.os }} + path: | + buildroot/modules/*.so - uses: actions/upload-artifact@v4 name: "Upload License Files" with: diff --git a/config/ext.json b/config/ext.json index 4352f2e2c..79d3831ec 100644 --- a/config/ext.json +++ b/config/ext.json @@ -252,6 +252,7 @@ "arg-type-unix": "enable-path", "cpp-extension": true, "lib-depends": [ + "grpc", "zlib", "openssl", "libcares" diff --git a/config/lib.json b/config/lib.json index ebbf4b87b..f960ab82f 100644 --- a/config/lib.json +++ b/config/lib.json @@ -143,9 +143,7 @@ "zlib" ], "lib-suggests": [ - "libpng", - "bzip2", - "brotli" + "libpng" ] }, "gettext": { @@ -355,6 +353,9 @@ "static-libs-unix": [ "libaom.a" ], + "static-libs-windows": [ + "aom.lib" + ], "cpp-library": true }, "libargon2": { @@ -493,7 +494,7 @@ "static-libs-windows": [ "libjpeg_a.lib" ], - "lib-suggests-windows": [ + "lib-depends": [ "zlib" ] }, @@ -862,6 +863,9 @@ }, "openssl": { "source": "openssl", + "pkg-configs": [ + "openssl" + ], "static-libs-unix": [ "libssl.a", "libcrypto.a" @@ -974,6 +978,11 @@ }, "unixodbc": { "source": "unixodbc", + "pkg-configs": [ + "odbc", + "odbccr", + "odbcinst" + ], "static-libs-unix": [ "libodbc.a", "libodbccr.a", @@ -1015,6 +1024,9 @@ }, "zlib": { "source": "zlib", + "pkg-configs": [ + "zlib" + ], "static-libs-unix": [ "libz.a" ], @@ -1028,6 +1040,9 @@ }, "zstd": { "source": "zstd", + "pkg-configs": [ + "libzstd" + ], "static-libs-unix": [ "libzstd.a" ], diff --git a/config/pkg/ext/builtin-extensions.yml b/config/pkg/ext/builtin-extensions.yml index b938182c0..e5a6d28a9 100644 --- a/config/pkg/ext/builtin-extensions.yml +++ b/config/pkg/ext/builtin-extensions.yml @@ -16,6 +16,7 @@ ext-curl: depends: - curl depends@windows: + - curl - ext-zlib - ext-openssl php-extension: @@ -342,6 +343,6 @@ ext-zlib: - zlib php-extension: arg-type: custom - arg-type@windows: with + arg-type@windows: enable build-with-php: true build-shared: false diff --git a/config/pkg/ext/ext-amqp.yml b/config/pkg/ext/ext-amqp.yml index 1c8023602..6c73bf203 100644 --- a/config/pkg/ext/ext-amqp.yml +++ b/config/pkg/ext/ext-amqp.yml @@ -10,6 +10,7 @@ ext-amqp: depends: - librabbitmq depends@windows: + - librabbitmq - ext-openssl php-extension: arg-type: '--with-amqp@shared_suffix@ --with-librabbitmq-dir=@build_root_path@' diff --git a/config/source.json b/config/source.json index 040197cb1..4b3f7b594 100644 --- a/config/source.json +++ b/config/source.json @@ -641,6 +641,7 @@ "libjpeg": { "type": "ghtar", "repo": "libjpeg-turbo/libjpeg-turbo", + "prefer-stable": true, "license": { "type": "file", "path": "LICENSE.md" @@ -1054,7 +1055,7 @@ }, "protobuf": { "type": "url", - "url": "https://pecl.php.net/get/protobuf", + "url": "https://pecl.php.net/get/protobuf-5.34.1.tgz", "path": "php-src/ext/protobuf", "filename": "protobuf.tgz", "license": { diff --git a/docs/en/guide/action-build.md b/docs/en/guide/action-build.md index 7d4bba327..b22549569 100644 --- a/docs/en/guide/action-build.md +++ b/docs/en/guide/action-build.md @@ -16,8 +16,10 @@ while also defining the extensions to compile. 1. Fork project. 2. Go to the Actions of the project and select `CI`. -3. Select `Run workflow`, fill in the PHP version you want to compile, the target type, and the list of extensions. (extensions comma separated, e.g. `bcmath,curl,mbstring`) -4. After waiting for about a period of time, enter the corresponding task and get `Artifacts`. +3. Select `Run workflow`, fill in the PHP version you want to compile, the target type, and the list of static extensions. (comma separated, e.g. `bcmath,curl,mbstring`) +4. If you need shared extensions (for example `xdebug`), set `shared-extensions` (comma separated, e.g. `xdebug`). +5. If you need FrankenPHP, enable `build-frankenphp` and also enable `enable-zts`. +6. After waiting for about a period of time, enter the corresponding task and get `Artifacts`. If you enable `debug`, all logs will be output at build time, including compiled logs, for troubleshooting. diff --git a/docs/zh/guide/action-build.md b/docs/zh/guide/action-build.md index 11f382d5e..7adcc456b 100644 --- a/docs/zh/guide/action-build.md +++ b/docs/zh/guide/action-build.md @@ -14,7 +14,9 @@ Action 构建指的是直接使用 GitHub Action 进行编译。 1. Fork 本项目。 2. 进入项目的 Actions,选择 CI 开头的 Workflow(根据你需要的操作系统选择)。 3. 选择 `Run workflow`,填入你要编译的 PHP 版本、目标类型、扩展列表。(扩展列表使用英文逗号分割,例如 `bcmath,curl,mbstring`) -4. 等待大约一段时间后,进入对应的任务中,获取 `Artifacts`。 +4. 如果需要共享扩展(例如 `xdebug`),请设置 `shared-extensions`(使用英文逗号分割,例如 `xdebug`)。 +5. 如果需要 FrankenPHP,请启用 `build-frankenphp`,同时也需要启用 `enable-zts`。 +6. 等待大约一段时间后,进入对应的任务中,获取 `Artifacts`。 如果你选择了 `debug`,则会在构建时输出所有日志,包括编译的日志,以供排查错误。 diff --git a/src/Package/Extension/curl.php b/src/Package/Extension/curl.php new file mode 100644 index 000000000..f5c05b570 --- /dev/null +++ b/src/Package/Extension/curl.php @@ -0,0 +1,26 @@ +build(); + + FileSystem::replaceFileStr($lib->getIncludeDir() . '\nghttp2\nghttp2.h', '#ifdef NGHTTP2_STATICLIB', '#if 1'); + } #[BuildFor('Linux')] diff --git a/src/Package/Target/php.php b/src/Package/Target/php.php index 38e2ad91b..29b6848a9 100644 --- a/src/Package/Target/php.php +++ b/src/Package/Target/php.php @@ -12,6 +12,7 @@ use StaticPHP\Attribute\Package\Info; use StaticPHP\Attribute\Package\InitPackage; use StaticPHP\Attribute\Package\ResolveBuild; +use StaticPHP\Attribute\Package\Stage; use StaticPHP\Attribute\Package\Target; use StaticPHP\Attribute\Package\Validate; use StaticPHP\Config\PackageConfig; @@ -29,6 +30,7 @@ use StaticPHP\Toolchain\ToolchainManager; use StaticPHP\Util\DependencyResolver; use StaticPHP\Util\FileSystem; +use StaticPHP\Util\InteractiveTerm; use StaticPHP\Util\SourcePatcher; use StaticPHP\Util\V2CompatLayer; use Symfony\Component\Console\Input\InputArgument; @@ -339,6 +341,35 @@ public function beforeBuild(PackageBuilder $builder, Package $package): void FileSystem::removeDir(BUILD_MODULES_PATH); } + #[Stage('postInstall')] + public function postInstall(TargetPackage $package, PackageInstaller $installer): void + { + if ($package->getName() === 'frankenphp') { + $package->runStage([$this, 'smokeTestFrankenphpForUnix']); + return; + } + if ($package->getName() !== 'php') { + return; + } + if (SystemTarget::isUnix()) { + if ($installer->interactive) { + InteractiveTerm::indicateProgress('Running PHP smoke tests'); + } + $package->runStage([$this, 'smokeTestForUnix']); + if ($installer->interactive) { + InteractiveTerm::finish('PHP smoke tests passed'); + } + } elseif (SystemTarget::getTargetOS() === 'Windows') { + if ($installer->interactive) { + InteractiveTerm::indicateProgress('Running PHP smoke tests'); + } + $package->runStage([$this, 'smokeTestForWindows']); + if ($installer->interactive) { + InteractiveTerm::finish('PHP smoke tests passed'); + } + } + } + private function makeStaticExtensionString(PackageInstaller $installer): string { $arg = []; diff --git a/src/Package/Target/php/unix.php b/src/Package/Target/php/unix.php index f16d879f6..78eca80d2 100644 --- a/src/Package/Target/php/unix.php +++ b/src/Package/Target/php/unix.php @@ -469,27 +469,6 @@ public function build(TargetPackage $package): void $package->runStage([$this, 'unixBuildSharedExt']); } - #[Stage('postInstall')] - public function postInstall(TargetPackage $package, PackageInstaller $installer): void - { - if ($package->getName() === 'frankenphp') { - $package->runStage([$this, 'smokeTestFrankenphpForUnix']); - return; - } - if ($package->getName() !== 'php') { - return; - } - if (SystemTarget::isUnix()) { - if ($installer->interactive) { - InteractiveTerm::indicateProgress('Running PHP smoke tests'); - } - $package->runStage([$this, 'smokeTestForUnix']); - if ($installer->interactive) { - InteractiveTerm::finish('PHP smoke tests passed'); - } - } - } - /** * Patch phpize and php-config if needed */ @@ -662,7 +641,7 @@ protected function seekPhpSrcLogFileOnException(callable $callback, string $sour /** * Generate micro extension test php code. */ - private function generateMicroExtTests(PackageInstaller $installer): string + protected function generateMicroExtTests(PackageInstaller $installer): string { $php = "getResolvedPackages(PhpExtensionPackage::class) as $ext) { diff --git a/src/Package/Target/php/windows.php b/src/Package/Target/php/windows.php index 74e746e2b..1188a0e49 100644 --- a/src/Package/Target/php/windows.php +++ b/src/Package/Target/php/windows.php @@ -1,239 +1,783 @@ -getSourceDir()}/win32/build/config.w32", 'dllmain.c ', ''); - } - - #[Stage] - public function buildconfForWindows(TargetPackage $package): void - { - InteractiveTerm::setMessage('Building php: ' . ConsoleColor::yellow('./buildconf.bat')); - V2CompatLayer::emitPatchPoint('before-php-buildconf'); - cmd()->cd($package->getSourceDir())->exec('.\buildconf.bat'); - } - - #[Stage] - public function configureForWindows(TargetPackage $package, PackageInstaller $installer): void - { - InteractiveTerm::setMessage('Building php: ' . ConsoleColor::yellow('./configure.bat')); - V2CompatLayer::emitPatchPoint('before-php-configure'); - $args = [ - '--disable-all', - "--with-php-build={$package->getBuildRootPath()}", - "--with-extra-includes={$package->getIncludeDir()}", - "--with-extra-libs={$package->getLibDir()}", - ]; - // sapis - $cli = $installer->isPackageResolved('php-cli'); - $cgi = $installer->isPackageResolved('php-cgi'); - $micro = $installer->isPackageResolved('php-micro'); - $args[] = $cli ? '--enable-cli=yes' : '--enable-cli=no'; - $args[] = $cgi ? '--enable-cgi=yes' : '--enable-cgi=no'; - $args[] = $micro ? '--enable-micro=yes' : '--enable-micro=no'; - - // zts - $args[] = $package->getBuildOption('enable-zts', false) ? '--enable-zts=yes' : '--enable-zts=no'; - // opcache-jit - $args[] = !$package->getBuildOption('disable-opcache-jit', false) ? '--enable-opcache-jit=yes' : '--enable-opcache-jit=no'; - // micro win32 - if ($micro && $package->getBuildOption('enable-micro-win32', false)) { - $args[] = '--enable-micro-win32=yes'; - } - // config-file-scan-dir - if ($option = $package->getBuildOption('with-config-file-scan-dir', false)) { - $args[] = "--with-config-file-scan-dir={$option}"; - } - // micro logo - if ($micro && ($logo = $this->getBuildOption('with-micro-logo')) !== null) { - $args[] = "--enable-micro-logo={$logo}"; - copy($logo, SOURCE_PATH . '\php-src\\' . $logo); - } - $args = implode(' ', $args); - $static_extension_str = $this->makeStaticExtensionString($installer); - cmd()->cd($package->getSourceDir())->exec(".\\configure.bat {$args} {$static_extension_str}"); - } - - #[BeforeStage('php', [self::class, 'makeCliForWindows'])] - #[PatchDescription('Patch Windows Makefile for CLI target')] - public function patchCLITarget(TargetPackage $package): void - { - // search Makefile code line contains "$(BUILD_DIR)\php.exe:" - $content = FileSystem::readFile("{$package->getSourceDir()}\\Makefile"); - $lines = explode("\r\n", $content); - $line_num = 0; - $found = false; - foreach ($lines as $v) { - if (str_contains($v, '$(BUILD_DIR)\php.exe:')) { - $found = $line_num; - break; - } - ++$line_num; - } - if ($found === false) { - throw new PatchException('Windows Makefile patching for php.exe target', 'Cannot patch windows CLI Makefile, Makefile does not contain "$(BUILD_DIR)\php.exe:" line'); - } - $lines[$line_num] = '$(BUILD_DIR)\php.exe: generated_files $(DEPS_CLI) $(PHP_GLOBAL_OBJS) $(CLI_GLOBAL_OBJS) $(STATIC_EXT_OBJS) $(ASM_OBJS) $(BUILD_DIR)\php.exe.res $(BUILD_DIR)\php.exe.manifest'; - $lines[$line_num + 1] = "\t" . '"$(LINK)" /nologo $(PHP_GLOBAL_OBJS_RESP) $(CLI_GLOBAL_OBJS_RESP) $(STATIC_EXT_OBJS_RESP) $(STATIC_EXT_LIBS) $(ASM_OBJS) $(LIBS) $(LIBS_CLI) $(BUILD_DIR)\php.exe.res /out:$(BUILD_DIR)\php.exe $(LDFLAGS) $(LDFLAGS_CLI) /ltcg /nodefaultlib:msvcrt /nodefaultlib:msvcrtd /ignore:4286'; - FileSystem::writeFile("{$package->getSourceDir()}\\Makefile", implode("\r\n", $lines)); - } - - #[Stage] - public function makeCliForWindows(TargetPackage $package, PackageBuilder $builder): void - { - InteractiveTerm::setMessage('Building php: ' . ConsoleColor::yellow('php.exe')); - - // extra lib - $extra_libs = getenv('SPC_EXTRA_LIBS') ?: ''; - - // Add debug symbols for release build if --no-strip is specified - // We need to modify CFLAGS to replace /Ox with /Zi and add /DEBUG to LDFLAGS - $debug_overrides = ''; - if ($package->getBuildOption('no-strip', false)) { - // Read current CFLAGS from Makefile and replace optimization flags - $makefile_content = file_get_contents("{$package->getSourceDir()}\\Makefile"); - if (preg_match('/^CFLAGS=(.+?)$/m', $makefile_content, $matches)) { - $cflags = $matches[1]; - // Replace /Ox (full optimization) with /Zi (debug info) and /Od (disable optimization) - // Keep optimization for speed: /O2 /Zi instead of /Od /Zi - $cflags = str_replace('/Ox ', '/O2 /Zi ', $cflags); - $debug_overrides = '"CFLAGS=' . $cflags . '" "LDFLAGS=/DEBUG /LTCG /INCREMENTAL:NO" "LDFLAGS_CLI=/DEBUG" '; - } - } - - cmd()->cd($package->getSourceDir()) - ->exec("nmake /nologo {$debug_overrides}LIBS_CLI=\"ws2_32.lib shell32.lib {$extra_libs}\" EXTRA_LD_FLAGS_PROGRAM= php.exe"); - - $this->deployWindowsBinary($builder, $package, 'php-cli'); - } - - #[Stage] - public function makeForWindows(TargetPackage $package, PackageInstaller $installer): void - { - V2CompatLayer::emitPatchPoint('before-php-make'); - InteractiveTerm::setMessage('Building php: ' . ConsoleColor::yellow('nmake clean')); - cmd()->cd($package->getSourceDir())->exec('nmake clean'); - - if ($installer->isPackageResolved('php-cli')) { - $package->runStage([$this, 'makeCliForWindows']); - } - if ($installer->isPackageResolved('php-cgi')) { - $package->runStage([$this, 'makeCgiForWindows']); - } - if ($installer->isPackageResolved('php-micro')) { - $package->runStage([$this, 'makeMicroForWindows']); - } - } - - #[BuildFor('Windows')] - public function buildWin(TargetPackage $package): void - { - if ($package->getName() !== 'php') { - return; - } - - $package->runStage([$this, 'buildconfForWindows']); - $package->runStage([$this, 'configureForWindows']); - $package->runStage([$this, 'makeForWindows']); - } - - #[BeforeStage('php', [self::class, 'buildconfForWindows'])] - #[PatchDescription('Patch SPC_MICRO_PATCHES defined patches')] - #[PatchDescription('Fix PHP 8.1 static build bug on Windows')] - #[PatchDescription('Fix PHP Visual Studio version detection')] - public function patchBeforeBuildconfForWindows(TargetPackage $package): void - { - // php-src patches from micro - SourcePatcher::patchPhpSrc(); - - // php 8.1 bug - if ($this->getPHPVersionID() >= 80100 && $this->getPHPVersionID() < 80200) { - logger()->info('Patching PHP 8.1 windows Fiber bug'); - FileSystem::replaceFileStr( - "{$package->getSourceDir()}\\win32\\build\\config.w32", - "ADD_FLAG('LDFLAGS', '$(BUILD_DIR)\\\\Zend\\\\jump_' + FIBER_ASM_ARCH + '_ms_pe_masm.obj');", - "ADD_FLAG('ASM_OBJS', '$(BUILD_DIR)\\\\Zend\\\\jump_' + FIBER_ASM_ARCH + '_ms_pe_masm.obj $(BUILD_DIR)\\\\Zend\\\\make_' + FIBER_ASM_ARCH + '_ms_pe_masm.obj');" - ); - FileSystem::replaceFileStr( - "{$package->getSourceDir()}\\win32\\build\\config.w32", - "ADD_FLAG('LDFLAGS', '$(BUILD_DIR)\\\\Zend\\\\make_' + FIBER_ASM_ARCH + '_ms_pe_masm.obj');", - '' - ); - } - - // Fix PHP VS version - // get vs version - $vc = WindowsUtil::findVisualStudio(); - if ($vc === false) { - $vc_matches = ['unknown', 'unknown']; - } else { - $vc_matches = match ($vc['major_version']) { - '17' => ['VS17', 'Visual C++ 2022'], - '16' => ['VS16', 'Visual C++ 2019'], - default => ['unknown', 'unknown'], - }; - } - // patch php-src/win32/build/confutils.js - FileSystem::replaceFileStr( - "{$package->getSourceDir()}\\win32\\build\\confutils.js", - 'var name = "unknown";', - "var name = short ? \"{$vc_matches[0]}\" : \"{$vc_matches[1]}\";return name;" - ); - - // patch micro win32 - if ($package->getBuildOption('enable-micro-win32') && !file_exists("{$package->getSourceDir()}\\sapi\\micro\\php_micro.c.win32bak")) { - copy("{$package->getSourceDir()}\\sapi\\micro\\php_micro.c", "{$package->getSourceDir()}\\sapi\\micro\\php_micro.c.win32bak"); - FileSystem::replaceFileStr("{$package->getSourceDir()}\\sapi\\micro\\php_micro.c", '#include "php_variables.h"', '#include "php_variables.h"' . "\n#define PHP_MICRO_WIN32_NO_CONSOLE 1"); - } else { - if (file_exists("{$package->getSourceDir()}\\sapi\\micro\\php_micro.c.win32bak")) { - rename("{$package->getSourceDir()}\\sapi\\micro\\php_micro.c.win32bak", "{$package->getSourceDir()}\\sapi\\micro\\php_micro.c"); - } - } - } - - protected function deployWindowsBinary(PackageBuilder $builder, TargetPackage $package, string $sapi): void - { - $rel_type = 'Release'; // TODO: Debug build support - $ts = $builder->getOption('enable-zts') ? '_TS' : ''; - $debug_dir = BUILD_ROOT_PATH . '\debug'; - $src = match ($sapi) { - 'php-cli' => ["{$package->getSourceDir()}\\x64\\{$rel_type}{$ts}", 'php.exe', 'php.pdb'], - 'php-micro' => ["{$package->getSourceDir()}\\x64\\{$rel_type}{$ts}", 'micro.sfx', 'micro.pdb'], - 'php-cgi' => ["{$package->getSourceDir()}\\x64\\{$rel_type}{$ts}", 'php-cgi.exe', 'php-cgi.pdb'], - default => throw new SPCInternalException("Deployment does not accept type {$sapi}"), - }; - $src_file = "{$src[0]}\\{$src[1]}"; - $dst_file = BUILD_BIN_PATH . '\\' . basename($src_file); - - $builder->deployBinary($src_file, $dst_file); - - // make debug info file path - if ($builder->getOption('no-strip', false) && file_exists("{$src[0]}\\{$src[2]}")) { - FileSystem::copy("{$src[0]}\\{$src[2]}", "{$debug_dir}\\{$src[2]}"); - } - } -} +getSourceDir()}/win32/build/config.w32", 'dllmain.c ', ''); + } + + #[Stage] + public function buildconfForWindows(TargetPackage $package): void + { + InteractiveTerm::setMessage('Building php: ' . ConsoleColor::yellow('./buildconf.bat')); + V2CompatLayer::emitPatchPoint('before-php-buildconf'); + cmd()->cd($package->getSourceDir())->exec('.\buildconf.bat'); + } + + #[Stage] + public function configureForWindows(TargetPackage $package, PackageInstaller $installer): void + { + InteractiveTerm::setMessage('Building php: ' . ConsoleColor::yellow('./configure.bat')); + V2CompatLayer::emitPatchPoint('before-php-configure'); + $args = [ + '--disable-all', + "--with-php-build={$package->getBuildRootPath()}", + "--with-extra-includes={$package->getIncludeDir()}", + "--with-extra-libs={$package->getLibDir()}", + ]; + // sapis + $cli = $installer->isPackageResolved('php-cli'); + $cgi = $installer->isPackageResolved('php-cgi'); + $micro = $installer->isPackageResolved('php-micro'); + $embed = $installer->isPackageResolved('php-embed'); + $args[] = $cli ? '--enable-cli=yes' : '--enable-cli=no'; + $args[] = $cgi ? '--enable-cgi=yes' : '--enable-cgi=no'; + $args[] = $micro ? '--enable-micro=yes' : '--enable-micro=no'; + $args[] = $embed ? '--enable-embed=yes' : '--enable-embed=no'; + + // zts + $args[] = $package->getBuildOption('enable-zts', false) ? '--enable-zts=yes' : '--enable-zts=no'; + // opcache-jit + $args[] = !$package->getBuildOption('disable-opcache-jit', false) ? '--enable-opcache-jit=yes' : '--enable-opcache-jit=no'; + // micro win32 + if ($micro && $package->getBuildOption('enable-micro-win32', false)) { + $args[] = '--enable-micro-win32=yes'; + } + // config-file-scan-dir + if ($option = $package->getBuildOption('with-config-file-scan-dir', false)) { + $args[] = "--with-config-file-scan-dir={$option}"; + } + // micro logo + if ($micro && ($logo = $this->getBuildOption('with-micro-logo')) !== null) { + $args[] = "--enable-micro-logo={$logo}"; + copy($logo, SOURCE_PATH . '\php-src\\' . $logo); + } + $args = implode(' ', $args); + $static_extension_str = $this->makeStaticExtensionString($installer); + cmd()->cd($package->getSourceDir())->exec(".\\configure.bat {$args} {$static_extension_str}"); + } + + #[BeforeStage('php', [self::class, 'makeCliForWindows'])] + #[PatchDescription('Patch Windows Makefile for CLI target')] + public function patchCLITarget(TargetPackage $package): void + { + // search Makefile code line contains "$(BUILD_DIR)\php.exe:" + $content = FileSystem::readFile("{$package->getSourceDir()}\\Makefile"); + $lines = explode("\r\n", $content); + $line_num = 0; + $found = false; + foreach ($lines as $v) { + if (str_contains($v, '$(BUILD_DIR)\php.exe:')) { + $found = $line_num; + break; + } + ++$line_num; + } + if ($found === false) { + throw new PatchException('Windows Makefile patching for php.exe target', 'Cannot patch windows CLI Makefile, Makefile does not contain "$(BUILD_DIR)\php.exe:" line'); + } + $lines[$line_num] = '$(BUILD_DIR)\php.exe: generated_files $(DEPS_CLI) $(PHP_GLOBAL_OBJS) $(CLI_GLOBAL_OBJS) $(STATIC_EXT_OBJS) $(ASM_OBJS) $(BUILD_DIR)\php.exe.res $(BUILD_DIR)\php.exe.manifest'; + $lines[$line_num + 1] = "\t" . '"$(LINK)” /nologo $(PHP_GLOBAL_OBJS_RESP) $(CLI_GLOBAL_OBJS_RESP) $(STATIC_EXT_OBJS_RESP) $(STATIC_EXT_LIBS) $(ASM_OBJS) $(LIBS) $(LIBS_CLI) $(BUILD_DIR)\php.exe.res /out:$(BUILD_DIR)\php.exe $(LDFLAGS) $(LDFLAGS_CLI) /ltcg /nodefaultlib:msvcrt /nodefaultlib:msvcrtd /ignore:4286'; + FileSystem::writeFile("{$package->getSourceDir()}\\Makefile", implode("\r\n", $lines)); + } + + #[Stage] + public function makeCliForWindows(TargetPackage $package, PackageBuilder $builder, PackageInstaller $installer): void + { + InteractiveTerm::setMessage('Building php: ' . ConsoleColor::yellow('php.exe')); + + // Collect static-libs@windows from all resolved library packages. + // PHP's configure.bat only adds libs declared by enabled extensions via config.w32; + // transitive library-only deps (e.g. zlibstatic.lib needed by libcrypto.lib) are + // not covered. Inject them here so the final link step has all required symbols. + $resolved_libs = []; + foreach ($installer->getResolvedPackages(LibraryPackage::class) as $lib) { + foreach (PackageConfig::get($lib->getName(), 'static-libs', []) as $lib_file) { + if (file_exists("{$package->getLibDir()}\\{$lib_file}")) { + $resolved_libs[] = $lib_file; + } + } + } + $resolved_libs = array_unique($resolved_libs); + + // extra lib + $extra_libs = trim((getenv('SPC_EXTRA_LIBS') ?: '') . ' ' . implode(' ', $resolved_libs)); + // Add debug symbols for release build if --no-strip is specified + // We need to modify CFLAGS to replace /Ox with /Zi and add /DEBUG to LDFLAGS + $debug_overrides = ''; + if ($package->getBuildOption('no-strip', false)) { + // Read current CFLAGS from Makefile and replace optimization flags + $makefile_content = file_get_contents("{$package->getSourceDir()}\\Makefile"); + if (preg_match('/^CFLAGS=(.+?)$/m', $makefile_content, $matches)) { + $cflags = $matches[1]; + // Replace /Ox (full optimization) with /Zi (debug info) and /Od (disable optimization) + // Keep optimization for speed: /O2 /Zi instead of /Od /Zi + $cflags = str_replace('/Ox ', '/O2 /Zi ', $cflags); + $debug_overrides = '"CFLAGS=' . $cflags . '" "LDFLAGS=/DEBUG /LTCG /INCREMENTAL:NO" "LDFLAGS_CLI=/DEBUG" '; + } + } + + cmd()->cd($package->getSourceDir()) + ->exec("nmake /nologo {$debug_overrides}LIBS_CLI=\"ws2_32.lib shell32.lib {$extra_libs}\" EXTRA_LD_FLAGS_PROGRAM= php.exe"); + + $this->deployWindowsBinary($builder, $package, 'php-cli'); + } + + #[BeforeStage('php', [self::class, 'makeCgiForWindows'])] + #[PatchDescription('Patch Windows Makefile for CGI target')] + public function patchCGITarget(TargetPackage $package): void + { + // search Makefile code line contains "$(BUILD_DIR)\php-cgi.exe:" + $content = FileSystem::readFile("{$package->getSourceDir()}\\Makefile"); + $lines = explode("\r\n", $content); + $line_num = 0; + $found = false; + foreach ($lines as $v) { + if (str_contains($v, '$(BUILD_DIR)\php-cgi.exe:')) { + $found = $line_num; + break; + } + ++$line_num; + } + if ($found === false) { + throw new PatchException('Windows Makefile patching for php-cgi.exe target', 'Cannot patch windows CGI Makefile, Makefile does not contain "$(BUILD_DIR)\php-cgi.exe:" line'); + } + $lines[$line_num] = '$(BUILD_DIR)\php-cgi.exe: $(DEPS_CGI) $(CGI_GLOBAL_OBJS) $(PHP_GLOBAL_OBJS) $(STATIC_EXT_OBJS) $(ASM_OBJS) $(BUILD_DIR)\php-cgi.exe.res $(BUILD_DIR)\php-cgi.exe.manifest'; + $lines[$line_num + 1] = "\t" . '@"$(LINK)” /nologo $(PHP_GLOBAL_OBJS_RESP) $(CGI_GLOBAL_OBJS_RESP) $(STATIC_EXT_OBJS_RESP) $(STATIC_EXT_LIBS) $(ASM_OBJS) $(LIBS) $(LIBS_CGI) $(BUILD_DIR)\php-cgi.exe.res /out:$(BUILD_DIR)\php-cgi.exe $(LDFLAGS) $(LDFLAGS_CGI) /ltcg /nodefaultlib:msvcrt /nodefaultlib:msvcrtd /ignore:4286'; + FileSystem::writeFile("{$package->getSourceDir()}\\Makefile", implode("\r\n", $lines)); + + // Patch cgi-static, comment ZEND_TSRMLS_CACHE_DEFINE() + FileSystem::replaceFileRegex("{$package->getSourceDir()}\\sapi\\cgi\\cgi_main.c", '/^ZEND_TSRMLS_CACHE_DEFINE\(\)/m', '// ZEND_TSRMLS_CACHE_DEFINE()'); + } + + #[Stage] + public function makeCgiForWindows(TargetPackage $package, PackageBuilder $builder, PackageInstaller $installer): void + { + InteractiveTerm::setMessage('Building php: ' . ConsoleColor::yellow('php-cgi.exe')); + + // Collect static-libs@windows from all resolved library packages. + $resolved_libs = []; + foreach ($installer->getResolvedPackages(LibraryPackage::class) as $lib) { + foreach (PackageConfig::get($lib->getName(), 'static-libs', []) as $lib_file) { + if (file_exists("{$package->getLibDir()}\\{$lib_file}")) { + $resolved_libs[] = $lib_file; + } + } + } + $resolved_libs = array_unique($resolved_libs); + + // extra lib + $extra_libs = trim((getenv('SPC_EXTRA_LIBS') ?: '') . ' ' . implode(' ', $resolved_libs)); + // Add debug symbols for release build if --no-strip is specified + $debug_overrides = ''; + if ($package->getBuildOption('no-strip', false)) { + $makefile_content = file_get_contents("{$package->getSourceDir()}\\Makefile"); + if (preg_match('/^CFLAGS=(.+?)$/m', $makefile_content, $matches)) { + $cflags = $matches[1]; + $cflags = str_replace('/Ox ', '/O2 /Zi ', $cflags); + $debug_overrides = '"CFLAGS=' . $cflags . '" "LDFLAGS=/DEBUG /LTCG /INCREMENTAL:NO" "LDFLAGS_CGI=/DEBUG" '; + } + } + + cmd()->cd($package->getSourceDir()) + ->exec("nmake /nologo {$debug_overrides}LIBS_CGI=\"ws2_32.lib kernel32.lib advapi32.lib {$extra_libs}\" EXTRA_LD_FLAGS_PROGRAM= php-cgi.exe"); + + $this->deployWindowsBinary($builder, $package, 'php-cgi'); + } + + #[Stage] + public function makeForWindows(TargetPackage $package, PackageInstaller $installer): void + { + V2CompatLayer::emitPatchPoint('before-php-make'); + InteractiveTerm::setMessage('Building php: ' . ConsoleColor::yellow('nmake clean')); + cmd()->cd($package->getSourceDir())->exec('nmake clean'); + + if ($installer->isPackageResolved('php-cli')) { + $package->runStage([$this, 'makeCliForWindows']); + } + if ($installer->isPackageResolved('php-cgi')) { + $package->runStage([$this, 'makeCgiForWindows']); + } + if ($installer->isPackageResolved('php-micro')) { + $package->runStage([$this, 'makeMicroForWindows']); + } + if ($installer->isPackageResolved('php-embed')) { + $package->runStage([$this, 'makeEmbedForWindows']); + } + } + + #[Stage] + public function makeMicroForWindows(TargetPackage $package, PackageBuilder $builder, PackageInstaller $installer): void + { + InteractiveTerm::setMessage('Building php: ' . ConsoleColor::yellow('micro.sfx')); + + // workaround for fiber (originally from https://github.com/dixyes/lwmbs/blob/master/windows/MicroBuild.php) + $makefile = FileSystem::readFile("{$package->getSourceDir()}\\Makefile"); + if ($this->getPHPVersionID() >= 80200 && str_contains($makefile, 'FIBER_ASM_ARCH')) { + $makefile .= "\r\n" . '$(MICRO_SFX): $(BUILD_DIR)\Zend\jump_$(FIBER_ASM_ARCH)_ms_pe_masm.obj $(BUILD_DIR)\Zend\make_$(FIBER_ASM_ARCH)_ms_pe_masm.obj' . "\r\n\r\n"; + } elseif ($this->getPHPVersionID() >= 80400 && str_contains($makefile, 'FIBER_ASM_ABI')) { + $makefile .= "\r\n" . '$(MICRO_SFX): $(BUILD_DIR)\Zend\jump_$(FIBER_ASM_ABI).obj $(BUILD_DIR)\Zend\make_$(FIBER_ASM_ABI).obj' . "\r\n\r\n"; + } + FileSystem::writeFile("{$package->getSourceDir()}\\Makefile", $makefile); + + // Collect static-libs@windows from all resolved library packages. + $resolved_libs = []; + foreach ($installer->getResolvedPackages(LibraryPackage::class) as $lib) { + foreach (PackageConfig::get($lib->getName(), 'static-libs', []) as $lib_file) { + if (file_exists("{$package->getLibDir()}\\{$lib_file}")) { + $resolved_libs[] = $lib_file; + } + } + } + $resolved_libs = array_unique($resolved_libs); + + // extra lib + $extra_libs = trim((getenv('SPC_EXTRA_LIBS') ?: '') . ' ' . implode(' ', $resolved_libs)); + // Add debug symbols for release build if --no-strip is specified + $debug_overrides = ''; + if ($package->getBuildOption('no-strip', false)) { + $makefile_content = file_get_contents("{$package->getSourceDir()}\\Makefile"); + if (preg_match('/^CFLAGS=(.+?)$/m', $makefile_content, $matches)) { + $cflags = $matches[1]; + $cflags = str_replace('/Ox ', '/O2 /Zi ', $cflags); + $debug_overrides = '"CFLAGS=' . $cflags . '" "LDFLAGS=/DEBUG /LTCG /INCREMENTAL:NO" "LDFLAGS_MICRO=/DEBUG" '; + } + } + + $fake_cli = $package->getBuildOption('with-micro-fake-cli', false) ? ' /DPHP_MICRO_FAKE_CLI' : ''; + + // phar patch for micro + $phar_patched = false; + if ($installer->isPackageResolved('ext-phar')) { + $phar_patched = true; + SourcePatcher::patchMicroPhar(self::getPHPVersionID()); + } + + try { + cmd()->cd($package->getSourceDir()) + ->exec("nmake /nologo {$debug_overrides}LIBS_MICRO=\"ws2_32.lib shell32.lib {$extra_libs}\" CFLAGS_MICRO=\"/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1{$fake_cli}\" EXTRA_LD_FLAGS_PROGRAM= micro"); + } finally { + if ($phar_patched) { + SourcePatcher::unpatchMicroPhar(); + } + } + + $this->deployWindowsBinary($builder, $package, 'php-micro'); + } + + #[BeforeStage('php', [self::class, 'makeEmbedForWindows'])] + #[PatchDescription('Patch Windows Makefile for embed static library target')] + public function patchEmbedTarget(TargetPackage $package): void + { + $makefile_path = "{$package->getSourceDir()}\\Makefile"; + $content = FileSystem::readFile($makefile_path); + + // PHP's configure.bat generates PHP_LDFLAGS with /nodefaultlib:libcmt to avoid CRT + // duplication in a normal /MD build. But our static build compiles everything with /MT, + // so every .obj file has DEFAULTLIB:LIBCMT embedded. Removing /nodefaultlib:libcmt lets + // the linker pick up libcmt.lib. We also exclude the dynamic CRT (/nodefaultlib:msvcrt + // /nodefaultlib:msvcrtd) to keep the DLL dependency-free, consistent with CLI/CGI/micro. + $content = str_replace( + 'PHP_LDFLAGS=$(DLL_LDFLAGS) /nodefaultlib:libcmt /def:$(PHPDEF)', + 'PHP_LDFLAGS=$(DLL_LDFLAGS) /nodefaultlib:msvcrt /nodefaultlib:msvcrtd /def:$(PHPDEF) /ltcg /ignore:4286', + $content + ); + + // Patch embed lib target to build a REAL static library instead of just an import lib. + // The default embed target only includes embed SAPI objects and links against php8.lib (import lib). + // We need to include PHP core objects (PHP_GLOBAL_OBJS) and static extension objects (STATIC_EXT_OBJS) + // to create a self-contained static library that doesn't require php8.dll at runtime. + $major = intdiv($this->getPHPVersionID(), 10000); + $embed_lib = "php{$major}embed.lib"; + + // Find and replace the embed lib build rule + // Actual Makefile format (note the backslash before $(PHPLIB)): + // $(BUILD_DIR)\php8embed.lib: $(DEPS_EMBED) $(EMBED_GLOBAL_OBJS) $(BUILD_DIR)\$(PHPLIB) $(BUILD_DIR)\php8embed.lib.res $(BUILD_DIR)\php8embed.lib.manifest + // @$(MAKE_LIB) /nologo /out:$(BUILD_DIR)\php8embed.lib $(ARFLAGS) $(EMBED_GLOBAL_OBJS_RESP) $(BUILD_DIR)\$(PHPLIB) $(ARFLAGS_EMBED) $(LIBS_EMBED) $(BUILD_DIR)\php8embed.lib.res + $lines = explode("\r\n", $content); + $new_lines = []; + $i = 0; + while ($i < count($lines)) { + $line = $lines[$i]; + // Check if this is the embed lib target dependency line (contains the lib name and $(BUILD_DIR)\$(PHPLIB)) + if (str_contains($line, "\$(BUILD_DIR)\\{$embed_lib}:") && str_contains($line, '$(BUILD_DIR)\\$(PHPLIB)')) { + // Replace the dependency line + // Original: $(BUILD_DIR)\php8embed.lib: $(DEPS_EMBED) $(EMBED_GLOBAL_OBJS) $(BUILD_DIR)\$(PHPLIB) $(BUILD_DIR)\php8embed.lib.res $(BUILD_DIR)\php8embed.lib.manifest + // New: $(BUILD_DIR)\php8embed.lib: $(DEPS_EMBED) $(EMBED_GLOBAL_OBJS) $(PHP_GLOBAL_OBJS) $(STATIC_EXT_OBJS) $(ASM_OBJS) $(BUILD_DIR)\php8embed.lib.res $(BUILD_DIR)\php8embed.lib.manifest + $new_deps = "\$(BUILD_DIR)\\{$embed_lib}: \$(DEPS_EMBED) \$(EMBED_GLOBAL_OBJS) \$(PHP_GLOBAL_OBJS) \$(STATIC_EXT_OBJS) \$(ASM_OBJS) \$(BUILD_DIR)\\{$embed_lib}.res \$(BUILD_DIR)\\{$embed_lib}.manifest"; + $new_lines[] = $new_deps; + // Skip the original line (we replaced it) + ++$i; + // Now look for the lib.exe command line (should be the next non-empty line starting with tab) + while ($i < count($lines) && trim($lines[$i]) === '') { + $new_lines[] = $lines[$i]; + ++$i; + } + // Replace the lib.exe command to include PHP_GLOBAL_OBJS_RESP and STATIC_EXT_OBJS_RESP + // Original: @$(MAKE_LIB) /nologo /out:$(BUILD_DIR)\php8embed.lib $(ARFLAGS) $(EMBED_GLOBAL_OBJS_RESP) $(BUILD_DIR)\$(PHPLIB) $(ARFLAGS_EMBED) $(LIBS_EMBED) $(BUILD_DIR)\php8embed.lib.res + // New: @$(MAKE_LIB) /nologo /out:$(BUILD_DIR)\php8embed.lib $(ARFLAGS) $(EMBED_GLOBAL_OBJS_RESP) $(PHP_GLOBAL_OBJS_RESP) $(STATIC_EXT_OBJS_RESP) $(ASM_OBJS) $(STATIC_EXT_LIBS) $(ARFLAGS_EMBED) $(LIBS_EMBED) $(BUILD_DIR)\php8embed.lib.res + if ($i < count($lines) && str_contains($lines[$i], '$(MAKE_LIB)')) { + $cmd_line = $lines[$i]; + // Remove $(BUILD_DIR)\$(PHPLIB) from the command (note the backslash) + $cmd_line = str_replace(' $(BUILD_DIR)\\$(PHPLIB)', '', $cmd_line); + // Add PHP_GLOBAL_OBJS_RESP and STATIC_EXT_OBJS_RESP after EMBED_GLOBAL_OBJS_RESP + $cmd_line = str_replace( + '$(EMBED_GLOBAL_OBJS_RESP)', + '$(EMBED_GLOBAL_OBJS_RESP) $(PHP_GLOBAL_OBJS_RESP) $(STATIC_EXT_OBJS_RESP) $(ASM_OBJS) $(STATIC_EXT_LIBS)', + $cmd_line + ); + $new_lines[] = $cmd_line; + ++$i; + } + } else { + $new_lines[] = $line; + ++$i; + } + } + $content = implode("\r\n", $new_lines); + + FileSystem::writeFile($makefile_path, $content); + } + + #[Stage] + public function makeEmbedForWindows(TargetPackage $package, PackageBuilder $builder, PackageInstaller $installer): void + { + $major = intdiv($this->getPHPVersionID(), 10000); + $embed_lib = "php{$major}embed.lib"; + InteractiveTerm::setMessage('Building php: ' . ConsoleColor::yellow($embed_lib)); + + // Add debug symbols for release build if --no-strip is specified + $debug_overrides = ''; + if ($package->getBuildOption('no-strip', false)) { + $makefile_content = file_get_contents("{$package->getSourceDir()}\\Makefile"); + if (preg_match('/^CFLAGS=(.+?)$/m', $makefile_content, $matches)) { + $cflags = $matches[1]; + $cflags = str_replace('/Ox ', '/O2 /Zi ', $cflags); + $debug_overrides = '"CFLAGS=' . $cflags . '" "LDFLAGS=/DEBUG /LTCG /INCREMENTAL:NO" '; + } + } + + // Build the embed static library (patched to include PHP core and extension objects) + cmd()->cd($package->getSourceDir()) + ->exec("nmake /nologo {$debug_overrides}{$embed_lib}"); + + // Deploy: php8embed.lib is now a REAL static library containing all PHP code + $rel_type = 'Release'; // TODO: Debug build support + $ts = $builder->getOption('enable-zts') ? '_TS' : ''; + $build_dir = "{$package->getSourceDir()}\\x64\\{$rel_type}{$ts}"; + + // copy static embed lib to buildroot/lib + $embed_lib_src = "{$build_dir}\\{$embed_lib}"; + if (file_exists($embed_lib_src)) { + FileSystem::copy($embed_lib_src, "{$package->getLibDir()}\\{$embed_lib}"); + $package->setOutput('Static library path for embed SAPI', "{$package->getLibDir()}\\{$embed_lib}"); + } + + // Note: We no longer deploy php8.dll because the embed static library is self-contained. + // All PHP core code, extensions, and embed SAPI are statically linked into php8embed.lib. + + // copy .pdb debug info if --no-strip + $debug_dir = BUILD_ROOT_PATH . '\debug'; + if ($builder->getOption('no-strip', false)) { + $pdb = "{$build_dir}\\php{$major}embed.pdb"; + if (file_exists($pdb)) { + FileSystem::createDir($debug_dir); + FileSystem::copy($pdb, "{$debug_dir}\\php{$major}embed.pdb"); + } + } + + // Install PHP headers for embed SAPI development + $this->installPhpHeadersForWindows($package, $installer); + } + + /** + * Install PHP headers to buildroot/include for embed SAPI development. + * This mirrors the 'make install-headers' behavior on Unix. + */ + private function installPhpHeadersForWindows(TargetPackage $package, PackageInstaller $installer): void + { + InteractiveTerm::setMessage('Installing PHP headers for embed SAPI'); + + $source_dir = $package->getSourceDir(); + $include_dir = $package->getIncludeDir(); + $php_include_dir = "{$include_dir}\\php"; + + // Create directory structure + FileSystem::createDir("{$php_include_dir}\\main"); + FileSystem::createDir("{$php_include_dir}\\Zend"); + FileSystem::createDir("{$php_include_dir}\\TSRM"); + FileSystem::createDir("{$php_include_dir}\\sapi\\embed"); + + // Copy main/*.h + foreach (glob("{$source_dir}\\main\\*.h") as $h) { + FileSystem::copy($h, "{$php_include_dir}\\main\\" . basename($h)); + } + + // Copy Zend/*.h + foreach (glob("{$source_dir}\\Zend\\*.h") as $h) { + $target = "{$php_include_dir}\\Zend\\" . basename($h); + FileSystem::copy($h, $target); + // Fix GCC-specific #warning directive not supported by MSVC + if (basename($h) === 'zend_atomic.h') { + FileSystem::replaceFileStr($target, '#warning No atomics support detected. Please open an issue with platform details.', '#pragma message("No atomics support detected. Please open an issue with platform details.")'); + } + } + + // Copy TSRM/*.h + foreach (glob("{$source_dir}\\TSRM\\*.h") as $h) { + FileSystem::copy($h, "{$php_include_dir}\\TSRM\\" . basename($h)); + } + + // Copy embed SAPI header + FileSystem::copy("{$source_dir}\\sapi\\embed\\php_embed.h", "{$php_include_dir}\\sapi\\embed\\php_embed.h"); + + // Copy generated config.h (config.w32.h on Windows) to php_config.h + $rel_type = 'Release'; + $ts = $package->getBuildOption('enable-zts', false) ? '_TS' : ''; + $build_dir = "{$source_dir}\\x64\\{$rel_type}{$ts}"; + + // Always copy config.w32.h from source (it's used for both build and headers) + if (file_exists("{$source_dir}\\main\\config.w32.h")) { + FileSystem::copy("{$source_dir}\\main\\config.w32.h", "{$php_include_dir}\\main\\php_config.h"); + } + + // Windows: zend_config.w32.h must be copied as zend_config.h for Zend headers to work + if (file_exists("{$source_dir}\\Zend\\zend_config.w32.h")) { + FileSystem::copy("{$source_dir}\\Zend\\zend_config.w32.h", "{$php_include_dir}\\Zend\\zend_config.h"); + } + + // Copy extension headers for enabled extensions + foreach ($installer->getResolvedPackages(PhpExtensionPackage::class) as $ext) { + $ext_name = $ext->getExtensionName(); + $ext_dir = "{$source_dir}\\ext\\{$ext_name}"; + if (is_dir($ext_dir)) { + $target_ext_dir = "{$php_include_dir}\\ext\\{$ext_name}"; + FileSystem::createDir($target_ext_dir); + foreach (glob("{$ext_dir}\\*.h") as $h) { + FileSystem::copy($h, "{$target_ext_dir}\\" . basename($h)); + } + // Also copy any arginfo headers + foreach (glob("{$ext_dir}\\*_arginfo.h") as $h) { + if (!file_exists("{$target_ext_dir}\\" . basename($h))) { + FileSystem::copy($h, "{$target_ext_dir}\\" . basename($h)); + } + } + } + } + + $package->setOutput('PHP headers path for embed SAPI', $php_include_dir); + } + + #[BuildFor('Windows')] + public function buildWin(TargetPackage $package): void + { + if ($package->getName() !== 'php') { + return; + } + + $package->runStage([$this, 'buildconfForWindows']); + $package->runStage([$this, 'configureForWindows']); + $package->runStage([$this, 'makeForWindows']); + } + + #[BeforeStage('php', [self::class, 'buildconfForWindows'])] + #[PatchDescription('Patch SPC_MICRO_PATCHES defined patches')] + #[PatchDescription('Fix PHP 8.1 static build bug on Windows')] + #[PatchDescription('Fix PHP Visual Studio version detection')] + public function patchBeforeBuildconfForWindows(TargetPackage $package): void + { + // php-src patches from micro + SourcePatcher::patchPhpSrc(); + + // php 8.1 bug + if ($this->getPHPVersionID() >= 80100 && $this->getPHPVersionID() < 80200) { + logger()->info('Patching PHP 8.1 windows Fiber bug'); + FileSystem::replaceFileStr( + "{$package->getSourceDir()}\\win32\\build\\config.w32", + "ADD_FLAG('LDFLAGS', '$(BUILD_DIR)\\\\Zend\\\\jump_' + FIBER_ASM_ARCH + '_ms_pe_masm.obj');", + "ADD_FLAG('ASM_OBJS', '$(BUILD_DIR)\\\\Zend\\\\jump_' + FIBER_ASM_ARCH + '_ms_pe_masm.obj $(BUILD_DIR)\\\\Zend\\\\make_' + FIBER_ASM_ARCH + '_ms_pe_masm.obj');" + ); + FileSystem::replaceFileStr( + "{$package->getSourceDir()}\\win32\\build\\config.w32", + "ADD_FLAG('LDFLAGS', '$(BUILD_DIR)\\\\Zend\\\\make_' + FIBER_ASM_ARCH + '_ms_pe_masm.obj');", + '' + ); + } + + // Fix PHP VS version + // get vs version + $vc = WindowsUtil::findVisualStudio(); + if ($vc === false) { + $vc_matches = ['unknown', 'unknown']; + } else { + $vc_matches = match ($vc['major_version']) { + '17' => ['VS17', 'Visual C++ 2022'], + '16' => ['VS16', 'Visual C++ 2019'], + default => ['unknown', 'unknown'], + }; + } + // patch php-src/win32/build/confutils.js + FileSystem::replaceFileStr( + "{$package->getSourceDir()}\\win32\\build\\confutils.js", + 'var name = "unknown";', + "var name = short ? \"{$vc_matches[0]}\" : \"{$vc_matches[1]}\";return name;" + ); + + // patch micro win32 + if ($package->getBuildOption('enable-micro-win32') && !file_exists("{$package->getSourceDir()}\\sapi\\micro\\php_micro.c.win32bak")) { + copy("{$package->getSourceDir()}\\sapi\\micro\\php_micro.c", "{$package->getSourceDir()}\\sapi\\micro\\php_micro.c.win32bak"); + FileSystem::replaceFileStr("{$package->getSourceDir()}\\sapi\\micro\\php_micro.c", '#include "php_variables.h"', '#include "php_variables.h"' . "\n#define PHP_MICRO_WIN32_NO_CONSOLE 1"); + } else { + if (file_exists("{$package->getSourceDir()}\\sapi\\micro\\php_micro.c.win32bak")) { + rename("{$package->getSourceDir()}\\sapi\\micro\\php_micro.c.win32bak", "{$package->getSourceDir()}\\sapi\\micro\\php_micro.c"); + } + } + } + + #[Stage] + public function smokeTestForWindows(PackageBuilder $builder, TargetPackage $package, PackageInstaller $installer): void + { + // analyse --no-smoke-test option + $no_smoke_test = $builder->getOption('no-smoke-test'); + $option = match ($no_smoke_test) { + false => false, + null => 'all', + default => parse_comma_list($no_smoke_test), + }; + $valid_tests = ['cli', 'cgi', 'micro', 'micro-exts', 'embed']; + // compat: --without-micro-ext-test is equivalent to --no-smoke-test=micro-exts + if ($builder->getOption('without-micro-ext-test', false)) { + $valid_tests = array_diff($valid_tests, ['micro-exts']); + } + if (is_array($option)) { + foreach ($option as $test) { + if (!in_array($test, $valid_tests, true)) { + throw new WrongUsageException("Invalid value for --no-smoke-test: {$test}. Valid values are: " . implode(', ', $valid_tests)); + } + $valid_tests = array_diff($valid_tests, [$test]); + } + } elseif ($option === 'all') { + $valid_tests = []; + } + + // remove all .dll from buildroot/bin/ + $dlls = glob(BUILD_BIN_PATH . '\*.dll') ?: []; + foreach ($dlls as $dll) { + @unlink($dll); + } + + if (in_array('cli', $valid_tests, true) && $installer->isPackageResolved('php-cli')) { + $package->runStage([$this, 'smokeTestCliForWindows']); + } + if (in_array('cgi', $valid_tests, true) && $installer->isPackageResolved('php-cgi')) { + $package->runStage([$this, 'smokeTestCgiForWindows']); + } + if (in_array('micro', $valid_tests, true) && $installer->isPackageResolved('php-micro')) { + $skipExtTest = !in_array('micro-exts', $valid_tests, true); + $package->runStage([$this, 'smokeTestMicroForWindows'], ['skipExtTest' => $skipExtTest]); + } + if (in_array('embed', $valid_tests, true) && $installer->isPackageResolved('php-embed')) { + $package->runStage([$this, 'smokeTestEmbedForWindows'], ['installer' => $installer]); + } + } + + #[Stage] + public function smokeTestCliForWindows(PackageInstaller $installer): void + { + InteractiveTerm::setMessage('Running basic php-cli smoke test'); + [$ret, $output] = cmd()->execWithResult(BUILD_BIN_PATH . '\php.exe -n -r "echo \"hello\";"'); + $raw_output = implode('', $output); + if ($ret !== 0 || trim($raw_output) !== 'hello') { + throw new ValidationException("cli failed smoke test. code: {$ret}, output: {$raw_output}", validation_module: 'php-cli smoke test'); + } + + $exts = $installer->getResolvedPackages(PhpExtensionPackage::class); + foreach ($exts as $ext) { + InteractiveTerm::setMessage('Running php-cli smoke test for ' . ConsoleColor::yellow($ext->getExtensionName()) . ' extension'); + $ext->runSmokeTestCliWindows(); + } + } + + #[Stage] + public function smokeTestCgiForWindows(): void + { + InteractiveTerm::setMessage('Running basic php-cgi smoke test'); + FileSystem::writeFile(SOURCE_PATH . '\php-cgi-test.php', 'Hello, World!"; ?>'); + [$ret, $output] = cmd()->execWithResult(BUILD_BIN_PATH . '\php-cgi.exe -n -f ' . SOURCE_PATH . '\php-cgi-test.php'); + $raw_output = implode("\n", $output); + if ($ret !== 0 || !str_contains($raw_output, 'Hello, World!')) { + throw new ValidationException("cgi failed smoke test. code: {$ret}, output: {$raw_output}", validation_module: 'php-cgi smoke test'); + } + } + + #[Stage] + public function smokeTestMicroForWindows(PackageInstaller $installer, bool $skipExtTest = false): void + { + $micro_sfx = BUILD_BIN_PATH . '\micro.sfx'; + + InteractiveTerm::setMessage('Running php-micro smoke test'); + $content = $skipExtTest + ? 'generateMicroExtTests($installer); + $test_file = SOURCE_PATH . '\micro_ext_test.exe'; + if (file_exists($test_file)) { + @unlink($test_file); + } + file_put_contents($test_file, file_get_contents($micro_sfx) . $content); + [$ret, $out] = cmd()->execWithResult($test_file); + $raw_out = trim(implode('', $out)); + if ($ret !== 0 || !str_starts_with($raw_out, '[micro-test-start]') || !str_ends_with($raw_out, '[micro-test-end]')) { + throw new ValidationException( + "micro_ext_test failed. code: {$ret}, output: {$raw_out}", + validation_module: 'phpmicro sanity check item [micro_ext_test]' + ); + } + } + + #[Stage] + public function smokeTestEmbedForWindows(PackageInstaller $installer, TargetPackage $package): void + { + $test_dir = SOURCE_PATH . '\embed-test'; + FileSystem::createDir($test_dir); + + // Create embed.c test file (Windows version) + $embed_c = <<<'C_CODE' +#include + +int main(int argc, char **argv) { + PHP_EMBED_START_BLOCK(argc, argv) + + zend_file_handle file_handle; + zend_stream_init_filename(&file_handle, "embed.php"); + + if (!php_execute_script(&file_handle)) { + php_printf("Failed to execute PHP script.\n"); + } + + PHP_EMBED_END_BLOCK() + return 0; +} +C_CODE; + FileSystem::writeFile($test_dir . '\embed.c', $embed_c); + + // Create embed.php test file + FileSystem::writeFile($test_dir . '\embed.php', "config(array_map(fn ($x) => $x->getName(), $installer->getResolvedPackages())); + + // Build the embed test executable using cl.exe + // Note: MSVCToolchain already initialized the VC environment, no need for vcvarsall + InteractiveTerm::setMessage('Running php-embed build smoke test'); + + // For Windows, we need to use PHP source directory headers directly + // because Windows PHP doesn't use php_config.h like Unix + $source_dir = $package->getSourceDir(); + $rel_type = 'Release'; + $ts = $package->getBuildOption('enable-zts', false) ? '_TS' : ''; + $build_dir = "{$source_dir}\\x64\\{$rel_type}{$ts}"; + + // Build include flags pointing to source dirs (like PHP Windows build does) + // Note: embed.c uses #include , so we need $source_dir itself + $include_flags = sprintf( + '/I"%s" /I"%s\main" /I"%s\Zend" /I"%s\TSRM" /I"%s" ' . + '/D ZEND_WIN32=1 /D PHP_WIN32=1 /D WIN32 /D _WINDOWS /D WINDOWS=1 /D _MBCS /D _USE_MATH_DEFINES', + $build_dir, + $source_dir, + $source_dir, + $source_dir, + $source_dir + ); + + // MSVC cl.exe format: compiler flags must come before /link, linker flags after + // ldflags contains /LIBPATH which must be after /link + $compile_cmd = sprintf( + 'cl.exe /nologo /O2 /MT /Z7 %s embed.c /Fe:embed.exe /link /LIBPATH:"%s\lib" %s %s', + $include_flags, + BUILD_ROOT_PATH, + $config['libs'], + 'kernel32.lib ole32.lib user32.lib advapi32.lib shell32.lib ws2_32.lib dnsapi.lib psapi.lib bcrypt.lib' // Windows system libs (match Makefile LIBS) + ); + + // Log command explicitly (workaround for cmd() not logging complex commands properly) + logger()->debug('Embed smoke test compile command: ' . $compile_cmd); + + [$ret, $out] = cmd()->cd($test_dir)->execWithResult($compile_cmd); + if ($ret !== 0) { + throw new ValidationException( + 'embed failed to build. Error message: ' . implode("\n", $out), + validation_module: 'php-embed build smoke test' + ); + } + + // Run the embed test + InteractiveTerm::setMessage('Running php-embed run smoke test'); + [$ret, $output] = cmd()->cd($test_dir)->execWithResult('embed.exe'); + $raw_output = implode('', $output); + if ($ret !== 0 || trim($raw_output) !== 'hello') { + throw new ValidationException( + 'embed failed to run. Error message: ' . $raw_output, + validation_module: 'php-embed run smoke test' + ); + } + } + + protected function deployWindowsBinary(PackageBuilder $builder, TargetPackage $package, string $sapi): void + { + $rel_type = 'Release'; // TODO: Debug build support + $ts = $builder->getOption('enable-zts') ? '_TS' : ''; + $debug_dir = BUILD_ROOT_PATH . '\debug'; + $src = match ($sapi) { + 'php-cli' => ["{$package->getSourceDir()}\\x64\\{$rel_type}{$ts}", 'php.exe', 'php.pdb'], + 'php-micro' => ["{$package->getSourceDir()}\\x64\\{$rel_type}{$ts}", 'micro.sfx', 'micro.pdb'], + 'php-cgi' => ["{$package->getSourceDir()}\\x64\\{$rel_type}{$ts}", 'php-cgi.exe', 'php-cgi.pdb'], + default => throw new SPCInternalException("Deployment does not accept type {$sapi}"), + }; + $src_file = "{$src[0]}\\{$src[1]}"; + $dst_file = BUILD_BIN_PATH . '\\' . basename($src_file); + + $builder->deployBinary($src_file, $dst_file); + + // copy .pdb debug info file + if ($builder->getOption('no-strip', false) && file_exists("{$src[0]}\\{$src[2]}")) { + FileSystem::createDir($debug_dir); + FileSystem::copy("{$src[0]}\\{$src[2]}", "{$debug_dir}\\{$src[2]}"); + } + + // with-upx-pack for cli, cgi and micro + if ($builder->getOption('with-upx-pack', false)) { + if (in_array($sapi, ['php-cli', 'php-cgi', 'php-micro'], true)) { + cmd()->exec(getenv('UPX_EXEC') . ' --best ' . escapeshellarg($dst_file)); + } + } + } +} diff --git a/src/SPC/ConsoleApplication.php b/src/SPC/ConsoleApplication.php index 750c49e4b..79f7d724a 100644 --- a/src/SPC/ConsoleApplication.php +++ b/src/SPC/ConsoleApplication.php @@ -34,7 +34,7 @@ */ final class ConsoleApplication extends Application { - public const string VERSION = '2.8.3'; + public const string VERSION = '2.8.4'; public function __construct() { diff --git a/src/SPC/builder/Extension.php b/src/SPC/builder/Extension.php index f5a5d9561..9077269e5 100644 --- a/src/SPC/builder/Extension.php +++ b/src/SPC/builder/Extension.php @@ -96,7 +96,8 @@ public function getLibFilesString(): string fn ($x) => $x->getStaticLibFiles(), $this->getLibraryDependencies(recursive: true) ); - return implode(' ', $ret); + $libs = implode(' ', $ret); + return deduplicate_flags($libs); } /** diff --git a/src/SPC/builder/LibraryBase.php b/src/SPC/builder/LibraryBase.php index 383faa41a..73b1f9ed9 100644 --- a/src/SPC/builder/LibraryBase.php +++ b/src/SPC/builder/LibraryBase.php @@ -365,6 +365,27 @@ protected function installLicense(): void protected function isLibraryInstalled(): bool { + if ($pkg_configs = Config::getLib(static::NAME, 'pkg-configs', [])) { + $pkg_config_path = getenv('PKG_CONFIG_PATH') ?: ''; + $search_paths = array_unique(array_filter(explode(is_unix() ? ':' : ';', $pkg_config_path))); + + foreach ($pkg_configs as $name) { + $found = false; + foreach ($search_paths as $path) { + if (file_exists($path . "/{$name}.pc")) { + $found = true; + break; + } + } + if (!$found) { + return false; + } + } + // allow using system dependencies if pkg_config_path is explicitly defined + if (count($search_paths) > 1) { + return true; + } + } foreach (Config::getLib(static::NAME, 'static-libs', []) as $name) { if (!file_exists(BUILD_LIB_PATH . "/{$name}")) { return false; diff --git a/src/SPC/builder/extension/grpc.php b/src/SPC/builder/extension/grpc.php index 42dcdd5b8..ba15518f2 100644 --- a/src/SPC/builder/extension/grpc.php +++ b/src/SPC/builder/extension/grpc.php @@ -11,7 +11,6 @@ use SPC\util\CustomExt; use SPC\util\GlobalEnvManager; use SPC\util\SPCConfigUtil; -use SPC\util\SPCTarget; #[CustomExt('grpc')] class grpc extends Extension @@ -21,18 +20,50 @@ public function patchBeforeBuildconf(): bool if ($this->builder instanceof WindowsBuilder) { throw new ValidationException('grpc extension does not support windows yet'); } + + // Fix deprecated PHP API usage in call.c FileSystem::replaceFileStr( - $this->source_dir . '/src/php/ext/grpc/call.c', + "{$this->source_dir}/src/php/ext/grpc/call.c", 'zend_exception_get_default(TSRMLS_C),', 'zend_ce_exception,', ); - if (SPCTarget::getTargetOS() === 'Darwin') { - FileSystem::replaceFileRegex( - $this->source_dir . '/config.m4', - '/GRPC_LIBDIR=.*$/m', - 'GRPC_LIBDIR=' . BUILD_LIB_PATH . "\n" . 'LDFLAGS="$LDFLAGS -framework CoreFoundation"' - ); - } + + $config_m4 = <<<'M4' +PHP_ARG_ENABLE(grpc, [whether to enable grpc support], [AS_HELP_STRING([--enable-grpc], [Enable grpc support])]) + +if test "$PHP_GRPC" != "no"; then + PHP_ADD_INCLUDE(PHP_EXT_SRCDIR()/include) + PHP_ADD_INCLUDE(PHP_EXT_SRCDIR()/src/php/ext/grpc) + GRPC_LIBDIR=@@build_lib_path@@ + PHP_ADD_LIBPATH($GRPC_LIBDIR) + PHP_ADD_LIBRARY(grpc,,GRPC_SHARED_LIBADD) + LIBS="-lpthread $LIBS" + PHP_ADD_LIBRARY(pthread) + + case $host in + *darwin*) + PHP_ADD_LIBRARY(c++,1,GRPC_SHARED_LIBADD) + ;; + *) + PHP_ADD_LIBRARY(stdc++,1,GRPC_SHARED_LIBADD) + PHP_ADD_LIBRARY(rt,,GRPC_SHARED_LIBADD) + PHP_ADD_LIBRARY(rt) + ;; + esac + + PHP_NEW_EXTENSION(grpc, @grpc_c_files@, $ext_shared, , -DGRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK=1) + PHP_SUBST(GRPC_SHARED_LIBADD) + PHP_INSTALL_HEADERS([ext/grpc], [php_grpc.h]) +fi +M4; + $replace = get_pack_replace(); + // load grpc c files from src/php/ext/grpc + $c_files = glob($this->source_dir . '/src/php/ext/grpc/*.c'); + $replace['@grpc_c_files@'] = implode(" \\\n ", array_map(fn ($f) => 'src/php/ext/grpc/' . basename($f), $c_files)); + $config_m4 = str_replace(array_keys($replace), array_values($replace), $config_m4); + file_put_contents($this->source_dir . '/config.m4', $config_m4); + + copy($this->source_dir . '/src/php/ext/grpc/php_grpc.h', $this->source_dir . '/php_grpc.h'); return true; } @@ -48,7 +79,6 @@ public function patchBeforeConfigure(): bool public function patchBeforeMake(): bool { parent::patchBeforeMake(); - // add -Wno-strict-prototypes GlobalEnvManager::putenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS=' . getenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS') . ' -Wno-strict-prototypes'); return true; } diff --git a/src/SPC/builder/extension/swoole.php b/src/SPC/builder/extension/swoole.php index 4e292a362..86098ec51 100644 --- a/src/SPC/builder/extension/swoole.php +++ b/src/SPC/builder/extension/swoole.php @@ -50,19 +50,16 @@ public function getUnixConfigureArg(bool $shared = false): string // commonly used feature: coroutine-time $arg .= ' --enable-swoole-coro-time --with-pic'; + $arg .= ' --enable-swoole-ssh --enable-swoole-curl'; $arg .= $this->builder->getOption('enable-zts') ? ' --enable-swoole-thread --disable-thread-context' : ' --disable-swoole-thread --enable-thread-context'; - // required features: curl, openssl (but curl hook is buggy for php 8.0) - $arg .= $this->builder->getPHPVersionID() >= 80100 ? ' --enable-swoole-curl' : ' --disable-swoole-curl'; - $arg .= ' --enable-openssl'; - // additional features that only require libraries $arg .= $this->builder->getLib('libcares') ? ' --enable-cares' : ''; $arg .= $this->builder->getLib('brotli') ? (' --enable-brotli --with-brotli-dir=' . BUILD_ROOT_PATH) : ''; $arg .= $this->builder->getLib('nghttp2') ? (' --with-nghttp2-dir=' . BUILD_ROOT_PATH) : ''; $arg .= $this->builder->getLib('zstd') ? ' --enable-zstd' : ''; - $arg .= $this->builder->getLib('liburing') ? ' --enable-iouring' : ''; + $arg .= $this->builder->getLib('liburing') ? ' --enable-iouring --enable-uring-socket' : ''; $arg .= $this->builder->getExt('sockets') ? ' --enable-sockets' : ''; // enable additional features that require the pdo extension, but conflict with pdo_* extensions @@ -74,6 +71,7 @@ public function getUnixConfigureArg(bool $shared = false): string $config = (new SPCConfigUtil($this->builder))->getLibraryConfig($this->builder->getLib('unixodbc')); $arg .= ' --with-swoole-odbc=unixODBC,' . BUILD_ROOT_PATH . ' SWOOLE_ODBC_LIBS="' . $config['libs'] . '"'; } + $arg .= $this->builder->getExt('ftp')?->isBuildStatic() ? ' --disable-swoole-ftp' : ' --enable-swoole-ftp'; if ($this->getExtVersion() >= '6.1.0') { $arg .= ' --enable-swoole-stdext'; diff --git a/src/SPC/builder/linux/LinuxBuilder.php b/src/SPC/builder/linux/LinuxBuilder.php index 004c37def..d959aade6 100644 --- a/src/SPC/builder/linux/LinuxBuilder.php +++ b/src/SPC/builder/linux/LinuxBuilder.php @@ -162,7 +162,7 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void throw new WrongUsageException( "You're building against musl libc statically (the default on Linux), but you're trying to build shared extensions.\n" . 'Static musl libc does not implement `dlopen`, so your php binary is not able to load shared extensions.' . "\n" . - 'Either use SPC_LIBC=glibc to link against glibc on a glibc OS, or use SPC_TARGET="native-native-musl -dynamic" to link against musl libc dynamically using `zig cc`.' + 'Either use SPC_LIBC=glibc to link against glibc on a glibc OS, use SPC_TARGET="native-native-musl -dynamic" to link against musl libc dynamically using `zig cc` or use SPC_MUSL_DYNAMIC=true on alpine.' ); } logger()->info('Building shared extensions...'); diff --git a/src/SPC/builder/traits/UnixSystemUtilTrait.php b/src/SPC/builder/traits/UnixSystemUtilTrait.php index ff75bf7c2..33f824f36 100644 --- a/src/SPC/builder/traits/UnixSystemUtilTrait.php +++ b/src/SPC/builder/traits/UnixSystemUtilTrait.php @@ -72,6 +72,10 @@ public static function getDynamicExportedSymbols(string $lib_file): ?string if (!is_file($symbol_file)) { throw new SPCInternalException("The symbol file {$symbol_file} does not exist, please check if nm command is available."); } + // https://github.com/ziglang/zig/issues/24662 + if (ToolchainManager::getToolchainClass() === ZigToolchain::class) { + return '-Wl,--export-dynamic'; // needs release 0.16, can be removed then + } // macOS/zig if (SPCTarget::getTargetOS() !== 'Linux' || ToolchainManager::getToolchainClass() === ZigToolchain::class) { return "-Wl,-exported_symbols_list,{$symbol_file}"; diff --git a/src/SPC/builder/unix/UnixBuilderBase.php b/src/SPC/builder/unix/UnixBuilderBase.php index 1b532bbcf..fd16656ce 100644 --- a/src/SPC/builder/unix/UnixBuilderBase.php +++ b/src/SPC/builder/unix/UnixBuilderBase.php @@ -457,6 +457,7 @@ protected function buildFrankenphp(): void 'XCADDY_GO_BUILD_FLAGS' => '-buildmode=pie ' . '-ldflags \"-linkmode=external ' . $extLdFlags . ' ' . '-X \'github.com/caddyserver/caddy/v2/modules/caddyhttp.ServerHeader=FrankenPHP Caddy\' ' . + '-X \'github.com/caddyserver/caddy/v2.CustomBinaryName=frankenphp\' ' . '-X \'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP ' . "v{$frankenPhpVersion} PHP {$libphpVersion} Caddy'\\\" " . "-tags={$muslTags}nobadger,nomysql,nopgx{$nobrotli}{$nowatcher}", diff --git a/src/SPC/builder/unix/library/freetype.php b/src/SPC/builder/unix/library/freetype.php index a10b7fb28..57c1ac05c 100644 --- a/src/SPC/builder/unix/library/freetype.php +++ b/src/SPC/builder/unix/library/freetype.php @@ -13,8 +13,8 @@ protected function build(): void { $cmake = UnixCMakeExecutor::create($this) ->optionalLib('libpng', ...cmake_boolean_args('FT_DISABLE_PNG', true)) - ->optionalLib('bzip2', ...cmake_boolean_args('FT_DISABLE_BZIP2', true)) - ->optionalLib('brotli', ...cmake_boolean_args('FT_DISABLE_BROTLI', true)) + ->addConfigureArgs('-DFT_DISABLE_BZIP2=ON') + ->addConfigureArgs('-DFT_DISABLE_BROTLI=ON') ->addConfigureArgs('-DFT_DISABLE_HARFBUZZ=ON'); // fix cmake 4.0 compatibility diff --git a/src/SPC/builder/unix/library/libde265.php b/src/SPC/builder/unix/library/libde265.php index 9549a2ec9..184a44261 100644 --- a/src/SPC/builder/unix/library/libde265.php +++ b/src/SPC/builder/unix/library/libde265.php @@ -11,7 +11,10 @@ trait libde265 protected function build(): void { UnixCMakeExecutor::create($this) - ->addConfigureArgs('-DENABLE_SDL=OFF') + ->addConfigureArgs( + '-DENABLE_SDL=OFF', + '-DENABLE_DECODER=OFF' + ) ->build(); $this->patchPkgconfPrefix(['libde265.pc']); } diff --git a/src/SPC/builder/unix/library/libjpeg.php b/src/SPC/builder/unix/library/libjpeg.php index 862c1e88d..98881b762 100644 --- a/src/SPC/builder/unix/library/libjpeg.php +++ b/src/SPC/builder/unix/library/libjpeg.php @@ -14,6 +14,7 @@ protected function build(): void ->addConfigureArgs( '-DENABLE_STATIC=ON', '-DENABLE_SHARED=OFF', + '-DWITH_SYSTEM_ZLIB=ON' ) ->build(); // patch pkgconfig diff --git a/src/SPC/builder/unix/library/postgresql.php b/src/SPC/builder/unix/library/postgresql.php index 2ad4f51be..a3aed130b 100644 --- a/src/SPC/builder/unix/library/postgresql.php +++ b/src/SPC/builder/unix/library/postgresql.php @@ -93,8 +93,7 @@ protected function build(): void // remove dynamic libs shell()->cd($this->source_dir . '/build') - ->exec("rm -rf {$this->getBuildRootPath()}/lib/*.so.*") - ->exec("rm -rf {$this->getBuildRootPath()}/lib/*.so") + ->exec("rm -rf {$this->getBuildRootPath()}/lib/*.so*") ->exec("rm -rf {$this->getBuildRootPath()}/lib/*.dylib"); FileSystem::replaceFileStr("{$this->getLibDir()}/pkgconfig/libpq.pc", '-lldap', '-lldap -llber'); diff --git a/src/SPC/builder/unix/library/unixodbc.php b/src/SPC/builder/unix/library/unixodbc.php index 9a3cb63d1..cf923f24b 100644 --- a/src/SPC/builder/unix/library/unixodbc.php +++ b/src/SPC/builder/unix/library/unixodbc.php @@ -5,6 +5,7 @@ namespace SPC\builder\unix\library; use SPC\exception\WrongUsageException; +use SPC\store\FileSystem; use SPC\util\executor\UnixAutoconfExecutor; trait unixodbc @@ -30,7 +31,15 @@ protected function build(): void '--enable-gui=no', ) ->make(); - $this->patchPkgconfPrefix(['odbc.pc', 'odbccr.pc', 'odbcinst.pc']); + $pkgConfigs = ['odbc.pc', 'odbccr.pc', 'odbcinst.pc']; + $this->patchPkgconfPrefix($pkgConfigs); + foreach ($pkgConfigs as $file) { + FileSystem::replaceFileStr( + BUILD_LIB_PATH . "/pkgconfig/{$file}", + '$(top_build_prefix)libltdl/libltdlc.la', + '' + ); + } $this->patchLaDependencyPrefix(); } } diff --git a/src/SPC/builder/windows/library/brotli.php b/src/SPC/builder/windows/library/brotli.php new file mode 100644 index 000000000..f22f402ce --- /dev/null +++ b/src/SPC/builder/windows/library/brotli.php @@ -0,0 +1,36 @@ +source_dir . '\build'); + + // start build + cmd()->cd($this->source_dir) + ->execWithWrapper( + $this->builder->makeSimpleWrapper('cmake'), + '-B build ' . + '-A x64 ' . + "-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " . + '-DCMAKE_BUILD_TYPE=Release ' . + '-DBUILD_SHARED_LIBS=OFF ' . + '-DBROTLI_BUILD_TOOLS=OFF ' . + '-DBROTLI_BUNDLED_MODE=OFF ' . + '-DCMAKE_INSTALL_PREFIX=' . BUILD_ROOT_PATH . ' ' + ) + ->execWithWrapper( + $this->builder->makeSimpleWrapper('cmake'), + "--build build --config Release --target install -j{$this->builder->concurrency}" + ); + } +} diff --git a/src/SPC/builder/windows/library/freetype.php b/src/SPC/builder/windows/library/freetype.php index ef13c8fe6..8401b4de4 100644 --- a/src/SPC/builder/windows/library/freetype.php +++ b/src/SPC/builder/windows/library/freetype.php @@ -24,6 +24,8 @@ protected function build(): void "-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " . '-DCMAKE_BUILD_TYPE=Release ' . '-DBUILD_SHARED_LIBS=OFF ' . + '-DFT_DISABLE_BROTLI=TRUE ' . + '-DFT_DISABLE_BZIP2=TRUE ' . '-DCMAKE_INSTALL_PREFIX=' . BUILD_ROOT_PATH . ' ' ) ->execWithWrapper( diff --git a/src/SPC/builder/windows/library/libaom.php b/src/SPC/builder/windows/library/libaom.php new file mode 100644 index 000000000..06d53cbc7 --- /dev/null +++ b/src/SPC/builder/windows/library/libaom.php @@ -0,0 +1,41 @@ +source_dir . '\builddir'); + + // start build + cmd()->cd($this->source_dir) + ->execWithWrapper( + $this->builder->makeSimpleWrapper('cmake'), + '-S . -B builddir ' . + '-A x64 ' . + "-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " . + '-DCMAKE_BUILD_TYPE=Release ' . + '-DBUILD_SHARED_LIBS=OFF ' . + '-DAOM_TARGET_CPU=generic ' . + '-DENABLE_DOCS=OFF ' . + '-DENABLE_EXAMPLES=OFF ' . + '-DENABLE_TESTDATA=OFF ' . + '-DENABLE_TESTS=OFF ' . + '-DENABLE_TOOLS=OFF ' . + '-DCMAKE_INSTALL_PREFIX=' . BUILD_ROOT_PATH . ' ' + ) + ->execWithWrapper( + $this->builder->makeSimpleWrapper('cmake'), + "--build builddir --config Release --target install -j{$this->builder->concurrency}" + ); + } +} diff --git a/src/SPC/builder/windows/library/zlib.php b/src/SPC/builder/windows/library/zlib.php index 03fd033b9..a5fd346ba 100644 --- a/src/SPC/builder/windows/library/zlib.php +++ b/src/SPC/builder/windows/library/zlib.php @@ -31,8 +31,24 @@ protected function build(): void $this->builder->makeSimpleWrapper('cmake'), "--build build --config Release --target install -j{$this->builder->concurrency}" ); - copy(BUILD_LIB_PATH . '\zlibstatic.lib', BUILD_LIB_PATH . '\zlib_a.lib'); - unlink(BUILD_ROOT_PATH . '\bin\zlib.dll'); - unlink(BUILD_LIB_PATH . '\zlib.lib'); + $detect_list = [ + 'zlibstatic.lib', + 'zs.lib', + 'libzs.lib', + 'libz.lib', + ]; + foreach ($detect_list as $item) { + if (file_exists(BUILD_LIB_PATH . '\\' . $item)) { + FileSystem::copy(BUILD_LIB_PATH . '\\' . $item, BUILD_LIB_PATH . '\zlib_a.lib'); + FileSystem::copy(BUILD_LIB_PATH . '\\' . $item, BUILD_LIB_PATH . '\zlibstatic.lib'); + break; + } + } + FileSystem::removeFileIfExists(BUILD_ROOT_PATH . '\bin\zlib.dll'); + FileSystem::removeFileIfExists(BUILD_LIB_PATH . '\zlib.lib'); + FileSystem::removeFileIfExists(BUILD_LIB_PATH . '\libz.dll'); + FileSystem::removeFileIfExists(BUILD_LIB_PATH . '\libz.lib'); + FileSystem::removeFileIfExists(BUILD_LIB_PATH . '\z.lib'); + FileSystem::removeFileIfExists(BUILD_LIB_PATH . '\z.dll'); } } diff --git a/src/SPC/command/DownloadCommand.php b/src/SPC/command/DownloadCommand.php index 00bcc1948..b7f2b078b 100644 --- a/src/SPC/command/DownloadCommand.php +++ b/src/SPC/command/DownloadCommand.php @@ -30,7 +30,7 @@ public function configure(): void $this->addArgument('sources', InputArgument::REQUIRED, 'The sources will be compiled, comma separated'); $this->addOption('shallow-clone', null, null, 'Clone shallow'); $this->addOption('with-openssl11', null, null, 'Use openssl 1.1'); - $this->addOption('with-php', null, InputOption::VALUE_REQUIRED, 'version in major.minor format (default 8.4)', '8.4'); + $this->addOption('with-php', null, InputOption::VALUE_REQUIRED, 'version in major.minor format (default 8.5)', '8.5'); $this->addOption('clean', null, null, 'Clean old download cache and source before fetch'); $this->addOption('all', 'A', null, 'Fetch all sources that static-php-cli needed'); $this->addOption('custom-url', 'U', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Specify custom source download url, e.g "php-src:https://downloads.php.net/~eric/php-8.3.0beta1.tar.gz"'); diff --git a/src/SPC/store/SourcePatcher.php b/src/SPC/store/SourcePatcher.php index 7317c4f9d..43b750c43 100644 --- a/src/SPC/store/SourcePatcher.php +++ b/src/SPC/store/SourcePatcher.php @@ -634,7 +634,13 @@ public static function patchGDWin32(): bool FileSystem::replaceFileStr(SOURCE_PATH . '/php-src/ext/gd/libgd/gdft.c', '#ifndef MSWIN32', '#ifndef _WIN32'); } // custom config.w32, because official config.w32 is hard-coded many things - $origin = $ver_id >= 80100 ? file_get_contents(ROOT_DIR . '/src/globals/extra/gd_config_81.w32') : file_get_contents(ROOT_DIR . '/src/globals/extra/gd_config_80.w32'); + if ($ver_id >= 80500) { + $origin = file_get_contents(ROOT_DIR . '/src/globals/extra/gd_config_85.w32'); + } elseif ($ver_id >= 80100) { + $origin = file_get_contents(ROOT_DIR . '/src/globals/extra/gd_config_81.w32'); + } else { + $origin = file_get_contents(ROOT_DIR . '/src/globals/extra/gd_config_80.w32'); + } file_put_contents(SOURCE_PATH . '/php-src/ext/gd/config.w32.bak', file_get_contents(SOURCE_PATH . '/php-src/ext/gd/config.w32')); return file_put_contents(SOURCE_PATH . '/php-src/ext/gd/config.w32', $origin) !== false; } diff --git a/src/SPC/store/source/PhpSource.php b/src/SPC/store/source/PhpSource.php index 02ba87554..bf33d6b7d 100644 --- a/src/SPC/store/source/PhpSource.php +++ b/src/SPC/store/source/PhpSource.php @@ -20,7 +20,7 @@ class PhpSource extends CustomSourceBase public function fetch(bool $force = false, ?array $config = null, int $lock_as = SPC_DOWNLOAD_SOURCE): void { - $major = defined('SPC_BUILD_PHP_VERSION') ? SPC_BUILD_PHP_VERSION : '8.4'; + $major = defined('SPC_BUILD_PHP_VERSION') ? SPC_BUILD_PHP_VERSION : '8.5'; if ($major === 'git') { Downloader::downloadSource('php-src', ['type' => 'git', 'url' => 'https://github.com/php/php-src.git', 'rev' => 'master'], $force); } else { diff --git a/src/SPC/util/ConfigValidator.php b/src/SPC/util/ConfigValidator.php index 445c6242f..d9f751ef9 100644 --- a/src/SPC/util/ConfigValidator.php +++ b/src/SPC/util/ConfigValidator.php @@ -393,7 +393,7 @@ public static function validateAndParseCraftFile(mixed $craft_file, Command $com } // check php-version if (isset($craft['php-version'])) { - // validdate version, accept 8.x, 7.x, 8.x.x, 7.x.x, 8, 7 + // validate version, accept 8.x, 7.x, 8.x.x, 7.x.x, 8, 7 $version = strval($craft['php-version']); if (!preg_match('/^(\d+)(\.\d+)?(\.\d+)?$/', $version, $matches)) { throw new ValidationException('Craft file php-version is invalid'); diff --git a/src/SPC/util/executor/UnixAutoconfExecutor.php b/src/SPC/util/executor/UnixAutoconfExecutor.php index e04fe4f9c..9d57ef4eb 100644 --- a/src/SPC/util/executor/UnixAutoconfExecutor.php +++ b/src/SPC/util/executor/UnixAutoconfExecutor.php @@ -16,12 +16,11 @@ class UnixAutoconfExecutor extends Executor protected array $configure_args = []; - protected array $ignore_args = []; - public function __construct(protected BSDLibraryBase|LinuxLibraryBase|MacOSLibraryBase $library) { parent::__construct($library); $this->initShell(); + $this->configure_args = $this->getDefaultConfigureArgs(); } /** @@ -29,19 +28,12 @@ public function __construct(protected BSDLibraryBase|LinuxLibraryBase|MacOSLibra */ public function configure(...$args): static { - // remove all the ignored args - $args = array_merge($args, $this->getDefaultConfigureArgs(), $this->configure_args); - $args = array_diff($args, $this->ignore_args); + $args = array_merge($args, $this->configure_args); $configure_args = implode(' ', $args); return $this->seekLogFileOnException(fn () => $this->shell->exec("./configure {$configure_args}")); } - public function getConfigureArgsString(): string - { - return implode(' ', array_merge($this->getDefaultConfigureArgs(), $this->configure_args)); - } - /** * Run make * @@ -111,7 +103,7 @@ public function addConfigureArgs(...$args): static */ public function removeConfigureArgs(...$args): static { - $this->ignore_args = [...$this->ignore_args, ...$args]; + $this->configure_args = array_diff($this->configure_args, $args); return $this; } @@ -133,8 +125,8 @@ public function appendEnv(array $env): static private function getDefaultConfigureArgs(): array { return [ - '--disable-shared', '--enable-static', + '--disable-shared', "--prefix={$this->library->getBuildRootPath()}", '--with-pic', '--enable-pic', diff --git a/src/StaticPHP/ConsoleApplication.php b/src/StaticPHP/ConsoleApplication.php index a02b38c7d..2882b5741 100644 --- a/src/StaticPHP/ConsoleApplication.php +++ b/src/StaticPHP/ConsoleApplication.php @@ -29,7 +29,7 @@ class ConsoleApplication extends Application { - public const string VERSION = '3.0.0-dev'; + public const string VERSION = '3.0.0-alpha1'; private static array $additional_commands = []; diff --git a/src/StaticPHP/Package/PhpExtensionPackage.php b/src/StaticPHP/Package/PhpExtensionPackage.php index baaa27531..ca3dab739 100644 --- a/src/StaticPHP/Package/PhpExtensionPackage.php +++ b/src/StaticPHP/Package/PhpExtensionPackage.php @@ -155,6 +155,43 @@ public function getDistName(): string return $this->extension_config['display-name'] ?? $this->getExtensionName(); } + /** + * Run smoke test for the extension on Unix CLI. + * Override this method in a subclass. + */ + public function runSmokeTestCliWindows(): void + { + if (($this->extension_config['smoke-test'] ?? true) === false) { + return; + } + + $distName = $this->getDistName(); + // empty display-name → no --ri check (e.g. password_argon2) + if ($distName === '') { + return; + } + + [$ret] = cmd()->execWithResult(BUILD_BIN_PATH . '\php.exe -n --ri "' . $distName . '"', false); + if ($ret !== 0) { + throw new ValidationException( + "extension {$this->getName()} failed compile check: php-cli returned {$ret}", + validation_module: 'Extension ' . $this->getName() . ' sanity check' + ); + } + + $test_file = ROOT_DIR . '/src/globals/ext-tests/' . $this->getExtensionName() . '.php'; + if (file_exists($test_file)) { + $test = self::escapeInlineTestWindows(file_get_contents($test_file)); + [$ret, $out] = cmd()->execWithResult(BUILD_BIN_PATH . '\php.exe -n -r "' . trim($test) . '"'); + if ($ret !== 0) { + throw new ValidationException( + "extension {$this->getName()} failed sanity check. Code: {$ret}, output: " . implode("\n", $out), + validation_module: 'Extension ' . $this->getName() . ' function check' + ); + } + } + } + /** * Run smoke test for the extension on Unix CLI. * Override this method in a subclass. @@ -394,4 +431,17 @@ private static function escapeInlineTest(string $code): string $code ); } + + /** + * Escape PHP test file content for inline `-r` usage on Windows cmd. + * Strips console_putput ? '' : ' 2>/dev/null'; - $tar = SystemTarget::isUnix() ? 'tar' : '"C:\\Windows\\system32\\tar.exe"'; + $tar = SystemTarget::isUnix() ? 'tar' : '"C:\Windows\system32\tar.exe"'; $cmd = "{$tar} {$compression_flag}xf {$archive_arg} --strip-components {$strip} -C {$target_arg}{$mute}"; $this->logCommandInfo($cmd); @@ -187,7 +187,7 @@ public function execute7zExtract(string $archive_path, string $target_path): boo }; $extname = FileSystem::extname($archive_path); - $tar = SystemTarget::isUnix() ? 'tar' : '"C:\\Windows\\system32\\tar.exe"'; + $tar = SystemTarget::isUnix() ? 'tar' : '"C:\Windows\system32\tar.exe"'; match ($extname) { 'tar' => $this->executeTarExtract($archive_path, $target_path, 'none'), diff --git a/src/StaticPHP/Runtime/Shell/WindowsCmd.php b/src/StaticPHP/Runtime/Shell/WindowsCmd.php index e9d7a6c0d..75c62bea9 100644 --- a/src/StaticPHP/Runtime/Shell/WindowsCmd.php +++ b/src/StaticPHP/Runtime/Shell/WindowsCmd.php @@ -1,62 +1,64 @@ -info(ConsoleColor::yellow('[EXEC] ') . ConsoleColor::green($cmd)); - - $original_command = $cmd; - $this->logCommandInfo($original_command); - $this->last_cmd = $cmd = $this->getExecString($cmd); - // echo $cmd . PHP_EOL; - - $this->passthru($cmd, $this->console_putput, $original_command, cwd: $this->cd); - return $this; - } - - public function execWithWrapper(string $wrapper, string $args): WindowsCmd - { - return $this->exec($wrapper . ' "' . str_replace('"', '^"', $args) . '"'); - } - - public function execWithResult(string $cmd, bool $with_log = true): array - { - if ($with_log) { - /* @phpstan-ignore-next-line */ - logger()->info(ConsoleColor::blue('[EXEC] ') . ConsoleColor::green($cmd)); - } else { - logger()->debug('Running command with result: ' . $cmd); - } - $cmd = $this->getExecString($cmd); - $result = $this->passthru($cmd, $this->console_putput, $cmd, capture_output: true, throw_on_error: false, cwd: $this->cd, env: $this->env); - $out = explode("\n", $result['output']); - return [$result['code'], $out]; - } - - public function getLastCommand(): string - { - return $this->last_cmd; - } - - private function getExecString(string $cmd): string - { - return $cmd; - } -} +info(ConsoleColor::yellow('[EXEC] ') . ConsoleColor::green($cmd)); + + $original_command = $cmd; + $this->logCommandInfo($original_command); + $this->last_cmd = $cmd = $this->getExecString($cmd); + // echo $cmd . PHP_EOL; + + $this->passthru($cmd, $this->console_putput, $original_command, cwd: $this->cd); + return $this; + } + + public function execWithWrapper(string $wrapper, string $args): WindowsCmd + { + return $this->exec($wrapper . ' "' . str_replace('"', '^"', $args) . '"'); + } + + public function execWithResult(string $cmd, bool $with_log = true): array + { + if ($with_log) { + /* @phpstan-ignore-next-line */ + logger()->info(ConsoleColor::blue('[EXEC] ') . ConsoleColor::green($cmd)); + } else { + logger()->debug('Running command with result: ' . $cmd); + } + $original_command = $cmd; + $this->logCommandInfo($original_command); + $cmd = $this->getExecString($cmd); + $result = $this->passthru($cmd, $this->console_putput, $original_command, capture_output: true, throw_on_error: false, cwd: $this->cd, env: $this->env); + $out = explode("\n", $result['output']); + return [$result['code'], $out]; + } + + public function getLastCommand(): string + { + return $this->last_cmd; + } + + private function getExecString(string $cmd): string + { + return $cmd; + } +} diff --git a/src/StaticPHP/Util/SPCConfigUtil.php b/src/StaticPHP/Util/SPCConfigUtil.php index 8b6fe6b37..d31e8201a 100644 --- a/src/StaticPHP/Util/SPCConfigUtil.php +++ b/src/StaticPHP/Util/SPCConfigUtil.php @@ -1,437 +1,509 @@ -no_php = $options['no_php'] ?? false; - $this->libs_only_deps = $options['libs_only_deps'] ?? false; - $this->absolute_libs = $options['absolute_libs'] ?? false; - } - - public function config(array $packages = [], bool $include_suggests = false): array - { - // if have php, make php as all extension's dependency - if (!$this->no_php) { - $dep_override = ['php' => array_filter($packages, fn ($y) => str_starts_with($y, 'ext-'))]; - } else { - $dep_override = []; - } - $resolved = DependencyResolver::resolve($packages, $dep_override, $include_suggests); - - $ldflags = $this->getLdflagsString(); - $cflags = $this->getIncludesString($resolved); - $libs = $this->getLibsString($resolved, !$this->absolute_libs); - - // additional OS-specific libraries (e.g. macOS -lresolv) - // embed - if ($extra_libs = SystemTarget::getRuntimeLibs()) { - $libs .= " {$extra_libs}"; - } - - $extra_env = getenv('SPC_EXTRA_LIBS'); - if (is_string($extra_env) && !empty($extra_env)) { - $libs .= " {$extra_env}"; - } - // package frameworks - if (SystemTarget::getTargetOS() === 'Darwin') { - $libs .= " {$this->getFrameworksString($resolved)}"; - } - // C++ - if ($this->hasCpp($resolved)) { - $libcpp = SystemTarget::getTargetOS() === 'Darwin' ? '-lc++' : '-lstdc++'; - $libs = str_replace($libcpp, '', $libs) . " {$libcpp}"; - } - - if ($this->libs_only_deps) { - // mimalloc must come first - if (in_array('mimalloc', $resolved) && file_exists(BUILD_LIB_PATH . '/libmimalloc.a')) { - $libs = BUILD_LIB_PATH . '/libmimalloc.a ' . str_replace([BUILD_LIB_PATH . '/libmimalloc.a', '-lmimalloc'], ['', ''], $libs); - } - return [ - 'cflags' => clean_spaces(getenv('CFLAGS') . ' ' . $cflags), - 'ldflags' => clean_spaces(getenv('LDFLAGS') . ' ' . $ldflags), - 'libs' => clean_spaces(getenv('LIBS') . ' ' . $libs), - ]; - } - - // embed - if (!$this->no_php) { - $libs = "-lphp {$libs} -lc"; - } - - $allLibs = getenv('LIBS') . ' ' . $libs; - - // mimalloc must come first - if (in_array('mimalloc', $resolved) && file_exists(BUILD_LIB_PATH . '/libmimalloc.a')) { - $allLibs = BUILD_LIB_PATH . '/libmimalloc.a ' . str_replace([BUILD_LIB_PATH . '/libmimalloc.a', '-lmimalloc'], ['', ''], $allLibs); - } - - return [ - 'cflags' => clean_spaces(getenv('CFLAGS') . ' ' . $cflags), - 'ldflags' => clean_spaces(getenv('LDFLAGS') . ' ' . $ldflags), - 'libs' => clean_spaces($allLibs), - ]; - } - - /** - * [Helper function] - * Get configuration for a specific extension(s) dependencies. - * - * @param array|PhpExtensionPackage $extension_packages Extension instance or list - * @return array{ - * cflags: string, - * ldflags: string, - * libs: string - * } - */ - public function getExtensionConfig(array|PhpExtensionPackage $extension_packages, bool $include_suggests = false): array - { - if (!is_array($extension_packages)) { - $extension_packages = [$extension_packages]; - } - return $this->config( - packages: array_map(fn ($y) => $y->getName(), $extension_packages), - include_suggests: $include_suggests, - ); - } - - /** - * [Helper function] - * Get configuration for a specific library(s) dependencies. - * - * @param array|LibraryPackage $lib Library instance or list - * @param bool $include_suggests Whether to include suggested libraries - * @return array{ - * cflags: string, - * ldflags: string, - * libs: string - * } - */ - public function getLibraryConfig(array|LibraryPackage $lib, bool $include_suggests = false): array - { - if (!is_array($lib)) { - $lib = [$lib]; - } - $save_no_php = $this->no_php; - $this->no_php = true; - $save_libs_only_deps = $this->libs_only_deps; - $this->libs_only_deps = true; - $ret = $this->config( - packages: array_map(fn ($y) => $y->getName(), $lib), - include_suggests: $include_suggests, - ); - $this->no_php = $save_no_php; - $this->libs_only_deps = $save_libs_only_deps; - return $ret; - } - - /** - * Get build configuration for a package and its sub-dependencies within a resolved set. - * - * This is useful when you need to statically link something against a specific - * library and all its transitive dependencies. It properly handles optional - * dependencies by only including those that were actually resolved. - * - * @param string $package_name The package to get config for - * @param string[] $resolved_packages The full resolved package list - * @param bool $include_suggests Whether to include resolved suggests - * @return array{ - * cflags: string, - * ldflags: string, - * libs: string - * } - */ - public function getPackageDepsConfig(string $package_name, array $resolved_packages, bool $include_suggests = false): array - { - // Get sub-dependencies within the resolved set - $sub_deps = DependencyResolver::getSubDependencies($package_name, $resolved_packages, $include_suggests); - - if (empty($sub_deps)) { - return [ - 'cflags' => '', - 'ldflags' => '', - 'libs' => '', - ]; - } - - // Use libs_only_deps mode and no_php for library linking - $save_no_php = $this->no_php; - $save_libs_only_deps = $this->libs_only_deps; - $this->no_php = true; - $this->libs_only_deps = true; - - $ret = $this->configWithResolvedPackages($sub_deps); - - $this->no_php = $save_no_php; - $this->libs_only_deps = $save_libs_only_deps; - - return $ret; - } - - /** - * Get configuration using already-resolved packages (skip dependency resolution). - * - * @param string[] $resolved_packages Already resolved package names in build order - * @return array{ - * cflags: string, - * ldflags: string, - * libs: string - * } - */ - public function configWithResolvedPackages(array $resolved_packages): array - { - $ldflags = $this->getLdflagsString(); - $cflags = $this->getIncludesString($resolved_packages); - $libs = $this->getLibsString($resolved_packages, !$this->absolute_libs); - - // additional OS-specific libraries (e.g. macOS -lresolv) - if ($extra_libs = SystemTarget::getRuntimeLibs()) { - $libs .= " {$extra_libs}"; - } - - $extra_env = getenv('SPC_EXTRA_LIBS'); - if (is_string($extra_env) && !empty($extra_env)) { - $libs .= " {$extra_env}"; - } - - // package frameworks - if (SystemTarget::getTargetOS() === 'Darwin') { - $libs .= " {$this->getFrameworksString($resolved_packages)}"; - } - - // C++ - if ($this->hasCpp($resolved_packages)) { - $libcpp = SystemTarget::getTargetOS() === 'Darwin' ? '-lc++' : '-lstdc++'; - $libs = str_replace($libcpp, '', $libs) . " {$libcpp}"; - } - - if ($this->libs_only_deps) { - // mimalloc must come first - if (in_array('mimalloc', $resolved_packages) && file_exists(BUILD_LIB_PATH . '/libmimalloc.a')) { - $libs = BUILD_LIB_PATH . '/libmimalloc.a ' . str_replace([BUILD_LIB_PATH . '/libmimalloc.a', '-lmimalloc'], ['', ''], $libs); - } - return [ - 'cflags' => clean_spaces(getenv('CFLAGS') . ' ' . $cflags), - 'ldflags' => clean_spaces(getenv('LDFLAGS') . ' ' . $ldflags), - 'libs' => clean_spaces(getenv('LIBS') . ' ' . $libs), - ]; - } - - // embed - if (!$this->no_php) { - $libs = "-lphp {$libs} -lc"; - } - - $allLibs = getenv('LIBS') . ' ' . $libs; - - // mimalloc must come first - if (in_array('mimalloc', $resolved_packages) && file_exists(BUILD_LIB_PATH . '/libmimalloc.a')) { - $allLibs = BUILD_LIB_PATH . '/libmimalloc.a ' . str_replace([BUILD_LIB_PATH . '/libmimalloc.a', '-lmimalloc'], ['', ''], $allLibs); - } - - return [ - 'cflags' => clean_spaces(getenv('CFLAGS') . ' ' . $cflags), - 'ldflags' => clean_spaces(getenv('LDFLAGS') . ' ' . $ldflags), - 'libs' => clean_spaces($allLibs), - ]; - } - - private function hasCpp(array $packages): bool - { - foreach ($packages as $package) { - $lang = PackageConfig::get($package, 'lang', 'c'); - if ($lang === 'cpp') { - return true; - } - } - return false; - } - - private function getIncludesString(array $packages): string - { - $base = BUILD_INCLUDE_PATH; - $includes = ["-I{$base}"]; - - // link with libphp - if (!$this->no_php) { - $includes = [ - ...$includes, - "-I{$base}/php", - "-I{$base}/php/main", - "-I{$base}/php/TSRM", - "-I{$base}/php/Zend", - "-I{$base}/php/ext", - ]; - } - - // parse pkg-configs - foreach ($packages as $package) { - $pc = PackageConfig::get($package, 'pkg-configs', []); - $pkg_config_path = getenv('PKG_CONFIG_PATH') ?: ''; - $search_paths = array_filter(explode(SystemTarget::isUnix() ? ':' : ';', $pkg_config_path)); - foreach ($pc as $file) { - $found = false; - foreach ($search_paths as $path) { - if (file_exists($path . "/{$file}.pc")) { - $found = true; - break; - } - } - if (!$found) { - throw new WrongUsageException("pkg-config file '{$file}.pc' for lib [{$package}] does not exist. Please build it first."); - } - } - $pc_cflags = implode(' ', $pc); - if ($pc_cflags !== '' && ($pc_cflags = PkgConfigUtil::getCflags($pc_cflags)) !== '') { - $arr = explode(' ', $pc_cflags); - $arr = array_unique($arr); - $arr = array_filter($arr, fn ($x) => !str_starts_with($x, 'SHELL:-Xarch_')); - $pc_cflags = implode(' ', $arr); - $includes[] = $pc_cflags; - } - } - $includes = array_unique($includes); - return implode(' ', $includes); - } - - private function getLdflagsString(): string - { - return '-L' . BUILD_LIB_PATH; - } - - private function getLibsString(array $packages, bool $use_short_libs = true): string - { - $lib_names = []; - $frameworks = []; - - foreach ($packages as $package) { - // parse pkg-configs only for unix systems - if (SystemTarget::isUnix()) { - // add pkg-configs libs - $pkg_configs = PackageConfig::get($package, 'pkg-configs', []); - $pkg_config_path = getenv('PKG_CONFIG_PATH') ?: ''; - $search_paths = array_filter(explode(':', $pkg_config_path)); - foreach ($pkg_configs as $pkg_config) { - $found = false; - foreach ($search_paths as $path) { - if (file_exists($path . "/{$pkg_config}.pc")) { - $found = true; - break; - } - } - if (!$found) { - throw new WrongUsageException("pkg-config file '{$pkg_config}.pc' for lib [{$package}] does not exist. Please build it first."); - } - } - $pkg_configs = implode(' ', $pkg_configs); - if ($pkg_configs !== '') { - // static libs with dependencies come in reverse order, so reverse this too - $pc_libs = array_reverse(PkgConfigUtil::getLibsArray($pkg_configs)); - $lib_names = [...$lib_names, ...$pc_libs]; - } - } - // convert all static-libs to short names - $libs = array_reverse(PackageConfig::get($package, 'static-libs', [])); - foreach ($libs as $lib) { - if (FileSystem::isRelativePath($lib)) { - // check file existence - if (!file_exists(BUILD_LIB_PATH . "/{$lib}")) { - throw new WrongUsageException("Library file '{$lib}' for lib [{$package}] does not exist in '" . BUILD_LIB_PATH . "'. Please build it first."); - } - $lib_names[] = $this->getShortLibName($lib); - } else { - $lib_names[] = $lib; - } - } - // add frameworks for macOS - if (SystemTarget::getTargetOS() === 'Darwin') { - $frameworks = array_merge($frameworks, PackageConfig::get($package, 'frameworks', [])); - } - } - - // post-process - $lib_names = array_filter($lib_names, fn ($x) => $x !== ''); - $lib_names = array_reverse(array_unique($lib_names)); - $frameworks = array_unique($frameworks); - - // process frameworks to short_name - if (SystemTarget::getTargetOS() === 'Darwin') { - foreach ($frameworks as $fw) { - $ks = '-framework ' . $fw; - if (!in_array($ks, $lib_names)) { - $lib_names[] = $ks; - } - } - } - - if (in_array('imap', $packages) && SystemTarget::getTargetOS() === 'Linux' && SystemTarget::getLibc() === 'glibc') { - if (file_exists(BUILD_LIB_PATH . '/libcrypt.a')) { - $lib_names[] = '-lcrypt'; - } - } - if (!$use_short_libs) { - $lib_names = array_map(fn ($l) => $this->getFullLibName($l), $lib_names); - } - return implode(' ', $lib_names); - } - - private function getShortLibName(string $lib): string - { - if (!str_starts_with($lib, 'lib') || !str_ends_with($lib, '.a')) { - return BUILD_LIB_PATH . '/' . $lib; - } - // get short name - return '-l' . substr($lib, 3, -2); - } - - private function getFullLibName(string $lib): string - { - if (!str_starts_with($lib, '-l')) { - return $lib; - } - $libname = substr($lib, 2); - $staticLib = BUILD_LIB_PATH . '/' . "lib{$libname}.a"; - if (file_exists($staticLib)) { - return $staticLib; - } - return $lib; - } - - private function getFrameworksString(array $extensions): string - { - $list = []; - foreach ($extensions as $extension) { - foreach (PackageConfig::get($extension, 'frameworks', []) as $fw) { - $ks = '-framework ' . $fw; - if (!in_array($ks, $list)) { - $list[] = $ks; - } - } - } - return implode(' ', $list); - } -} +no_php = $options['no_php'] ?? false; + $this->libs_only_deps = $options['libs_only_deps'] ?? false; + $this->absolute_libs = $options['absolute_libs'] ?? false; + } + + public function config(array $packages = [], bool $include_suggests = false): array + { + // if have php, make php as all extension's dependency + if (!$this->no_php) { + $dep_override = ['php' => array_filter($packages, fn ($y) => str_starts_with($y, 'ext-'))]; + } else { + $dep_override = []; + } + $resolved = DependencyResolver::resolve($packages, $dep_override, $include_suggests); + + $ldflags = $this->getLdflagsString(); + $cflags = $this->getIncludesString($resolved); + $libs = $this->getLibsString($resolved, !$this->absolute_libs); + + // additional OS-specific libraries (e.g. macOS -lresolv) + // embed + if ($extra_libs = SystemTarget::getRuntimeLibs()) { + $libs .= " {$extra_libs}"; + } + + $extra_env = getenv('SPC_EXTRA_LIBS'); + if (is_string($extra_env) && !empty($extra_env)) { + $libs .= " {$extra_env}"; + } + // package frameworks + if (SystemTarget::getTargetOS() === 'Darwin') { + $libs .= " {$this->getFrameworksString($resolved)}"; + } + // C++ + if ($this->hasCpp($resolved)) { + $target_os = SystemTarget::getTargetOS(); + if ($target_os === 'Darwin') { + $libcpp = '-lc++'; + $libs = str_replace($libcpp, '', $libs) . " {$libcpp}"; + } elseif ($target_os !== 'Windows') { + // Linux and other Unix-like systems use libstdc++ + $libcpp = '-lstdc++'; + $libs = str_replace($libcpp, '', $libs) . " {$libcpp}"; + } + // Windows (MSVC): C++ runtime is linked automatically, no explicit lib needed + } + + if ($this->libs_only_deps) { + // mimalloc must come first + if (in_array('mimalloc', $resolved) && file_exists(BUILD_LIB_PATH . '/libmimalloc.a')) { + $libs = BUILD_LIB_PATH . '/libmimalloc.a ' . str_replace([BUILD_LIB_PATH . '/libmimalloc.a', '-lmimalloc'], ['', ''], $libs); + } + return [ + 'cflags' => clean_spaces(getenv('CFLAGS') . ' ' . $cflags), + 'ldflags' => clean_spaces(getenv('LDFLAGS') . ' ' . $ldflags), + 'libs' => clean_spaces(getenv('LIBS') . ' ' . $libs), + ]; + } + + // embed + if (!$this->no_php) { + if (SystemTarget::getTargetOS() === 'Windows') { + // Windows: use php8embed.lib directly (either full path or short name) + $major = intdiv(PHP_VERSION_ID, 10000); + $php_lib = $this->absolute_libs ? BUILD_LIB_PATH . "\\php{$major}embed.lib" : "php{$major}embed.lib"; + // Windows system libs required by PHP + // Use same system libs as PHP Makefile: LIBS=kernel32.lib ole32.lib user32.lib advapi32.lib shell32.lib ws2_32.lib Dnsapi.lib psapi.lib bcrypt.lib + $libs = "{$php_lib} {$libs} kernel32.lib ole32.lib user32.lib advapi32.lib shell32.lib ws2_32.lib dnsapi.lib psapi.lib bcrypt.lib"; + } else { + $libs = "-lphp {$libs} -lc"; + } + } + + $allLibs = getenv('LIBS') . ' ' . $libs; + + // mimalloc must come first + if (in_array('mimalloc', $resolved) && file_exists(BUILD_LIB_PATH . '/libmimalloc.a')) { + $allLibs = BUILD_LIB_PATH . '/libmimalloc.a ' . str_replace([BUILD_LIB_PATH . '/libmimalloc.a', '-lmimalloc'], ['', ''], $allLibs); + } + + return [ + 'cflags' => clean_spaces(getenv('CFLAGS') . ' ' . $cflags), + 'ldflags' => clean_spaces(getenv('LDFLAGS') . ' ' . $ldflags), + 'libs' => clean_spaces($allLibs), + ]; + } + + /** + * [Helper function] + * Get configuration for a specific extension(s) dependencies. + * + * @param array|PhpExtensionPackage $extension_packages Extension instance or list + * @return array{ + * cflags: string, + * ldflags: string, + * libs: string + * } + */ + public function getExtensionConfig(array|PhpExtensionPackage $extension_packages, bool $include_suggests = false): array + { + if (!is_array($extension_packages)) { + $extension_packages = [$extension_packages]; + } + return $this->config( + packages: array_map(fn ($y) => $y->getName(), $extension_packages), + include_suggests: $include_suggests, + ); + } + + /** + * [Helper function] + * Get configuration for a specific library(s) dependencies. + * + * @param array|LibraryPackage $lib Library instance or list + * @param bool $include_suggests Whether to include suggested libraries + * @return array{ + * cflags: string, + * ldflags: string, + * libs: string + * } + */ + public function getLibraryConfig(array|LibraryPackage $lib, bool $include_suggests = false): array + { + if (!is_array($lib)) { + $lib = [$lib]; + } + $save_no_php = $this->no_php; + $this->no_php = true; + $save_libs_only_deps = $this->libs_only_deps; + $this->libs_only_deps = true; + $ret = $this->config( + packages: array_map(fn ($y) => $y->getName(), $lib), + include_suggests: $include_suggests, + ); + $this->no_php = $save_no_php; + $this->libs_only_deps = $save_libs_only_deps; + return $ret; + } + + /** + * Get build configuration for a package and its sub-dependencies within a resolved set. + * + * This is useful when you need to statically link something against a specific + * library and all its transitive dependencies. It properly handles optional + * dependencies by only including those that were actually resolved. + * + * @param string $package_name The package to get config for + * @param string[] $resolved_packages The full resolved package list + * @param bool $include_suggests Whether to include resolved suggests + * @return array{ + * cflags: string, + * ldflags: string, + * libs: string + * } + */ + public function getPackageDepsConfig(string $package_name, array $resolved_packages, bool $include_suggests = false): array + { + // Get sub-dependencies within the resolved set + $sub_deps = DependencyResolver::getSubDependencies($package_name, $resolved_packages, $include_suggests); + + if (empty($sub_deps)) { + return [ + 'cflags' => '', + 'ldflags' => '', + 'libs' => '', + ]; + } + + // Use libs_only_deps mode and no_php for library linking + $save_no_php = $this->no_php; + $save_libs_only_deps = $this->libs_only_deps; + $this->no_php = true; + $this->libs_only_deps = true; + + $ret = $this->configWithResolvedPackages($sub_deps); + + $this->no_php = $save_no_php; + $this->libs_only_deps = $save_libs_only_deps; + + return $ret; + } + + /** + * Get configuration using already-resolved packages (skip dependency resolution). + * + * @param string[] $resolved_packages Already resolved package names in build order + * @return array{ + * cflags: string, + * ldflags: string, + * libs: string + * } + */ + public function configWithResolvedPackages(array $resolved_packages): array + { + $ldflags = $this->getLdflagsString(); + $cflags = $this->getIncludesString($resolved_packages); + $libs = $this->getLibsString($resolved_packages, !$this->absolute_libs); + + // additional OS-specific libraries (e.g. macOS -lresolv) + if ($extra_libs = SystemTarget::getRuntimeLibs()) { + $libs .= " {$extra_libs}"; + } + + $extra_env = getenv('SPC_EXTRA_LIBS'); + if (is_string($extra_env) && !empty($extra_env)) { + $libs .= " {$extra_env}"; + } + + // package frameworks + if (SystemTarget::getTargetOS() === 'Darwin') { + $libs .= " {$this->getFrameworksString($resolved_packages)}"; + } + + // C++ + if ($this->hasCpp($resolved_packages)) { + $target_os = SystemTarget::getTargetOS(); + if ($target_os === 'Darwin') { + $libcpp = '-lc++'; + $libs = str_replace($libcpp, '', $libs) . " {$libcpp}"; + } elseif ($target_os !== 'Windows') { + // Linux and other Unix-like systems use libstdc++ + $libcpp = '-lstdc++'; + $libs = str_replace($libcpp, '', $libs) . " {$libcpp}"; + } + // Windows (MSVC): C++ runtime is linked automatically, no explicit lib needed + } + + if ($this->libs_only_deps) { + // mimalloc must come first + if (in_array('mimalloc', $resolved_packages) && file_exists(BUILD_LIB_PATH . '/libmimalloc.a')) { + $libs = BUILD_LIB_PATH . '/libmimalloc.a ' . str_replace([BUILD_LIB_PATH . '/libmimalloc.a', '-lmimalloc'], ['', ''], $libs); + } + return [ + 'cflags' => clean_spaces(getenv('CFLAGS') . ' ' . $cflags), + 'ldflags' => clean_spaces(getenv('LDFLAGS') . ' ' . $ldflags), + 'libs' => clean_spaces(getenv('LIBS') . ' ' . $libs), + ]; + } + + // embed + if (!$this->no_php) { + $libs = "-lphp {$libs} -lc"; + } + + $allLibs = getenv('LIBS') . ' ' . $libs; + + // mimalloc must come first + if (in_array('mimalloc', $resolved_packages) && file_exists(BUILD_LIB_PATH . '/libmimalloc.a')) { + $allLibs = BUILD_LIB_PATH . '/libmimalloc.a ' . str_replace([BUILD_LIB_PATH . '/libmimalloc.a', '-lmimalloc'], ['', ''], $allLibs); + } + + return [ + 'cflags' => clean_spaces(getenv('CFLAGS') . ' ' . $cflags), + 'ldflags' => clean_spaces(getenv('LDFLAGS') . ' ' . $ldflags), + 'libs' => clean_spaces($allLibs), + ]; + } + + private function hasCpp(array $packages): bool + { + foreach ($packages as $package) { + $lang = PackageConfig::get($package, 'lang', 'c'); + if ($lang === 'cpp') { + return true; + } + } + return false; + } + + private function getIncludesString(array $packages): string + { + $base = BUILD_INCLUDE_PATH; + + // Windows MSVC uses /I flag instead of -I + if (SystemTarget::getTargetOS() === 'Windows') { + $includes = ["/I\"{$base}\""]; + + // link with libphp + if (!$this->no_php) { + $includes = [ + ...$includes, + "/I\"{$base}\\php\"", + "/I\"{$base}\\php\\main\"", + "/I\"{$base}\\php\\TSRM\"", + "/I\"{$base}\\php\\Zend\"", + "/I\"{$base}\\php\\ext\"", + ]; + } + } else { + $includes = ["-I{$base}"]; + + // link with libphp + if (!$this->no_php) { + $includes = [ + ...$includes, + "-I{$base}/php", + "-I{$base}/php/main", + "-I{$base}/php/TSRM", + "-I{$base}/php/Zend", + "-I{$base}/php/ext", + ]; + } + } + + // parse pkg-configs (only for Unix) + if (SystemTarget::isUnix()) { + foreach ($packages as $package) { + $pc = PackageConfig::get($package, 'pkg-configs', []); + $pkg_config_path = getenv('PKG_CONFIG_PATH') ?: ''; + $search_paths = array_filter(explode(':', $pkg_config_path)); + foreach ($pc as $file) { + $found = false; + foreach ($search_paths as $path) { + if (file_exists($path . "/{$file}.pc")) { + $found = true; + break; + } + } + if (!$found) { + throw new WrongUsageException("pkg-config file '{$file}.pc' for lib [{$package}] does not exist. Please build it first."); + } + } + $pc_cflags = implode(' ', $pc); + if ($pc_cflags !== '' && ($pc_cflags = PkgConfigUtil::getCflags($pc_cflags)) !== '') { + $arr = explode(' ', $pc_cflags); + $arr = array_unique($arr); + $arr = array_filter($arr, fn ($x) => !str_starts_with($x, 'SHELL:-Xarch_')); + $pc_cflags = implode(' ', $arr); + $includes[] = $pc_cflags; + } + } + } + $includes = array_unique($includes); + return implode(' ', $includes); + } + + private function getLdflagsString(): string + { + // Windows MSVC uses /LIBPATH flag instead of -L + if (SystemTarget::getTargetOS() === 'Windows') { + return '/LIBPATH:"' . BUILD_LIB_PATH . '"'; + } + return '-L' . BUILD_LIB_PATH; + } + + private function getLibsString(array $packages, bool $use_short_libs = true): string + { + $lib_names = []; + $frameworks = []; + + foreach ($packages as $package) { + // parse pkg-configs only for unix systems + if (SystemTarget::isUnix()) { + // add pkg-configs libs + $pkg_configs = PackageConfig::get($package, 'pkg-configs', []); + $pkg_config_path = getenv('PKG_CONFIG_PATH') ?: ''; + $search_paths = array_filter(explode(':', $pkg_config_path)); + foreach ($pkg_configs as $pkg_config) { + $found = false; + foreach ($search_paths as $path) { + if (file_exists($path . "/{$pkg_config}.pc")) { + $found = true; + break; + } + } + if (!$found) { + throw new WrongUsageException("pkg-config file '{$pkg_config}.pc' for lib [{$package}] does not exist. Please build it first."); + } + } + $pkg_configs = implode(' ', $pkg_configs); + if ($pkg_configs !== '') { + // static libs with dependencies come in reverse order, so reverse this too + $pc_libs = array_reverse(PkgConfigUtil::getLibsArray($pkg_configs)); + $lib_names = [...$lib_names, ...$pc_libs]; + } + } + // convert all static-libs to short names + $libs = array_reverse(PackageConfig::get($package, 'static-libs', [])); + foreach ($libs as $lib) { + if (FileSystem::isRelativePath($lib)) { + // check file existence + if (!file_exists(BUILD_LIB_PATH . "/{$lib}")) { + throw new WrongUsageException("Library file '{$lib}' for lib [{$package}] does not exist in '" . BUILD_LIB_PATH . "'. Please build it first."); + } + $lib_names[] = $this->getShortLibName($lib); + } else { + $lib_names[] = $lib; + } + } + // add frameworks for macOS + if (SystemTarget::getTargetOS() === 'Darwin') { + $frameworks = array_merge($frameworks, PackageConfig::get($package, 'frameworks', [])); + } + } + + // post-process + $lib_names = array_filter($lib_names, fn ($x) => $x !== ''); + $lib_names = array_reverse(array_unique($lib_names)); + $frameworks = array_unique($frameworks); + + // process frameworks to short_name + if (SystemTarget::getTargetOS() === 'Darwin') { + foreach ($frameworks as $fw) { + $ks = '-framework ' . $fw; + if (!in_array($ks, $lib_names)) { + $lib_names[] = $ks; + } + } + } + + if (in_array('imap', $packages) && SystemTarget::getTargetOS() === 'Linux' && SystemTarget::getLibc() === 'glibc') { + if (file_exists(BUILD_LIB_PATH . '/libcrypt.a')) { + $lib_names[] = '-lcrypt'; + } + } + if (!$use_short_libs) { + $lib_names = array_map(fn ($l) => $this->getFullLibName($l), $lib_names); + } + return implode(' ', $lib_names); + } + + private function getShortLibName(string $lib): string + { + // Windows: library files are xxx.lib format (not libxxx.a) + if (SystemTarget::getTargetOS() === 'Windows') { + if (!str_ends_with($lib, '.lib')) { + return BUILD_LIB_PATH . '\\' . $lib; + } + // For Windows, return just the library filename (e.g., "libssl.lib") + return $lib; + } + + // Unix: library files are libxxx.a format + if (!str_starts_with($lib, 'lib') || !str_ends_with($lib, '.a')) { + return BUILD_LIB_PATH . '/' . $lib; + } + // get short name (e.g., "libssl.a" -> "-lssl") + return '-l' . substr($lib, 3, -2); + } + + private function getFullLibName(string $lib): string + { + // Windows: libraries don't use -l prefix, return as-is or with full path + if (SystemTarget::getTargetOS() === 'Windows') { + if (str_ends_with($lib, '.lib') && !str_contains($lib, '\\') && !str_contains($lib, '/')) { + // It's a short lib name like "libssl.lib", convert to full path + $fullPath = BUILD_LIB_PATH . '\\' . $lib; + if (file_exists($fullPath)) { + return $fullPath; + } + } + return $lib; + } + + // Unix: convert -lxxx to full path + if (!str_starts_with($lib, '-l')) { + return $lib; + } + $libname = substr($lib, 2); + $staticLib = BUILD_LIB_PATH . '/' . "lib{$libname}.a"; + if (file_exists($staticLib)) { + return $staticLib; + } + return $lib; + } + + private function getFrameworksString(array $extensions): string + { + $list = []; + foreach ($extensions as $extension) { + foreach (PackageConfig::get($extension, 'frameworks', []) as $fw) { + $ks = '-framework ' . $fw; + if (!in_array($ks, $list)) { + $list[] = $ks; + } + } + } + return implode(' ', $list); + } +} diff --git a/src/StaticPHP/Util/System/WindowsUtil.php b/src/StaticPHP/Util/System/WindowsUtil.php index b6d943be4..150730c0a 100644 --- a/src/StaticPHP/Util/System/WindowsUtil.php +++ b/src/StaticPHP/Util/System/WindowsUtil.php @@ -138,6 +138,28 @@ public static function writeCmakeFindModules(): void FileSystem::writeFile($cmake_find_dir . DIRECTORY_SEPARATOR . 'FindOpenSSL.cmake', <<<'CMAKE' # Custom FindOpenSSL.cmake wrapper for static-php-cli Windows builds. +set(_spc_saved_module_path "${CMAKE_MODULE_PATH}") +list(REMOVE_ITEM CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") + +set(_spc_find_args "") +if(OpenSSL_FIND_VERSION) + list(APPEND _spc_find_args "${OpenSSL_FIND_VERSION}") + if(OpenSSL_FIND_VERSION_EXACT) + list(APPEND _spc_find_args EXACT) + endif() +endif() +if(OpenSSL_FIND_REQUIRED) + list(APPEND _spc_find_args REQUIRED) +endif() +if(OpenSSL_FIND_QUIETLY) + list(APPEND _spc_find_args QUIET) +endif() +find_package(OpenSSL ${_spc_find_args}) +unset(_spc_find_args) + +set(CMAKE_MODULE_PATH "${_spc_saved_module_path}") +unset(_spc_saved_module_path) + if(WIN32 AND (OpenSSL_FOUND OR OPENSSL_FOUND)) list(GET CMAKE_FIND_ROOT_PATH 0 _spc_buildroot) # Normalize to forward slashes — backslash paths cause 'Invalid character diff --git a/src/globals/extra/gd_config_85.w32 b/src/globals/extra/gd_config_85.w32 new file mode 100644 index 000000000..e980b003e --- /dev/null +++ b/src/globals/extra/gd_config_85.w32 @@ -0,0 +1,94 @@ +// vim:ft=javascript + +ARG_WITH("gd", "Bundled GD support", "yes"); + +if (PHP_GD != "no") { + // check for gd.h (required) + if (!CHECK_HEADER_ADD_INCLUDE("gd.h", "CFLAGS_GD", PHP_GD + ";ext\\gd\\libgd")) { + ERROR("gd not enabled; libraries and headers not found"); + } + + // zlib ext support (required) + if (!CHECK_LIB("zlib_a.lib;zlib.lib", "gd", PHP_GD)) { + ERROR("gd not enabled; zlib not enabled"); + } + + // libjpeg lib support + if (CHECK_LIB("libjpeg_a.lib;libjpeg.lib", "gd", PHP_GD) && + CHECK_HEADER_ADD_INCLUDE("jpeglib.h", "CFLAGS_GD", PHP_GD + ";" + PHP_PHP_BUILD + "\\include")) { + AC_DEFINE("HAVE_LIBJPEG", 1, "JPEG support"); + AC_DEFINE("HAVE_GD_JPG", 1, "JPEG support"); + } + + // libpng16 lib support + if (CHECK_LIB("libpng_a.lib;libpng.lib", "gd", PHP_GD) && + CHECK_HEADER_ADD_INCLUDE("png.h", "CFLAGS_GD", PHP_GD + ";" + PHP_PHP_BUILD + "\\include\\libpng16")) { + AC_DEFINE("HAVE_LIBPNG", 1, "PNG support"); + AC_DEFINE("HAVE_GD_PNG", 1, "PNG support"); + } + + // freetype lib support + if (CHECK_LIB("libfreetype_a.lib;libfreetype.lib", "gd", PHP_GD) && + CHECK_HEADER_ADD_INCLUDE("ft2build.h", "CFLAGS_GD", PHP_GD + ";" + PHP_PHP_BUILD + "\\include\\freetype2;" + PHP_PHP_BUILD + "\\include\\freetype")) { + AC_DEFINE("HAVE_LIBFREETYPE", 1, "FreeType support"); + AC_DEFINE("HAVE_GD_FREETYPE", 1, "FreeType support"); + } + + // xpm lib support + if (CHECK_LIB("libXpm_a.lib", "gd", PHP_GD) && + CHECK_HEADER_ADD_INCLUDE("xpm.h", "CFLAGS_GD", PHP_GD + ";" + PHP_PHP_BUILD + "\\include\\X11")) { + AC_DEFINE("HAVE_LIBXPM", 1, "XPM support"); + AC_DEFINE("HAVE_GD_XPM", 1, "XPM support"); + } + + // iconv lib support + if ((CHECK_LIB("libiconv_a.lib;libiconv.lib", "gd", PHP_GD) || CHECK_LIB("iconv_a.lib;iconv.lib", "gd", PHP_GD)) && + CHECK_HEADER_ADD_INCLUDE("iconv.h", "CFLAGS_GD", PHP_GD)) { + AC_DEFINE("HAVE_LIBICONV", 1, "Iconv support"); + } + + // libwebp lib support + if ((CHECK_LIB("libwebp_a.lib", "gd", PHP_GD) || CHECK_LIB("libwebp.lib", "gd", PHP_GD)) && + CHECK_LIB("libsharpyuv.lib", "gd", PHP_GD) && + CHECK_HEADER_ADD_INCLUDE("decode.h", "CFLAGS_GD", PHP_GD + ";" + PHP_PHP_BUILD + "\\include\\webp") && + CHECK_HEADER_ADD_INCLUDE("encode.h", "CFLAGS_GD", PHP_GD + ";" + PHP_PHP_BUILD + "\\include\\webp")) { + AC_DEFINE("HAVE_LIBWEBP", 1, "WebP support"); + AC_DEFINE("HAVE_GD_WEBP", 1, "WebP support"); + } + + // libavif lib support + if (CHECK_LIB("avif_a.lib", "gd", PHP_GD) && + CHECK_LIB("aom_a.lib", "gd", PHP_GD) && + CHECK_HEADER_ADD_INCLUDE("avif.h", "CFLAGS_GD", PHP_GD + ";" + PHP_PHP_BUILD + "\\include\\avif")) { + ADD_FLAG("CFLAGS_GD", "/D HAVE_LIBAVIF /D HAVE_GD_AVIF"); + } else if (CHECK_LIB("avif.lib", "gd", PHP_GD) && + CHECK_HEADER_ADD_INCLUDE("avif.h", "CFLAGS_GD", PHP_GD + ";" + PHP_PHP_BUILD + "\\include\\avif")) { + ADD_FLAG("CFLAGS_GD", "/D HAVE_LIBAVIF /D HAVE_GD_AVIF"); + } + + CHECK_LIB("User32.lib", "gd", PHP_GD); + CHECK_LIB("Gdi32.lib", "gd", PHP_GD); + + EXTENSION("gd", "gd.c", null, "-Iext/gd/libgd"); + ADD_SOURCES("ext/gd/libgd", "gd.c \ + gdcache.c gdfontg.c gdfontl.c gdfontmb.c gdfonts.c gdfontt.c \ + gdft.c gd_gd2.c gd_gd.c gd_gif_in.c gd_gif_out.c gdhelpers.c gd_io.c gd_io_dp.c \ + gd_io_file.c gd_io_ss.c gd_jpeg.c gdkanji.c gd_png.c gd_ss.c \ + gdtables.c gd_topal.c gd_wbmp.c gdxpm.c wbmp.c gd_xbm.c gd_security.c gd_transform.c \ + gd_filter.c gd_rotate.c gd_color_match.c gd_webp.c gd_avif.c \ + gd_crop.c gd_interpolation.c gd_matrix.c gd_bmp.c gd_tga.c", "gd"); + + AC_DEFINE('HAVE_LIBGD', 1, 'GD support'); + AC_DEFINE('HAVE_GD_BUNDLED', 1, "Bundled GD"); + AC_DEFINE('HAVE_GD_BMP', 1, "BMP support"); + AC_DEFINE('HAVE_GD_TGA', 1, "TGA support"); + ADD_FLAG("CFLAGS_GD", " \ +/D PHP_GD_EXPORTS=1 \ +/D HAVE_GD_GET_INTERPOLATION \ + "); + if (ICC_TOOLSET) { + ADD_FLAG("LDFLAGS_GD", "/nodefaultlib:libcmt"); + } + + PHP_INSTALL_HEADERS("", "ext/gd ext/gd/libgd"); +} diff --git a/src/globals/test-extensions.php b/src/globals/test-extensions.php index ba02e672d..ccc561052 100644 --- a/src/globals/test-extensions.php +++ b/src/globals/test-extensions.php @@ -16,7 +16,7 @@ // '8.1', // '8.2', // '8.3', - // '8.4', + '8.4', '8.5', // 'git', ]; @@ -24,14 +24,14 @@ // test os (macos-15-intel, macos-15, ubuntu-latest, windows-latest are available) $test_os = [ // 'macos-15-intel', // bin/spc for x86_64 - // 'macos-15', // bin/spc for arm64 - // 'ubuntu-latest', // bin/spc-alpine-docker for x86_64 + 'macos-15', // bin/spc for arm64 + 'ubuntu-latest', // bin/spc-alpine-docker for x86_64 'ubuntu-22.04', // bin/spc-gnu-docker for x86_64 - 'ubuntu-24.04', // bin/spc for x86_64 + // 'ubuntu-24.04', // bin/spc for x86_64 'ubuntu-22.04-arm', // bin/spc-gnu-docker for arm64 - 'ubuntu-24.04-arm', // bin/spc for arm64 + // 'ubuntu-24.04-arm', // bin/spc for arm64 // 'windows-2022', // .\bin\spc.ps1 - // 'windows-2025', + 'windows-2025', ]; // whether enable thread safe @@ -42,7 +42,7 @@ // compress with upx $upx = false; -// whether to test frankenphp build, only available for macos and linux +// whether to test frankenphp build, only available for macOS and linux $frankenphp = false; // prefer downloading pre-built packages to speed up the build process @@ -50,8 +50,8 @@ // If you want to test your added extensions and libs, add below (comma separated, example `bcmath,openssl`). $extensions = match (PHP_OS_FAMILY) { - 'Linux', 'Darwin' => 'pgsql', - 'Windows' => 'com_dotnet', + 'Linux', 'Darwin' => 'zlib', + 'Windows' => 'gd,zlib,mbstring,filter', }; // If you want to test shared extensions, add them below (comma separated, example `bcmath,openssl`). @@ -62,11 +62,11 @@ }; // If you want to test lib-suggests for all extensions and libraries, set it to true. -$with_suggested_libs = false; +$with_suggested_libs = true; // If you want to test extra libs for extensions, add them below (comma separated, example `libwebp,libavif`). Unnecessary, when $with_suggested_libs is true. $with_libs = match (PHP_OS_FAMILY) { - 'Linux', 'Darwin' => '', + 'Linux', 'Darwin' => 'libjpeg', 'Windows' => '', };