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!("cargo:rustc-env=KANIDM_PKG_VERSION_HASH={kanidm_pkg_version_hash}");
104
105    let version_pre = env!("CARGO_PKG_VERSION_PRE");
106    if version_pre == "dev" {
107        println!("cargo:rustc-env=KANIDM_PRE_RELEASE=1");
108    }
109
110    // For some checks we only want the series (i.e. exclude the patch version).
111    let version_major = env!("CARGO_PKG_VERSION_MAJOR");
112    let version_minor = env!("CARGO_PKG_VERSION_MINOR");
113    println!("cargo:rustc-env=KANIDM_PKG_SERIES={version_major}.{version_minor}");
114
115    match profile_cfg.cpu_flags {
116        CpuOptLevel::apple_m1 => println!("cargo:rustc-env=RUSTFLAGS=-Ctarget-cpu=apple_m1"),
117        CpuOptLevel::none => {}
118        CpuOptLevel::native => println!("cargo:rustc-env=RUSTFLAGS=-Ctarget-cpu=native"),
119        CpuOptLevel::neon_v8 => {
120            println!("cargo:rustc-env=RUSTFLAGS=-Ctarget-features=+neon,+fp-armv8")
121        }
122        CpuOptLevel::x86_64_legacy => println!("cargo:rustc-env=RUSTFLAGS=-Ctarget-cpu=x86-64"),
123        CpuOptLevel::x86_64_v2 => println!("cargo:rustc-env=RUSTFLAGS=-Ctarget-cpu=x86-64-v2"),
124        CpuOptLevel::x86_64_v3 => println!("cargo:rustc-env=RUSTFLAGS=-Ctarget-cpu=x86-64-v3"),
125    }
126    println!("cargo:rustc-env=KANIDM_PROFILE_NAME={profile}");
127    println!("cargo:rustc-env=KANIDM_CPU_FLAGS={}", profile_cfg.cpu_flags);
128    println!(
129        "cargo:rustc-env=KANIDM_SERVER_UI_PKG_PATH={}",
130        profile_cfg.server_ui_pkg_path
131    );
132    println!(
133        "cargo:rustc-env=KANIDM_SERVER_ADMIN_BIND_PATH={}",
134        profile_cfg.server_admin_bind_path
135    );
136    println!(
137        "cargo:rustc-env=KANIDM_SERVER_CONFIG_PATH={}",
138        profile_cfg.server_config_path
139    );
140    println!(
141        "cargo:rustc-env=KANIDM_CLIENT_CONFIG_PATH={}",
142        profile_cfg.client_config_path
143    );
144    println!(
145        "cargo:rustc-env=KANIDM_RESOLVER_CONFIG_PATH={}",
146        profile_cfg.resolver_config_path
147    );
148    println!(
149        "cargo:rustc-env=KANIDM_RESOLVER_UNIX_SHELL_PATH={}",
150        profile_cfg.resolver_unix_shell_path
151    );
152}