Repository navigation
feat: add Build::create_archive and emit_link_directives - #1972
Merged
NobodyXu merged 3 commits intoOct 3, 2026
Merged
Conversation
`compile` compiles objects, archives them and emits the cargo link lines. `compile_intermediates` covered only the first step. The new `Build::create_archive` and the free function `emit_link_directives` (with `try_` variants) cover the other two, and `compile` now calls the same code, so its commands and output are unchanged.
NobodyXu
requested changes
Oct 2, 2026
NobodyXu
left a comment
Contributor
There was a problem hiding this comment.
Thank you!
Some feedback on the PR
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1818.
Motivation
compilecompiles the objects, archives them and emits the cargo link lines.compile_intermediatesonly does the first step, so a build script that wants to change the objects or build its own archive has to redo the rest by hand, including the C++ stdlib, link modifiers and CUDA runtime handling. For example, rust-lang/rust#157914 builds compiler-rt's archive itself, andmusl-math-sysin rust-lang/rust prints its link lines by hand.What's added
As discussed in the issue:
Build::create_archiveandBuild::try_create_archivetake a library name (same rules ascompile) and the objects, createlib<name>.ain the output directory with the same archiver commands ascompile, and return its path. Objects added withBuild::objectare included, CUDA device code is linked in, and MSVC targets also get<name>.lib, all ascompiledoes.emit_link_directivesandtry_emit_link_directivesare free functions that take theBuildand the path of a library namedlib<name>.a(or<name>.libon MSVC targets, the names rustc looks for) and emit the linescompileemits: the library with its link modifiers, its folder as search path, the C++ stdlib (still once perBuild), the wasm and pauthtest sysroot libraries and the CUDA runtime.Behavior of
compilecompilenow callstry_create_archiveandtry_emit_link_directivesitself, and its commands and link lines are unchanged. It readsCC_FORCE_DISABLEonce more, intry_create_archive, so it printscargo:rerun-if-env-changed=CC_FORCE_DISABLEtwice. Two odd inputs now fail in cc after archiving:compile("lib.a")andout_dir(""). Before, they emitted an empty library name or an empty search path, which rustc rejects, so only builds withcargo_metadata(false)see a difference.Tests
CC_FORCE_DISABLE.compileand the three step path run the same commands and emit the same link lines on linux-gnu (static stdlib and link modifiers), pauthtest, CUDA (device link and cudart, with an nvcc shim) and MSVC.compileoutput against main only gains that secondCC_FORCE_DISABLEline, for linux-gnu (C, C++, static stdlib, link modifiers,Build::object), pauthtest, wasm32 (WASI and Linux), CUDA and MSVC, and for its error cases.Tested on Windows with
cargo testwith and withoutparallel, clippy with-D warningsfor the feature sets, rustdoc with-D warnings, and a check of the library with rustc 1.65. Linux and macOS hosts are covered by CI only.