Skip to content

fix cross-compiling with zig - #1299

Merged
henderkes merged 15 commits into
v3from
fix/zig-cross
Nov 25, 2026
Merged

henderkes merged 15 commits into
v3from
fix/zig-cross

Conversation

@henderkes

Copy link
Copy Markdown
Collaborator

we may have flat brained before

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

macOS cross-compilation passes Zig-style host triples to Autoconf instead of GNU-compatible Darwin triples, risking validation failures or incorrect configuration and overriding libffi’s valid host argument.

1 open issue
What changed in this PR

This PR addresses cross-compilation setup in StaticPHP by selecting Zig for the machine running the build and passing target information to Autoconf.

Changes:

  • Select Zig downloads using the current machine’s architecture and operating system.
  • Add a default --host argument to Autoconf configuration during cross-compilation.
File Description
src/​StaticPHP/​Runtime/​Executor/​UnixAutoconfExecutor.php Adds target host information for cross-build configuration.
src/​Package/​Artifact/​zig.php Selects Zig binaries for the build machine rather than the target.

Review effort: Balanced (adaptive)

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new --host argument breaks liburing cross-build configuration, while omitting --build can leave Autoconf executing target probes instead of entering cross-compilation mode.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Pass --build to reliably enable Autoconf cross-compilation

src/​StaticPHP/​Runtime/​Executor/​UnixAutoconfExecutor.php:172

Passing only --host does not force Autoconf into cross-compilation mode: when --build is omitted, configure first attempts to run a compiled test program and stays in native mode if it succeeds. On Linux cross builds with binfmt/QEMU enabled, this still executes target probes instead of using their cross-compilation fallback paths, contrary to the intent of this change. Please also pass an explicit GNU triplet for the build machine so cross mode is selected independently of whether target executables happen to run.

1 resolved since last review

Review effort: Balanced (adaptive)

$host = SystemTarget::getTargetOS() === 'Darwin'
? SystemTarget::getTargetArch() . '-apple-darwin'
: SystemTarget::getCanonicalTriple();
$args[] = '--host=' . $host;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Added liburing’s removal of the generated --host argument. Commit: 61f09137.

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Zig’s cache remains keyed by the target platform rather than the host, allowing incompatible archives to be reused and leaving execution-format failures unresolved.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

High severity Key Zig archive cache by host platform, not target

src/​Package/​Artifact/​zig.php:43

Zig's cache still uses the target platform, so an x86_64 host targeting aarch64 reuses a previously cached aarch64 archive and skips this host-selection callback, leaving the execution-format failure unresolved. New host archives are also saved under the target key, allowing incompatible reuse when caches are shared across hosts. Use a host-specific identity consistently for Zig's cache lookup, locking, and extraction, and invalidate legacy entries that do not match the host.

Review effort: Balanced (adaptive)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>

Copilot AI commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

🟡 Changes recommended...

Added an explicit build-machine GNU triplet for Autoconf cross builds. Commit: cb4070d3.

php_micro_fileinfo.c only typedefs Elf_Shdr in its 64-bit (__LP64__)
branch; the 32-bit branch defines Elf_Ehdr and Elf_Phdr but not
Elf_Shdr, and neither glibc's nor musl's elf.h provides a native-class
alias. 64-bit targets build fine; 32-bit targets (arm-linux-musleabihf)
fail with 'use of undeclared identifier Elf_Shdr'.
configureForUnix() ran ./configure without --host/--build, so autoconf
assumed a native build for cross targets and died with exit 77 trying
to execute the arm conftest binary on the x86_64 host
('cannot run C compiled programs'). UnixAutoconfExecutor already does
this for library builds; mirror it for the php target.
…fibers

spc craft prepares artifact downloads as concurrent fibers. Two providers
(llvm-compiler-rt downSource, llvm-tools downBinary) probe the toolchain
clang version at the same moment. getCompilerVersion() set
compiler_version_resolved BEFORE running 'zig cc -dumpversion'; while
the first fiber was suspended inside that probe, the second saw the
resolved flag and got the still-null value, which llvm_tools turned into
'DownloaderException: Cannot determine the clang version of the active
toolchain'. The abort then proc_terminated the first fiber's child before
it produced any output. Set the resolved marker after the probe instead;
concurrent callers redo the cheap probe and all observe a correct value.
zig's musl headers only define the LFS64 typedefs (off64_t etc.) with
_LARGEFILE64_SOURCE; _GNU_SOURCE alone does not, unlike glibc. php-src's
cookie seeker probe resolves HAVE_COOKIE_IO_FUNCTIONS_T_OFF64_T, so
main/streams/cast.c then fails with 'unknown type name off64_t'. Add the
define to the default C/CXX flags and php make extra flags for -musl
targets; it is a no-op on glibc.
- Custom binary fetchers (zig, rust, llvm-tools, go, ...) deliver tools that execute on the build machine, but their download cache, update checks and callback resolution were keyed by SystemTarget::getCurrentPlatformString(), i.e. the build target.
- An x86_64 host building for aarch64 reused a previously cached aarch64 archive (skipping the host-selection callback) and failed with an execution-format error; the zig wrong-arch fix was therefore defeated by the cache.
- Add SystemTarget::getHostPlatformString() and Artifact::getBinaryPlatform(); use the host key consistently for lock (ArtifactDownloader), queue skip (isBinaryDownloaded default), extraction (ArtifactExtractor), tool version record (PackageInstaller), check updates and callback resolution. Config-based binaries keep the target key.
- Legacy target-keyed entries are simply not consulted anymore; extraction re-runs via the .spc-hash check when content differs, so stale incompatible archives recover without manual cache clearing.
- Also fix the same wrong-arch class of bug in rust (GNU_ARCH instead of getTargetArch()) and go-xcaddy (getHostPlatformString() instead of getCurrentPlatformString()).
- EMULATE_PLATFORM is set only by tests to fake the current platform for target-keyed lookups; nothing in src/ or CI ever sets it in production.
- Honoring it in a host platform string would key host-executed tools under a platform they cannot run on, which is exactly the class of failure this key exists to prevent.
- Tests now use SystemTarget::getHostPlatformString() directly instead of the EMULATE-aware getCurrentPlatform() helper for host keys.
@henderkes henderkes closed this Nov 22, 2026
@henderkes henderkes reopened this Nov 22, 2026
@henderkes henderkes added the need-test This PR has not been tested yet, cannot merge now label Nov 22, 2026
@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

StaticPHP Test Bot

Detected: Extensions: none | Libraries: liburing | Targets: llvm-compiler-rt, llvm_compiler_rt, micro, php
Active labels: none
Available labels: need-test (gate), test/linux test/windows test/macos (platform), test/tier2 (extra arch), test/php-83 test/php-84 (PHP version), sapi/frankenphp (extra FrankenPHP SAPI build)
Config: Linux x86_64 + Windows x86_64 + macOS arm64 | PHP 8.5 NTS

@crazywhalecc

Copy link
Copy Markdown
Owner

Is this PR intended to fully implement cross-compilation, or is it only for fixing wrong download arch?

@henderkes

Copy link
Copy Markdown
Collaborator Author

The latter originally, but it seems the --host arg changes never made it into v3 so that needs to be fixed too. So it's pretty much the latter now.

@henderkes henderkes changed the title fix cross compiling zig error (downloading wrong arch) fix cross-compiling with zig Nov 23, 2026
@henderkes

Copy link
Copy Markdown
Collaborator Author

This is a state where e.g. sculpin can successfully cross-compile from x86_64-linux-gnu to arm-linux-musleabihf.

@crazywhalecc

Copy link
Copy Markdown
Owner

I've done a quick review of the changes, but the current implementation is not yet complete I think.

  • Replace custom_binary_callbacks !== [] with a call to isHostRun(), and then include logic to check whether the package is of the tool type or contains custom methods.
  • The execution path for hasPlatformBinary() does not behave as expected. It should not rely on the binary's current state for its determination.
  • While the $pkg_name is known at the time an inline artifact is registered, the current registerInlineArtifact call only stores the configuration without any ownership information. It should record the "owner package", combined with the isHostRun() check (or a similar mechanism) mentioned in the first point, will enable full support for cross-compilation at the artifact level.

- ArtifactConfig::registerInlineArtifact() records the owning package; Artifact::isHostRun()
  returns true for tool-owned artifacts and artifacts with custom binary callbacks, so
  host-run tools are keyed by the host platform regardless of callback load order.
- hasPlatformBinary(), getBinaryDownloadConfig(), getBinaryExtractConfig() and the
  downloader queue/check-update paths select binary config under the artifact's binary
  platform instead of the build target.
- ToolVersionRegistry::record() takes the platform from the caller (binary platform for
  tools).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Cross builds still fail because liburing rejects the generated --build argument, and unresolved artifact-platform selection and LLVM license-packaging issues remain.

6 open issues

Review effort: Balanced (adaptive)

Comment thread src/Package/Library/liburing.php Resolved
Comment thread config/pkg/tool/llvm-tools.yml Resolved
Comment thread src/StaticPHP/Artifact/Artifact.php Resolved
Comment thread src/StaticPHP/Artifact/Artifact.php Resolved
… callbacks

- liburing: strip the executor-generated --build/--host triples; its hand-written
  configure rejects unknown options.
- llvm-tools: copy llvm/LICENSE.TXT into the install root during source builds so
  license-files resolves for both install modes.
- Track ownership for referenced (non-inline) artifacts: Package::getArtifact() claims
  ownership, with tool packages taking precedence over non-tool claimers.
- Resolve custom binary check-update callbacks and binary extract callbacks under the
  artifact's binary platform instead of the host/current platform.
- Cross builds emit target-architecture binaries, so smokeTestForUnix()/
  smokeTestFrankenphpForUnix() would exec target binaries on the host and fail the
  build with an exec-format error after an otherwise successful compile. Return early
  from php::postInstall() while cross-compiling.
- Add PhpPostInstallTest covering the cross-build guard.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human assessment is needed because direct downloads of string-referenced tool artifacts can bypass ownership registration and select target binaries instead of host binaries during cross-builds.

1 open issue
5 resolved since last review

Review effort: Balanced (adaptive)

@henderkes

Copy link
Copy Markdown
Collaborator Author

🔵 Needs a closer look

Human assessment is needed because direct downloads of string-referenced tool artifacts can bypass ownership registration and select target binaries instead of host binaries during cross-builds.

1 open issue

No, that's not open.

@henderkes
henderkes merged commit 34eebd0 into v3 Nov 25, 2026
16 of 17 checks passed
@henderkes
henderkes deleted the fix/zig-cross branch November 25, 2026 03:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

need-test This PR has not been tested yet, cannot merge now

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants