Skip to main content

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