diff --git a/config/env.ini b/config/env.ini index 1f17797e0..7448cc373 100644 --- a/config/env.ini +++ b/config/env.ini @@ -1,40 +1,41 @@ -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; static-php-cli (spc) env configuration ; ; This file is used to set default env vars for static-php-cli build. ; As dynamic build process, some of these vars can be overwritten by CLI options. ; And you can also overwrite these vars by setting them in your shell environment. +; The value should be changed only if you know what you are doing. Otherwise, please leave them as default. ; ; We need to use some pre-defined internal env vars, like `BUILD_ROOT_PATH`, `DOWNLOAD_PATH`, etc. -; Please note that these vars cannot be defined in this file, they are only be defined before static-php-cli running. +; Please note that these vars cannot be defined in this file, they should only be defined before static-php-cli running. ; -; Here's a list of env vars, these value cannot be changed anywhere: +; Here's a list of env vars, these variables will be defined if not defined: ; -; SPC_VERSION: the version of static-php-cli. -; WORKING_DIR: the working directory of the build process. (default: `$(pwd)`) -; ROOT_DIR: the root directory of static-php-cli. (default: `/path/to/static-php-cli`, when running in phar or micro mode: `phar://path/to/spc.phar`) ; BUILD_ROOT_PATH: the root path of the build process. (default: `$(pwd)/buildroot`) ; BUILD_INCLUDE_PATH: the path of the include files. (default: `$BUILD_ROOT_PATH/include`) ; BUILD_LIB_PATH: the path of the lib files. (default: `$BUILD_ROOT_PATH/lib`) ; BUILD_BIN_PATH: the path of the bin files. (default: `$BUILD_ROOT_PATH/bin`) -; PKG_ROOT_PATH: the root path of the package files. (default: `$(pwd)/pkgroot`) +; BUILD_MODULES_PATH: the path of the php modules (shared extensions) files. (default: `$BUILD_ROOT_PATH/modules`) +; PKG_ROOT_PATH: the root path of the package files. (default: `$(pwd)/pkgroot/$GNU_ARCH-{darwin|linux|windows}`) ; SOURCE_PATH: the path of the source files. (default: `$(pwd)/source`) ; DOWNLOAD_PATH: the path of the download files. (default: `$(pwd)/downloads`) -; CPU_COUNT: the count of the CPU cores. (default: `$(nproc)`) -; SPC_ARCH: the arch of the current system, for some libraries needed `--host=XXX` args. (default: `$(uname -m)`, e.g. `x86_64`, `aarch64`, `arm64`) -; GNU_ARCH: the GNU arch of the current system. (default: `$(uname -m)`, e.g. `x86_64`, `aarch64`) -; MAC_ARCH: the MAC arch of the current system. (default: `$(uname -m)`, e.g. `x86_64`, `arm64`) +; PATH: (*nix only) static-php-cli will add `$BUILD_BIN_PATH` to PATH. +; PKG_CONFIG_PATH: (*nix only) static-php-cli will set `$BUILD_LIB_PATH/pkgconfig` to PKG_CONFIG_PATH. ; -; * These vars are only be defined in Unix (macOS, Linux, FreeBSD)Builder and cannot be changed anywhere: -; PATH: static-php-cli will add `$BUILD_BIN_PATH` to PATH. -; PKG_CONFIG: static-php-cli will set `$BUILD_BIN_PATH/pkg-config` to PKG_CONFIG. -; PKG_CONFIG_PATH: static-php-cli will set `$BUILD_LIB_PATH/pkgconfig` to PKG_CONFIG_PATH. +; Here's a list of env vars, these variables is defined in SPC and cannot be changed anywhere: ; -; * These vars are only be defined in LinuxBuilder and cannot be changed anywhere: -; SPC_LINUX_DEFAULT_CC: the default compiler for linux. (For alpine linux: `gcc`, default: `$GNU_ARCH-linux-musl-gcc`) -; SPC_LINUX_DEFAULT_CXX: the default c++ compiler for linux. (For alpine linux: `g++`, default: `$GNU_ARCH-linux-musl-g++`) -; SPC_LINUX_DEFAULT_AR: the default archiver for linux. (For alpine linux: `ar`, default: `$GNU_ARCH-linux-musl-ar`) -; SPC_EXTRA_PHP_VARS: the extra vars for building php, used in `configure` and `make` command. +; SPC_VERSION: the version of static-php-cli. +; WORKING_DIR: the working directory of the build process. (default: `$(pwd)`) +; ROOT_DIR: the root directory of static-php-cli. (default: `/path/to/static-php-cli`, when running in phar or micro mode: `phar://path/to/spc.phar`) +; CPU_COUNT: the count of the CPU cores. (default: `$(nproc)`) +; SPC_ARCH: the arch of the current system, for some libraries needed `--host=XXX` args. (default: `$(uname -m)`, e.g. `x86_64`, `aarch64`, `arm64`) +; GNU_ARCH: the GNU arch of the current system. (default: `$(uname -m)`, e.g. `x86_64`, `aarch64`) +; MAC_ARCH: the MAC arch of the current system. (default: `$(uname -m)`, e.g. `x86_64`, `arm64`) +; PKG_CONFIG: (*nix only) static-php-cli will set `$BUILD_BIN_PATH/pkg-config` to PKG_CONFIG. +; SPC_LINUX_DEFAULT_CC: (linux only) the default compiler for linux. (For alpine linux: `gcc`, default: `$GNU_ARCH-linux-musl-gcc`) +; SPC_LINUX_DEFAULT_CXX: (linux only) the default c++ compiler for linux. (For alpine linux: `g++`, default: `$GNU_ARCH-linux-musl-g++`) +; SPC_LINUX_DEFAULT_AR: (linux only) the default archiver for linux. (For alpine linux: `ar`, default: `$GNU_ARCH-linux-musl-ar`) +; SPC_EXTRA_PHP_VARS: (linux only) the extra vars for building php, used in `configure` and `make` command. [global] ; Build concurrency for make -jN, default is CPU_COUNT, this value are used in every libs. @@ -109,9 +110,7 @@ SPC_CMD_PREFIX_PHP_CONFIGURE="./configure --prefix= --with-valgrind=no --disable ; *** default build vars for building php *** ; embed type for php, static (libphp.a) or shared (libphp.so) SPC_CMD_VAR_PHP_EMBED_TYPE="static" -; CFLAGS for configuring php -SPC_CMD_VAR_PHP_CONFIGURE_CFLAGS="${SPC_DEFAULT_C_FLAGS} -fPIE" -; EXTRA_CFLAGS for `make` php +; EXTRA_CFLAGS for `configure` and `make` php SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS="-g -fstack-protector-strong -fno-ident -fPIE ${SPC_DEFAULT_C_FLAGS}" ; EXTRA_LDFLAGS for `make` php, can use -release to set a soname for libphp.so SPC_CMD_VAR_PHP_MAKE_EXTRA_LDFLAGS="" @@ -141,10 +140,8 @@ SPC_CMD_PREFIX_PHP_CONFIGURE="./configure --prefix= --with-valgrind=no --enable- ; *** default build vars for building php *** ; embed type for php, static (libphp.a) or shared (libphp.dylib) SPC_CMD_VAR_PHP_EMBED_TYPE="static" -; CFLAGS for configuring php -SPC_CMD_VAR_PHP_CONFIGURE_CFLAGS="${SPC_DEFAULT_C_FLAGS} -Werror=unknown-warning-option" -; EXTRA_CFLAGS for `make` php -SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS="-g -fstack-protector-strong -fpic -fpie ${SPC_DEFAULT_C_FLAGS}" +; EXTRA_CFLAGS for `configure` and `make` php +SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS="-g -fstack-protector-strong -fpic -fpie -Werror=unknown-warning-option ${SPC_DEFAULT_C_FLAGS}" [freebsd] ; compiler environments diff --git a/config/lib.json b/config/lib.json index d74658a6d..4f66a1e55 100644 --- a/config/lib.json +++ b/config/lib.json @@ -13,7 +13,8 @@ "lib-depends-macos": [ "lib-base", "micro", - "libxml2" + "libxml2", + "frankenphp" ], "lib-suggests-linux": [ "libacl", @@ -25,6 +26,10 @@ "watcher" ] }, + "frankenphp": { + "source": "frankenphp", + "type": "target" + }, "micro": { "type": "target", "source": "micro" diff --git a/config/source.json b/config/source.json index ca1edfb02..ff6eb4d23 100644 --- a/config/source.json +++ b/config/source.json @@ -301,6 +301,16 @@ "path": "LICENSE.MIT" } }, + "frankenphp": { + "type": "ghtar", + "repo": "php/frankenphp", + "prefer-stable": true, + "provide-pre-built": false, + "license": { + "type": "file", + "path": "LICENSE" + } + }, "freetype": { "type": "ghtagtar", "repo": "freetype/freetype", diff --git a/src/SPC/ConsoleApplication.php b/src/SPC/ConsoleApplication.php index da143de1a..9a957223c 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.7.6'; + public const string VERSION = '2.7.7'; public function __construct() { diff --git a/src/SPC/builder/Extension.php b/src/SPC/builder/Extension.php index c9cd06577..08b403e61 100644 --- a/src/SPC/builder/Extension.php +++ b/src/SPC/builder/Extension.php @@ -4,6 +4,7 @@ namespace SPC\builder; +use SPC\builder\unix\UnixBuilderBase; use SPC\exception\EnvironmentException; use SPC\exception\SPCException; use SPC\exception\ValidationException; @@ -448,6 +449,15 @@ public function buildUnixShared(): void ->exec('make clean') ->exec('make -j' . $this->builder->concurrency) ->exec('make install'); + + // process *.so file + $soFile = BUILD_MODULES_PATH . '/' . $this->getName() . '.so'; + if (!file_exists($soFile)) { + throw new ValidationException("extension {$this->getName()} build failed: {$soFile} not found", validation_module: "Extension {$this->getName()} build"); + } + /** @var UnixBuilderBase $builder */ + $builder = $this->builder; + $builder->deployBinary($soFile, $soFile, false); } /** diff --git a/src/SPC/builder/freebsd/BSDBuilder.php b/src/SPC/builder/freebsd/BSDBuilder.php index 0c1d1ee44..b746dff56 100644 --- a/src/SPC/builder/freebsd/BSDBuilder.php +++ b/src/SPC/builder/freebsd/BSDBuilder.php @@ -153,7 +153,7 @@ protected function buildCli(): void if (!$this->getOption('no-strip', false)) { $shell->exec('strip sapi/cli/php'); } - $this->deployBinary(BUILD_TARGET_CLI); + $this->deploySAPIBinary(BUILD_TARGET_CLI); } /** @@ -184,7 +184,7 @@ protected function buildMicro(): void if (!$this->getOption('no-strip', false)) { shell()->cd(SOURCE_PATH . '/php-src/sapi/micro')->exec('strip --strip-unneeded micro.sfx'); } - $this->deployBinary(BUILD_TARGET_MICRO); + $this->deploySAPIBinary(BUILD_TARGET_MICRO); if ($this->phar_patched) { SourcePatcher::unpatchMicroPhar(); @@ -206,7 +206,7 @@ protected function buildFpm(): void if (!$this->getOption('no-strip', false)) { $shell->exec('strip sapi/fpm/php-fpm'); } - $this->deployBinary(BUILD_TARGET_FPM); + $this->deploySAPIBinary(BUILD_TARGET_FPM); } /** diff --git a/src/SPC/builder/linux/LinuxBuilder.php b/src/SPC/builder/linux/LinuxBuilder.php index 3995c3a85..0d6f77fba 100644 --- a/src/SPC/builder/linux/LinuxBuilder.php +++ b/src/SPC/builder/linux/LinuxBuilder.php @@ -8,6 +8,7 @@ use SPC\exception\PatchException; use SPC\exception\WrongUsageException; use SPC\store\Config; +use SPC\store\DirDiff; use SPC\store\FileSystem; use SPC\store\SourcePatcher; use SPC\util\GlobalEnvManager; @@ -90,7 +91,7 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void // prepare build php envs // $musl_flag = SPCTarget::getLibc() === 'musl' ? ' -D__MUSL__' : ' -U__MUSL__'; $php_configure_env = SystemUtil::makeEnvVarString([ - 'CFLAGS' => getenv('SPC_CMD_VAR_PHP_CONFIGURE_CFLAGS'), + 'CFLAGS' => getenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS'), 'CPPFLAGS' => '-I' . BUILD_INCLUDE_PATH, // . ' -Dsomethinghere', // . $musl_flag, 'LDFLAGS' => '-L' . BUILD_LIB_PATH, // 'LIBS' => SPCTarget::getRuntimeLibs(), // do not pass static libraries here yet, they may contain polyfills for libc functions! @@ -194,15 +195,7 @@ protected function buildCli(): void SourcePatcher::patchFile('musl_static_readline.patch', SOURCE_PATH . '/php-src', true); } - if (!$this->getOption('no-strip', false)) { - shell()->cd(SOURCE_PATH . '/php-src/sapi/cli')->exec('strip --strip-unneeded php'); - } - if ($this->getOption('with-upx-pack')) { - shell()->cd(SOURCE_PATH . '/php-src/sapi/cli') - ->exec(getenv('UPX_EXEC') . ' --best php'); - } - - $this->deployBinary(BUILD_TARGET_CLI); + $this->deploySAPIBinary(BUILD_TARGET_CLI); } protected function buildCgi(): void @@ -213,15 +206,7 @@ protected function buildCgi(): void ->exec('sed -i "s|//lib|/lib|g" Makefile') ->exec("make {$concurrency} {$vars} cgi"); - if (!$this->getOption('no-strip', false)) { - shell()->cd(SOURCE_PATH . '/php-src/sapi/cgi')->exec('strip --strip-unneeded php-cgi'); - } - if ($this->getOption('with-upx-pack')) { - shell()->cd(SOURCE_PATH . '/php-src/sapi/cgi') - ->exec(getenv('UPX_EXEC') . ' --best php-cgi'); - } - - $this->deployBinary(BUILD_TARGET_CGI); + $this->deploySAPIBinary(BUILD_TARGET_CGI); } /** @@ -232,29 +217,33 @@ protected function buildMicro(): void if ($this->getPHPVersionID() < 80000) { throw new WrongUsageException('phpmicro only support PHP >= 8.0!'); } - if ($this->getExt('phar')) { - $this->phar_patched = true; - SourcePatcher::patchMicroPhar($this->getPHPVersionID()); - } + try { + if ($this->getExt('phar')) { + $this->phar_patched = true; + SourcePatcher::patchMicroPhar($this->getPHPVersionID()); + } - $enable_fake_cli = $this->getOption('with-micro-fake-cli', false) ? ' -DPHP_MICRO_FAKE_CLI' : ''; - $vars = $this->getMakeExtraVars(); + $enable_fake_cli = $this->getOption('with-micro-fake-cli', false) ? ' -DPHP_MICRO_FAKE_CLI' : ''; + $vars = $this->getMakeExtraVars(); - // patch fake cli for micro - $vars['EXTRA_CFLAGS'] .= $enable_fake_cli; - $vars = SystemUtil::makeEnvVarString($vars); - $concurrency = getenv('SPC_CONCURRENCY') ? '-j' . getenv('SPC_CONCURRENCY') : ''; + // patch fake cli for micro + $vars['EXTRA_CFLAGS'] .= $enable_fake_cli; + $vars = SystemUtil::makeEnvVarString($vars); + $concurrency = getenv('SPC_CONCURRENCY') ? '-j' . getenv('SPC_CONCURRENCY') : ''; - shell()->cd(SOURCE_PATH . '/php-src') - ->exec('sed -i "s|//lib|/lib|g" Makefile') - ->exec("make {$concurrency} {$vars} micro"); - - $this->processMicroUPX(); + shell()->cd(SOURCE_PATH . '/php-src') + ->exec('sed -i "s|//lib|/lib|g" Makefile') + ->exec("make {$concurrency} {$vars} micro"); - $this->deployBinary(BUILD_TARGET_MICRO); + // deploy micro.sfx + $dst = $this->deploySAPIBinary(BUILD_TARGET_MICRO); - if ($this->phar_patched) { - SourcePatcher::unpatchMicroPhar(); + // patch after UPX-ed micro.sfx + $this->processUpxedMicroSfx($dst); + } finally { + if ($this->phar_patched) { + SourcePatcher::unpatchMicroPhar(); + } } } @@ -269,14 +258,7 @@ protected function buildFpm(): void ->exec('sed -i "s|//lib|/lib|g" Makefile') ->exec("make {$concurrency} {$vars} fpm"); - if (!$this->getOption('no-strip', false)) { - shell()->cd(SOURCE_PATH . '/php-src/sapi/fpm')->exec('strip --strip-unneeded php-fpm'); - } - if ($this->getOption('with-upx-pack')) { - shell()->cd(SOURCE_PATH . '/php-src/sapi/fpm') - ->exec(getenv('UPX_EXEC') . ' --best php-fpm'); - } - $this->deployBinary(BUILD_TARGET_FPM); + $this->deploySAPIBinary(BUILD_TARGET_FPM); } /** @@ -289,6 +271,9 @@ protected function buildEmbed(): void return Config::getExt($ext->getName(), 'build-with-php') === true; }); $install_modules = $sharedExts ? 'install-modules' : ''; + + // detect changes in module path + $diff = new DirDiff(BUILD_MODULES_PATH, true); $vars = SystemUtil::makeEnvVarString($this->getMakeExtraVars()); $concurrency = getenv('SPC_CONCURRENCY') ? '-j' . getenv('SPC_CONCURRENCY') : ''; shell()->cd(SOURCE_PATH . '/php-src') @@ -296,10 +281,54 @@ protected function buildEmbed(): void ->exec('sed -i "s|^EXTENSION_DIR = .*|EXTENSION_DIR = /' . basename(BUILD_MODULES_PATH) . '|" Makefile') ->exec("make {$concurrency} INSTALL_ROOT=" . BUILD_ROOT_PATH . " {$vars} install-sapi {$install_modules} install-build install-headers install-programs"); + // process libphp.so for shared embed + $libphpSo = BUILD_LIB_PATH . '/libphp.so'; + if (file_exists($libphpSo)) { + // post actions: rename libphp.so to libphp-.so if -release is set in LDFLAGS + $this->processLibphpSoFile($libphpSo); + // deploy libphp.so + $this->deployBinary($libphpSo, $libphpSo, false); + } + + // process shared extensions build-with-php + $increment_files = $diff->getChangedFiles(); + foreach ($increment_files as $increment_file) { + $this->deployBinary($increment_file, $increment_file, false); + } + + // process libphp.a for static embed + if (getenv('SPC_CMD_VAR_PHP_EMBED_TYPE') === 'static') { + $AR = getenv('AR') ?: 'ar'; + f_passthru("{$AR} -t " . BUILD_LIB_PATH . "/libphp.a | grep '\\.a$' | xargs -n1 {$AR} d " . BUILD_LIB_PATH . '/libphp.a'); + // export dynamic symbols + SystemUtil::exportDynamicSymbols(BUILD_LIB_PATH . '/libphp.a'); + } + + // patch embed php scripts + $this->patchPhpScripts(); + } + + /** + * Return extra variables for php make command. + */ + private function getMakeExtraVars(): array + { + $config = (new SPCConfigUtil($this, ['libs_only_deps' => true, 'absolute_libs' => true]))->config($this->ext_list, $this->lib_list, $this->getOption('with-suggested-exts'), $this->getOption('with-suggested-libs')); + $static = SPCTarget::isStatic() ? '-all-static' : ''; + $lib = BUILD_LIB_PATH; + return array_filter([ + 'EXTRA_CFLAGS' => getenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS'), + 'EXTRA_LIBS' => $config['libs'], + 'EXTRA_LDFLAGS' => getenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_LDFLAGS'), + 'EXTRA_LDFLAGS_PROGRAM' => "-L{$lib} {$static} -pie", + ]); + } + + private function processLibphpSoFile(string $libphpSo): void + { $ldflags = getenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_LDFLAGS') ?: ''; $libDir = BUILD_LIB_PATH; $modulesDir = BUILD_MODULES_PATH; - $libphpSo = "{$libDir}/libphp.so"; $realLibName = 'libphp.so'; $cwd = getcwd(); @@ -361,60 +390,29 @@ protected function buildEmbed(): void } } } - - if (getenv('SPC_CMD_VAR_PHP_EMBED_TYPE') === 'static') { - $AR = getenv('AR') ?: 'ar'; - f_passthru("{$AR} -t " . BUILD_LIB_PATH . "/libphp.a | grep '\\.a$' | xargs -n1 {$AR} d " . BUILD_LIB_PATH . '/libphp.a'); - // export dynamic symbols - SystemUtil::exportDynamicSymbols(BUILD_LIB_PATH . '/libphp.a'); - } - - if (!$this->getOption('no-strip', false) && file_exists(BUILD_LIB_PATH . '/' . $realLibName)) { - shell()->cd(BUILD_LIB_PATH)->exec("strip --strip-unneeded {$realLibName}"); - } - $this->patchPhpScripts(); - } - - /** - * Return extra variables for php make command. - */ - private function getMakeExtraVars(): array - { - $config = (new SPCConfigUtil($this, ['libs_only_deps' => true, 'absolute_libs' => true]))->config($this->ext_list, $this->lib_list, $this->getOption('with-suggested-exts'), $this->getOption('with-suggested-libs')); - $static = SPCTarget::isStatic() ? '-all-static' : ''; - $lib = BUILD_LIB_PATH; - return array_filter([ - 'EXTRA_CFLAGS' => getenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS'), - 'EXTRA_LIBS' => $config['libs'], - 'EXTRA_LDFLAGS' => getenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_LDFLAGS'), - 'EXTRA_LDFLAGS_PROGRAM' => "-L{$lib} {$static} -pie", - ]); } /** - * Strip micro.sfx for Linux. - * The micro.sfx does not support UPX directly, but we can remove UPX-info segment to adapt. - * This will also make micro.sfx with upx-packed more like a malware fore antivirus :( + * Patch micro.sfx after UPX compression. + * micro needs special section handling in LinuxBuilder. + * The micro.sfx does not support UPX directly, but we can remove UPX + * info segment to adapt. + * This will also make micro.sfx with upx-packed more like a malware fore antivirus */ - private function processMicroUPX(): void + private function processUpxedMicroSfx(string $dst): void { - if (version_compare($this->getMicroVersion(), '0.2.0') >= 0 && !$this->getOption('no-strip', false)) { - shell()->exec('strip --strip-unneeded ' . SOURCE_PATH . '/php-src/sapi/micro/micro.sfx'); - - if ($this->getOption('with-upx-pack')) { - // strip first - shell()->exec(getenv('UPX_EXEC') . ' --best ' . SOURCE_PATH . '/php-src/sapi/micro/micro.sfx'); - // cut binary with readelf - [$ret, $out] = shell()->execWithResult('readelf -l ' . SOURCE_PATH . '/php-src/sapi/micro/micro.sfx | awk \'/LOAD|GNU_STACK/ {getline; print $1, $2, $3, $4, $6, $7}\''); - $out[1] = explode(' ', $out[1]); - $offset = $out[1][0]; - if ($ret !== 0 || !str_starts_with($offset, '0x')) { - throw new PatchException('phpmicro UPX patcher', 'Cannot find offset in readelf output'); - } - $offset = hexdec($offset); - // remove upx extra wastes - file_put_contents(SOURCE_PATH . '/php-src/sapi/micro/micro.sfx', substr(file_get_contents(SOURCE_PATH . '/php-src/sapi/micro/micro.sfx'), 0, $offset)); + if ($this->getOption('with-upx-pack') && version_compare($this->getMicroVersion(), '0.2.0') >= 0) { + // strip first + // cut binary with readelf + [$ret, $out] = shell()->execWithResult("readelf -l {$dst} | awk '/LOAD|GNU_STACK/ {getline; print \$1, \$2, \$3, \$4, \$6, \$7}'"); + $out[1] = explode(' ', $out[1]); + $offset = $out[1][0]; + if ($ret !== 0 || !str_starts_with($offset, '0x')) { + throw new PatchException('phpmicro UPX patcher', 'Cannot find offset in readelf output'); } + $offset = hexdec($offset); + // remove upx extra wastes + file_put_contents($dst, substr(file_get_contents($dst), 0, $offset)); } } } diff --git a/src/SPC/builder/macos/MacOSBuilder.php b/src/SPC/builder/macos/MacOSBuilder.php index bcfaf6cf0..07ef275d6 100644 --- a/src/SPC/builder/macos/MacOSBuilder.php +++ b/src/SPC/builder/macos/MacOSBuilder.php @@ -8,6 +8,7 @@ use SPC\builder\unix\UnixBuilderBase; use SPC\exception\WrongUsageException; use SPC\store\Config; +use SPC\store\DirDiff; use SPC\store\FileSystem; use SPC\store\SourcePatcher; use SPC\util\GlobalEnvManager; @@ -105,7 +106,7 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void // prepare build php envs $envs_build_php = SystemUtil::makeEnvVarString([ - 'CFLAGS' => getenv('SPC_CMD_VAR_PHP_CONFIGURE_CFLAGS'), + 'CFLAGS' => getenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS'), 'CPPFLAGS' => '-I' . BUILD_INCLUDE_PATH, 'LDFLAGS' => '-L' . BUILD_LIB_PATH, ]); @@ -189,10 +190,7 @@ protected function buildCli(): void $shell = shell()->cd(SOURCE_PATH . '/php-src'); $concurrency = getenv('SPC_CONCURRENCY') ? '-j' . getenv('SPC_CONCURRENCY') : ''; $shell->exec("make {$concurrency} {$vars} cli"); - if (!$this->getOption('no-strip', false)) { - $shell->exec('dsymutil -f sapi/cli/php')->exec('strip -S sapi/cli/php'); - } - $this->deployBinary(BUILD_TARGET_CLI); + $this->deploySAPIBinary(BUILD_TARGET_CLI); } protected function buildCgi(): void @@ -202,10 +200,7 @@ protected function buildCgi(): void $shell = shell()->cd(SOURCE_PATH . '/php-src'); $concurrency = getenv('SPC_CONCURRENCY') ? '-j' . getenv('SPC_CONCURRENCY') : ''; $shell->exec("make {$concurrency} {$vars} cgi"); - if (!$this->getOption('no-strip', false)) { - $shell->exec('dsymutil -f sapi/cgi/php-cgi')->exec('strip -S sapi/cgi/php-cgi'); - } - $this->deployBinary(BUILD_TARGET_CGI); + $this->deploySAPIBinary(BUILD_TARGET_CGI); } /** @@ -216,31 +211,30 @@ protected function buildMicro(): void if ($this->getPHPVersionID() < 80000) { throw new WrongUsageException('phpmicro only support PHP >= 8.0!'); } - if ($this->getExt('phar')) { - $this->phar_patched = true; - SourcePatcher::patchMicroPhar($this->getPHPVersionID()); - } - $enable_fake_cli = $this->getOption('with-micro-fake-cli', false) ? ' -DPHP_MICRO_FAKE_CLI' : ''; - $vars = $this->getMakeExtraVars(); + try { + if ($this->getExt('phar')) { + $this->phar_patched = true; + SourcePatcher::patchMicroPhar($this->getPHPVersionID()); + } - // patch fake cli for micro - $vars['EXTRA_CFLAGS'] .= $enable_fake_cli; - $vars = SystemUtil::makeEnvVarString($vars); + $enable_fake_cli = $this->getOption('with-micro-fake-cli', false) ? ' -DPHP_MICRO_FAKE_CLI' : ''; + $vars = $this->getMakeExtraVars(); - $shell = shell()->cd(SOURCE_PATH . '/php-src'); - // build - $concurrency = getenv('SPC_CONCURRENCY') ? '-j' . getenv('SPC_CONCURRENCY') : ''; - $shell->exec("make {$concurrency} {$vars} micro"); - // strip - if (!$this->getOption('no-strip', false)) { - $shell->exec('dsymutil -f sapi/micro/micro.sfx')->exec('strip -S sapi/micro/micro.sfx'); - } + // patch fake cli for micro + $vars['EXTRA_CFLAGS'] .= $enable_fake_cli; + $vars = SystemUtil::makeEnvVarString($vars); - $this->deployBinary(BUILD_TARGET_MICRO); + $shell = shell()->cd(SOURCE_PATH . '/php-src'); + // build + $concurrency = getenv('SPC_CONCURRENCY') ? '-j' . getenv('SPC_CONCURRENCY') : ''; + $shell->exec("make {$concurrency} {$vars} micro"); - if ($this->phar_patched) { - SourcePatcher::unpatchMicroPhar(); + $this->deploySAPIBinary(BUILD_TARGET_MICRO); + } finally { + if ($this->phar_patched) { + SourcePatcher::unpatchMicroPhar(); + } } } @@ -254,10 +248,7 @@ protected function buildFpm(): void $shell = shell()->cd(SOURCE_PATH . '/php-src'); $concurrency = getenv('SPC_CONCURRENCY') ? '-j' . getenv('SPC_CONCURRENCY') : ''; $shell->exec("make {$concurrency} {$vars} fpm"); - if (!$this->getOption('no-strip', false)) { - $shell->exec('dsymutil -f sapi/fpm/php-fpm')->exec('strip -S sapi/fpm/php-fpm'); - } - $this->deployBinary(BUILD_TARGET_FPM); + $this->deploySAPIBinary(BUILD_TARGET_FPM); } /** @@ -272,9 +263,25 @@ protected function buildEmbed(): void $install_modules = $sharedExts ? 'install-modules' : ''; $vars = SystemUtil::makeEnvVarString($this->getMakeExtraVars()); $concurrency = getenv('SPC_CONCURRENCY') ? '-j' . getenv('SPC_CONCURRENCY') : ''; + + $diff = new DirDiff(BUILD_MODULES_PATH, true); + shell()->cd(SOURCE_PATH . '/php-src') + ->exec('sed -i "" "s|^EXTENSION_DIR = .*|EXTENSION_DIR = /' . basename(BUILD_MODULES_PATH) . '|" Makefile') ->exec("make {$concurrency} INSTALL_ROOT=" . BUILD_ROOT_PATH . " {$vars} install-sapi {$install_modules} install-build install-headers install-programs"); + $libphp = BUILD_LIB_PATH . '/libphp.dylib'; + if (file_exists($libphp)) { + $this->deployBinary($libphp, $libphp, false); + // macOS currently have no -release option for dylib, so we just rename it here + } + + // process shared extensions build-with-php + $increment_files = $diff->getChangedFiles(); + foreach ($increment_files as $increment_file) { + $this->deployBinary($increment_file, $increment_file, false); + } + if (getenv('SPC_CMD_VAR_PHP_EMBED_TYPE') === 'static') { $AR = getenv('AR') ?: 'ar'; f_passthru("{$AR} -t " . BUILD_LIB_PATH . "/libphp.a | grep '\\.a$' | xargs -n1 {$AR} d " . BUILD_LIB_PATH . '/libphp.a'); diff --git a/src/SPC/builder/unix/UnixBuilderBase.php b/src/SPC/builder/unix/UnixBuilderBase.php index 2a9bcb9bb..d98059cb0 100644 --- a/src/SPC/builder/unix/UnixBuilderBase.php +++ b/src/SPC/builder/unix/UnixBuilderBase.php @@ -5,6 +5,7 @@ namespace SPC\builder\unix; use SPC\builder\BuilderBase; +use SPC\builder\linux\SystemUtil; use SPC\builder\linux\SystemUtil as LinuxSystemUtil; use SPC\exception\SPCException; use SPC\exception\SPCInternalException; @@ -79,6 +80,92 @@ public function proveLibs(array $sorted_libraries): void $this->lib_list = $sorted_libraries; } + /** + * Strip unneeded symbols from binary file. + */ + public function stripBinary(string $binary_path): void + { + shell()->exec(match (PHP_OS_FAMILY) { + 'Darwin' => "strip -S {$binary_path}", + 'Linux' => "strip --strip-unneeded {$binary_path}", + default => throw new SPCInternalException('stripBinary is only supported on Linux and macOS'), + }); + } + + /** + * Extract debug information from binary file. + * + * @param string $binary_path the path to the binary file, including executables, shared libraries, etc + */ + public function extractDebugInfo(string $binary_path): string + { + $target_dir = BUILD_ROOT_PATH . '/debug'; + FileSystem::createDir($target_dir); + $basename = basename($binary_path); + $debug_file = "{$target_dir}/{$basename}" . (PHP_OS_FAMILY === 'Darwin' ? '.dwarf' : '.debug'); + if (PHP_OS_FAMILY === 'Darwin') { + shell()->exec("dsymutil -f {$binary_path} -o {$debug_file}"); + } elseif (PHP_OS_FAMILY === 'Linux') { + if ($eu_strip = SystemUtil::findCommand('eu-strip')) { + shell() + ->exec("{$eu_strip} -f {$debug_file} {$binary_path}") + ->exec("objcopy --add-gnu-debuglink={$debug_file} {$binary_path}"); + } else { + shell() + ->exec("objcopy --only-keep-debug {$binary_path} {$debug_file}") + ->exec("objcopy --add-gnu-debuglink={$debug_file} {$binary_path}"); + } + } else { + throw new SPCInternalException('extractDebugInfo is only supported on Linux and macOS'); + } + return $debug_file; + } + + /** + * Deploy the binary file from src to dst. + */ + public function deployBinary(string $src, string $dst, bool $executable = true): string + { + logger()->debug('Deploying binary from ' . $src . ' to ' . $dst); + + // file must exists + if (!file_exists($src)) { + throw new SPCInternalException("Deploy failed. Cannot find file: {$src}"); + } + // dst dir must exists + FileSystem::createDir(dirname($dst)); + + // ignore copy to self + if (realpath($src) !== realpath($dst)) { + shell()->exec('cp ' . escapeshellarg($src) . ' ' . escapeshellarg($dst)); + } + + // file exist + if (!file_exists($dst)) { + throw new SPCInternalException("Deploy failed. Cannot find file after copy: {$dst}"); + } + + // extract debug info + $this->extractDebugInfo($dst); + + // strip + if (!$this->getOption('no-strip')) { + $this->stripBinary($dst); + } + + // UPX for linux + $upx_option = $this->getOption('with-upx-pack'); + if ($upx_option && PHP_OS_FAMILY === 'Linux' && $executable) { + if ($this->getOption('no-strip')) { + logger()->warning('UPX compression is not recommended when --no-strip is enabled.'); + } + logger()->info("Compressing {$dst} with UPX"); + shell()->exec(getenv('UPX_EXEC') . " --best {$dst}"); + } + + return $dst; + } + /** * Sanity check after build complete. */ @@ -209,23 +296,20 @@ protected function sanityCheck(int $build_target): void } /** - * Deploy the binary file to the build bin path. - * - * @param int $type Type integer, one of BUILD_TARGET_CLI, BUILD_TARGET_MICRO, BUILD_TARGET_FPM + * Deploy binaries that produces executable SAPI */ - protected function deployBinary(int $type): bool + protected function deploySAPIBinary(int $type): string { $src = match ($type) { BUILD_TARGET_CLI => SOURCE_PATH . '/php-src/sapi/cli/php', BUILD_TARGET_MICRO => SOURCE_PATH . '/php-src/sapi/micro/micro.sfx', BUILD_TARGET_FPM => SOURCE_PATH . '/php-src/sapi/fpm/php-fpm', BUILD_TARGET_CGI => SOURCE_PATH . '/php-src/sapi/cgi/php-cgi', + BUILD_TARGET_FRANKENPHP => BUILD_BIN_PATH . '/frankenphp', default => throw new SPCInternalException("Deployment does not accept type {$type}"), }; - logger()->info('Deploying ' . $this->getBuildTypeName($type) . ' file'); - FileSystem::createDir(BUILD_BIN_PATH); - shell()->exec('cp ' . escapeshellarg($src) . ' ' . escapeshellarg(BUILD_BIN_PATH)); - return true; + $dst = BUILD_BIN_PATH . '/' . basename($src); + return $this->deployBinary($src, $dst); } /** @@ -272,8 +356,10 @@ protected function patchPhpScripts(): void */ protected function processFrankenphpApp(): void { - $frankenphpSourceDir = SOURCE_PATH . '/frankenphp'; - SourceManager::initSource(['frankenphp'], ['frankenphp']); + $frankenphpSourceDir = getenv('FRANKENPHP_SOURCE_PATH') ?: SOURCE_PATH . '/frankenphp'; + if (!is_dir($frankenphpSourceDir)) { + SourceManager::initSource(['frankenphp'], ['frankenphp']); + } $frankenphpAppPath = $this->getOption('with-frankenphp-app'); if ($frankenphpAppPath) { @@ -297,7 +383,11 @@ protected function processFrankenphpApp(): void protected function getFrankenPHPVersion(): string { - $goModPath = SOURCE_PATH . '/frankenphp/caddy/go.mod'; + if ($version = getenv('FRANKENPHP_VERSION')) { + return $version; + } + $frankenphpSourceDir = getenv('FRANKENPHP_SOURCE_PATH') ?: SOURCE_PATH . '/frankenphp'; + $goModPath = $frankenphpSourceDir . '/caddy/go.mod'; if (!file_exists($goModPath)) { throw new SPCInternalException("FrankenPHP caddy/go.mod file not found at {$goModPath}, why did we not download FrankenPHP?"); @@ -318,7 +408,7 @@ protected function buildFrankenphp(): void $nobrotli = $this->getLib('brotli') === null ? ',nobrotli' : ''; $nowatcher = $this->getLib('watcher') === null ? ',nowatcher' : ''; $xcaddyModules = getenv('SPC_CMD_VAR_FRANKENPHP_XCADDY_MODULES'); - $frankenphpSourceDir = SOURCE_PATH . '/frankenphp'; + $frankenphpSourceDir = getenv('FRANKENPHP_SOURCE_PATH') ?: SOURCE_PATH . '/frankenphp'; $xcaddyModules = preg_replace('#--with github.com/dunglas/frankenphp\S*#', '', $xcaddyModules); $xcaddyModules = "--with github.com/dunglas/frankenphp={$frankenphpSourceDir} " . @@ -338,7 +428,6 @@ protected function buildFrankenphp(): void $dynamic_exports = ' ' . $dynamicSymbolsArgument; } } - $debugFlags = $this->getOption('no-strip') ? '' : '-w -s '; $extLdFlags = "-extldflags '-pie{$dynamic_exports} {$this->arch_ld_flags}'"; $muslTags = ''; $staticFlags = ''; @@ -363,7 +452,7 @@ protected function buildFrankenphp(): void 'CGO_CFLAGS' => clean_spaces($cflags), 'CGO_LDFLAGS' => "{$this->arch_ld_flags} {$staticFlags} {$config['ldflags']} {$libs}", 'XCADDY_GO_BUILD_FLAGS' => '-buildmode=pie ' . - '-ldflags \"-linkmode=external ' . $extLdFlags . ' ' . $debugFlags . + '-ldflags \"-linkmode=external ' . $extLdFlags . ' ' . '-X \'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP ' . "v{$frankenPhpVersion} PHP {$libphpVersion} Caddy'\\\" " . "-tags={$muslTags}nobadger,nomysql,nopgx{$nobrotli}{$nowatcher}", @@ -373,13 +462,7 @@ protected function buildFrankenphp(): void ->setEnv($env) ->exec("xcaddy build --output frankenphp {$xcaddyModules}"); - if (!$this->getOption('no-strip', false) && file_exists(BUILD_BIN_PATH . '/frankenphp')) { - if (PHP_OS_FAMILY === 'Linux') { - shell()->cd(BUILD_BIN_PATH)->exec('strip --strip-unneeded frankenphp'); - } else { // macOS doesn't understand strip-unneeded - shell()->cd(BUILD_BIN_PATH)->exec('strip -S frankenphp'); - } - } + $this->deploySAPIBinary(BUILD_TARGET_FRANKENPHP); } /** diff --git a/src/SPC/builder/windows/WindowsBuilder.php b/src/SPC/builder/windows/WindowsBuilder.php index 1892d8014..e4f1578c4 100644 --- a/src/SPC/builder/windows/WindowsBuilder.php +++ b/src/SPC/builder/windows/WindowsBuilder.php @@ -67,21 +67,6 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void $zts = $this->zts ? '--enable-zts=yes ' : '--enable-zts=no '; - // with-upx-pack for phpmicro - if ($enableMicro && version_compare($this->getMicroVersion(), '0.2.0') < 0) { - $makefile = FileSystem::convertPath(SOURCE_PATH . '/php-src/sapi/micro/Makefile.frag.w32'); - if ($this->getOption('with-upx-pack', false)) { - if (!file_exists($makefile . '.originfile')) { - copy($makefile, $makefile . '.originfile'); - FileSystem::replaceFileStr($makefile, '$(MICRO_SFX):', '_MICRO_UPX = ' . getenv('UPX_EXEC') . " --best $(MICRO_SFX)\n$(MICRO_SFX):"); - FileSystem::replaceFileStr($makefile, '@$(_MICRO_MT)', "@$(_MICRO_MT)\n\t@$(_MICRO_UPX)"); - } - } elseif (file_exists($makefile . '.originfile')) { - copy($makefile . '.originfile', $makefile); - unlink($makefile . '.originfile'); - } - } - $opcache_jit = !$this->getOption('disable-opcache-jit', false); $opcache_jit_arg = $opcache_jit ? '--enable-opcache-jit=yes ' : '--enable-opcache-jit=no '; @@ -145,7 +130,7 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void } } - public function testPHP(int $build_target = BUILD_TARGET_NONE) + public function testPHP(int $build_target = BUILD_TARGET_NONE): void { $this->sanityCheck($build_target); } @@ -156,12 +141,27 @@ public function buildCli(): void $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 ($this->getOption('no-strip', false)) { + // Read current CFLAGS from Makefile and replace optimization flags + $makefile_content = file_get_contents(SOURCE_PATH . '\php-src\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" '; + } + } + // add nmake wrapper - FileSystem::writeFile(SOURCE_PATH . '\php-src\nmake_cli_wrapper.bat', "nmake /nologo LIBS_CLI=\"ws2_32.lib shell32.lib {$extra_libs}\" EXTRA_LD_FLAGS_PROGRAM= %*"); + FileSystem::writeFile(SOURCE_PATH . '\php-src\nmake_cli_wrapper.bat', "nmake /nologo {$debug_overrides}LIBS_CLI=\"ws2_32.lib shell32.lib {$extra_libs}\" EXTRA_LD_FLAGS_PROGRAM= %*"); cmd()->cd(SOURCE_PATH . '\php-src')->exec("{$this->sdk_prefix} nmake_cli_wrapper.bat --task-args php.exe"); - $this->deployBinary(BUILD_TARGET_CLI); + $this->deploySAPIBinary(BUILD_TARGET_CLI); } public function buildCgi(): void @@ -170,12 +170,23 @@ public function buildCgi(): void $extra_libs = getenv('SPC_EXTRA_LIBS') ?: ''; + // Add debug symbols for release build if --no-strip is specified + $debug_overrides = ''; + if ($this->getOption('no-strip', false)) { + $makefile_content = file_get_contents(SOURCE_PATH . '\php-src\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" '; + } + } + // add nmake wrapper - FileSystem::writeFile(SOURCE_PATH . '\php-src\nmake_cgi_wrapper.bat', "nmake /nologo LIBS_CGI=\"ws2_32.lib kernel32.lib advapi32.lib {$extra_libs}\" EXTRA_LD_FLAGS_PROGRAM= %*"); + FileSystem::writeFile(SOURCE_PATH . '\php-src\nmake_cgi_wrapper.bat', "nmake /nologo {$debug_overrides}LIBS_CGI=\"ws2_32.lib kernel32.lib advapi32.lib {$extra_libs}\" EXTRA_LD_FLAGS_PROGRAM= %*"); cmd()->cd(SOURCE_PATH . '\php-src')->exec("{$this->sdk_prefix} nmake_cgi_wrapper.bat --task-args php-cgi.exe"); - $this->deployBinary(BUILD_TARGET_CGI); + $this->deploySAPIBinary(BUILD_TARGET_CGI); } public function buildEmbed(): void @@ -202,9 +213,20 @@ public function buildMicro(): void $extra_libs = getenv('SPC_EXTRA_LIBS') ?: ''; + // Add debug symbols for release build if --no-strip is specified + $debug_overrides = ''; + if ($this->getOption('no-strip', false) && !$this->getOption('debug', false)) { + $makefile_content = file_get_contents(SOURCE_PATH . '\php-src\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" '; + } + } + // add nmake wrapper $fake_cli = $this->getOption('with-micro-fake-cli', false) ? ' /DPHP_MICRO_FAKE_CLI" ' : ''; - $wrapper = "nmake /nologo LIBS_MICRO=\"ws2_32.lib shell32.lib {$extra_libs}\" CFLAGS_MICRO=\"/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1{$fake_cli}\" %*"; + $wrapper = "nmake /nologo {$debug_overrides}LIBS_MICRO=\"ws2_32.lib shell32.lib {$extra_libs}\" CFLAGS_MICRO=\"/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1{$fake_cli}\" %*"; FileSystem::writeFile(SOURCE_PATH . '\php-src\nmake_micro_wrapper.bat', $wrapper); // phar patch for micro @@ -221,7 +243,7 @@ public function buildMicro(): void } } - $this->deployBinary(BUILD_TARGET_MICRO); + $this->deploySAPIBinary(BUILD_TARGET_MICRO); } public function proveLibs(array $sorted_libraries): void @@ -335,28 +357,53 @@ public function sanityCheck(mixed $build_target): void * * @param int $type Deploy type */ - public function deployBinary(int $type): bool + public function deploySAPIBinary(int $type): void { + logger()->info('Deploying ' . $this->getBuildTypeName($type) . ' file'); + + $debug_dir = BUILD_ROOT_PATH . '\debug'; + $bin_path = BUILD_BIN_PATH; + + // create dirs + FileSystem::createDir($debug_dir); + FileSystem::createDir($bin_path); + + // determine source path for different SAPI + $rel_type = 'Release'; // TODO: Debug build support $ts = $this->zts ? '_TS' : ''; $src = match ($type) { - BUILD_TARGET_CLI => SOURCE_PATH . "\\php-src\\x64\\Release{$ts}\\php.exe", - BUILD_TARGET_MICRO => SOURCE_PATH . "\\php-src\\x64\\Release{$ts}\\micro.sfx", - BUILD_TARGET_CGI => SOURCE_PATH . "\\php-src\\x64\\Release{$ts}\\php-cgi.exe", + BUILD_TARGET_CLI => [SOURCE_PATH . "\\php-src\\x64\\{$rel_type}{$ts}", 'php.exe', 'php.pdb'], + BUILD_TARGET_MICRO => [SOURCE_PATH . "\\php-src\\x64\\{$rel_type}{$ts}", 'micro.sfx', 'micro.pdb'], + BUILD_TARGET_CGI => [SOURCE_PATH . "\\php-src\\x64\\{$rel_type}{$ts}", 'php-cgi.exe', 'php-cgi.pdb'], default => throw new SPCInternalException("Deployment does not accept type {$type}"), }; + $src = "{$src[0]}\\{$src[1]}"; + $dst = BUILD_BIN_PATH . '\\' . basename($src); + + // file must exists + if (!file_exists($src)) { + throw new SPCInternalException("Deploy failed. Cannot find file: {$src}"); + } + // dst dir must exists + FileSystem::createDir(dirname($dst)); + + // copy file + if (realpath($src) !== realpath($dst)) { + cmd()->exec('copy ' . escapeshellarg($src) . ' ' . escapeshellarg($dst)); + } + + // extract debug info in buildroot/debug + if ($this->getOption('no-strip', false) && file_exists("{$src[0]}\\{$src[2]}")) { + cmd()->exec('copy ' . escapeshellarg("{$src[0]}\\{$src[2]}") . ' ' . escapeshellarg($debug_dir)); + } + // with-upx-pack for cli and micro if ($this->getOption('with-upx-pack', false)) { if ($type === BUILD_TARGET_CLI || $type === BUILD_TARGET_CGI || ($type === BUILD_TARGET_MICRO && version_compare($this->getMicroVersion(), '0.2.0') >= 0)) { - cmd()->exec(getenv('UPX_EXEC') . ' --best ' . escapeshellarg($src)); + cmd()->exec(getenv('UPX_EXEC') . ' --best ' . escapeshellarg($dst)); } } - - logger()->info('Deploying ' . $this->getBuildTypeName($type) . ' file'); - FileSystem::createDir(BUILD_BIN_PATH); - - cmd()->exec('copy ' . escapeshellarg($src) . ' ' . escapeshellarg(BUILD_BIN_PATH . '\\')); - return true; } /** diff --git a/src/SPC/store/DirDiff.php b/src/SPC/store/DirDiff.php new file mode 100644 index 000000000..8cd5c1d40 --- /dev/null +++ b/src/SPC/store/DirDiff.php @@ -0,0 +1,95 @@ +reset(); + } + + /** + * Reset the baseline to current state. + */ + public function reset(): void + { + $this->before = FileSystem::scanDirFiles($this->dir, relative: true) ?: []; + + if ($this->track_content_changes) { + $this->before_file_hashes = []; + foreach ($this->before as $file) { + $this->before_file_hashes[$file] = md5_file($this->dir . DIRECTORY_SEPARATOR . $file); + } + } + } + + /** + * Get the list of incremented files. + * + * @param bool $relative Return relative paths or absolute paths + * @return array List of incremented files + */ + public function getIncrementFiles(bool $relative = false): array + { + $after = FileSystem::scanDirFiles($this->dir, relative: true) ?: []; + $diff = array_diff($after, $this->before); + if ($relative) { + return $diff; + } + return array_map(fn ($f) => $this->dir . DIRECTORY_SEPARATOR . $f, $diff); + } + + /** + * Get the list of changed files (including new files). + * + * @param bool $relative Return relative paths or absolute paths + * @param bool $include_new_files Include new files as changed files + * @return array List of changed files + */ + public function getChangedFiles(bool $relative = false, bool $include_new_files = true): array + { + $after = FileSystem::scanDirFiles($this->dir, relative: true) ?: []; + $changed = []; + foreach ($after as $file) { + if (isset($this->before_file_hashes[$file])) { + $after_hash = md5_file($this->dir . DIRECTORY_SEPARATOR . $file); + if ($after_hash !== $this->before_file_hashes[$file]) { + $changed[] = $file; + } + } elseif ($include_new_files) { + // New file, consider as changed + $changed[] = $file; + } + } + if ($relative) { + return $changed; + } + return array_map(fn ($f) => $this->dir . DIRECTORY_SEPARATOR . $f, $changed); + } + + /** + * Get the list of removed files. + * + * @param bool $relative Return relative paths or absolute paths + * @return array List of removed files + */ + public function getRemovedFiles(bool $relative = false): array + { + $after = FileSystem::scanDirFiles($this->dir, relative: true) ?: []; + $removed = array_diff($this->before, $after); + if ($relative) { + return $removed; + } + return array_map(fn ($f) => $this->dir . DIRECTORY_SEPARATOR . $f, $removed); + } +} diff --git a/src/SPC/store/FileSystem.php b/src/SPC/store/FileSystem.php index f6ce371e0..86daefc43 100644 --- a/src/SPC/store/FileSystem.php +++ b/src/SPC/store/FileSystem.php @@ -174,6 +174,9 @@ public static function copy(string $from, string $to): void logger()->debug("Copying file from {$from} to {$to}"); $dst_path = FileSystem::convertPath($to); $src_path = FileSystem::convertPath($from); + if ($src_path === $dst_path) { + return; + } if (!copy($src_path, $dst_path)) { throw new FileSystemException('Cannot copy file from ' . $src_path . ' to ' . $dst_path); } diff --git a/src/globals/test-extensions.php b/src/globals/test-extensions.php index 43ce5d152..62d7d4112 100644 --- a/src/globals/test-extensions.php +++ b/src/globals/test-extensions.php @@ -31,7 +31,7 @@ 'ubuntu-22.04-arm', // bin/spc-gnu-docker for arm64 'ubuntu-24.04-arm', // bin/spc for arm64 // 'windows-2022', // .\bin\spc.ps1 - // 'windows-2025', + 'windows-2025', ]; // whether enable thread safe @@ -51,13 +51,13 @@ // If you want to test your added extensions and libs, add below (comma separated, example `bcmath,openssl`). $extensions = match (PHP_OS_FAMILY) { 'Linux', 'Darwin' => 'rdkafka', - 'Windows' => 'bcmath,bz2,calendar,ctype,curl,dom,exif,fileinfo,filter,ftp,iconv,xml,mbstring,mbregex,mysqlnd,openssl,pdo,pdo_mysql,pdo_sqlite,phar,session,simplexml,soap,sockets,sqlite3,tokenizer,xmlwriter,xmlreader,zlib,zip', + 'Windows' => 'bcmath', }; // If you want to test shared extensions, add them below (comma separated, example `bcmath,openssl`). $shared_extensions = match (PHP_OS_FAMILY) { - 'Linux' => '', - 'Darwin' => '', + 'Linux' => 'snmp', + 'Darwin' => 'snmp', 'Windows' => '', };