Skip to content

Commit 139d741

Browse files
committed
Auto merge of rust-lang#38654 - alexcrichton:rustbuild-destdir, r=brson
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
2 parents 408c2f7 + 16f8372 commit 139d741

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
@@ -186,7 +186,7 @@ pub fn rustc(build: &Build, target: &str, compiler: &Compiler) {
186186
cargo.env("CFG_RELEASE", &build.release)
187187
.env("CFG_RELEASE_CHANNEL", &build.config.channel)
188188
.env("CFG_VERSION", &build.version)
189-
.env("CFG_PREFIX", build.config.prefix.clone().unwrap_or(String::new()))
189+
.env("CFG_PREFIX", build.config.prefix.clone().unwrap_or(PathBuf::new()))
190190
.env("CFG_LIBDIR_RELATIVE", "lib");
191191

192192
// If we're not building a compiler with debugging information then remove

src/bootstrap/config.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ pub struct Config {
8787
pub quiet_tests: bool,
8888
// Fallback musl-root for all targets
8989
pub musl_root: Option<PathBuf>,
90-
pub prefix: Option<String>,
91-
pub docdir: Option<String>,
92-
pub libdir: Option<String>,
93-
pub mandir: Option<String>,
90+
pub prefix: Option<PathBuf>,
91+
pub docdir: Option<PathBuf>,
92+
pub libdir: Option<PathBuf>,
93+
pub mandir: Option<PathBuf>,
9494
pub codegen_tests: bool,
9595
pub nodejs: Option<PathBuf>,
9696
pub gdb: Option<PathBuf>,
@@ -145,6 +145,9 @@ struct Build {
145145
#[derive(RustcDecodable, Default, Clone)]
146146
struct Install {
147147
prefix: Option<String>,
148+
mandir: Option<String>,
149+
docdir: Option<String>,
150+
libdir: Option<String>,
148151
}
149152

150153
/// TOML representation of how the LLVM build is configured.
@@ -274,7 +277,10 @@ impl Config {
274277
set(&mut config.full_bootstrap, build.full_bootstrap);
275278

276279
if let Some(ref install) = toml.install {
277-
config.prefix = install.prefix.clone();
280+
config.prefix = install.prefix.clone().map(PathBuf::from);
281+
config.mandir = install.mandir.clone().map(PathBuf::from);
282+
config.docdir = install.docdir.clone().map(PathBuf::from);
283+
config.libdir = install.libdir.clone().map(PathBuf::from);
278284
}
279285

280286
if let Some(ref llvm) = toml.llvm {
@@ -463,16 +469,16 @@ impl Config {
463469
self.channel = value.to_string();
464470
}
465471
"CFG_PREFIX" => {
466-
self.prefix = Some(value.to_string());
472+
self.prefix = Some(PathBuf::from(value));
467473
}
468474
"CFG_DOCDIR" => {
469-
self.docdir = Some(value.to_string());
475+
self.docdir = Some(PathBuf::from(value));
470476
}
471477
"CFG_LIBDIR" => {
472-
self.libdir = Some(value.to_string());
478+
self.libdir = Some(PathBuf::from(value));
473479
}
474480
"CFG_MANDIR" => {
475-
self.mandir = Some(value.to_string());
481+
self.mandir = Some(PathBuf::from(value));
476482
}
477483
"CFG_LLVM_ROOT" if value.len() > 0 => {
478484
let target = self.target_config.entry(self.build.clone())

src/bootstrap/config.toml.example

+10-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,16 @@
124124
[install]
125125

126126
# Instead of installing to /usr/local, install to this path instead.
127-
#prefix = "/path/to/install"
127+
#prefix = "/usr/local"
128+
129+
# Where to install libraries in `prefix` above
130+
#libdir = "lib"
131+
132+
# Where to install man pages in `prefix` above
133+
#mandir = "share/man"
134+
135+
# Where to install documentation in `prefix` above
136+
#docdir = "share/doc/rust"
128137

129138
# =============================================================================
130139
# 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)