Skip to content

Commit 7a06be1

Browse files
committed
Add CrateType to replace LibKind.
1 parent d7966eb commit 7a06be1

File tree

12 files changed

+89
-145
lines changed

12 files changed

+89
-145
lines changed

src/cargo/core/compiler/build_context/target_info.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::core::compiler::{BuildOutput, CompileKind, CompileTarget};
1+
use crate::core::compiler::{BuildOutput, CompileKind, CompileTarget, CrateType};
22
use crate::core::{Dependency, TargetKind, Workspace};
33
use crate::util::config::{Config, StringList, TargetConfig};
44
use crate::util::{CargoResult, CargoResultExt, ProcessBuilder, Rustc};
@@ -25,7 +25,7 @@ pub struct TargetInfo {
2525
/// `Some((prefix, suffix))`, for example `libcargo.so` would be
2626
/// `Some(("lib", ".so")). The value is `None` if the crate type is not
2727
/// supported.
28-
crate_types: RefCell<HashMap<String, Option<(String, String)>>>,
28+
crate_types: RefCell<HashMap<CrateType, Option<(String, String)>>>,
2929
/// `cfg` information extracted from `rustc --print=cfg`.
3030
cfg: Vec<Cfg>,
3131
/// Path to the sysroot.
@@ -123,10 +123,16 @@ impl TargetInfo {
123123
}
124124

125125
let crate_type_process = process.clone();
126-
const KNOWN_CRATE_TYPES: &[&str] =
127-
&["bin", "rlib", "dylib", "cdylib", "staticlib", "proc-macro"];
126+
const KNOWN_CRATE_TYPES: &[CrateType] = &[
127+
CrateType::Bin,
128+
CrateType::Rlib,
129+
CrateType::Dylib,
130+
CrateType::Cdylib,
131+
CrateType::Staticlib,
132+
CrateType::ProcMacro,
133+
];
128134
for crate_type in KNOWN_CRATE_TYPES.iter() {
129-
process.arg("--crate-type").arg(crate_type);
135+
process.arg("--crate-type").arg(crate_type.as_str());
130136
}
131137

132138
process.arg("--print=sysroot");
@@ -140,7 +146,7 @@ impl TargetInfo {
140146
let mut map = HashMap::new();
141147
for crate_type in KNOWN_CRATE_TYPES {
142148
let out = parse_crate_type(crate_type, &process, &output, &error, &mut lines)?;
143-
map.insert(crate_type.to_string(), out);
149+
map.insert(crate_type.clone(), out);
144150
}
145151

146152
let line = match lines.next() {
@@ -228,13 +234,19 @@ impl TargetInfo {
228234
/// Returns `None` if the target does not support the given crate type.
229235
pub fn file_types(
230236
&self,
231-
crate_type: &str,
237+
crate_type: &CrateType,
232238
flavor: FileFlavor,
233239
kind: &TargetKind,
234240
target_triple: &str,
235241
) -> CargoResult<Option<Vec<FileType>>> {
242+
let crate_type = if *crate_type == CrateType::Lib {
243+
CrateType::Rlib
244+
} else {
245+
crate_type.clone()
246+
};
247+
236248
let mut crate_types = self.crate_types.borrow_mut();
237-
let entry = crate_types.entry(crate_type.to_string());
249+
let entry = crate_types.entry(crate_type.clone());
238250
let crate_type_info = match entry {
239251
Entry::Occupied(o) => &*o.into_mut(),
240252
Entry::Vacant(v) => {
@@ -255,7 +267,7 @@ impl TargetInfo {
255267

256268
// See rust-lang/cargo#4500.
257269
if target_triple.ends_with("-windows-msvc")
258-
&& crate_type.ends_with("dylib")
270+
&& (crate_type == CrateType::Dylib || crate_type == CrateType::Cdylib)
259271
&& suffix == ".dll"
260272
{
261273
ret.push(FileType {
@@ -265,7 +277,7 @@ impl TargetInfo {
265277
should_replace_hyphens: false,
266278
})
267279
} else if target_triple.ends_with("windows-gnu")
268-
&& crate_type.ends_with("dylib")
280+
&& (crate_type == CrateType::Dylib || crate_type == CrateType::Cdylib)
269281
&& suffix == ".dll"
270282
{
271283
// LD can link DLL directly, but LLD requires the import library.
@@ -278,7 +290,7 @@ impl TargetInfo {
278290
}
279291

280292
// See rust-lang/cargo#4535.
281-
if target_triple.starts_with("wasm32-") && crate_type == "bin" && suffix == ".js" {
293+
if target_triple.starts_with("wasm32-") && crate_type == CrateType::Bin && suffix == ".js" {
282294
ret.push(FileType {
283295
suffix: ".wasm".to_string(),
284296
prefix: prefix.clone(),
@@ -319,10 +331,10 @@ impl TargetInfo {
319331
Ok(Some(ret))
320332
}
321333

322-
fn discover_crate_type(&self, crate_type: &str) -> CargoResult<Option<(String, String)>> {
334+
fn discover_crate_type(&self, crate_type: &CrateType) -> CargoResult<Option<(String, String)>> {
323335
let mut process = self.crate_type_process.clone();
324336

325-
process.arg("--crate-type").arg(crate_type);
337+
process.arg("--crate-type").arg(crate_type.as_str());
326338

327339
let output = process.exec_with_output().chain_err(|| {
328340
format!(
@@ -353,7 +365,7 @@ impl TargetInfo {
353365
/// This function can not handle more than one file per type (with wasm32-unknown-emscripten, there
354366
/// are two files for bin (`.wasm` and `.js`)).
355367
fn parse_crate_type(
356-
crate_type: &str,
368+
crate_type: &CrateType,
357369
cmd: &ProcessBuilder,
358370
output: &str,
359371
error: &str,

src/cargo/core/compiler/compilation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl<'cfg> Compilation<'cfg> {
167167
}
168168

169169
for crate_type in unit.target.rustc_crate_types() {
170-
p.arg("--crate-type").arg(crate_type);
170+
p.arg("--crate-type").arg(crate_type.as_str());
171171
}
172172

173173
Ok(p)

src/cargo/core/compiler/context/compilation_files.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use lazycell::LazyCell;
99
use log::info;
1010

1111
use super::{BuildContext, CompileKind, Context, FileFlavor, Layout};
12-
use crate::core::compiler::{CompileMode, CompileTarget, Unit};
12+
use crate::core::compiler::{CompileMode, CompileTarget, CrateType, Unit};
1313
use crate::core::{Target, TargetKind, Workspace};
1414
use crate::util::{self, CargoResult};
1515

@@ -274,7 +274,7 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
274274
let info = bcx.target_data.info(kind);
275275
let file_types = info
276276
.file_types(
277-
"bin",
277+
&CrateType::Bin,
278278
FileFlavor::Normal,
279279
&TargetKind::Bin,
280280
bcx.target_data.short_name(&kind),
@@ -416,12 +416,7 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
416416
let info = bcx.target_data.info(unit.kind);
417417
let file_stem = self.file_stem(unit);
418418

419-
let mut add = |crate_type: &str, flavor: FileFlavor| -> CargoResult<()> {
420-
let crate_type = if crate_type == "lib" {
421-
"rlib"
422-
} else {
423-
crate_type
424-
};
419+
let mut add = |crate_type: &CrateType, flavor: FileFlavor| -> CargoResult<()> {
425420
let file_types = info.file_types(
426421
crate_type,
427422
flavor,
@@ -465,22 +460,22 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
465460
}
466461
Ok(())
467462
};
468-
match *unit.target.kind() {
463+
match unit.target.kind() {
469464
TargetKind::Bin
470465
| TargetKind::CustomBuild
471466
| TargetKind::ExampleBin
472467
| TargetKind::Bench
473468
| TargetKind::Test => {
474-
add("bin", FileFlavor::Normal)?;
469+
add(&CrateType::Bin, FileFlavor::Normal)?;
475470
}
476471
TargetKind::Lib(..) | TargetKind::ExampleLib(..) if unit.mode.is_any_test() => {
477-
add("bin", FileFlavor::Normal)?;
472+
add(&CrateType::Bin, FileFlavor::Normal)?;
478473
}
479-
TargetKind::ExampleLib(ref kinds) | TargetKind::Lib(ref kinds) => {
480-
for kind in kinds {
474+
TargetKind::ExampleLib(crate_types) | TargetKind::Lib(crate_types) => {
475+
for crate_type in crate_types {
481476
add(
482-
kind.crate_type(),
483-
if kind.linkable() {
477+
crate_type,
478+
if crate_type.is_linkable() {
484479
FileFlavor::Linkable { rmeta: false }
485480
} else {
486481
FileFlavor::Normal

src/cargo/core/compiler/custom_build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ pub fn build_map(cx: &mut Context<'_, '_>) -> CargoResult<()> {
738738

739739
if dep_unit.target.for_host() {
740740
ret.plugins.extend(dep_scripts.to_link.iter().cloned());
741-
} else if dep_unit.target.linkable() {
741+
} else if dep_unit.target.is_linkable() {
742742
for &(pkg, metadata) in dep_scripts.to_link.iter() {
743743
add_to_link(&mut ret, pkg, metadata);
744744
}

src/cargo/core/compiler/mod.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod build_plan;
44
mod compilation;
55
mod compile_kind;
66
mod context;
7+
mod crate_type;
78
mod custom_build;
89
mod fingerprint;
910
mod job;
@@ -35,6 +36,7 @@ use self::build_plan::BuildPlan;
3536
pub use self::compilation::{Compilation, Doctest};
3637
pub use self::compile_kind::{CompileKind, CompileTarget};
3738
pub use self::context::{Context, Metadata};
39+
pub use self::crate_type::CrateType;
3840
pub use self::custom_build::{BuildOutput, BuildScriptOutputs, BuildScripts};
3941
pub use self::job::Freshness;
4042
use self::job::{Job, Work};
@@ -532,7 +534,7 @@ where
532534

533535
fn prepare_rustc(
534536
cx: &mut Context<'_, '_>,
535-
crate_types: &[&str],
537+
crate_types: &[CrateType],
536538
unit: &Unit,
537539
) -> CargoResult<ProcessBuilder> {
538540
let is_primary = cx.is_primary_package(unit);
@@ -734,7 +736,7 @@ fn build_base_args(
734736
cx: &mut Context<'_, '_>,
735737
cmd: &mut ProcessBuilder,
736738
unit: &Unit,
737-
crate_types: &[&str],
739+
crate_types: &[CrateType],
738740
) -> CargoResult<()> {
739741
assert!(!unit.mode.is_run_custom_build());
740742

@@ -764,7 +766,7 @@ fn build_base_args(
764766

765767
if !test {
766768
for crate_type in crate_types.iter() {
767-
cmd.arg("--crate-type").arg(crate_type);
769+
cmd.arg("--crate-type").arg(crate_type.as_str());
768770
}
769771
}
770772

@@ -780,7 +782,7 @@ fn build_base_args(
780782
}
781783

782784
let prefer_dynamic = (unit.target.for_host() && !unit.target.is_custom_build())
783-
|| (crate_types.contains(&"dylib") && bcx.ws.members().any(|p| *p != unit.pkg));
785+
|| (crate_types.contains(&CrateType::Dylib) && bcx.ws.members().any(|p| *p != unit.pkg));
784786
if prefer_dynamic {
785787
cmd.arg("-C").arg("prefer-dynamic");
786788
}
@@ -984,7 +986,7 @@ fn build_deps_args(
984986
// error in the future (see PR #4797).
985987
if !deps
986988
.iter()
987-
.any(|dep| !dep.unit.mode.is_doc() && dep.unit.target.linkable())
989+
.any(|dep| !dep.unit.mode.is_doc() && dep.unit.target.is_linkable())
988990
{
989991
if let Some(dep) = deps
990992
.iter()
@@ -1088,7 +1090,7 @@ pub fn extern_args(
10881090
};
10891091

10901092
for dep in deps {
1091-
if dep.unit.target.linkable() && !dep.unit.mode.is_doc() {
1093+
if dep.unit.target.is_linkable() && !dep.unit.mode.is_doc() {
10921094
link_to(dep, dep.extern_crate_name, dep.noprelude)?;
10931095
}
10941096
}

src/cargo/core/compiler/unit.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::core::compiler::{CompileKind, CompileMode};
2-
use crate::core::manifest::{LibKind, Target, TargetKind};
1+
use crate::core::compiler::{CompileKind, CompileMode, CrateType};
2+
use crate::core::manifest::{Target, TargetKind};
33
use crate::core::{profiles::Profile, InternedString, Package};
44
use crate::util::hex::short_hash;
55
use crate::util::Config;
@@ -178,9 +178,9 @@ impl UnitInterner {
178178
//
179179
// At some point in the future, it would be nice to have a
180180
// first-class way of overriding or specifying crate-types.
181-
(true, TargetKind::Lib(crate_types)) if crate_types.contains(&LibKind::Dylib) => {
181+
(true, TargetKind::Lib(crate_types)) if crate_types.contains(&CrateType::Dylib) => {
182182
let mut new_target = Target::clone(target);
183-
new_target.set_kind(TargetKind::Lib(vec![LibKind::Rlib]));
183+
new_target.set_kind(TargetKind::Lib(vec![CrateType::Rlib]));
184184
new_target
185185
}
186186
_ => target.clone(),

src/cargo/core/compiler/unit_dependencies.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ fn maybe_lib(
476476
unit.pkg
477477
.targets()
478478
.iter()
479-
.find(|t| t.linkable())
479+
.find(|t| t.is_linkable())
480480
.map(|t| {
481481
let mode = check_or_build_mode(unit.mode, t);
482482
new_unit_dep(
@@ -681,7 +681,7 @@ fn connect_run_custom_build_deps(unit_dependencies: &mut UnitGraph) {
681681
// Only deps with `links`.
682682
.filter(|other| {
683683
other.unit.pkg != unit.pkg
684-
&& other.unit.target.linkable()
684+
&& other.unit.target.is_linkable()
685685
&& other.unit.pkg.manifest().links().is_some()
686686
})
687687
// Get the RunCustomBuild for other lib.

0 commit comments

Comments
 (0)