Skip to content

Commit 4ec2d81

Browse files
committed
wip: Document heuristics and disable them for libdir, if cross compiling
1 parent c23cb4a commit 4ec2d81

File tree

3 files changed

+46
-31
lines changed

3 files changed

+46
-31
lines changed

src/build.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1043,15 +1043,15 @@ pub fn cbuild(
10431043
) -> anyhow::Result<(Vec<CPackage>, CompileOptions)> {
10441044
let rustc = config.load_global_rustc(Some(ws))?;
10451045
let targets = args.targets()?;
1046-
let target = match targets.len() {
1047-
0 => rustc.host.to_string(),
1048-
1 => targets[0].to_string(),
1046+
let (target, is_target_overridden) = match targets.len() {
1047+
0 => (rustc.host.to_string(), false),
1048+
1 => (targets[0].to_string(), true),
10491049
_ => {
10501050
anyhow::bail!("Multiple targets not supported yet");
10511051
}
10521052
};
10531053

1054-
let rustc_target = target::Target::new(&target)?;
1054+
let rustc_target = target::Target::new(&target, is_target_overridden)?;
10551055

10561056
let default_kind = || match (rustc_target.os.as_str(), rustc_target.env.as_str()) {
10571057
("none", _) | (_, "musl") => vec!["staticlib"],

src/cli.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,35 @@ struct Common {
2020
/// `bindir`, `datarootdir`, `includedir`, `libdir`
2121
///
2222
/// If they are absolute the prefix is ignored.
23+
///
24+
/// [default = c:/ if Windows, /boot/system/non-packaged if Haiku, else /usr/local]
2325
#[clap(long = "prefix")]
24-
prefix: PathBuf,
26+
prefix: Option<PathBuf>,
2527
/// Path to directory for installing generated library files
28+
///
29+
/// [default = {prefix}/bin if Windows, {prefix}/lib/{target} if Debian and target is not set manually, else {prefix}/lib]
2630
#[clap(long = "libdir")]
27-
libdir: PathBuf,
31+
libdir: Option<PathBuf>,
2832
/// Path to directory for installing generated headers files
33+
///
34+
/// [default = {prefix}/include]
2935
#[clap(long = "includedir")]
30-
includedir: PathBuf,
36+
includedir: Option<PathBuf>,
3137
/// Path to directory for installing generated executable files
32-
#[clap(long = "bindir", default_value = "bin")]
33-
bindir: PathBuf,
38+
///
39+
/// [default = {prefix}/bin]
40+
#[clap(long = "bindir")]
41+
bindir: Option<PathBuf>,
3442
/// Path to directory for installing generated pkg-config .pc files
3543
///
3644
/// [default: {libdir}/pkgconfig]
3745
#[clap(long = "pkgconfigdir")]
3846
pkgconfigdir: Option<PathBuf>,
3947
/// Path to directory for installing read-only data
48+
///
49+
/// [default = {prefix}/data if Haiku, else {prefix}/share]
4050
#[clap(long = "datarootdir")]
41-
datarootdir: PathBuf,
51+
datarootdir: Option<PathBuf>,
4252
/// Path to directory for installing read-only application-specific data
4353
///
4454
/// [default: {datarootdir}]

src/target.rs

+26-21
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ use crate::build::CApiConfig;
1111
/// It uses internally `rustc` to validate the string.
1212
#[derive(Clone, Debug)]
1313
pub struct Target {
14+
pub is_target_overridden: bool,
1415
pub arch: String,
1516
// pub vendor: String,
1617
pub os: String,
1718
pub env: String,
1819
}
1920

2021
impl Target {
21-
pub fn new<T: AsRef<std::ffi::OsStr>>(target: T) -> Result<Self, anyhow::Error> {
22+
pub fn new<T: AsRef<std::ffi::OsStr>>(target: T, is_target_overridden: bool) -> Result<Self, anyhow::Error> {
2223
let rustc = std::env::var("RUSTC").unwrap_or_else(|_| "rustc".into());
2324
let mut cmd = std::process::Command::new(rustc);
2425

@@ -45,6 +46,7 @@ impl Target {
4546
// vendor: match_re(vendor_re, s),
4647
os: match_re(os_re, s),
4748
env: match_re(env_re, s),
49+
is_target_overridden,
4850
})
4951
} else {
5052
Err(anyhow!("Cannot run {:?}", cmd))
@@ -134,32 +136,35 @@ impl Target {
134136
}
135137

136138
pub fn default_libdir(&self) -> PathBuf {
137-
if self.is_debianlike() {
138-
let pc = std::process::Command::new("dpkg-architecture")
139-
.arg("-qDEB_HOST_MULTIARCH")
140-
.output();
141-
match pc {
142-
std::io::Result::Ok(v) => {
143-
if v.status.success() {
144-
let archpath = String::from_utf8_lossy(&v.stdout);
145-
return format!("lib/{archpath}").into();
139+
if self.is_target_overridden {
140+
if self.is_debianlike() {
141+
let pc = std::process::Command::new("dpkg-architecture")
142+
.arg("-qDEB_HOST_MULTIARCH")
143+
.output();
144+
match pc {
145+
std::io::Result::Ok(v) => {
146+
if v.status.success() {
147+
let archpath = String::from_utf8_lossy(&v.stdout);
148+
return format!("lib/{archpath}").into();
149+
}
146150
}
151+
std::io::Result::Err(_) => {}
147152
}
148-
std::io::Result::Err(_) => {}
149153
}
150-
}
151154

152-
if self.is_freebsd() {
153-
return "lib".into();
154-
}
155-
if consts::ARCH.eq_ignore_ascii_case(&self.arch)
156-
&& consts::OS.eq_ignore_ascii_case(&self.os)
157-
{
158-
let usr_lib64 = PathBuf::from("/usr/lib64");
159-
if usr_lib64.exists() && !usr_lib64.is_symlink() {
160-
return "lib64".into();
155+
if self.is_freebsd() {
156+
return "lib".into();
157+
}
158+
if consts::ARCH.eq_ignore_ascii_case(&self.arch)
159+
&& consts::OS.eq_ignore_ascii_case(&self.os)
160+
{
161+
let usr_lib64 = PathBuf::from("/usr/lib64");
162+
if usr_lib64.exists() && !usr_lib64.is_symlink() {
163+
return "lib64".into();
164+
}
161165
}
162166
}
167+
163168
"lib".into()
164169
}
165170

0 commit comments

Comments
 (0)