profiles/
lib.rs

1use base64::prelude::BASE64_STANDARD;
2use base64::{engine::general_purpose, Engine as _};
3use serde::Deserialize;
4use sha2::Digest;
5use std::env;
6
7// To debug why a rebuild is requested.
8// CARGO_LOG=cargo::core::compiler::fingerprint=info cargo ...
9
10#[derive(Debug, Deserialize)]
11#[allow(non_camel_case_types)]
12enum CpuOptLevel {
13    apple_m1,
14    none,
15    native,
16    neon_v8,
17    x86_64_legacy, // don't use this it's the oldest and worst. unless you've got a really old CPU, in which case, sorry?
18    x86_64_v2,
19    x86_64_v3,
20}
21
22impl Default for CpuOptLevel {
23    fn default() -> Self {
24        if cfg!(target_arch = "x86_64") {
25            CpuOptLevel::x86_64_v2
26        } else if cfg!(target_arch = "aarch64") && cfg!(target_os = "macos") {
27            CpuOptLevel::apple_m1
28        /*
29        } else if cfg!(target_arch = "aarch64") && cfg!(target_os = "linux") {
30            // Disable neon_v8 on linux - this has issues on non-apple hardware and on
31            // opensuse/distro builds.
32            CpuOptLevel::neon_v8
33        */
34        } else {
35            CpuOptLevel::none
36        }
37    }
38}
39
40impl std::fmt::Display for CpuOptLevel {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42        match &self {
43            CpuOptLevel::apple_m1 => write!(f, "apple_m1"),
44            CpuOptLevel::none => write!(f, "none"),
45            CpuOptLevel::native => write!(f, "native"),
46            CpuOptLevel::neon_v8 => write!(f, "neon_v8"),
47            CpuOptLevel::x86_64_legacy => write!(f, "x86_64"),
48            CpuOptLevel::x86_64_v2 => write!(f, "x86_64_v2"),
49            CpuOptLevel::x86_64_v3 => write!(f, "x86_64_v3"),
50        }
51    }
52}
53
54#[derive(Debug, Deserialize)]
55#[serde(deny_unknown_fields)]
56struct ProfileConfig {
57    #[serde(default)]
58    cpu_flags: CpuOptLevel,
59    server_admin_bind_path: String,
60    server_config_path: String,
61    server_ui_pkg_path: String,
62    client_config_path: String,
63    resolver_config_path: String,
64    resolver_unix_shell_path: String,
65}
66
67pub fn apply_profile() {
68    println!("cargo:rerun-if-env-changed=KANIDM_BUILD_PROFILE");
69    println!("cargo:rerun-if-env-changed=KANIDM_BUILD_PROFILE_TOML");
70
71    // transform any requested paths for our server. We do this by reading
72    // our profile that we have been provided.
73    let profile = env!("KANIDM_BUILD_PROFILE");
74    let contents = env!("KANIDM_BUILD_PROFILE_TOML");
75
76    let data = general_purpose::STANDARD
77        .decode(contents)
78        .unwrap_or_else(|_| panic!("Failed to parse profile - {} - {}", profile, contents));
79
80    let data_str = String::from_utf8(data)
81        .unwrap_or_else(|_| panic!("Failed to read profile data to UTF-8 string - {}", profile));
82
83    let profile_cfg: ProfileConfig = toml::from_str(&data_str)
84        .unwrap_or_else(|_| panic!("Failed to parse profile - {} - {}", profile, contents));
85
86    // We have to setup for our pkg version to be passed into things correctly
87    // now. This relies on the profile build.rs to get the commit rev if present, but
88    // we combine it with the local package version
89    println!("cargo:rerun-if-env-changed=CARGO_PKG_VERSION");
90    println!("cargo:rerun-if-env-changed=KANIDM_PKG_COMMIT_REV");
91
92    let kanidm_pkg_version = match option_env!("KANIDM_PKG_COMMIT_REV") {
93        Some(commit_rev) => format!("{} {}", env!("CARGO_PKG_VERSION"), commit_rev),
94        None => env!("CARGO_PKG_VERSION").to_string(),
95    };
96
97    println!("cargo:rustc-env=KANIDM_PKG_VERSION={}", kanidm_pkg_version);
98
99    // KANIDM_PKG_VERSION_HASH is used for cache busting in the web UI
100    let mut kanidm_pkg_version_hash = sha2::Sha256::new();
101    kanidm_pkg_version_hash.update(kanidm_pkg_version.as_bytes());
102    let kanidm_pkg_version_hash = &BASE64_STANDARD.encode(kanidm_pkg_version_hash.finalize())[..8];
103    println!(
104        "cargo:rustc-env=KANIDM_PKG_VERSION_HASH={}",
105        kanidm_pkg_version_hash
106    );
107
108    let version_pre = env!("CARGO_PKG_VERSION_PRE");
109    if version_pre == "dev" {
110        println!("cargo:rustc-env=KANIDM_PRE_RELEASE=1");
111    }
112
113    // For some checks we only want the series (i.e. exclude the patch version).
114    let version_major = env!("CARGO_PKG_VERSION_MAJOR");
115    let version_minor = env!("CARGO_PKG_VERSION_MINOR");
116    println!(
117        "cargo:rustc-env=KANIDM_PKG_SERIES={}.{}",
118        version_major, version_minor
119    );
120
121    match profile_cfg.cpu_flags {
122        CpuOptLevel::apple_m1 => println!("cargo:rustc-env=RUSTFLAGS=-Ctarget-cpu=apple_m1"),
123        CpuOptLevel::none => {}
124        CpuOptLevel::native => println!("cargo:rustc-env=RUSTFLAGS=-Ctarget-cpu=native"),
125        CpuOptLevel::neon_v8 => {
126            println!("cargo:rustc-env=RUSTFLAGS=-Ctarget-features=+neon,+fp-armv8")
127        }
128        CpuOptLevel::x86_64_legacy => println!("cargo:rustc-env=RUSTFLAGS=-Ctarget-cpu=x86-64"),
129        CpuOptLevel::x86_64_v2 => println!("cargo:rustc-env=RUSTFLAGS=-Ctarget-cpu=x86-64-v2"),
130        CpuOptLevel::x86_64_v3 => println!("cargo:rustc-env=RUSTFLAGS=-Ctarget-cpu=x86-64-v3"),
131    }
132    println!("cargo:rustc-env=KANIDM_PROFILE_NAME={}", profile);
133    println!("cargo:rustc-env=KANIDM_CPU_FLAGS={}", profile_cfg.cpu_flags);
134    println!(
135        "cargo:rustc-env=KANIDM_SERVER_UI_PKG_PATH={}",
136        profile_cfg.server_ui_pkg_path
137    );
138    println!(
139        "cargo:rustc-env=KANIDM_SERVER_ADMIN_BIND_PATH={}",
140        profile_cfg.server_admin_bind_path
141    );
142    println!(
143        "cargo:rustc-env=KANIDM_SERVER_CONFIG_PATH={}",
144        profile_cfg.server_config_path
145    );
146    println!(
147        "cargo:rustc-env=KANIDM_CLIENT_CONFIG_PATH={}",
148        profile_cfg.client_config_path
149    );
150    println!(
151        "cargo:rustc-env=KANIDM_RESOLVER_CONFIG_PATH={}",
152        profile_cfg.resolver_config_path
153    );
154    println!(
155        "cargo:rustc-env=KANIDM_RESOLVER_UNIX_SHELL_PATH={}",
156        profile_cfg.resolver_unix_shell_path
157    );
158}