Skip to content

llvm-tools: build host llvm-objcopy/strip/profdata under ZigToolchain - #1172

Open
henderkes wants to merge 7 commits into
v3from
v3c/llvm-tools
Open

llvm-tools: build host llvm-objcopy/strip/profdata under ZigToolchain#1172
henderkes wants to merge 7 commits into
v3from
v3c/llvm-tools

Conversation

@henderkes

Copy link
Copy Markdown
Collaborator

Add an llvm-tools target artifact that downloads llvm-project source matching the version of clang shipped by the active zig install, builds llvm-objcopy, llvm-strip and llvm-profdata into PKG_ROOT_PATH/llvm-tools/bin, and exposes them through the same path/binary/isInstalled static surface as the other artifacts.

A new LlvmToolsCheck doctor item runs when the active toolchain is ZigToolchain and reports whether the three tools are built, with a fix that installs the package and runs the build.

PackageBuilder now picks the right tool when the active toolchain is ZigToolchain:

  • extractDebugInfo() honours OBJCOPY from the environment, then falls back to llvm-tools' llvm-objcopy under Zig and plain objcopy otherwise.
  • stripBinary() uses llvm-strip under Zig and plain strip otherwise.

System strip/objcopy refuse zig-produced archives and bitcode sections, so without this the strip stage breaks LTO builds. Other toolchains keep using the system binaries.

ApplicationContext::tryGet() wraps the container's get() in a try/catch and returns null on failure, so PackageBuilder can ask "which toolchain is active right now" without PHP-DI throwing on autowirable-but-unconstructable classes.

Depends on v3c/artifact-static-helpers (uses zig::isInstalled() and zig::binary()).

What does this PR do?

Checklist before merging

  • If you modified *.php or *.yml, run them locally to ensure your changes are valid:
    • composer cs-fix
    • composer analyse
    • composer test
    • bin/spc dev:lint-config

@crazywhalecc

Copy link
Copy Markdown
Owner

@henderkes Looks using tool instead of target is a better approach.

@henderkes

Copy link
Copy Markdown
Collaborator Author

Yes, I'll need to rework this.

@crazywhalecc crazywhalecc left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Most of it seems fine, but I don't think installing these two packages by default offers any benefit. Most people don't need PGO and shared builds. They could be triggered under specific conditions, similar to --with-upx-pack (either in doctor or the toolchain).

Comment thread config/pkg/target/llvm-compiler-rt.yml Outdated
@henderkes

Copy link
Copy Markdown
Collaborator Author

Most of it seems fine, but I don't think installing these two packages by default offers any benefit. Most people don't need PGO and shared builds. They could be triggered under specific conditions, similar to --with-upx-pack (either in doctor or the toolchain).

Do you want to scan cflags for lto/pgo instead and error out?

libcompiler_rt needs to be built for cpu_model anyway, though. Many libraries/extensions use builtin_cpu_supports and building it only takes a few seconds anyways.

@henderkes

Copy link
Copy Markdown
Collaborator Author

Forgot to mention, but gnu objcopy also can't work for cross compilation (e.g. from aarch64 to arm32). I think if we host the tools it's preferable to always have.

Co-authored-by: Jerry Ma <jesse2061@outlook.com>
Copilot AI lite review requested due to automatic review settings August 22, 2026 08:39

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

A detected flag-handling bug in zig-cc.sh can prevent linking the profile runtime when profile flags come from SPC_COMPILER_EXTRA, and binutil override precedence in PackageBuilder needs adjustment for expected behavior.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Adds a new llvm-tools tool artifact and a llvm-compiler-rt target to make ZigToolchain builds reliable when binutils/strip choke on Zig-produced archives/bitcode, and wires these tools into the build/doctor flow.

Changes:

  • Introduces llvm-tools (llvm-objcopy/llvm-strip/llvm-profdata) and llvm-compiler-rt packages/artifacts aligned to the active Zig-shipped clang/LLVM version.
  • Updates ZigToolchain to export SPC_COMPILER_RT_DIR and to auto-ensure required LLVM deps when the toolchain is initialized for real builds.
  • Updates PackageBuilder to prefer LLVM binutils under Zig, and extends the zig wrapper to link missing compiler-rt components when needed.
File summaries
File Description
src/StaticPHP/Toolchain/ZigToolchain.php Exports compiler-rt dir and ensures Zig-specific build deps are present.
src/StaticPHP/Runtime/SystemTarget.php Adds a canonical target triple helper used by Zig/LLVM packages.
src/StaticPHP/Package/PackageBuilder.php Routes strip/objcopy selection through a Zig-aware binutil resolver.
src/StaticPHP/Doctor/Item/LlvmToolsCheck.php Adds doctor checks/fixes for llvm-tools and llvm-compiler-rt when ZigToolchain is active.
src/Package/Tool/llvm_tools.php Implements building host llvm tools (plus private zlib) from source.
src/Package/Target/llvm_compiler_rt.php Builds missing compiler-rt bits Zig needs into zig/lib/{triple}.
src/Package/Artifact/zig.php Adds helper to detect Zig-shipped clang/LLVM version; refactors wrapper generation.
src/Package/Artifact/llvm_tools.php Custom artifact for llvm-tools (source + hosted binary) keyed to Zig LLVM version.
src/Package/Artifact/llvm_compiler_rt.php Custom source artifact for compiler-rt keyed to Zig LLVM version.
src/globals/scripts/zig-cc.sh Injects compiler-rt libs/objects into Zig link steps when required by flags.
config/pkg/tool/llvm-tools.yml Declares the llvm-tools tool package (provides + install layout + license metadata).
config/pkg/target/llvm-compiler-rt.yml Declares the llvm-compiler-rt target package and its Zig tool dependency.
Review details
  • Files reviewed: 12/12 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/globals/scripts/zig-cc.sh Outdated
Comment thread src/StaticPHP/Package/PackageBuilder.php
henderkes and others added 3 commits August 22, 2026 20:34
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants