Skip to content

Add optional mold linker support - #861

Merged
helly25 merged 12 commits into
bazel-contrib:masterfrom
helly25:mold-linker
Sep 24, 2026
Merged

helly25 merged 12 commits into
bazel-contrib:masterfrom
helly25:mold-linker

Conversation

@helly25

@helly25 helly25 commented Sep 20, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Add generic, versioned linker-distribution resolution and use it to provide mold as an explicit, optional linker on top of #860 and #866.

The linker target map remains the only linker selector:

  • an empty value (or omitted attribute) uses the linker from the selected LLVM distribution;
  • auto discovers the native execution-platform linker;
  • an absolute path uses that local executable;
  • every other value is a linker name resolved through the generic catalogue.

linker_version and its per-target form linker_versions select the catalogue version with the same exact/first/latest requirement syntax as LLVM versions. There is no hardcoded lld/mold implementation whitelist in rules.bzl or configure.bzl; for example, catalogued lld is handled exactly like any other name rather than meaning “use the LLVM-distribution linker.”

Optional mold-module integration

Mold is the first linker represented in the catalogue, but it is fully opt-in and toolchains_llvm does not depend on the mold module.

Users can select a version explicitly:

llvm.toolchain(
    name = "llvm_toolchain",
    llvm_version = "23.1.2",
    linker = {"": "mold"},
    linker_version = "latest:>=2.40.0,<3.0.0",
)

Alternatively, Bzlmod users may inject their own mold dependency:

bazel_dep(name = "mold", version = "2.40.4")

llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm")
inject_repo(llvm, "mold")

llvm.toolchain(
    name = "llvm_toolchain",
    llvm_version = "23.1.2",
    linker = {"linux-x86_64": "mold"},
)

Bare mold is the only mold-specific behavior: the resolver reads project(mold VERSION ...) from the injected module's upstream CMakeLists.txt and uses that as the default version. This keeps the version in a normal bazel_dep, where Dependabot/Renovate can update it. An explicit version does not require the mold module.

There are no mold_version or mold_binary attributes.

Distribution model

toolchain/distributions/linkers.jsonc records URLs, SHA-256 digests, archive prefixes, and executable paths by linker name, version, and execution OS/architecture. The target selects a linker name/version while the execution platform selects the artifact, because the linker runs on the build executor during cross-compilation.

The initial catalogue contains official Linux x86-64 and AArch64 mold artifacts for the BCR version (2.40.4) and the three newest stable releases (2.41.0, 2.42.0, and 2.42.1). Downloaded executables are materialized into the generated toolchain repository and declared as linker inputs for sandboxed and remote actions.

The updater is now Python-backed and refreshes both LLVM distributions and the linker catalogue. It preserves JSONC output, reads the current BCR mold version, adds the newest mold releases, requires GitHub-provided SHA-256 digests, updates generated LLVM goldens, and is deterministic across repeated runs. It deliberately never substitutes predictable asset names for checksum verification.

Tests

  • bazel test //...
  • Python updater unit tests
  • generic catalogue-reference regression, including an arbitrary gold@1.2.3 reference
  • Bzlmod mold repository materialization using the injected module version
  • WORKSPACE mold repository materialization using linker_version = "latest:<3.0.0"
  • updater executed twice against live GitHub/BCR metadata with identical output hashes
  • full trunk check
  • git diff --check

Linux x86-64 end-to-end mold CI runs in both Bzlmod and WORKSPACE modes.

@helly25
helly25 force-pushed the mold-linker branch 2 times, most recently from 15e80be to ef091db Compare September 23, 2026 21:08
helly25 added a commit that referenced this pull request Sep 24, 2026
The `lld` selection added in #860 does not represent an arbitrary LLD
implementation: it selects the linker included in the chosen LLVM
distribution. Remove that reserved name before the API ships in
1.10.0.\n\nAn omitted linker attribute or an explicit empty selection
(`linker = {"": ""}`) now selects the LLVM-distribution linker. This
avoids introducing another reserved linker name and leaves non-empty
implementation names available for generic catalogue lookup in #861.
Both Bzlmod and WORKSPACE integration fixtures exercise the explicit
empty spelling.\n\n`auto` and absolute-path selection are
unchanged.\n\nChecks:\n- `bazel test //toolchain/internal:all`\n- Bzlmod
and WORKSPACE queries for `@llvm_toolchain_cxx20//:*`\n- Trunk checks on
changed files
@helly25
helly25 merged commit 1a7e515 into bazel-contrib:master Sep 24, 2026
251 of 255 checks passed
@helly25
helly25 deleted the mold-linker branch September 24, 2026 20:20
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.

1 participant