Skip to content

Commit

Permalink
Add crate type flag to rustc command
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <[email protected]>
  • Loading branch information
Rustin170506 committed Nov 21, 2021
1 parent e475fe4 commit 0c5f348
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 4 deletions.
20 changes: 18 additions & 2 deletions src/bin/cargo/commands/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use cargo::ops;
use cargo::util::interning::InternedString;

const PRINT_ARG_NAME: &str = "print";
const CRATE_TYPE_ARG_NAME: &str = "crate-type";

pub fn cli() -> App {
subcommand("rustc")
Expand Down Expand Up @@ -35,6 +36,11 @@ pub fn cli() -> App {
)
.value_name("INFO"),
)
.arg(multi_opt(
CRATE_TYPE_ARG_NAME,
"CRATE-TYPE",
"Rustc crate-types",
))
.arg_target_dir()
.arg_manifest_path()
.arg_message_format()
Expand Down Expand Up @@ -75,8 +81,18 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
.cli_unstable()
.fail_if_stable_opt(PRINT_ARG_NAME, 9357)?;
ops::print(&ws, &compile_opts, opt_value)?;
} else {
ops::compile(&ws, &compile_opts)?;
return Ok(());
}
let crate_types = values(args, CRATE_TYPE_ARG_NAME);
compile_opts.target_rustc_crate_types = if crate_types.is_empty() {
None
} else {
config
.cli_unstable()
.fail_if_stable_opt(CRATE_TYPE_ARG_NAME, 10083)?;
Some(crate_types)
};
ops::compile(&ws, &compile_opts)?;

Ok(())
}
9 changes: 9 additions & 0 deletions src/cargo/core/compiler/build_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ pub struct BuildContext<'a, 'cfg> {
/// Extra compiler args for either `rustc` or `rustdoc`.
pub extra_compiler_args: HashMap<Unit, Vec<String>>,

// Crate types for `rustc`.
pub target_rustc_crate_types: HashMap<Unit, Vec<String>>,

/// Package downloader.
///
/// This holds ownership of the `Package` objects.
Expand Down Expand Up @@ -61,6 +64,7 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
build_config: &'a BuildConfig,
profiles: Profiles,
extra_compiler_args: HashMap<Unit, Vec<String>>,
target_rustc_crate_types: HashMap<Unit, Vec<String>>,
target_data: RustcTargetData<'cfg>,
roots: Vec<Unit>,
unit_graph: UnitGraph,
Expand All @@ -80,6 +84,7 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
build_config,
profiles,
extra_compiler_args,
target_rustc_crate_types,
target_data,
roots,
unit_graph,
Expand Down Expand Up @@ -127,4 +132,8 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
pub fn extra_args_for(&self, unit: &Unit) -> Option<&Vec<String>> {
self.extra_compiler_args.get(unit)
}

pub fn rustc_crate_types_args_for(&self, unit: &Unit) -> Option<&Vec<String>> {
self.target_rustc_crate_types.get(unit)
}
}
10 changes: 8 additions & 2 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,8 +862,14 @@ fn build_base_args(
add_allow_features(cx, cmd);

if !test {
for crate_type in crate_types.iter() {
cmd.arg("--crate-type").arg(crate_type.as_str());
if let Some(crate_types) = cx.bcx.rustc_crate_types_args_for(unit) {
for crate_type in crate_types.iter() {
cmd.arg("--crate-type").arg(crate_type);
}
} else {
for crate_type in crate_types.iter() {
cmd.arg("--crate-type").arg(crate_type.as_str());
}
}
}

Expand Down
26 changes: 26 additions & 0 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub struct CompileOptions {
/// The specified target will be compiled with all the available arguments,
/// note that this only accounts for the *final* invocation of rustc
pub target_rustc_args: Option<Vec<String>>,
pub target_rustc_crate_types: Option<Vec<String>>,
/// Extra arguments passed to all selected targets for rustdoc.
pub local_rustdoc_args: Option<Vec<String>>,
/// Whether the `--document-private-items` flags was specified and should
Expand All @@ -92,6 +93,7 @@ impl<'a> CompileOptions {
},
target_rustdoc_args: None,
target_rustc_args: None,
target_rustc_crate_types: None,
local_rustdoc_args: None,
rustdoc_document_private_items: false,
honor_rust_version: true,
Expand Down Expand Up @@ -332,6 +334,7 @@ pub fn create_bcx<'a, 'cfg>(
ref filter,
ref target_rustdoc_args,
ref target_rustc_args,
ref target_rustc_crate_types,
ref local_rustdoc_args,
rustdoc_document_private_items,
honor_rust_version,
Expand Down Expand Up @@ -644,6 +647,28 @@ pub fn create_bcx<'a, 'cfg>(
}
}

let mut crate_types = HashMap::new();
if let Some(args) = target_rustc_crate_types {
if units.len() != 1 {
anyhow::bail!(
"crate types to rustc can only be passed to one \
target, consider filtering\nthe package by passing, \
e.g., `--lib` to specify a single target"
);
}
match units[0].target.kind() {
TargetKind::Lib(_) | TargetKind::ExampleLib(_) => {
crate_types.insert(units[0].clone(), args.clone());
}
_ => {
anyhow::bail!(
"crate types can only be specified for libraries and examples. \
Binaries, tests, and benchmarks are always the `bin` crate type"
);
}
}
}

if honor_rust_version {
// Remove any pre-release identifiers for easier comparison
let current_version = &target_data.rustc.version;
Expand Down Expand Up @@ -680,6 +705,7 @@ pub fn create_bcx<'a, 'cfg>(
build_config,
profiles,
extra_compiler_args,
crate_types,
target_data,
units,
unit_graph,
Expand Down
1 change: 1 addition & 0 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,7 @@ fn run_verify(
},
target_rustdoc_args: None,
target_rustc_args: rustc_args,
target_rustc_crate_types: None,
local_rustdoc_args: None,
rustdoc_document_private_items: false,
honor_rust_version: true,
Expand Down
1 change: 1 addition & 0 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ pub trait ArgMatchesExt {
),
target_rustdoc_args: None,
target_rustc_args: None,
target_rustc_crate_types: None,
local_rustdoc_args: None,
rustdoc_document_private_items: false,
honor_rust_version: !self._is_present("ignore-rust-version"),
Expand Down

0 comments on commit 0c5f348

Please sign in to comment.