Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --cfg and --rustc-cfg flags to output compiler configuration #9002

Merged
merged 51 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
0018ef0
Add minimum working implementation
volks73 Dec 15, 2020
d6e28b8
Merge branch 'master' of https://github.com/rust-lang/cargo into feat…
volks73 Dec 15, 2020
7397f31
Add `--cfg` to build and check subcommands
volks73 Dec 16, 2020
3a6850f
Add `--cfg` to bench and test subcommands
volks73 Dec 16, 2020
6edd818
Merge branch 'master' of https://github.com/rust-lang/cargo into feat…
volks73 Dec 16, 2020
e9e4593
Change option name for subcommands
volks73 Dec 16, 2020
d662cc3
Fix `--rustc-cfg` not working
volks73 Dec 16, 2020
6562f6a
Add `--cfg` or `--rustc-cfg` behind unstable flag
volks73 Dec 16, 2020
d49a32f
Change field name
volks73 Dec 16, 2020
efdf647
Change module name
volks73 Dec 16, 2020
6789a4c
Fix compile-time errors from module rename
volks73 Dec 16, 2020
234089f
Change JSON format
volks73 Dec 16, 2020
3d0fcbe
Add better example JSON to internal comment
volks73 Dec 16, 2020
80e8fce
Merge branch 'master' of https://github.com/rust-lang/cargo into feat…
volks73 Dec 16, 2020
af3a843
Change the `--cfg` and `--rustc-cfg` to hidden
volks73 Dec 19, 2020
1f3fb66
Merge branch 'master' of https://github.com/rust-lang/cargo into feat…
volks73 Dec 19, 2020
9be620a
Fix formatting
volks73 Dec 19, 2020
105a8bd
Remove `--rustc-cfg` argument
volks73 Jan 8, 2021
e078a6c
Change to lighter weight `--print-cfg`
volks73 Jan 8, 2021
7f1c268
Fix typo
volks73 Jan 8, 2021
fc83cbe
Refactor to use constant for arg name
volks73 Jan 8, 2021
97a821d
Change JSON format
volks73 Jan 8, 2021
698fe70
Fix formatting
volks73 Jan 8, 2021
0c133f3
Fix compile-time errors
volks73 Jan 8, 2021
6d48e50
Fix formatting
volks73 Jan 8, 2021
afa1905
Remove `--print-cfg` argument
volks73 Jan 30, 2021
1380755
Change implementation
volks73 Jan 30, 2021
bf3ed17
Remove cache output and error handling
volks73 Feb 9, 2021
db77c31
Fix warnings
volks73 Feb 9, 2021
2f985ae
Remove unnecessary arguments and env removal
volks73 Feb 9, 2021
2cd5d9d
Change to enumerate implementation
volks73 Feb 9, 2021
a05dac3
Remove duplicate check for empty kinds
volks73 Feb 9, 2021
5a24ad1
Change println macro to drop_println
volks73 Feb 9, 2021
d7034c6
Fix formatting
volks73 Feb 9, 2021
d0b15d4
Merge branch 'master' into feature-rustc-cfg-argument
volks73 Feb 9, 2021
a740608
Merge branch 'master' of https://github.com/rust-lang/cargo into feat…
volks73 Feb 9, 2021
c86864b
Fix missing import after merge
volks73 Feb 9, 2021
a8fffa8
Merge branch 'feature-rustc-cfg-argument' of https://github.com/volks…
volks73 Feb 10, 2021
37065f0
Merge branch 'master' of https://github.com/rust-lang/cargo into feat…
volks73 Feb 10, 2021
f32f72f
Fix missing import
volks73 Feb 10, 2021
2aa4fba
Change scope of function
volks73 Feb 11, 2021
39fb01c
Change to use TargetInfo type
volks73 Feb 11, 2021
fa8e9ae
Change to forwarding rustc stdio to cargo
volks73 Feb 20, 2021
8ce7633
Fix warning with unused import
volks73 Feb 20, 2021
de340a2
Add first test
volks73 Feb 22, 2021
8b80e52
Add multitarget test
volks73 Feb 22, 2021
c7038b2
Add test using RUSTFLAGS env var
volks73 Feb 22, 2021
1f05730
Add test with cargo configuration file
volks73 Feb 22, 2021
70a423e
Fix formatting
volks73 Feb 22, 2021
9b02dd4
Fix tests for CI environment
volks73 Feb 22, 2021
2a5355f
Fix usage of assert methods
volks73 Feb 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/bin/cargo/commands/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::command_prelude::*;

use cargo::ops;

const PRINT_ARG_NAME: &str = "print";

pub fn cli() -> App {
subcommand("rustc")
.setting(AppSettings::TrailingVarArg)
Expand All @@ -26,6 +28,13 @@ pub fn cli() -> App {
.arg_profile("Build artifacts with the specified profile")
.arg_features()
.arg_target_triple("Target triple which compiles will be for")
.arg(
opt(
PRINT_ARG_NAME,
"Output compiler information without compiling",
)
.value_name("INFO"),
)
.arg_target_dir()
.arg_manifest_path()
.arg_message_format()
Expand Down Expand Up @@ -62,6 +71,13 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
} else {
Some(target_args)
};
ops::compile(&ws, &compile_opts)?;
if let Some(opt_value) = args.value_of(PRINT_ARG_NAME) {
config
.cli_unstable()
.fail_if_stable_opt(PRINT_ARG_NAME, 8923)?;
ops::print(&ws, &compile_opts, opt_value)?;
} else {
ops::compile(&ws, &compile_opts)?;
}
Ok(())
}
40 changes: 38 additions & 2 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@

use std::collections::{BTreeSet, HashMap, HashSet};
use std::hash::{Hash, Hasher};
use std::io::Write;
use std::sync::Arc;

use crate::core::compiler::standard_lib;
use crate::core::compiler::unit_dependencies::build_unit_dependencies;
use crate::core::compiler::unit_graph::{self, UnitDep, UnitGraph};
use crate::core::compiler::{standard_lib, TargetInfo};
use crate::core::compiler::{BuildConfig, BuildContext, Compilation, Context};
use crate::core::compiler::{CompileKind, CompileMode, CompileTarget, RustcTargetData, Unit};
use crate::core::compiler::{DefaultExecutor, Executor, UnitInterner};
Expand All @@ -37,6 +38,7 @@ use crate::core::resolver::features::{self, FeaturesFor, RequestedFeatures};
use crate::core::resolver::{HasDevUnits, Resolve, ResolveOpts};
use crate::core::{FeatureValue, Package, PackageSet, Shell, Summary, Target};
use crate::core::{PackageId, PackageIdSpec, SourceId, TargetKind, Workspace};
use crate::drop_println;
use crate::ops;
use crate::ops::resolve::WorkspaceResolve;
use crate::util::config::Config;
Expand Down Expand Up @@ -289,12 +291,46 @@ pub fn compile_ws<'a>(
unit_graph::emit_serialized_unit_graph(&bcx.roots, &bcx.unit_graph)?;
return Compilation::new(&bcx);
}

let _p = profile::start("compiling");
let cx = Context::new(&bcx)?;
cx.compile(exec)
}

pub fn print<'a>(
ws: &Workspace<'a>,
options: &CompileOptions,
print_opt_value: &str,
) -> CargoResult<()> {
let CompileOptions {
ref build_config,
ref target_rustc_args,
..
} = *options;
let config = ws.config();
let rustc = config.load_global_rustc(Some(ws))?;
for (index, kind) in build_config.requested_kinds.iter().enumerate() {
if index != 0 {
drop_println!(config);
}
let target_info = TargetInfo::new(config, &build_config.requested_kinds, &rustc, *kind)?;
let mut process = rustc.process();
process.args(&target_info.rustflags);
if let Some(args) = target_rustc_args {
process.args(args);
}
if let CompileKind::Target(t) = kind {
process.arg("--target").arg(t.short_name());
}
process.arg("--print").arg(print_opt_value);
let output = process.exec_with_output()?;
Copy link
Member

Choose a reason for hiding this comment

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

Instead of capturing the output here I think it's best to jsutt forward the output to Cargo's own stdout/stderr streams

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am sorry, but I am not sure how to forward the output to Cargo's own stdout/stderr streams. Do you have an example of doing this within the Cargo codebase for another process/cmd? I looked into the ProcessBuilder type and initially tried the exec method, but it just hanged. I believe the equivalent method in the std::process::Command type would be status() because it says it the child (rustc) inherients stdio from the parent (Cargo). Would this be the same as "forwarding"?

Copy link
Member

Choose a reason for hiding this comment

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

I believe you can basically call exec instead of exec_with_output here

let stdout = std::io::stdout();
let mut lock = stdout.lock();
lock.write_all(&output.stdout)?;
drop(lock);
}
Ok(())
}

pub fn create_bcx<'a, 'cfg>(
ws: &'a Workspace<'cfg>,
options: &'a CompileOptions,
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub use self::cargo_clean::{clean, CleanOptions};
pub use self::cargo_compile::{
compile, compile_with_exec, compile_ws, create_bcx, resolve_all_features, CompileOptions,
compile, compile_with_exec, compile_ws, create_bcx, print, resolve_all_features, CompileOptions,
};
pub use self::cargo_compile::{CompileFilter, FilterRule, LibRule, Packages};
pub use self::cargo_doc::{doc, DocOptions};
Expand Down
1 change: 0 additions & 1 deletion src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,6 @@ pub trait ArgMatchesExt {
.cli_unstable()
.fail_if_stable_opt("--unit-graph", 8002)?;
}

let opts = CompileOptions {
build_config,
features: self._values_of("features"),
Expand Down