]> git.proxmox.com Git - rustc.git/blame - src/tools/cargo/src/bin/cargo/commands/rustc.rs
New upstream version 1.73.0+dfsg1
[rustc.git] / src / tools / cargo / src / bin / cargo / commands / rustc.rs
CommitLineData
0a29b90c
FG
1use crate::command_prelude::*;
2use cargo::ops;
3use cargo::util::interning::InternedString;
4
5const PRINT_ARG_NAME: &str = "print";
6const CRATE_TYPE_ARG_NAME: &str = "crate-type";
7
8pub fn cli() -> Command {
9 subcommand("rustc")
10 .about("Compile a package, and pass extra options to the compiler")
0a29b90c
FG
11 .arg(
12 Arg::new("args")
13 .num_args(0..)
14 .help("Extra rustc flags")
15 .trailing_var_arg(true),
16 )
add651ee
FG
17 .arg(
18 opt(
19 PRINT_ARG_NAME,
20 "Output compiler information without compiling",
21 )
22 .value_name("INFO"),
23 )
24 .arg(multi_opt(
25 CRATE_TYPE_ARG_NAME,
26 "CRATE-TYPE",
27 "Comma separated list of types of crates for the compiler to emit",
28 ))
29 .arg_future_incompat_report()
30 .arg_ignore_rust_version()
31 .arg_message_format()
32 .arg_quiet()
0a29b90c 33 .arg_package("Package to build")
0a29b90c
FG
34 .arg_targets_all(
35 "Build only this package's library",
36 "Build only the specified binary",
37 "Build all binaries",
38 "Build only the specified example",
39 "Build all examples",
40 "Build only the specified test target",
41 "Build all tests",
42 "Build only the specified bench target",
43 "Build all benches",
44 "Build all targets",
45 )
add651ee
FG
46 .arg_features()
47 .arg_jobs()
0a29b90c
FG
48 .arg_release("Build artifacts in release mode, with optimizations")
49 .arg_profile("Build artifacts with the specified profile")
0a29b90c 50 .arg_target_triple("Target triple which compiles will be for")
0a29b90c 51 .arg_target_dir()
0a29b90c 52 .arg_unit_graph()
0a29b90c 53 .arg_timings()
add651ee 54 .arg_manifest_path()
0a29b90c
FG
55 .after_help("Run `cargo help rustc` for more detailed information.\n")
56}
57
58pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
59 let ws = args.workspace(config)?;
60 // This is a legacy behavior that changes the behavior based on the profile.
61 // If we want to support this more formally, I think adding a --mode flag
62 // would be warranted.
63 let mode = match args.get_one::<String>("profile").map(String::as_str) {
64 Some("test") => CompileMode::Test,
65 Some("bench") => CompileMode::Bench,
66 Some("check") => CompileMode::Check { test: false },
67 _ => CompileMode::Build,
68 };
69 let mut compile_opts = args.compile_options_for_single_package(
70 config,
71 mode,
72 Some(&ws),
73 ProfileChecking::LegacyRustc,
74 )?;
75 if compile_opts.build_config.requested_profile == "check" {
76 compile_opts.build_config.requested_profile = InternedString::new("dev");
77 }
78 let target_args = values(args, "args");
79 compile_opts.target_rustc_args = if target_args.is_empty() {
80 None
81 } else {
82 Some(target_args)
83 };
84 if let Some(opt_value) = args.get_one::<String>(PRINT_ARG_NAME) {
85 config
86 .cli_unstable()
87 .fail_if_stable_opt(PRINT_ARG_NAME, 9357)?;
88 ops::print(&ws, &compile_opts, opt_value)?;
89 return Ok(());
90 }
91 let crate_types = values(args, CRATE_TYPE_ARG_NAME);
92 compile_opts.target_rustc_crate_types = if crate_types.is_empty() {
93 None
94 } else {
95 Some(crate_types)
96 };
97 ops::compile(&ws, &compile_opts)?;
98
99 Ok(())
100}