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    resolver_service_account_token_path: String,
66}
67
68pub fn apply_profile() {
69    println!("cargo:rerun-if-env-changed=KANIDM_BUILD_PROFILE");
70    println!("cargo:rerun-if-env-changed=KANIDM_BUILD_PROFILE_TOML");
71
72    // transform any requested paths for our server. We do this by reading
73    // our profile that we have been provided.
74    let profile = env!("KANIDM_BUILD_PROFILE");
75    let contents = env!("KANIDM_BUILD_PROFILE_TOML");
76
77    let data = general_purpose::STANDARD
78        .decode(contents)
79        .unwrap_or_else(|_| panic!("Failed to parse profile - {profile} - {contents}"));
80
81    let data_str = String::from_utf8(data)
82        .unwrap_or_else(|_| panic!("Failed to read profile data to UTF-8 string - {profile}"));
83
84    let profile_cfg: ProfileConfig = toml::from_str(&data_str)
85        .unwrap_or_else(|_| panic!("Failed to parse profile - {profile} - {contents}"));
86
87    // We have to setup for our pkg version to be passed into things correctly
88    // now. This relies on the profile build.rs to get the commit rev if present, but
89    // we combine it with the local package version
90    println!("cargo:rerun-if-env-changed=CARGO_PKG_VERSION");
91    println!("cargo:rerun-if-env-changed=KANIDM_PKG_COMMIT_REV");
92
93    let kanidm_pkg_version = match option_env!("KANIDM_PKG_COMMIT_REV") {
94        Some(commit_rev) => format!("{} {}", env!("CARGO_PKG_VERSION"), commit_rev),
95        None => env!("CARGO_PKG_VERSION").to_string(),
96    };
97
98    println!("cargo:rustc-env=KANIDM_PKG_VERSION={kanidm_pkg_version}");
99
100    // KANIDM_PKG_VERSION_HASH is used for cache busting in the web UI
101    let mut kanidm_pkg_version_hash = sha2::Sha256::new();
102    kanidm_pkg_version_hash.update(kanidm_pkg_version.as_bytes());
103    let kanidm_pkg_version_hash = &BASE64_STANDARD.encode(kanidm_pkg_version_hash.finalize())[..8];
104    println!("cargo:rustc-env=KANIDM_PKG_VERSION_HASH={kanidm_pkg_version_hash}");
105
106    let version_pre = env!("CARGO_PKG_VERSION_PRE");
107    if version_pre == "dev" {
108        println!("cargo:rustc-env=KANIDM_PRE_RELEASE=1");
109    }
110
111    // For some checks we only want the series (i.e. exclude the patch version).
112    let version_major = env!("CARGO_PKG_VERSION_MAJOR");
113    let version_minor = env!("CARGO_PKG_VERSION_MINOR");
114    println!("cargo:rustc-env=KANIDM_PKG_SERIES={version_major}.{version_minor}");
115
116    match profile_cfg.cpu_flags {
117        CpuOptLevel::apple_m1 => println!("cargo:rustc-env=RUSTFLAGS=-Ctarget-cpu=apple_m1"),
118        CpuOptLevel::none => {}
119        CpuOptLevel::native => println!("cargo:rustc-env=RUSTFLAGS=-Ctarget-cpu=native"),
120        CpuOptLevel::neon_v8 => {
121            println!("cargo:rustc-env=RUSTFLAGS=-Ctarget-features=+neon,+fp-armv8")
122        }
123        CpuOptLevel::x86_64_legacy => println!("cargo:rustc-env=RUSTFLAGS=-Ctarget-cpu=x86-64"),
124        CpuOptLevel::x86_64_v2 => println!("cargo:rustc-env=RUSTFLAGS=-Ctarget-cpu=x86-64-v2"),
125        CpuOptLevel::x86_64_v3 => println!("cargo:rustc-env=RUSTFLAGS=-Ctarget-cpu=x86-64-v3"),
126    }
127    println!("cargo:rustc-env=KANIDM_PROFILE_NAME={profile}");
128    println!("cargo:rustc-env=KANIDM_CPU_FLAGS={}", profile_cfg.cpu_flags);
129    println!(
130        "cargo:rustc-env=KANIDM_SERVER_UI_PKG_PATH={}",
131        profile_cfg.server_ui_pkg_path
132    );
133    println!(
134        "cargo:rustc-env=KANIDM_SERVER_ADMIN_BIND_PATH={}",
135        profile_cfg.server_admin_bind_path
136    );
137    println!(
138        "cargo:rustc-env=KANIDM_SERVER_CONFIG_PATH={}",
139        profile_cfg.server_config_path
140    );
141    println!(
142        "cargo:rustc-env=KANIDM_CLIENT_CONFIG_PATH={}",
143        profile_cfg.client_config_path
144    );
145    println!(
146        "cargo:rustc-env=KANIDM_RESOLVER_CONFIG_PATH={}",
147        profile_cfg.resolver_config_path
148    );
149    println!(
150        "cargo:rustc-env=KANIDM_RESOLVER_SERVICE_ACCOUNT_TOKEN_PATH={}",
151        profile_cfg.resolver_service_account_token_path
152    );
153    println!(
154        "cargo:rustc-env=KANIDM_RESOLVER_UNIX_SHELL_PATH={}",
155        profile_cfg.resolver_unix_shell_path
156    );
157}