Skip to content

Commit 961974f

Browse files
committed
Use enum to distinguish dependency type
1 parent 638ebbc commit 961974f

File tree

3 files changed

+54
-26
lines changed

3 files changed

+54
-26
lines changed

src/bootstrap/compile.rs

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::builder::Cargo;
2323
use crate::dist;
2424
use crate::native;
2525
use crate::util::{exe, is_dylib, symlink_dir};
26-
use crate::{Compiler, GitRepo, Mode};
26+
use crate::{Compiler, DependencyType, GitRepo, Mode};
2727

2828
use crate::builder::{Builder, Kind, RunConfig, ShouldRun, Step};
2929
use crate::cache::{Interned, INTERNER};
@@ -84,7 +84,7 @@ impl Step for Std {
8484
return;
8585
}
8686

87-
target_deps.extend(copy_third_party_objects(builder, &compiler, target).into_iter());
87+
target_deps.extend(copy_third_party_objects(builder, &compiler, target));
8888
target_deps.extend(copy_self_contained_objects(builder, &compiler, target));
8989

9090
let mut cargo = builder.cargo(compiler, Mode::Std, target, "build");
@@ -116,7 +116,8 @@ fn copy_and_stamp(
116116
libdir: &Path,
117117
sourcedir: &Path,
118118
name: &str,
119-
target_deps: &mut Vec<PathBuf>,
119+
target_deps: &mut Vec<(PathBuf, DependencyType)>,
120+
dependency_type: DependencyType,
120121
) {
121122
let target = libdir.join(name);
122123
builder.copy(&sourcedir.join(name), &target);
@@ -129,7 +130,7 @@ fn copy_third_party_objects(
129130
builder: &Builder<'_>,
130131
compiler: &Compiler,
131132
target: Interned<String>,
132-
) -> Vec<PathBuf> {
133+
) -> Vec<(PathBuf, DependencyType)> {
133134
let libdir = builder.sysroot_libdir(*compiler, target);
134135
let mut target_deps = vec![];
135136

@@ -148,13 +149,18 @@ fn copy_third_party_objects(
148149
Path::new(&src),
149150
"libunwind.a",
150151
&mut target_deps,
152+
DependencyType::Target,
151153
);
152154
}
153155

154156
if builder.config.sanitizers && compiler.stage != 0 {
155157
// The sanitizers are only copied in stage1 or above,
156158
// to avoid creating dependency on LLVM.
157-
target_deps.extend(copy_sanitizers(builder, &compiler, target));
159+
target_deps.extend(
160+
copy_sanitizers(builder, &compiler, target)
161+
.into_iter()
162+
.map(|d| (d, DependencyType::Target)),
163+
);
158164
}
159165

160166
target_deps
@@ -165,7 +171,7 @@ fn copy_self_contained_objects(
165171
builder: &Builder<'_>,
166172
compiler: &Compiler,
167173
target: Interned<String>,
168-
) -> Vec<PathBuf> {
174+
) -> Vec<(PathBuf, DependencyType)> {
169175
let libdir = builder.sysroot_libdir(*compiler, target);
170176
let mut target_deps = vec![];
171177

@@ -185,6 +191,7 @@ fn copy_self_contained_objects(
185191
&srcdir,
186192
obj,
187193
&mut target_deps,
194+
DependencyType::TargetSelfContained,
188195
);
189196
}
190197
} else if target.ends_with("-wasi") {
@@ -195,13 +202,14 @@ fn copy_self_contained_objects(
195202
&srcdir,
196203
"crt1.o",
197204
&mut target_deps,
205+
DependencyType::TargetSelfContained,
198206
);
199207
} else if target.contains("windows-gnu") {
200208
for obj in ["crt2.o", "dllcrt2.o"].iter() {
201209
let src = compiler_file(builder, builder.cc(target), target, obj);
202210
let target = libdir.join(obj);
203211
builder.copy(&src, &target);
204-
target_deps.push(target);
212+
target_deps.push((target, DependencyType::TargetSelfContained));
205213
}
206214
}
207215

@@ -370,7 +378,7 @@ pub struct StartupObjects {
370378
}
371379

372380
impl Step for StartupObjects {
373-
type Output = Vec<PathBuf>;
381+
type Output = Vec<(PathBuf, DependencyType)>;
374382

375383
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
376384
run.path("src/rtstartup")
@@ -389,7 +397,7 @@ impl Step for StartupObjects {
389397
/// They don't require any library support as they're just plain old object
390398
/// files, so we just use the nightly snapshot compiler to always build them (as
391399
/// no other compilers are guaranteed to be available).
392-
fn run(self, builder: &Builder<'_>) -> Vec<PathBuf> {
400+
fn run(self, builder: &Builder<'_>) -> Vec<(PathBuf, DependencyType)> {
393401
let for_compiler = self.compiler;
394402
let target = self.target;
395403
if !target.contains("windows-gnu") {
@@ -423,7 +431,7 @@ impl Step for StartupObjects {
423431

424432
let target = sysroot_dir.join((*file).to_string() + ".o");
425433
builder.copy(dst_file, &target);
426-
target_deps.push(target);
434+
target_deps.push((target, DependencyType::Target));
427435
}
428436

429437
target_deps
@@ -838,8 +846,8 @@ pub fn add_to_sysroot(
838846
) {
839847
t!(fs::create_dir_all(&sysroot_dst));
840848
t!(fs::create_dir_all(&sysroot_host_dst));
841-
for (path, host) in builder.read_stamp_file(stamp) {
842-
if host {
849+
for (path, dependency_type) in builder.read_stamp_file(stamp) {
850+
if dependency_type == DependencyType::Host {
843851
builder.copy(&path, &sysroot_host_dst.join(path.file_name().unwrap()));
844852
} else {
845853
builder.copy(&path, &sysroot_dst.join(path.file_name().unwrap()));
@@ -852,7 +860,7 @@ pub fn run_cargo(
852860
cargo: Cargo,
853861
tail_args: Vec<String>,
854862
stamp: &Path,
855-
additional_target_deps: Vec<PathBuf>,
863+
additional_target_deps: Vec<(PathBuf, DependencyType)>,
856864
is_check: bool,
857865
) -> Vec<PathBuf> {
858866
if builder.config.dry_run {
@@ -903,15 +911,15 @@ pub fn run_cargo(
903911
if filename.starts_with(&host_root_dir) {
904912
// Unless it's a proc macro used in the compiler
905913
if crate_types.iter().any(|t| t == "proc-macro") {
906-
deps.push((filename.to_path_buf(), true));
914+
deps.push((filename.to_path_buf(), DependencyType::Host));
907915
}
908916
continue;
909917
}
910918

911919
// If this was output in the `deps` dir then this is a precise file
912920
// name (hash included) so we start tracking it.
913921
if filename.starts_with(&target_deps_dir) {
914-
deps.push((filename.to_path_buf(), false));
922+
deps.push((filename.to_path_buf(), DependencyType::Target));
915923
continue;
916924
}
917925

@@ -963,17 +971,21 @@ pub fn run_cargo(
963971
let candidate = format!("{}.lib", path_to_add);
964972
let candidate = PathBuf::from(candidate);
965973
if candidate.exists() {
966-
deps.push((candidate, false));
974+
deps.push((candidate, DependencyType::Target));
967975
}
968976
}
969-
deps.push((path_to_add.into(), false));
977+
deps.push((path_to_add.into(), DependencyType::Target));
970978
}
971979

972-
deps.extend(additional_target_deps.into_iter().map(|d| (d, false)));
980+
deps.extend(additional_target_deps);
973981
deps.sort();
974982
let mut new_contents = Vec::new();
975-
for (dep, proc_macro) in deps.iter() {
976-
new_contents.extend(if *proc_macro { b"h" } else { b"t" });
983+
for (dep, dependency_type) in deps.iter() {
984+
new_contents.extend(match *dependency_type {
985+
DependencyType::Host => b"h",
986+
DependencyType::Target => b"t",
987+
DependencyType::TargetSelfContained => b"s",
988+
});
977989
new_contents.extend(dep.to_str().unwrap().as_bytes());
978990
new_contents.extend(b"\0");
979991
}

src/bootstrap/dist.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::channel;
2222
use crate::compile;
2323
use crate::tool::{self, Tool};
2424
use crate::util::{exe, is_dylib, timeit};
25-
use crate::{Compiler, Mode, LLVM_TOOLS};
25+
use crate::{Compiler, DependencyType, Mode, LLVM_TOOLS};
2626
use time::{self, Timespec};
2727

2828
pub fn pkgname(builder: &Builder<'_>, component: &str) -> String {
@@ -651,8 +651,8 @@ fn skip_host_target_lib(builder: &Builder<'_>, compiler: Compiler) -> bool {
651651
fn copy_target_libs(builder: &Builder<'_>, target: &str, image: &Path, stamp: &Path) {
652652
let dst = image.join("lib/rustlib").join(target).join("lib");
653653
t!(fs::create_dir_all(&dst));
654-
for (path, host) in builder.read_stamp_file(stamp) {
655-
if !host || builder.config.build == target {
654+
for (path, dependency_type) in builder.read_stamp_file(stamp) {
655+
if dependency_type != DependencyType::Host || builder.config.build == target {
656656
builder.copy(&path, &dst.join(path.file_name().unwrap()));
657657
}
658658
}

src/bootstrap/lib.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,17 @@ impl Crate {
280280
}
281281
}
282282

283+
/// When building Rust various objects are handled differently.
284+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
285+
pub enum DependencyType {
286+
/// Libraries originating from proc-macros.
287+
Host,
288+
/// Typical Rust libraries.
289+
Target,
290+
/// Non Rust libraries and objects shipped to ease usage of certain targets.
291+
TargetSelfContained,
292+
}
293+
283294
/// The various "modes" of invoking Cargo.
284295
///
285296
/// These entries currently correspond to the various output directories of the
@@ -1097,7 +1108,7 @@ impl Build {
10971108
ret
10981109
}
10991110

1100-
fn read_stamp_file(&self, stamp: &Path) -> Vec<(PathBuf, bool)> {
1111+
fn read_stamp_file(&self, stamp: &Path) -> Vec<(PathBuf, DependencyType)> {
11011112
if self.config.dry_run {
11021113
return Vec::new();
11031114
}
@@ -1110,9 +1121,14 @@ impl Build {
11101121
if part.is_empty() {
11111122
continue;
11121123
}
1113-
let host = part[0] as char == 'h';
1124+
let dependency_type = match part[0] as char {
1125+
'h' => DependencyType::Host,
1126+
's' => DependencyType::TargetSelfContained,
1127+
't' => DependencyType::Target,
1128+
_ => unreachable!(),
1129+
};
11141130
let path = PathBuf::from(t!(str::from_utf8(&part[1..])));
1115-
paths.push((path, host));
1131+
paths.push((path, dependency_type));
11161132
}
11171133
paths
11181134
}

0 commit comments

Comments
 (0)