From a41da3bfb453f9d1fe7c6794f3219dbd3e9cec29 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Wed, 20 May 2026 00:05:07 +0100 Subject: [PATCH] deps: gate selinux-related dependency set behind feat_selinux --- build.rs | 8 ++++++-- src/uu/chcon/Cargo.toml | 31 +++++++++++++++++++++++-------- src/uu/chcon/src/chcon.rs | 2 +- src/uu/chcon/src/errors.rs | 2 -- src/uu/chcon/src/fts.rs | 2 -- src/uu/chcon/src/main.rs | 4 ++-- src/uu/runcon/Cargo.toml | 29 ++++++++++++++++++++++------- src/uu/runcon/src/errors.rs | 2 -- src/uu/runcon/src/main.rs | 4 ++-- src/uu/runcon/src/runcon.rs | 2 +- 10 files changed, 57 insertions(+), 29 deletions(-) diff --git a/build.rs b/build.rs index b2d4c52279d..971699f5c4d 100644 --- a/build.rs +++ b/build.rs @@ -37,6 +37,9 @@ pub fn main() { let out_dir = env::var("OUT_DIR").unwrap(); + let target_os = env::var_os("CARGO_CFG_TARGET_OS").unwrap(); + let is_feat_selinux = env::var_os("CARGO_FEATURE_FEAT_SELINUX").is_some(); + let mut crates = Vec::new(); for (key, val) in env::vars() { if val == "1" && key.starts_with(ENV_FEATURE_PREFIX) { @@ -44,8 +47,9 @@ pub fn main() { // Allow this as we have a bunch of info in the comments #[allow(clippy::match_same_arms)] match krate.as_ref() { - #[cfg(not(any(target_os = "linux", target_os = "android")))] - "chcon" | "runcon" => { + "chcon" | "runcon" + if !(is_feat_selinux && (target_os == "linux" || target_os == "android")) => + { continue; } "default" | "macos" | "unix" | "windows" | "selinux" | "zip" | "clap_complete" diff --git a/src/uu/chcon/Cargo.toml b/src/uu/chcon/Cargo.toml index 04665ba37e2..450bdb801ff 100644 --- a/src/uu/chcon/Cargo.toml +++ b/src/uu/chcon/Cargo.toml @@ -19,15 +19,30 @@ path = "src/chcon.rs" test = false doctest = false -# TODO: block fetching crates without feat_selinux [target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies] -clap = { workspace = true } -uucore = { workspace = true, features = ["entries", "fs", "perms"] } -selinux = { workspace = true } -thiserror = { workspace = true } -libc = { workspace = true } -fts-sys = { workspace = true } -fluent = { workspace = true } +clap = { workspace = true, optional = true } +fluent = { workspace = true, optional = true } +fts-sys = { workspace = true, optional = true } +libc = { workspace = true, optional = true } +selinux = { workspace = true, optional = true } +thiserror = { workspace = true, optional = true } +uucore = { workspace = true, features = [ + "entries", + "fs", + "perms", +], optional = true } + +[features] +feat_selinux = ["selinux"] +selinux = [ + "dep:clap", + "dep:fluent", + "dep:fts-sys", + "dep:libc", + "dep:selinux", + "dep:thiserror", + "dep:uucore", +] [[bin]] name = "chcon" diff --git a/src/uu/chcon/src/chcon.rs b/src/uu/chcon/src/chcon.rs index de4696913ee..dd560b6bba3 100644 --- a/src/uu/chcon/src/chcon.rs +++ b/src/uu/chcon/src/chcon.rs @@ -5,7 +5,7 @@ // spell-checker:ignore (vars) RFILE -#![cfg(any(target_os = "linux", target_os = "android"))] +#![cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] #![allow(clippy::upper_case_acronyms)] use clap::builder::ValueParser; diff --git a/src/uu/chcon/src/errors.rs b/src/uu/chcon/src/errors.rs index fa4ae6fee54..c81ff3d2d6f 100644 --- a/src/uu/chcon/src/errors.rs +++ b/src/uu/chcon/src/errors.rs @@ -3,8 +3,6 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -#![cfg(any(target_os = "linux", target_os = "android"))] - use std::ffi::OsString; use std::fmt::Write; use std::io; diff --git a/src/uu/chcon/src/fts.rs b/src/uu/chcon/src/fts.rs index 8214058a770..b26e19d2581 100644 --- a/src/uu/chcon/src/fts.rs +++ b/src/uu/chcon/src/fts.rs @@ -3,8 +3,6 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -#![cfg(any(target_os = "linux", target_os = "android"))] - use std::ffi::{CStr, CString, OsStr}; use std::marker::PhantomData; use std::os::raw::{c_int, c_long, c_short}; diff --git a/src/uu/chcon/src/main.rs b/src/uu/chcon/src/main.rs index bd502509571..d803037e4d1 100644 --- a/src/uu/chcon/src/main.rs +++ b/src/uu/chcon/src/main.rs @@ -9,10 +9,10 @@ //! entirely (via #![cfg(...)]), which can break tooling and cross builds that //! expect this binary to exist even when it's a no-op off Linux. -#[cfg(any(target_os = "linux", target_os = "android"))] +#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] uucore::bin!(uu_chcon); -#[cfg(not(any(target_os = "linux", target_os = "android")))] +#[cfg(not(all(feature = "selinux", any(target_os = "linux", target_os = "android"))))] fn main() { eprintln!("chcon: SELinux is not supported on this platform"); std::process::exit(1); diff --git a/src/uu/runcon/Cargo.toml b/src/uu/runcon/Cargo.toml index 51a468fb2b0..2f5ee7d26a0 100644 --- a/src/uu/runcon/Cargo.toml +++ b/src/uu/runcon/Cargo.toml @@ -19,14 +19,29 @@ path = "src/runcon.rs" test = false doctest = false -# TODO: block fetching crates without feat_selinux [target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies] -clap = { workspace = true } -uucore = { workspace = true, features = ["entries", "fs", "perms", "selinux"] } -selinux = { workspace = true } -thiserror = { workspace = true } -libc = { workspace = true } -fluent = { workspace = true } +clap = { workspace = true, optional = true } +fluent = { workspace = true, optional = true } +libc = { workspace = true, optional = true } +selinux = { workspace = true, optional = true } +thiserror = { workspace = true, optional = true } +uucore = { workspace = true, features = [ + "entries", + "fs", + "perms", + "selinux", +], optional = true } + +[features] +feat_selinux = ["selinux"] +selinux = [ + "dep:clap", + "dep:fluent", + "dep:libc", + "dep:selinux", + "dep:thiserror", + "dep:uucore", +] [[bin]] name = "runcon" diff --git a/src/uu/runcon/src/errors.rs b/src/uu/runcon/src/errors.rs index 49dc83d16c2..97b270a7e12 100644 --- a/src/uu/runcon/src/errors.rs +++ b/src/uu/runcon/src/errors.rs @@ -3,8 +3,6 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -#![cfg(any(target_os = "linux", target_os = "android"))] - use std::ffi::OsString; use std::fmt::{Display, Formatter, Write}; use std::io; diff --git a/src/uu/runcon/src/main.rs b/src/uu/runcon/src/main.rs index 947934af1cb..b31ef267ffb 100644 --- a/src/uu/runcon/src/main.rs +++ b/src/uu/runcon/src/main.rs @@ -9,10 +9,10 @@ //! entirely (via #![cfg(...)]), which can break tooling and cross builds that //! expect this binary to exist even when it's a no-op off Linux. -#[cfg(any(target_os = "linux", target_os = "android"))] +#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] uucore::bin!(uu_runcon); -#[cfg(not(any(target_os = "linux", target_os = "android")))] +#[cfg(not(all(feature = "selinux", any(target_os = "linux", target_os = "android"))))] fn main() { eprintln!("runcon: SELinux is not supported on this platform"); std::process::exit(1); diff --git a/src/uu/runcon/src/runcon.rs b/src/uu/runcon/src/runcon.rs index 1f6ab00dbd6..1baaa1f3544 100644 --- a/src/uu/runcon/src/runcon.rs +++ b/src/uu/runcon/src/runcon.rs @@ -5,7 +5,7 @@ // spell-checker:ignore (vars) RFILE execv execvp -#![cfg(any(target_os = "linux", target_os = "android"))] +#![cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] use clap::builder::ValueParser; use uucore::error::{UError, UResult};