From 2c9deaabf99dab9998f6ddbbe496d19ff7ce31f0 Mon Sep 17 00:00:00 2001 From: Fabio Valentini Date: Fri, 28 Aug 2020 21:56:45 +0200 Subject: [PATCH 2/2] Fix LTO with doctests --- .../src/cargo/core/compiler/context/mod.rs | 3 +- .../cargo/src/cargo/core/compiler/mod.rs | 59 +++++---------- src/tools/cargo/src/cargo/core/profiles.rs | 7 +- src/tools/cargo/tests/testsuite/lto.rs | 71 ++++++++++++++++--- 4 files changed, 85 insertions(+), 55 deletions(-) diff --git a/src/tools/cargo/src/cargo/core/compiler/context/mod.rs b/src/tools/cargo/src/cargo/core/compiler/context/mod.rs index 52b954dd1..82831f423 100644 --- a/src/tools/cargo/src/cargo/core/compiler/context/mod.rs +++ b/src/tools/cargo/src/cargo/core/compiler/context/mod.rs @@ -210,7 +210,8 @@ impl<'a, 'cfg> Context<'a, 'cfg> { // Collect information for `rustdoc --test`. if unit.mode.is_doc_test() { let mut unstable_opts = false; - let args = compiler::extern_args(&self, unit, &mut unstable_opts)?; + let mut args = compiler::extern_args(&self, unit, &mut unstable_opts)?; + args.extend(compiler::lto_args(&self, unit)); self.compilation.to_doc_test.push(compilation::Doctest { unit: unit.clone(), args, diff --git a/src/tools/cargo/src/cargo/core/compiler/mod.rs b/src/tools/cargo/src/cargo/core/compiler/mod.rs index 9399c5042..47d2b9bb4 100644 --- a/src/tools/cargo/src/cargo/core/compiler/mod.rs +++ b/src/tools/cargo/src/cargo/core/compiler/mod.rs @@ -783,49 +783,9 @@ fn build_base_args( cmd.arg("-C").arg(format!("panic={}", panic)); } - match cx.lto[unit] { - lto::Lto::Run(None) => { - cmd.arg("-C").arg("lto"); - } - lto::Lto::Run(Some(s)) => { - cmd.arg("-C").arg(format!("lto={}", s)); - } - lto::Lto::Off => { - cmd.arg("-C").arg("lto=off"); - } - lto::Lto::ObjectAndBitcode => {} // this is rustc's default - lto::Lto::OnlyBitcode => { - // Note that this compiler flag, like the one below, is just an - // optimization in terms of build time. If we don't pass it then - // both object code and bitcode will show up. This is lagely just - // compat until the feature lands on stable and we can remove the - // conditional branch. - if cx - .bcx - .target_data - .info(CompileKind::Host) - .supports_embed_bitcode - .unwrap() - { - cmd.arg("-Clinker-plugin-lto"); - } - } - lto::Lto::OnlyObject => { - if cx - .bcx - .target_data - .info(CompileKind::Host) - .supports_embed_bitcode - .unwrap() - { - cmd.arg("-Cembed-bitcode=no"); - } - } - } + cmd.args(<o_args(cx, unit)); if let Some(n) = codegen_units { - // There are some restrictions with LTO and codegen-units, so we - // only add codegen units when LTO is not used. cmd.arg("-C").arg(&format!("codegen-units={}", n)); } @@ -952,6 +912,23 @@ fn build_base_args( Ok(()) } +fn lto_args(cx: &Context<'_, '_>, unit: &Unit) -> Vec { + let mut result = Vec::new(); + let mut push = |arg: &str| { + result.push(OsString::from("-C")); + result.push(OsString::from(arg)); + }; + match cx.lto[unit] { + lto::Lto::Run(None) => push("lto"), + lto::Lto::Run(Some(s)) => push(&format!("lto={}", s)), + lto::Lto::Off => push("lto=off"), + lto::Lto::ObjectAndBitcode => {} // this is rustc's default + lto::Lto::OnlyBitcode => push("linker-plugin-lto"), + lto::Lto::OnlyObject => push("embed-bitcode=no"), + } + result +} + fn build_deps_args( cmd: &mut ProcessBuilder, cx: &mut Context<'_, '_>, diff --git a/src/tools/cargo/src/cargo/core/profiles.rs b/src/tools/cargo/src/cargo/core/profiles.rs index 6a28bd261..ca0c2e417 100644 --- a/src/tools/cargo/src/cargo/core/profiles.rs +++ b/src/tools/cargo/src/cargo/core/profiles.rs @@ -302,7 +302,7 @@ impl Profiles { }; match mode { - CompileMode::Test | CompileMode::Bench => { + CompileMode::Test | CompileMode::Bench | CompileMode::Doctest => { if release { ( InternedString::new("bench"), @@ -315,10 +315,7 @@ impl Profiles { ) } } - CompileMode::Build - | CompileMode::Check { .. } - | CompileMode::Doctest - | CompileMode::RunCustomBuild => { + CompileMode::Build | CompileMode::Check { .. } | CompileMode::RunCustomBuild => { // Note: `RunCustomBuild` doesn't normally use this code path. // `build_unit_profiles` normally ensures that it selects the // ancestor's profile. However, `cargo clean -p` can hit this -- 2.26.2