Skip to content

Commit ae322ff

Browse files
committed
Add a new option -Cbitcode-in-rlib.
It defaults to true, but Cargo will set this to false whenever it can to reduce compile times.
1 parent b9f6dfe commit ae322ff

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

src/doc/rustc/src/codegen-options/index.md

+20
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,26 @@ It takes one of the following values:
387387
For example, for gcc flavor linkers, this issues the `-nodefaultlibs` flag to
388388
the linker.
389389

390+
## bitcode-in-rlib
391+
392+
This flag controls whether or not the compiler puts compressed LLVM bitcode
393+
into generated rlibs. It takes one of the following values:
394+
395+
* `y`, `yes`, `on`, or no value: put bitcode in rlibs (the default).
396+
* `n`, `no`, or `off`: omit bitcode from rlibs.
397+
398+
LLVM bitcode is only needed when link-time optimization (LTO) is being
399+
performed, but it is enabled by default for backwards compatibility reasons.
400+
401+
The use of `-C bitcode-in-rlib=no` can significantly improve compile times and
402+
reduce generated file sizes. For these reasons, Cargo uses `-C
403+
bitcode-in-rlib=no` whenever possible. Likewise, if you are building directly
404+
with `rustc` we recommend using `-C bitcode-in-rlib=no` whenever you are not
405+
using LTO.
406+
407+
If combined with `-C lto`, `-C bitcode-in-rlib=no` will cause `rustc` to abort
408+
at start-up, because the combination is invalid.
409+
390410
[option-emit]: ../command-line-arguments.md#option-emit
391411
[option-o-optimize]: ../command-line-arguments.md#option-o-optimize
392412
[profile-guided optimization]: ../profile-guided-optimization.md

src/librustc_codegen_ssa/back/write.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,8 @@ pub struct CompiledModules {
378378
}
379379

380380
fn need_crate_bitcode_for_rlib(sess: &Session) -> bool {
381-
sess.crate_types.borrow().contains(&config::CrateType::Rlib)
381+
sess.opts.cg.bitcode_in_rlib
382+
&& sess.crate_types.borrow().contains(&config::CrateType::Rlib)
382383
&& sess.opts.output_types.contains_key(&OutputType::Exe)
383384
}
384385

src/librustc_interface/tests.rs

+4
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,10 @@ fn test_codegen_options_tracking_hash() {
505505
opts = reference.clone();
506506
opts.cg.linker_plugin_lto = LinkerPluginLto::LinkerPluginAuto;
507507
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
508+
509+
opts = reference.clone();
510+
opts.cg.bitcode_in_rlib = false;
511+
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
508512
}
509513

510514
#[test]

src/librustc_session/config.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,16 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
16851685
);
16861686
}
16871687

1688+
if !cg.bitcode_in_rlib {
1689+
match cg.lto {
1690+
LtoCli::No | LtoCli::Unspecified => {}
1691+
LtoCli::Yes | LtoCli::NoParam | LtoCli::Thin | LtoCli::Fat => early_error(
1692+
error_format,
1693+
"options `-C bitcode-in-rlib=no` and `-C lto` are incompatible",
1694+
),
1695+
}
1696+
}
1697+
16881698
let prints = collect_print_requests(&mut cg, &mut debugging_opts, matches, error_format);
16891699

16901700
let cg = cg;

src/librustc_session/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,8 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
703703
"compile the program with profiling instrumentation"),
704704
profile_use: Option<PathBuf> = (None, parse_opt_pathbuf, [TRACKED],
705705
"use the given `.profdata` file for profile-guided optimization"),
706+
bitcode_in_rlib: bool = (true, parse_bool, [TRACKED],
707+
"emit bitcode in rlibs (default: yes)"),
706708
}
707709

708710
options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,

0 commit comments

Comments
 (0)