From caef2c309d75021e441b6a67b4648826ebad1577 Mon Sep 17 00:00:00 2001 From: Daniel Rainer Date: Thu, 18 Dec 2025 01:32:26 +0100 Subject: [PATCH] build: extract some OS detection into build helper A subsequent commit will need to test for cygwin in a new crate. On current stable Rust (1.92) this works via `#[cfg(target_os = "cygwin)]`, but our MSRV (1.85) does not support this. To avoid code duplication, the OS detection logic is extracted into the build helper crate. For now, only `detect_cygwin` is needed, but it would be inconsistent to extract that but not the same functions for other operating systems. Part of #12183 --- build.rs | 42 ++++++---------------------------- crates/build-helper/src/lib.rs | 31 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/build.rs b/build.rs index 19b757005..8d9151080 100644 --- a/build.rs +++ b/build.rs @@ -1,4 +1,7 @@ -use fish_build_helper::{env_var, fish_build_dir, workspace_root}; +use fish_build_helper::{ + env_var, fish_build_dir, target_os, target_os_is_apple, target_os_is_bsd, target_os_is_cygwin, + workspace_root, +}; use rsconf::Target; use std::env; use std::path::{Path, PathBuf}; @@ -70,9 +73,9 @@ fn detect_cfgs(target: &mut Target) { for (name, handler) in [ // Ignore the first entry, it just sets up the type inference. ("", &(|_: &Target| false) as &dyn Fn(&Target) -> bool), - ("apple", &detect_apple), - ("bsd", &detect_bsd), - ("cygwin", &detect_cygwin), + ("apple", &(|_| target_os_is_apple())), + ("bsd", &(|_| target_os_is_bsd())), + ("cygwin", &(|_| target_os_is_cygwin())), ("have_eventfd", &|target| { // FIXME: NetBSD 10 has eventfd, but the libc crate does not expose it. if target_os() == "netbsd" { @@ -108,37 +111,6 @@ fn detect_cfgs(target: &mut Target) { } } -// Target OS for compiling our crates, as opposed to the build script. -fn target_os() -> String { - env_var("CARGO_CFG_TARGET_OS").unwrap() -} - -fn detect_apple(_: &Target) -> bool { - matches!(target_os().as_str(), "ios" | "macos") -} - -fn detect_cygwin(_: &Target) -> bool { - target_os() == "cygwin" -} - -/// Detect if we're being compiled for a BSD-derived OS, allowing targeting code conditionally with -/// `#[cfg(bsd)]`. -/// -/// 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. -fn detect_bsd(_: &Target) -> bool { - let target_os = target_os(); - let is_bsd = target_os.ends_with("bsd") || target_os == "dragonfly"; - if matches!( - target_os.as_str(), - "dragonfly" | "freebsd" | "netbsd" | "openbsd" - ) { - assert!(is_bsd, "Target incorrectly detected as not BSD!"); - } - is_bsd -} - /// Rust sets the stack size of newly created threads to a sane value, but is at at the mercy of the /// OS when it comes to the size of the main stack. Some platforms we support default to a tiny /// 0.5 MiB main stack, which is insufficient for fish's MAX_EVAL_DEPTH/MAX_STACK_DEPTH values. diff --git a/crates/build-helper/src/lib.rs b/crates/build-helper/src/lib.rs index b949c74ee..4bcbc4bd2 100644 --- a/crates/build-helper/src/lib.rs +++ b/crates/build-helper/src/lib.rs @@ -52,3 +52,34 @@ pub fn rebuild_if_embedded_path_changed>(path: P) { rebuild_if_path_changed(path); } } + +// Target OS for compiling our crates, as opposed to the build script. +pub fn target_os() -> String { + env_var("CARGO_CFG_TARGET_OS").unwrap() +} + +pub fn target_os_is_apple() -> bool { + matches!(target_os().as_str(), "ios" | "macos") +} + +/// Detect if we're being compiled for a BSD-derived OS, allowing targeting code conditionally with +/// `#[cfg(bsd)]`. +/// +/// 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. +pub fn target_os_is_bsd() -> bool { + let target_os = target_os(); + let is_bsd = target_os.ends_with("bsd") || target_os == "dragonfly"; + if matches!( + target_os.as_str(), + "dragonfly" | "freebsd" | "netbsd" | "openbsd" + ) { + assert!(is_bsd, "Target incorrectly detected as not BSD!"); + } + is_bsd +} + +pub fn target_os_is_cygwin() -> bool { + target_os() == "cygwin" +}