2024-01-12 13:12:38 +00:00
|
|
|
#![allow(clippy::uninlined_format_args)]
|
|
|
|
|
2023-05-16 18:02:22 +00:00
|
|
|
use rsconf::{LinkType, Target};
|
2023-08-18 04:57:56 +00:00
|
|
|
use std::env;
|
2023-05-25 15:19:07 +00:00
|
|
|
use std::error::Error;
|
2024-01-03 19:02:32 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2023-05-25 15:19:07 +00:00
|
|
|
|
|
|
|
fn main() {
|
2024-01-15 19:32:32 +00:00
|
|
|
setup_paths();
|
2023-08-18 04:57:56 +00:00
|
|
|
|
2024-01-12 11:50:58 +00:00
|
|
|
let rust_dir = env!("CARGO_MANIFEST_DIR");
|
|
|
|
// Add our default to enable tools that don't go through CMake, like "cargo test" and the
|
|
|
|
// language server.
|
2024-01-19 21:31:00 +00:00
|
|
|
let build_dir = std::env::var("FISH_BUILD_DIR").unwrap_or(format!("{rust_dir}/build"));
|
|
|
|
rsconf::set_env_value("FISH_BUILD_DIR", &build_dir);
|
2024-01-03 19:02:32 +00:00
|
|
|
|
2023-08-18 04:53:20 +00:00
|
|
|
cc::Build::new()
|
2024-01-13 02:45:58 +00:00
|
|
|
.file("src/libc.c")
|
2024-01-03 19:02:32 +00:00
|
|
|
.include(&build_dir)
|
2024-01-07 18:18:15 +00:00
|
|
|
.compile("flibc.a");
|
2023-03-26 15:23:05 +00:00
|
|
|
|
2023-05-16 18:02:22 +00:00
|
|
|
let mut build = cc::Build::new();
|
|
|
|
// Add to the default library search path
|
|
|
|
build.flag_if_supported("-L/usr/local/lib/");
|
|
|
|
rsconf::add_library_search_path("/usr/local/lib");
|
2024-01-19 22:02:31 +00:00
|
|
|
let mut target = Target::new_from(build).unwrap();
|
2023-05-16 18:02:22 +00:00
|
|
|
// Keep verbose mode on until we've ironed out rust build script stuff
|
2024-01-19 22:02:31 +00:00
|
|
|
target.set_verbose(true);
|
|
|
|
detect_cfgs(&mut target);
|
|
|
|
|
|
|
|
// Handle case where CMake has found curses for us and where we have to find it ourselves.
|
|
|
|
rsconf::rebuild_if_env_changed("CURSES_LIBRARY_LIST");
|
2024-01-21 10:12:03 +00:00
|
|
|
let curses_libraries = if let Ok(lib_path_list) = env::var("CURSES_LIBRARY_LIST") {
|
2024-01-19 22:02:31 +00:00
|
|
|
let lib_paths = lib_path_list.split(',').filter(|s| !s.is_empty());
|
|
|
|
let curses_libnames: Vec<_> = lib_paths
|
|
|
|
.map(|libpath| {
|
|
|
|
let stem = Path::new(libpath).file_stem().unwrap().to_str().unwrap();
|
|
|
|
// Ubuntu-32bit-fetched-pcre2's ncurses doesn't have the "lib" prefix.
|
2024-01-21 10:12:03 +00:00
|
|
|
stem.strip_prefix("lib").unwrap_or(stem).to_string()
|
2024-01-19 22:02:31 +00:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
// We don't need to test the libs because presumably CMake already did
|
|
|
|
rsconf::link_libraries(&curses_libnames, LinkType::Default);
|
2024-01-21 10:12:03 +00:00
|
|
|
curses_libnames
|
2024-01-19 22:02:31 +00:00
|
|
|
} else {
|
2024-01-21 10:12:03 +00:00
|
|
|
let mut curses_libraries = vec![];
|
|
|
|
let libs = ["ncurses", "curses"];
|
|
|
|
for lib in libs {
|
2024-01-19 22:02:31 +00:00
|
|
|
if target.has_library(lib) && target.has_symbol("setupterm", lib) {
|
|
|
|
rsconf::link_library(lib, LinkType::Default);
|
2024-01-21 10:12:03 +00:00
|
|
|
curses_libraries.push(lib.to_string());
|
2024-01-19 22:02:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-21 10:12:03 +00:00
|
|
|
if curses_libraries.is_empty() {
|
|
|
|
panic!("Could not locate a compatible curses library (tried {libs:?})");
|
|
|
|
}
|
|
|
|
curses_libraries
|
|
|
|
};
|
|
|
|
|
|
|
|
for lib in curses_libraries {
|
|
|
|
if target.has_symbol("_nc_cur_term", &lib) {
|
|
|
|
rsconf::enable_cfg("_nc_cur_term");
|
|
|
|
if target.has_symbol("cur_term", &lib) {
|
|
|
|
rsconf::warn!("curses provides both cur_term and _nc_cur_term");
|
|
|
|
}
|
|
|
|
break;
|
2024-01-19 22:02:31 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-14 22:56:24 +00:00
|
|
|
}
|
2023-03-19 22:50:32 +00:00
|
|
|
|
2024-01-13 21:16:47 +00:00
|
|
|
/// Check target system support for certain functionality dynamically when the build is invoked,
|
|
|
|
/// without their having to be explicitly enabled in the `cargo build --features xxx` invocation.
|
|
|
|
///
|
|
|
|
/// We are using [`rsconf::enable_cfg()`] instead of [`rsconf::enable_feature()`] as rust features
|
|
|
|
/// should be used for things that a user can/would reasonably enable or disable to tweak or coerce
|
|
|
|
/// behavior, but here we are testing for whether or not things are supported altogether.
|
2023-03-19 22:50:32 +00:00
|
|
|
///
|
|
|
|
/// This can be used to enable features that we check for and conditionally compile according to in
|
|
|
|
/// our own codebase, but [can't be used to pull in dependencies](0) even if they're gated (in
|
|
|
|
/// `Cargo.toml`) behind a feature we just enabled.
|
|
|
|
///
|
|
|
|
/// [0]: https://github.com/rust-lang/cargo/issues/5499
|
2024-01-19 08:17:55 +00:00
|
|
|
#[rustfmt::skip]
|
2024-01-19 22:02:31 +00:00
|
|
|
fn detect_cfgs(target: &mut Target) {
|
2024-01-13 21:16:47 +00:00
|
|
|
for (name, handler) in [
|
2023-05-16 18:02:22 +00:00
|
|
|
// Ignore the first entry, it just sets up the type inference. Model new entries after the
|
2023-03-19 23:11:09 +00:00
|
|
|
// second line.
|
2023-05-25 15:19:07 +00:00
|
|
|
(
|
|
|
|
"",
|
2023-05-16 18:02:22 +00:00
|
|
|
&(|_: &Target| Ok(false)) as &dyn Fn(&Target) -> Result<bool, Box<dyn Error>>,
|
2023-05-25 15:19:07 +00:00
|
|
|
),
|
2023-03-19 23:11:09 +00:00
|
|
|
("bsd", &detect_bsd),
|
2023-05-16 18:02:22 +00:00
|
|
|
("gettext", &have_gettext),
|
2024-01-19 00:10:47 +00:00
|
|
|
// See if libc supports the thread-safe localeconv_l(3) alternative to localeconv(3).
|
2024-01-13 22:04:05 +00:00
|
|
|
("localeconv_l", &|target| {
|
2024-01-19 00:22:07 +00:00
|
|
|
Ok(target.has_symbol("localeconv_l", None))
|
2024-01-13 22:04:05 +00:00
|
|
|
}),
|
|
|
|
("FISH_USE_POSIX_SPAWN", &|target| {
|
|
|
|
Ok(target.has_header("spawn.h"))
|
|
|
|
}),
|
|
|
|
("HAVE_PIPE2", &|target| {
|
2024-01-19 00:22:07 +00:00
|
|
|
Ok(target.has_symbol("pipe2", None))
|
2024-01-13 22:04:05 +00:00
|
|
|
}),
|
|
|
|
("HAVE_EVENTFD", &|target| {
|
|
|
|
Ok(target.has_header("sys/eventfd.h"))
|
|
|
|
}),
|
|
|
|
("HAVE_WAITSTATUS_SIGNAL_RET", &|target| {
|
|
|
|
Ok(target.r#if("WEXITSTATUS(0x007f) == 0x7f", "sys/wait.h"))
|
|
|
|
}),
|
2023-03-19 23:11:09 +00:00
|
|
|
] {
|
2024-01-20 09:26:02 +00:00
|
|
|
match handler(target) {
|
2024-01-13 21:16:47 +00:00
|
|
|
Err(e) => rsconf::warn!("{}: {}", name, e),
|
|
|
|
Ok(true) => rsconf::enable_cfg(name),
|
2023-03-19 22:50:32 +00:00
|
|
|
Ok(false) => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 23:38:52 +00:00
|
|
|
/// Detect if we're being compiled for a BSD-derived OS, allowing targeting code conditionally with
|
2024-01-19 00:10:47 +00:00
|
|
|
/// `#[cfg(bsd)]`.
|
2023-03-19 22:50:32 +00:00
|
|
|
///
|
|
|
|
/// Rust offers fine-grained conditional compilation per-os for the popular operating systems, but
|
|
|
|
/// doesn't necessarily include less-popular forks nor does it group them into families more
|
|
|
|
/// specific than "windows" vs "unix" so we can conditionally compile code for BSD systems.
|
2023-05-16 18:02:22 +00:00
|
|
|
fn detect_bsd(_: &Target) -> Result<bool, Box<dyn Error>> {
|
2023-05-05 23:38:52 +00:00
|
|
|
// Instead of using `uname`, we can inspect the TARGET env variable set by Cargo. This lets us
|
|
|
|
// support cross-compilation scenarios.
|
|
|
|
let mut target = std::env::var("TARGET").unwrap();
|
|
|
|
if !target.chars().all(|c| c.is_ascii_lowercase()) {
|
|
|
|
target = target.to_ascii_lowercase();
|
|
|
|
}
|
|
|
|
let result = target.ends_with("bsd") || target.ends_with("dragonfly");
|
|
|
|
#[cfg(any(
|
|
|
|
target_os = "dragonfly",
|
|
|
|
target_os = "freebsd",
|
|
|
|
target_os = "netbsd",
|
|
|
|
target_os = "openbsd",
|
|
|
|
))]
|
|
|
|
assert!(result, "Target incorrectly detected as not BSD!");
|
|
|
|
Ok(result)
|
2023-03-19 22:50:32 +00:00
|
|
|
}
|
2023-05-16 18:02:22 +00:00
|
|
|
|
|
|
|
/// Detect libintl/gettext and its needed symbols to enable internationalization/localization
|
|
|
|
/// support.
|
|
|
|
fn have_gettext(target: &Target) -> Result<bool, Box<dyn Error>> {
|
|
|
|
// The following script correctly detects and links against gettext, but so long as we are using
|
|
|
|
// C++ and generate a static library linked into the C++ binary via CMake, we need to account
|
|
|
|
// for the CMake option WITH_GETTEXT being explicitly disabled.
|
|
|
|
rsconf::rebuild_if_env_changed("CMAKE_WITH_GETTEXT");
|
|
|
|
if let Some(with_gettext) = std::env::var_os("CMAKE_WITH_GETTEXT") {
|
|
|
|
if with_gettext.eq_ignore_ascii_case("0") {
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// In order for fish to correctly operate, we need some way of notifying libintl to invalidate
|
|
|
|
// its localizations when the locale environment variables are modified. Without the libintl
|
|
|
|
// symbol _nl_msg_cat_cntr, we cannot use gettext even if we find it.
|
|
|
|
let mut libraries = Vec::new();
|
|
|
|
let mut found = 0;
|
|
|
|
let symbols = ["gettext", "_nl_msg_cat_cntr"];
|
|
|
|
for symbol in &symbols {
|
|
|
|
// Historically, libintl was required in order to use gettext() and co, but that
|
|
|
|
// functionality was subsumed by some versions of libc.
|
2024-01-19 21:31:00 +00:00
|
|
|
if target.has_symbol(symbol, None) {
|
2023-05-16 18:02:22 +00:00
|
|
|
// No need to link anything special for this symbol
|
|
|
|
found += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for library in ["intl", "gettextlib"] {
|
|
|
|
if target.has_symbol(symbol, library) {
|
|
|
|
libraries.push(library);
|
|
|
|
found += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match found {
|
|
|
|
0 => Ok(false),
|
|
|
|
1 => Err(format!("gettext found but cannot be used without {}", symbols[1]).into()),
|
|
|
|
_ => {
|
|
|
|
rsconf::link_libraries(&libraries, LinkType::Default);
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-15 19:32:32 +00:00
|
|
|
|
2024-01-19 21:31:00 +00:00
|
|
|
fn setup_paths() {
|
|
|
|
fn get_path(name: &str, default: &str, onvar: PathBuf) -> PathBuf {
|
|
|
|
let mut var = PathBuf::from(env::var(name).unwrap_or(default.to_string()));
|
|
|
|
if var.is_relative() {
|
|
|
|
var = onvar.join(var);
|
|
|
|
}
|
|
|
|
var
|
2024-01-15 19:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let prefix = PathBuf::from(env::var("PREFIX").unwrap_or("/usr/local".to_string()));
|
|
|
|
if prefix.is_relative() {
|
|
|
|
panic!("Can't have relative prefix");
|
|
|
|
}
|
2024-01-19 21:31:00 +00:00
|
|
|
rsconf::rebuild_if_env_changed("PREFIX");
|
|
|
|
rsconf::set_env_value("PREFIX", prefix.to_str().unwrap());
|
2024-01-15 19:32:32 +00:00
|
|
|
|
|
|
|
let datadir = get_path("DATADIR", "share/", prefix.clone());
|
2024-01-19 21:31:00 +00:00
|
|
|
rsconf::set_env_value("DATADIR", datadir.to_str().unwrap());
|
|
|
|
rsconf::rebuild_if_env_changed("DATADIR");
|
|
|
|
|
2024-01-15 19:32:32 +00:00
|
|
|
let bindir = get_path("BINDIR", "bin/", prefix.clone());
|
2024-01-19 21:31:00 +00:00
|
|
|
rsconf::set_env_value("BINDIR", bindir.to_str().unwrap());
|
|
|
|
rsconf::rebuild_if_env_changed("BINDIR");
|
|
|
|
|
2024-01-15 19:32:32 +00:00
|
|
|
let sysconfdir = get_path("SYSCONFDIR", "etc/", datadir.clone());
|
2024-01-19 21:31:00 +00:00
|
|
|
rsconf::set_env_value("SYSCONFDIR", sysconfdir.to_str().unwrap());
|
|
|
|
rsconf::rebuild_if_env_changed("SYSCONFDIR");
|
|
|
|
|
2024-01-15 19:32:32 +00:00
|
|
|
let localedir = get_path("LOCALEDIR", "locale/", datadir.clone());
|
2024-01-19 21:31:00 +00:00
|
|
|
rsconf::set_env_value("LOCALEDIR", localedir.to_str().unwrap());
|
|
|
|
rsconf::rebuild_if_env_changed("LOCALEDIR");
|
|
|
|
|
2024-01-15 19:32:32 +00:00
|
|
|
let docdir = get_path("DOCDIR", "doc/fish", datadir.clone());
|
2024-01-19 21:31:00 +00:00
|
|
|
rsconf::set_env_value("DOCDIR", docdir.to_str().unwrap());
|
|
|
|
rsconf::rebuild_if_env_changed("DOCDIR");
|
2024-01-15 19:32:32 +00:00
|
|
|
}
|