Skip to content

Build supports a crate-type flag. #7900

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions src/bin/cargo/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub fn cli() -> App {
.arg_manifest_path()
.arg_message_format()
.arg_build_plan()
.arg_crate_type()
.after_help(
"\
All packages in the workspace are built if the `--workspace` flag is supplied. The
Expand Down
4 changes: 4 additions & 0 deletions src/cargo/core/compiler/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ pub struct Context<'a, 'cfg> {
/// jobserver clients for each Unit (which eventually becomes a rustc
/// process).
pub rustc_clients: HashMap<Unit<'a>, Client>,

pub crate_type: Option<String>,
}

impl<'a, 'cfg> Context<'a, 'cfg> {
Expand All @@ -82,6 +84,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
bcx: &'a BuildContext<'a, 'cfg>,
unit_dependencies: UnitGraph<'a>,
default_kind: CompileKind,
crate_type: Option<String>,
) -> CargoResult<Self> {
// Load up the jobserver that we'll use to manage our parallelism. This
// is the same as the GNU make implementation of a jobserver, and
Expand Down Expand Up @@ -119,6 +122,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
rmeta_required: HashSet::new(),
rustc_clients: HashMap::new(),
pipelining,
crate_type,
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ impl BuildScriptOutputs {
}
Entry::Occupied(entry) => panic!(
"build script output collision for {}/{}\n\
old={:?}\nnew={:?}",
old={:?}\nnew={:?}",
pkg_id,
metadata,
entry.get(),
Expand Down
8 changes: 6 additions & 2 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,8 +737,12 @@ fn build_base_args<'a, 'cfg>(
add_error_format_and_color(cx, cmd, cx.rmeta_required(unit))?;

if !test {
for crate_type in crate_types.iter() {
cmd.arg("--crate-type").arg(crate_type);
if cx.crate_type.is_some() && cx.is_primary_package(unit) {
cmd.arg("--crate-type").arg(cx.crate_type.as_ref().unwrap());
} else {
for crate_type in crate_types.iter() {
cmd.arg("--crate-type").arg(crate_type);
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/cargo/ops/cargo_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {

let unit_dependencies =
unit_dependencies::build_unit_dependencies(&bcx, &resolve, None, &units, &[])?;
let mut cx = Context::new(config, &bcx, unit_dependencies, build_config.requested_kind)?;
let mut cx = Context::new(
config,
&bcx,
unit_dependencies,
build_config.requested_kind,
None,
)?;
cx.prepare_units(None, &units)?;

for unit in units.iter() {
Expand Down
12 changes: 11 additions & 1 deletion src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ pub struct CompileOptions<'a> {
// Note that, although the cmd-line flag name is `out-dir`, in code we use
// `export_dir`, to avoid confusion with out dir at `target/debug/deps`.
pub export_dir: Option<PathBuf>,
/// Crate type
pub crate_type: Option<String>,
}

impl<'a> CompileOptions<'a> {
Expand All @@ -94,6 +96,7 @@ impl<'a> CompileOptions<'a> {
local_rustdoc_args: None,
rustdoc_document_private_items: false,
export_dir: None,
crate_type: None,
})
}
}
Expand Down Expand Up @@ -277,6 +280,7 @@ pub fn compile_ws<'a>(
ref local_rustdoc_args,
rustdoc_document_private_items,
ref export_dir,
ref crate_type,
} = *options;

match build_config.mode {
Expand Down Expand Up @@ -465,7 +469,13 @@ pub fn compile_ws<'a>(

let ret = {
let _p = profile::start("compiling");
let cx = Context::new(config, &bcx, unit_dependencies, build_config.requested_kind)?;
let cx = Context::new(
config,
&bcx,
unit_dependencies,
build_config.requested_kind,
crate_type.clone(),
)?;
cx.compile(&units, export_dir.clone(), exec)?
};

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 @@ -665,6 +665,7 @@ fn run_verify(ws: &Workspace<'_>, tar: &FileLock, opts: &PackageOpts<'_>) -> Car
local_rustdoc_args: None,
rustdoc_document_private_items: false,
export_dir: None,
crate_type: None,
},
&exec,
)?;
Expand Down
5 changes: 5 additions & 0 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ pub trait AppExt: Sized {
))
}

fn arg_crate_type(self) -> Self {
self._arg(opt("crate-type", "Crate type").value_name("CRATE-TYPE"))
}

fn arg_new_opts(self) -> Self {
self._arg(
opt(
Expand Down Expand Up @@ -468,6 +472,7 @@ pub trait ArgMatchesExt {
local_rustdoc_args: None,
rustdoc_document_private_items: false,
export_dir: None,
crate_type: self._value_of("crate-type").map(|s| s.to_string()),
};

if let Some(ws) = workspace {
Expand Down