Skip to content

Commit

Permalink
WIP: Change build-std to use --sysroot
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Sep 23, 2019
1 parent cdf7f63 commit 1ed8760
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 24 deletions.
7 changes: 6 additions & 1 deletion src/cargo/core/compiler/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use super::custom_build::{self, BuildDeps, BuildScriptOutputs, BuildScripts};
use super::fingerprint::Fingerprint;
use super::job_queue::JobQueue;
use super::layout::Layout;
use super::standard_lib;
use super::unit_dependencies::{UnitDep, UnitGraph};
use super::{BuildContext, Compilation, CompileMode, Executor, FileFlavor, Kind};

Expand Down Expand Up @@ -304,7 +305,11 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
};
let host_layout = Layout::new(self.bcx.ws, None, dest)?;
let target_layout = match self.bcx.build_config.requested_target.as_ref() {
Some(target) => Some(Layout::new(self.bcx.ws, Some(target), dest)?),
Some(target) => {
let layout = Layout::new(self.bcx.ws, Some(target), dest)?;
standard_lib::prepare_sysroot(&layout)?;
Some(layout)
}
None => None,
};
self.primary_packages
Expand Down
7 changes: 6 additions & 1 deletion src/cargo/core/compiler/job_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use super::job::{
Freshness::{self, Dirty, Fresh},
Job,
};
use super::standard_lib;
use super::timings::Timings;
use super::{BuildContext, BuildPlan, CompileMode, Context, Unit};
use crate::core::{PackageId, TargetKind};
Expand Down Expand Up @@ -595,7 +596,7 @@ impl<'a, 'cfg> JobQueue<'a, 'cfg> {
id: u32,
unit: &Unit<'a>,
artifact: Artifact,
cx: &mut Context<'_, '_>,
cx: &mut Context<'a, '_>,
) -> CargoResult<()> {
if unit.mode.is_run_custom_build() && cx.bcx.show_warnings(unit.pkg.package_id()) {
self.emit_warnings(None, unit, cx)?;
Expand All @@ -605,6 +606,10 @@ impl<'a, 'cfg> JobQueue<'a, 'cfg> {
Artifact::All => self.timings.unit_finished(id, unlocked),
Artifact::Metadata => self.timings.unit_rmeta_finished(id, unlocked),
}
if unit.is_std && unit.kind == super::Kind::Target && !cx.bcx.build_config.build_plan {
let rmeta = artifact == Artifact::Metadata;
standard_lib::add_sysroot_artifact(cx, unit, rmeta)?;
}
Ok(())
}

Expand Down
44 changes: 34 additions & 10 deletions src/cargo/core/compiler/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ pub struct Layout {
examples: PathBuf,
/// The directory for rustdoc output: `$root/doc`
doc: PathBuf,
/// The sysroot.
sysroot: Option<PathBuf>,
sysroot_libdir: Option<PathBuf>,
/// The lockfile for a build (`.cargo-lock`). Will be unlocked when this
/// struct is `drop`ped.
_lock: FileLock,
Expand All @@ -139,18 +142,21 @@ impl Layout {
// Flexible target specifications often point at json files, so interpret
// the target triple as a Path and then just use the file stem as the
// component for the directory name in that case.
if let Some(triple) = triple {
let triple = Path::new(triple);
if triple.extension().and_then(|s| s.to_str()) == Some("json") {
root.push(
triple
.file_stem()
let triple_path = if let Some(s) = triple {
let p = Path::new(s);
let tp = if p.extension().and_then(|s| s.to_str()) == Some("json") {
Path::new(
p.file_stem()
.ok_or_else(|| failure::format_err!("invalid target"))?,
);
)
} else {
root.push(triple);
}
}
p
};
root.push(tp);
Some(tp)
} else {
None
};
let dest = root.join(dest);
// If the root directory doesn't already exist go ahead and create it
// here. Use this opportunity to exclude it from backups as well if the
Expand All @@ -167,6 +173,15 @@ impl Layout {
let root = root.into_path_unlocked();
let dest = dest.into_path_unlocked();

let build_std = ws.config().cli_unstable().build_std.as_ref();
let (sysroot, sysroot_libdir) = if let Some(tp) = build_std.and(triple_path) {
let sysroot = dest.join(".sysroot");
let sysroot_libdir = sysroot.join("lib").join("rustlib").join(tp).join("lib");
(Some(sysroot), Some(sysroot_libdir))
} else {
(None, None)
};

Ok(Layout {
deps: dest.join("deps"),
build: dest.join("build"),
Expand All @@ -176,6 +191,8 @@ impl Layout {
doc: root.join("doc"),
root,
dest,
sysroot,
sysroot_libdir,
_lock: lock,
})
}
Expand Down Expand Up @@ -223,6 +240,13 @@ impl Layout {
pub fn build(&self) -> &Path {
&self.build
}
/// TODO
pub fn sysroot(&self) -> Option<&Path> {
self.sysroot.as_ref().map(|p| p.as_ref())
}
pub fn sysroot_libdir(&self) -> Option<&Path> {
self.sysroot_libdir.as_ref().map(|p| p.as_ref())
}
}

#[cfg(not(target_os = "macos"))]
Expand Down
17 changes: 16 additions & 1 deletion src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use crate::util::paths;
use crate::util::{self, machine_message, ProcessBuilder};
use crate::util::{internal, join_paths, profile};

/// Indicates whether an object is for the host architcture or the target architecture.
/// Indicates whether an object is for the host architecture or the target architecture.
///
/// These will be the same unless cross-compiling.
#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy, PartialOrd, Ord, Serialize)]
Expand Down Expand Up @@ -915,6 +915,11 @@ fn build_base_args<'a, 'cfg>(
let dir = cx.files().layout(unit.kind).incremental().as_os_str();
opt(cmd, "-C", "incremental=", Some(dir));
}

if unit.is_std {
cmd.arg("-Zforce-unstable-if-unmarked")
.env("RUSTC_BOOTSTRAP", "1");
}
Ok(())
}

Expand Down Expand Up @@ -968,7 +973,17 @@ fn build_deps_args<'a, 'cfg>(

let mut unstable_opts = false;

if let Some(sysroot) = cx.files().layout(Kind::Target).sysroot() {
if unit.kind == Kind::Target {
cmd.arg("--sysroot").arg(sysroot);
}
}

for dep in deps {
if !unit.is_std && dep.unit.is_std {
// Dependency to sysroot crate uses --sysroot.
continue;
}
if dep.unit.mode.is_run_custom_build() {
cmd.env("OUT_DIR", &cx.files().build_script_out_dir(&dep.unit));
}
Expand Down
44 changes: 40 additions & 4 deletions src/cargo/core/compiler/standard_lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! Code for building the standard library.

use crate::core::compiler::{BuildContext, CompileMode, Kind, Unit};
use super::layout::Layout;
use crate::core::compiler::{BuildContext, CompileMode, Context, FileFlavor, Kind, Unit};
use crate::core::profiles::UnitFor;
use crate::core::resolver::ResolveOpts;
use crate::core::{Dependency, PackageId, PackageSet, Resolve, SourceId, Workspace};
use crate::ops::{self, Packages};
use crate::util::errors::CargoResult;
use crate::util::paths;
use std::collections::{HashMap, HashSet};
use std::env;
use std::path::PathBuf;
Expand Down Expand Up @@ -141,9 +143,15 @@ pub fn generate_std_roots<'a>(
bcx.build_config.release,
);
let features = std_resolve.features_sorted(pkg.package_id());
Ok(bcx
.units
.intern(pkg, lib, profile, Kind::Target, mode, features))
Ok(bcx.units.intern(
pkg,
lib,
profile,
Kind::Target,
mode,
features,
/*is_std*/ true,
))
})
.collect::<CargoResult<Vec<_>>>()
}
Expand Down Expand Up @@ -173,3 +181,31 @@ fn detect_sysroot_src_path(ws: &Workspace<'_>) -> CargoResult<PathBuf> {
}
Ok(src_path)
}

pub fn prepare_sysroot(layout: &Layout) -> CargoResult<()> {
if let Some(libdir) = layout.sysroot_libdir() {
if libdir.exists() {
paths::remove_dir_all(libdir)?;
}
paths::create_dir_all(libdir)?;
}
Ok(())
}

pub fn add_sysroot_artifact<'a>(
cx: &Context<'a, '_>,
unit: &Unit<'a>,
rmeta: bool,
) -> CargoResult<()> {
let outputs = cx.outputs(unit)?;
let outputs = outputs
.iter()
.filter(|output| output.flavor == FileFlavor::Linkable { rmeta })
.map(|output| &output.path);
for path in outputs {
let libdir = cx.files().layout(Kind::Target).sysroot_libdir().unwrap();
let dst = libdir.join(path.file_name().unwrap());
paths::link_or_copy(path, dst)?;
}
Ok(())
}
4 changes: 4 additions & 0 deletions src/cargo/core/compiler/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pub struct UnitInner<'a> {
/// The `cfg` features to enable for this unit.
/// This must be sorted.
pub features: Vec<&'a str>,
/// Whether this is a standard library unit.
pub is_std: bool,
}

impl UnitInner<'_> {
Expand Down Expand Up @@ -144,6 +146,7 @@ impl<'a> UnitInterner<'a> {
kind: Kind,
mode: CompileMode,
features: Vec<&'a str>,
is_std: bool,
) -> Unit<'a> {
let inner = self.intern_inner(&UnitInner {
pkg,
Expand All @@ -152,6 +155,7 @@ impl<'a> UnitInterner<'a> {
kind,
mode,
features,
is_std,
});
Unit { inner }
}
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/unit_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ fn new_unit_dep_with_profile<'a>(
let unit = state
.bcx
.units
.intern(pkg, target, profile, kind, mode, features);
.intern(pkg, target, profile, kind, mode, features, state.is_std);
Ok(UnitDep {
unit,
unit_for,
Expand Down
7 changes: 3 additions & 4 deletions src/cargo/ops/cargo_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,9 @@ pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {
)
};
let features = resolve.features_sorted(pkg.package_id());
units.push(
bcx.units
.intern(pkg, target, profile, *kind, *mode, features),
);
units.push(bcx.units.intern(
pkg, target, profile, *kind, *mode, features, /*is_std*/ false,
));
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,15 @@ fn generate_targets<'a>(
bcx.build_config.release,
);
let features = resolve.features_sorted(pkg.package_id());
bcx.units
.intern(pkg, target, profile, kind, target_mode, features)
bcx.units.intern(
pkg,
target,
profile,
kind,
target_mode,
features,
/*is_std*/ false,
)
};

// Create a list of proposed targets.
Expand Down

0 comments on commit 1ed8760

Please sign in to comment.