Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,21 @@ jobs:
- name: Install dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist --no-dev

# Keep doctor in its own step: on Windows the default pwsh shell does not stop
# a `run: |` block when a native command exits non-zero, so a failed doctor
# (e.g. an auto-fix download error) used to be swallowed and the build kept
# running until it failed on a confusing environment check.
- name: Doctor
env:
SPC_USE_SUDO: "yes"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: ./bin/spc doctor --auto-fix

- name: Build
env:
SPC_USE_SUDO: "yes"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
./bin/spc doctor --auto-fix
${{ matrix.combo.build-args }} --dl-with-php=${{ matrix.combo.php-version }}
run: ${{ matrix.combo.build-args }} --dl-with-php=${{ matrix.combo.php-version }}

# - name: Setup upterm session
# if: ${{ failure() }}
Expand Down
6 changes: 4 additions & 2 deletions docs/deps-craft-yml.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ build-options:
with-config-file-path: ""
# PHP configuration options (same as --with-config-file-scan-dir)
with-config-file-scan-dir: ""
# PHP configuration options (same as --with-sysconfdir, only on Unix)
with-sysconfdir: ""
# Hardcoded INI options for cli and micro SAPI (e.g. "memory_limit=4G", list accepted)
with-hardcoded-ini: [ ]
# Pretend micro SAPI as cli SAPI to avoid some frameworks to limit the usage of micro SAPI
Expand All @@ -54,8 +56,8 @@ shared-extensions: [ ]
download-options:
# Number of parallel downloads (default: 1)
parallel: 1
# Retries count for downloading sources (default: 0)
retry: 0
# Retries count for downloading sources (default: 3)
retry: 3
# Prefer source downloads when both source and binary are available (default: false)
prefer-source: false
# Prefer binary downloads when both source and binary are available (default: false)
Expand Down
3 changes: 2 additions & 1 deletion docs/en/guide/cli-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ spc download [artifacts] [options]
| `--source-only` | | Only download source artifacts |
| `--binary-only` | | Only download binary artifacts |
| `--parallel=<n>` | `-P` | Number of parallel downloads (default: `1`) |
| `--retry=<n>` | `-R` | Number of retries on failure (default: `0`) |
| `--retry=<n>` | `-R` | Number of retries on failure (default: `3`) |
| `--ignore-cache=<list>` | `-i` | Force re-download the specified artifacts |
| `--no-alt` | | Do not use alternative mirror URLs |
| `--no-shallow-clone` | | Do not clone git repositories shallowly |
Expand Down Expand Up @@ -99,6 +99,7 @@ These flags apply only to the combined `build:php` target. To build a specific S
| `--disable-opcache-jit` | | Disable OPcache JIT |
| `--with-config-file-path=<path>` | | Directory where PHP looks for `php.ini` (default: `/usr/local/etc/php`) |
| `--with-config-file-scan-dir=<path>` | | Directory PHP scans for additional `.ini` files (default: `/usr/local/etc/php/conf.d`) |
| `--with-sysconfdir=<path>` | | Set the `sysconfdir` passed to PHP's `configure` (Unix only, e.g. `/etc/php-zts`) |
| `--with-hardcoded-ini=<k=v>` | `-I` | Bake an INI setting into the binary at compile time (repeatable) |
| `--enable-zts` | | Enable thread-safe (ZTS) mode |
| `--no-smoke-test` | | Skip the post-build smoke tests |
Expand Down
3 changes: 2 additions & 1 deletion docs/zh/guide/cli-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ spc download [artifacts] [options]
| `--source-only` | | 仅下载源码制品 |
| `--binary-only` | | 仅下载二进制制品 |
| `--parallel=<n>` | `-P` | 并行下载数(默认 `1`) |
| `--retry=<n>` | `-R` | 失败重试次数(默认 `0`) |
| `--retry=<n>` | `-R` | 失败重试次数(默认 `3`) |
| `--ignore-cache=<list>` | `-i` | 强制重新下载指定制品 |
| `--no-alt` | | 不使用镜像站 |
| `--no-shallow-clone` | | 不使用浅层克隆 |
Expand Down Expand Up @@ -99,6 +99,7 @@ spc build:php <extensions> [options]
| `--disable-opcache-jit` | | 禁用 OPcache JIT |
| `--with-config-file-path=<path>` | | PHP 查找 `php.ini` 的目录(默认:`/usr/local/etc/php`) |
| `--with-config-file-scan-dir=<path>` | | PHP 扫描追加 `.ini` 文件的目录(默认:`/usr/local/etc/php/conf.d`) |
| `--with-sysconfdir=<path>` | | 传给 PHP `configure` 的 `sysconfdir`(仅 Unix,例如 `/etc/php-zts`) |
| `--with-hardcoded-ini=<k=v>` | `-I` | 编译时将 INI 配置硬编码进二进制(可重复使用) |
| `--enable-zts` | | 启用线程安全(ZTS)模式 |
| `--no-smoke-test` | | 跳过构建后的冒烟测试 |
Expand Down
121 changes: 109 additions & 12 deletions src/Package/Command/SwitchPhpVersionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use StaticPHP\Artifact\DownloaderOptions;
use StaticPHP\Command\BaseCommand;
use StaticPHP\DI\ApplicationContext;
use StaticPHP\Exception\FileSystemException;
use StaticPHP\Registry\PackageLoader;
use StaticPHP\Util\FileSystem;
use StaticPHP\Util\InteractiveTerm;
Expand Down Expand Up @@ -59,19 +60,33 @@ public function handle(): int
$cache->removeSource('php-src', delete_file: true);
}

// Collect the extension source trees linked into source/php-src/ext before php-src is
// removed: their build state is only valid for the PHP version that produced it.
$linked_extension_sources = self::collectLinkedExtensionSources(SOURCE_PATH);

// Remove extracted source directory if exists and --keep-source not set
$source_dir = SOURCE_PATH . '/php-src';
if (!$this->getOption('keep-source') && is_dir($source_dir)) {
$this->output->writeln('<info>Removing extracted PHP source directory...</info>');
InteractiveTerm::indicateProgress('Removing: ' . $source_dir);
FileSystem::removeDir($source_dir);
if (file_exists($source_dir)) {
InteractiveTerm::finish('Failed: ' . $source_dir, false);
throw new FileSystemException(
"Failed to remove the extracted PHP source directory: {$source_dir}\n" .
'The PHP version switch was aborted: continuing would build the new PHP on top of ' .
'stale sources and object files from the previous version (wrong headers/ABI, ' .
'undefined symbols at link time).' . "\n" .
'Remove the directory manually (on Windows: rmdir /s /q "' . FileSystem::convertPath($source_dir) . '") and retry.',
);
}
InteractiveTerm::finish('Removed: ' . $source_dir);
}

// Extension sources are extracted to source/ext-* and linked into php-src/ext, so their
// build state (objects, dependency files, patched config.m4, phpize leftovers) lives
// outside source/php-src and is only valid for the PHP version that produced it.
$this->cleanExtensionSources();
// Extension sources are extracted to source/ and linked into php-src/ext, so their build
// state (objects, dependency files, patched config.m4, phpize leftovers) lives outside
// source/php-src and is only valid for the PHP version that produced it.
$this->cleanExtensionSources($linked_extension_sources);

// Download new PHP source
$this->output->writeln("<info>Downloading PHP {$php_ver} source...</info>");
Expand Down Expand Up @@ -104,34 +119,116 @@ public function handle(): int
}

/**
* Delete every extracted extension source tree under source/ext-*, returning the count.
* Delete the extracted extension source trees of the previous PHP version, returning the count.
*
* Targets are the source/ext-* directories plus any extra source roots passed in (usually the
* ones linked into php-src/ext, see collectLinkedExtensionSources()).
*
* @param string[] $extra_source_dirs Additional source roots to remove
* @return int Number of removed source trees
* @throws FileSystemException When an extracted tree cannot be removed completely
*/
public static function removeExtensionSources(string $source_path): int
public static function removeExtensionSources(string $source_path, array $extra_source_dirs = []): int

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this logic will bite with opentelemetry's source root. It'll delete the parent directory and then the child which will crash.

{
$removed = 0;
$targets = [];
foreach (glob(FileSystem::convertPath("{$source_path}/ext-*"), GLOB_ONLYDIR) ?: [] as $ext_dir) {
$targets[$ext_dir] = true;
}
foreach ($extra_source_dirs as $extra_dir) {
if (is_dir($extra_dir) || FileSystem::isLink($extra_dir)) {
$targets[FileSystem::convertPath($extra_dir)] = true;
}
}

// A linked source root can be nested inside an extracted tree: artifacts declaring
// "source-root: ext" (e.g. ext-opentelemetry, ext-swow) link source/<artifact>/ext while the
// glob above also collects the parent source/<artifact>. Removing the parent already removes
// the child, so process short paths first and skip anything below a tree we just removed.
$paths = array_keys($targets);
usort($paths, static fn (string $a, string $b): int => strlen($a) <=> strlen($b));
$removed_roots = [];

$removed = 0;
foreach ($paths as $ext_dir) {
$normalized = rtrim(FileSystem::convertPath($ext_dir), '/\\');
foreach ($removed_roots as $removed_root) {
if (str_starts_with($normalized . DIRECTORY_SEPARATOR, $removed_root . DIRECTORY_SEPARATOR)) {
continue 2;
}
}
if (FileSystem::isLink($ext_dir)) {
FileSystem::removeLink($ext_dir);
$ok = FileSystem::removeLink($ext_dir);
} else {
FileSystem::removeDir($ext_dir);
$ok = FileSystem::removeDir($ext_dir);
}
if (!$ok || file_exists($ext_dir) || FileSystem::isLink($ext_dir)) {
throw new FileSystemException(
"Failed to remove extracted extension source directory: {$ext_dir}\n" .
'Its build state (objects, generated headers, patched sources) is only valid for the ' .
'PHP version that produced it and would leak into the next build.' . "\n" .
'Remove it manually (on Windows: rmdir /s /q "' . FileSystem::convertPath($ext_dir) . '") and retry.',
);
}
$removed_roots[] = $normalized;
++$removed;
}

return $removed;
}

/**
* Remove the extracted extension source trees (source/ext-*) of the previous PHP version.
* Collect the extracted source trees linked into source/php-src/ext.
*
* Extension artifacts are extracted under source/ with their artifact name, which is not
* necessarily "ext-<extension>" (e.g. the glfw extension uses source/glfw), so cleaning
* source/ext-* alone misses them. Their build state (objects, .dep files, patched configure
* scripts) is only valid for the PHP version that produced it and must be dropped together
* with php-src, otherwise stale dependency files leak into the next version's build.
*
* Must be called before source/php-src is removed.
*
* @return string[] Absolute source root paths managed by spc inside source/
*/
public static function collectLinkedExtensionSources(string $source_path): array
{
$source_real = realpath($source_path);
$source_root = rtrim(FileSystem::convertPath($source_real !== false ? $source_real : $source_path), '/\\') . DIRECTORY_SEPARATOR;
$php_src_dir = $source_root . 'php-src' . DIRECTORY_SEPARATOR;
$found = [];

foreach (glob($php_src_dir . 'ext' . DIRECTORY_SEPARATOR . '*') ?: [] as $link_dir) {
if (!FileSystem::isLink($link_dir)) {
continue;
}
$target = realpath($link_dir);
if ($target === false) {
continue;
}
$target = FileSystem::convertPath($target);
// Only remove sources spc manages under source/: never a user-provided local tree,
// and never something inside php-src itself (removed separately).
if (!str_starts_with($target, $source_root) || str_starts_with($target, $php_src_dir)) {
continue;
}
$found[$target] = true;
}

return array_keys($found);
}

/**
* Remove the extracted extension source trees of the previous PHP version.
*
* Extension sources are linked into php-src/ext for in-tree builds, so their objects,
* dependency files, patched config.m4 and phpize leftovers live outside source/php-src and
* would otherwise leak into the next PHP version's build. The original archives stay in
* downloads/ and are re-extracted automatically on the next build.
*
* @param string[] $linked_source_dirs Source roots collected before php-src removal
*/
private function cleanExtensionSources(): void
private function cleanExtensionSources(array $linked_source_dirs = []): void
{
$removed = self::removeExtensionSources(SOURCE_PATH);
$removed = self::removeExtensionSources(SOURCE_PATH, $linked_source_dirs);

if ($removed > 0) {
$this->output->writeln("<info>Removed {$removed} extracted extension source director" . ($removed === 1 ? 'y' : 'ies') . '; they will be re-extracted from the cached archives on the next build.</info>');
Expand Down
68 changes: 68 additions & 0 deletions src/Package/Library/imagemagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function buildWin(LibraryPackage $lib): void
// Use the prebuilt Configure tool (building it from source needs the MFC components).
default_shell()->executeCurlDownload($configure_url, "{$work}\\Configure\\Configure.Release.x64.exe", retries: 2);

self::patchCairoWin7MtaUsage($work);

// Generate a static, /MT (linkRuntime), x64, Q16-HDRI solution with the configs embedded
// (zeroConfigurationSupport) and OpenMP off (no vcomp runtime dependency).
$ver = WindowsUtil::findVisualStudio();
Expand All @@ -57,6 +59,8 @@ public function buildWin(LibraryPackage $lib): void
cmd()->cd("{$work}\\Configure")
->exec("Configure.Release.x64.exe /noWizard {$vs_arg} /x64 /static /linkRuntime /noOpenMP /zeroConfigurationSupport");

self::retargetGeneratedProjectsToWindows10($work);

// x64 IM7 defaults to a 64-bit channel mask, whose magick-baseconfig.h #errors unless the
// consuming translation unit is C++. ext-imagick is plain C, so force a 32-bit channel mask
// (ample: 32 channels >> RGBA/CMYK) before building, keeping libs and the installed header in sync.
Expand Down Expand Up @@ -158,4 +162,68 @@ public function buildUnix(LibraryPackage $lib, ToolchainInterface $toolchain): v
}
$lib->patchLaDependencyPrefix();
}

/**
* cairo 1.18.6 (vendored by ImageMagick) uses the Windows 8+ COM MTA usage API in
* cairo-win32-system.c, but the VisualMagick projects are generated with
* _WIN32_WINNT=0x0601 (Windows 7), where the Windows SDK hides CO_MTA_USAGE_COOKIE and
* CoIncrementMTAUsage behind "NTDDI_VERSION >= NTDDI_WIN8", breaking the static build.
*
* Those functions are resolved dynamically with GetProcAddress, so only the opaque cookie
* type declaration is missing: provide the DECLARE_HANDLE-compatible definition for Win7
* targets. No-op when the vendored cairo no longer needs it (patch string not found) and when
* the projects were retargeted to Windows 10 with retargetGeneratedProjectsToWindows10().
*/
private static function patchCairoWin7MtaUsage(string $work): void
{
$file = "{$work}\\Dependencies\\Dependencies\\cairo\\src\\win32\\cairo-win32-system.c";
if (!is_file($file)) {
return;
}
FileSystem::replaceFileStr(
$file,
'#include <stdbool.h>',
"#include <stdbool.h>\n\n"
. "#if !defined(NTDDI_WIN8) || (NTDDI_VERSION < NTDDI_WIN8)\n"
. '/* Windows 7 target: mirror DECLARE_HANDLE(CO_MTA_USAGE_COOKIE) from the Win8+ SDK. */' . "\n"
. "struct CO_MTA_USAGE_COOKIE__ { int unused; };\n"
. "typedef struct CO_MTA_USAGE_COOKIE__ *CO_MTA_USAGE_COOKIE;\n"
. '#endif' . "\n",
);
}

/**
* Retarget the generated VisualMagick projects from the Configure release's Windows 7 default
* (_WIN32_WINNT=0x0601) to Windows 10 (_WIN32_WINNT=0x0A00).
*
* With _WIN32_WINNT >= _WIN32_WINNT_WIN10 the Windows SDK sets NTDDI_VERSION to
* WDK_NTDDI_VERSION instead of deriving it from _WIN32_WINNT, which is what un-hides the
* DirectWrite RS1/RS3 declarations cairo needs. Configure writes the definition into every
* generated *.vcxproj, so patching those keeps the rest of the generation untouched.
*
* @param string $work VisualMagick work directory (contains ProjectFiles/)
*/
private static function retargetGeneratedProjectsToWindows10(string $work): void
{
$project_files = FileSystem::scanDirFiles("{$work}/ProjectFiles");
if ($project_files === false) {
logger()->debug("No VisualMagick ProjectFiles in {$work}; skipping _WIN32_WINNT retarget");
return;
}

$win7_define = '_WIN32_WINNT=0x0601';
$win10_define = '_WIN32_WINNT=0x0A00';
$retargeted = 0;
foreach ($project_files as $project) {
if (!str_ends_with($project, '.vcxproj')) {
continue;
}
if (!str_contains(FileSystem::readFile($project), $win7_define)) {
continue;
}
FileSystem::replaceFileStr($project, $win7_define, $win10_define);
++$retargeted;
}
logger()->debug("Retargeted {$retargeted} VisualMagick project(s) to {$win10_define}");
}
}
1 change: 1 addition & 0 deletions src/Package/Target/php.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public function init(TargetPackage $package): void
$package->addBuildOption('disable-opcache-jit', null, null, 'Disable opcache jit');
$package->addBuildOption('with-config-file-path', null, InputOption::VALUE_REQUIRED, 'Set the path in which to look for php.ini', PHP_OS_FAMILY === 'Windows' ? null : '/usr/local/etc/php');
$package->addBuildOption('with-config-file-scan-dir', null, InputOption::VALUE_REQUIRED, 'Set the directory to scan for .ini files after reading php.ini', PHP_OS_FAMILY === 'Windows' ? null : '/usr/local/etc/php/conf.d');
$package->addBuildOption('with-sysconfdir', null, InputOption::VALUE_REQUIRED, 'Set the sysconfdir (e.g. /etc/php-zts) used by configure (not available on Windows)');
$package->addBuildOption('with-hardcoded-ini', 'I', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Patch PHP source code, inject hardcoded INI');
$package->addBuildOption('enable-zts', null, null, 'Enable thread safe support');
$package->addBuildOption('no-smoke-test', null, InputOption::VALUE_OPTIONAL, 'Disable smoke test for specific SAPIs, or all if no value provided', false);
Expand Down
Loading
Loading