Skip to content

Commit 16f8372

Browse files
committed
rustbuild: Implement DESTDIR support
This commit primarily starts supporting the `DESTDIR` environment variable like the old build system. Along the way this brings `config.toml` up to date with support in `config.mk` with install options supported. Closes rust-lang#38441
1 parent 0807104 commit 16f8372

File tree

4 files changed

+65
-24
lines changed

4 files changed

+65
-24
lines changed

src/bootstrap/compile.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ pub fn rustc<'a>(build: &'a Build, target: &str, compiler: &Compiler<'a>) {
187187
cargo.env("CFG_RELEASE", &build.release)
188188
.env("CFG_RELEASE_CHANNEL", &build.config.channel)
189189
.env("CFG_VERSION", &build.version)
190-
.env("CFG_PREFIX", build.config.prefix.clone().unwrap_or(String::new()))
190+
.env("CFG_PREFIX", build.config.prefix.clone().unwrap_or(PathBuf::new()))
191191
.env("CFG_LIBDIR_RELATIVE", "lib");
192192

193193
if let Some(ref ver_date) = build.ver_date {

src/bootstrap/config.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ pub struct Config {
8484
pub quiet_tests: bool,
8585
// Fallback musl-root for all targets
8686
pub musl_root: Option<PathBuf>,
87-
pub prefix: Option<String>,
88-
pub docdir: Option<String>,
89-
pub libdir: Option<String>,
90-
pub mandir: Option<String>,
87+
pub prefix: Option<PathBuf>,
88+
pub docdir: Option<PathBuf>,
89+
pub libdir: Option<PathBuf>,
90+
pub mandir: Option<PathBuf>,
9191
pub codegen_tests: bool,
9292
pub nodejs: Option<PathBuf>,
9393
pub gdb: Option<PathBuf>,
@@ -140,6 +140,9 @@ struct Build {
140140
#[derive(RustcDecodable, Default, Clone)]
141141
struct Install {
142142
prefix: Option<String>,
143+
mandir: Option<String>,
144+
docdir: Option<String>,
145+
libdir: Option<String>,
143146
}
144147

145148
/// TOML representation of how the LLVM build is configured.
@@ -266,7 +269,10 @@ impl Config {
266269
set(&mut config.vendor, build.vendor);
267270

268271
if let Some(ref install) = toml.install {
269-
config.prefix = install.prefix.clone();
272+
config.prefix = install.prefix.clone().map(PathBuf::from);
273+
config.mandir = install.mandir.clone().map(PathBuf::from);
274+
config.docdir = install.docdir.clone().map(PathBuf::from);
275+
config.libdir = install.libdir.clone().map(PathBuf::from);
270276
}
271277

272278
if let Some(ref llvm) = toml.llvm {
@@ -451,16 +457,16 @@ impl Config {
451457
self.channel = value.to_string();
452458
}
453459
"CFG_PREFIX" => {
454-
self.prefix = Some(value.to_string());
460+
self.prefix = Some(PathBuf::from(value));
455461
}
456462
"CFG_DOCDIR" => {
457-
self.docdir = Some(value.to_string());
463+
self.docdir = Some(PathBuf::from(value));
458464
}
459465
"CFG_LIBDIR" => {
460-
self.libdir = Some(value.to_string());
466+
self.libdir = Some(PathBuf::from(value));
461467
}
462468
"CFG_MANDIR" => {
463-
self.mandir = Some(value.to_string());
469+
self.mandir = Some(PathBuf::from(value));
464470
}
465471
"CFG_LLVM_ROOT" if value.len() > 0 => {
466472
let target = self.target_config.entry(self.build.clone())

src/bootstrap/config.toml.example

+10-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,16 @@
106106
[install]
107107

108108
# Instead of installing to /usr/local, install to this path instead.
109-
#prefix = "/path/to/install"
109+
#prefix = "/usr/local"
110+
111+
# Where to install libraries in `prefix` above
112+
#libdir = "lib"
113+
114+
# Where to install man pages in `prefix` above
115+
#mandir = "share/man"
116+
117+
# Where to install documentation in `prefix` above
118+
#docdir = "share/doc/rust"
110119

111120
# =============================================================================
112121
# Options for compiling Rust code itself

src/bootstrap/install.rs

+39-13
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,45 @@
1313
//! This module is responsible for installing the standard library,
1414
//! compiler, and documentation.
1515
16+
use std::env;
1617
use std::fs;
17-
use std::borrow::Cow;
18-
use std::path::Path;
18+
use std::path::{Path, PathBuf, Component};
1919
use std::process::Command;
2020

2121
use Build;
2222
use dist::{package_vers, sanitize_sh, tmpdir};
2323

2424
/// Installs everything.
2525
pub fn install(build: &Build, stage: u32, host: &str) {
26-
let prefix = build.config.prefix.as_ref().clone().map(|x| Path::new(x))
27-
.unwrap_or(Path::new("/usr/local"));
28-
let docdir = build.config.docdir.as_ref().clone().map(|x| Cow::Borrowed(Path::new(x)))
29-
.unwrap_or(Cow::Owned(prefix.join("share/doc/rust")));
30-
let libdir = build.config.libdir.as_ref().clone().map(|x| Cow::Borrowed(Path::new(x)))
31-
.unwrap_or(Cow::Owned(prefix.join("lib")));
32-
let mandir = build.config.mandir.as_ref().clone().map(|x| Cow::Borrowed(Path::new(x)))
33-
.unwrap_or(Cow::Owned(prefix.join("share/man")));
26+
let prefix_default = PathBuf::from("/usr/local");
27+
let docdir_default = PathBuf::from("share/doc/rust");
28+
let mandir_default = PathBuf::from("share/man");
29+
let libdir_default = PathBuf::from("lib");
30+
let prefix = build.config.prefix.as_ref().unwrap_or(&prefix_default);
31+
let docdir = build.config.docdir.as_ref().unwrap_or(&docdir_default);
32+
let libdir = build.config.libdir.as_ref().unwrap_or(&libdir_default);
33+
let mandir = build.config.mandir.as_ref().unwrap_or(&mandir_default);
34+
35+
let docdir = prefix.join(docdir);
36+
let libdir = prefix.join(libdir);
37+
let mandir = prefix.join(mandir);
38+
39+
let destdir = env::var_os("DESTDIR").map(PathBuf::from);
40+
41+
let prefix = add_destdir(&prefix, &destdir);
42+
let docdir = add_destdir(&docdir, &destdir);
43+
let libdir = add_destdir(&libdir, &destdir);
44+
let mandir = add_destdir(&mandir, &destdir);
45+
3446
let empty_dir = build.out.join("tmp/empty_dir");
3547
t!(fs::create_dir_all(&empty_dir));
3648
if build.config.docs {
37-
install_sh(&build, "docs", "rust-docs", stage, host, prefix,
49+
install_sh(&build, "docs", "rust-docs", stage, host, &prefix,
3850
&docdir, &libdir, &mandir, &empty_dir);
3951
}
40-
install_sh(&build, "std", "rust-std", stage, host, prefix,
52+
install_sh(&build, "std", "rust-std", stage, host, &prefix,
4153
&docdir, &libdir, &mandir, &empty_dir);
42-
install_sh(&build, "rustc", "rustc", stage, host, prefix,
54+
install_sh(&build, "rustc", "rustc", stage, host, &prefix,
4355
&docdir, &libdir, &mandir, &empty_dir);
4456
t!(fs::remove_dir_all(&empty_dir));
4557
}
@@ -59,3 +71,17 @@ fn install_sh(build: &Build, package: &str, name: &str, stage: u32, host: &str,
5971
.arg("--disable-ldconfig");
6072
build.run(&mut cmd);
6173
}
74+
75+
fn add_destdir(path: &Path, destdir: &Option<PathBuf>) -> PathBuf {
76+
let mut ret = match *destdir {
77+
Some(ref dest) => dest.clone(),
78+
None => return path.to_path_buf(),
79+
};
80+
for part in path.components() {
81+
match part {
82+
Component::Normal(s) => ret.push(s),
83+
_ => {}
84+
}
85+
}
86+
return ret
87+
}

0 commit comments

Comments
 (0)