Debian Codegraph @

build.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 // spell-checker:ignore (vars) krate mangen tldr
7
8 use std::env;
9 use std::fs::File;
10 use std::io::Write;
11 use std::path::Path;
12
13 pub fn main() {
14 const ENV_FEATURE_PREFIX: &str = "CARGO_FEATURE_";
15 const FEATURE_PREFIX: &str = "feat_";
16 const OVERRIDE_PREFIX: &str = "uu_";
17
18 // Do not rebuild build script unless the script itself or the enabled features are modified
19 // See <https://doc.rust-lang.org/cargo/reference/build-scripts.html#change-detection>
20 println!("cargo:rerun-if-changed=build.rs");
21
22 // Check for tldr.zip when building uudoc to warn users once at build time
23 // instead of repeatedly at runtime for each utility
24 if env::var("CARGO_FEATURE_UUDOC").is_ok() && !Path::new("docs/tldr.zip").exists() {
25 println!(
26 "cargo:warning=No tldr archive found, so the documentation will not include examples."
27 );
28 println!("cargo:warning=To include examples, download the tldr archive:");
29 println!(
30 "cargo:warning= curl -L https://github.com/tldr-pages/tldr/releases/latest/download/tldr.zip -o docs/tldr.zip"
31 );
32 }
33
34 if let Ok(profile) = env::var("PROFILE") {
35 println!("cargo:rustc-cfg=build={profile:?}");
36 }
37
38 let mut crates = Vec::new();
39
40 let out_dir = env::var("OUT_DIR").unwrap();
41
42 let target_os = env::var_os("CARGO_CFG_TARGET_OS").unwrap();
43 for (key, val) in env::vars() {
44 if val == "1" && key.starts_with(ENV_FEATURE_PREFIX) {
45 let krate = key[ENV_FEATURE_PREFIX.len()..].to_lowercase();
46 // Allow this as we have a bunch of info in the comments
47 #[allow(clippy::match_same_arms)]
48 match krate.as_ref() {
49 "chcon" | "runcon" if !(target_os == "linux" || target_os == "android") => {
50 continue;
51 }
52 "default" | "macos" | "unix" | "windows" | "selinux" | "zip" | "clap_complete"
53 | "clap_mangen" | "fluent_syntax" | "openssl" => continue, // common/standard feature names
54 "nightly" | "test_unimplemented" | "expensive_tests" | "test_risky_names" => {
55 continue;
56 } // crate-local custom features
57 "uudoc" => continue, // is not a utility
58 "test" => continue, // over-ridden with 'uu_test' to avoid collision with rust core crate 'test'
59 s if s.starts_with(FEATURE_PREFIX) => continue, // crate feature sets
60 _ => {} // util feature name
61 }
62 crates.push(krate);
63 }
64 }
65 crates.sort();
66
67 let mut mf = File::create(Path::new(&out_dir).join("uutils_map.rs")).unwrap();
68
69 mf.write_all(
70 "type UtilityMap<T> = phf::OrderedMap<&'static str, (fn(T) -> i32, fn() -> Command)>;\n\
71 \n\
72 #[allow(clippy::too_many_lines)]
73 #[allow(clippy::unreadable_literal)]
74 fn util_map<T: Args>() -> UtilityMap<T> {\n"
75 .as_bytes(),
76 )
77 .unwrap();
78
79 let mut phf_map = phf_codegen::OrderedMap::<&str>::new();
80 let mut entries = Vec::new();
81
82 for krate in &crates {
83 let map_value = format!("({krate}::uumain, {krate}::uu_app)");
84 match krate.as_ref() {
85 // 'test' is named uu_test to avoid collision with rust core crate 'test'.
86 // It can also be invoked by name '[' for the '[ expr ] syntax'.
87 "uu_test" => {
88 entries.push(("test", map_value.clone()));
89 entries.push(("[", map_value.clone()));
90 }
91 k if k.starts_with(OVERRIDE_PREFIX) => {
92 entries.push((&k[OVERRIDE_PREFIX.len()..], map_value.clone()));
93 }
94 "false" | "true" => {
95 entries.push((
96 krate.as_str(),
97 format!("(r#{krate}::uumain, r#{krate}::uu_app)"),
98 ));
99 }
100 _ => {
101 entries.push((krate.as_str(), map_value.clone()));
102 }
103 }
104 }
105 entries.sort_by_key(|(name, _)| *name);
106
107 for (name, value) in entries {
108 phf_map.entry(name, value);
109 }
110
111 write!(mf, "{}", phf_map.build()).unwrap();
112 mf.write_all(b"\n}\n").unwrap();
113
114 mf.flush().unwrap();
115 }