2026-01-06 22:52:41 +01:00
|
|
|
use std::env;
|
2026-01-02 02:44:00 +01:00
|
|
|
use std::fs::{File, OpenOptions};
|
|
|
|
|
use std::io::{Read, Write};
|
2026-01-11 22:37:17 +01:00
|
|
|
use std::path::{Path, PathBuf};
|
2025-12-24 02:07:21 +01:00
|
|
|
|
|
|
|
|
use const_gen::*;
|
2026-01-30 00:38:43 +01:00
|
|
|
use embuild::cmd;
|
2026-01-31 15:24:36 +01:00
|
|
|
use indoc::writedoc;
|
2026-01-02 02:44:00 +01:00
|
|
|
use json::JsonValue;
|
2025-12-29 19:36:00 +01:00
|
|
|
use slint_build::{CompilerConfiguration, EmbedResourcesKind};
|
2025-12-24 02:07:21 +01:00
|
|
|
use xz2::read::XzEncoder;
|
|
|
|
|
|
2026-01-30 00:38:43 +01:00
|
|
|
fn build_xkbcommon() {
|
|
|
|
|
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
|
|
|
|
|
let build_script = manifest_dir.join("../libxkbcommon-compile.sh");
|
|
|
|
|
let build_dir_name = env::var("XKBCOMMON_BUILD_DIR_NAME").unwrap();
|
|
|
|
|
cmd!(build_script, build_dir_name)
|
|
|
|
|
.run()
|
|
|
|
|
.expect("Failed to compile xkbcommon.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn build_sodium() {
|
|
|
|
|
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
|
|
|
|
|
let build_script = manifest_dir.join("../libsodium-compile.sh");
|
|
|
|
|
let install_dir_name = env::var("SODIUM_INSTALL_DIR_NAME").unwrap();
|
|
|
|
|
cmd!(build_script, install_dir_name)
|
|
|
|
|
.run()
|
|
|
|
|
.expect("Failed to compile sodium.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn build_spectre() {
|
|
|
|
|
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
|
|
|
|
|
let build_script = manifest_dir.join("../spectre-api-compile.sh");
|
|
|
|
|
let build_dir_name = env::var("SPECTRE_API_BUILD_DIR_NAME").unwrap();
|
|
|
|
|
let sodium_install_dir = env::var("SODIUM_INSTALL_DIR").unwrap();
|
|
|
|
|
cmd!(build_script, build_dir_name, sodium_install_dir)
|
|
|
|
|
.run()
|
|
|
|
|
.expect("Failed to compile the Spectre API.");
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-24 02:07:21 +01:00
|
|
|
fn main() {
|
2026-01-11 22:37:17 +01:00
|
|
|
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
|
|
|
|
|
if let Ok(repo) = gix::discover(&manifest_dir) {
|
2025-12-31 22:24:26 +01:00
|
|
|
let commit_hash = repo.head_commit().unwrap().short_id().unwrap();
|
|
|
|
|
println!("cargo:rustc-env=GIT_COMMIT_HASH={}", commit_hash);
|
2026-01-06 22:52:41 +01:00
|
|
|
println!(
|
|
|
|
|
"cargo:rustc-env=GIT_COMMIT={}",
|
2025-12-31 22:24:26 +01:00
|
|
|
repo.find_tag(repo.head_id().unwrap())
|
|
|
|
|
.ok()
|
|
|
|
|
.map(|tag| format!("{} ({})", tag.decode().unwrap().name, commit_hash))
|
|
|
|
|
.unwrap_or_else(|| commit_hash.to_string())
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-12-31 00:54:48 +01:00
|
|
|
|
2025-12-24 02:07:21 +01:00
|
|
|
// Generate vial config at the root of project
|
|
|
|
|
println!("cargo:rerun-if-changed=vial.json");
|
|
|
|
|
generate_vial_config();
|
|
|
|
|
|
|
|
|
|
println!("cargo:rustc-link-arg-bins=-Tlinkall.x");
|
|
|
|
|
|
|
|
|
|
// Set the extra linker script from defmt
|
|
|
|
|
// println!("cargo:rustc-link-arg=-Tdefmt.x");
|
2025-12-29 19:36:00 +01:00
|
|
|
|
2026-01-30 00:38:43 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct NotBuilt {
|
2026-01-31 15:24:36 +01:00
|
|
|
#[allow(unused)]
|
2026-01-30 00:38:43 +01:00
|
|
|
lib_build_dir: String,
|
|
|
|
|
}
|
2026-01-05 04:16:05 +01:00
|
|
|
|
2026-01-30 00:38:43 +01:00
|
|
|
fn link_static_lib(env_var: &str, library: &str) -> Result<(), NotBuilt> {
|
|
|
|
|
let lib_build_dir_str = env::var(env_var)
|
2026-01-22 01:21:23 +01:00
|
|
|
.unwrap_or_else(|error| panic!("The build directory of lib{library} must be specified using the `{env_var}` environment variable: {error}"));
|
2026-01-30 00:38:43 +01:00
|
|
|
let lib_build_dir = PathBuf::from(&lib_build_dir_str)
|
|
|
|
|
.canonicalize()
|
|
|
|
|
.map_err(|_| NotBuilt {
|
|
|
|
|
lib_build_dir: lib_build_dir_str.clone(),
|
|
|
|
|
})?;
|
2026-01-22 01:21:23 +01:00
|
|
|
let lib_library_path = lib_build_dir
|
|
|
|
|
.join(format!("lib{library}.a"))
|
|
|
|
|
.canonicalize()
|
2026-01-30 00:38:43 +01:00
|
|
|
.map_err(|_| NotBuilt {
|
|
|
|
|
lib_build_dir: lib_build_dir_str.clone(),
|
|
|
|
|
})?;
|
2026-01-22 01:21:23 +01:00
|
|
|
let lib_build_dir = lib_build_dir.display();
|
2026-01-11 22:37:17 +01:00
|
|
|
|
2026-01-22 01:21:23 +01:00
|
|
|
if !lib_library_path.is_file() {
|
2026-01-30 00:38:43 +01:00
|
|
|
return Err(NotBuilt {
|
|
|
|
|
lib_build_dir: lib_build_dir_str,
|
|
|
|
|
});
|
2026-01-22 01:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
println!("cargo:rustc-link-search=native={lib_build_dir}");
|
|
|
|
|
println!("cargo:rustc-link-lib=static={library}");
|
|
|
|
|
println!("cargo:rerun-if-changed={lib_build_dir}/lib{library}.a");
|
2026-01-30 00:38:43 +01:00
|
|
|
|
|
|
|
|
Ok(())
|
2026-01-22 01:21:23 +01:00
|
|
|
}
|
2026-01-05 04:16:05 +01:00
|
|
|
|
2026-01-30 00:38:43 +01:00
|
|
|
fn link_static_lib_or_build(env_var: &str, library: &str, build: impl FnOnce()) {
|
|
|
|
|
if link_static_lib(env_var, library).is_err() {
|
|
|
|
|
(build)();
|
|
|
|
|
link_static_lib(env_var, library)
|
|
|
|
|
.unwrap_or_else(|err| panic!("Failed to link library after building it: {err:?}"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
link_static_lib_or_build("XKBCOMMON_BUILD_DIR", "xkbcommon", || {
|
|
|
|
|
build_xkbcommon();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
link_static_lib_or_build("SPECTRE_API_BUILD_DIR", "spectre", || {
|
|
|
|
|
build_sodium();
|
|
|
|
|
build_spectre();
|
|
|
|
|
});
|
2026-01-19 20:13:25 +01:00
|
|
|
|
2026-01-11 00:06:35 +01:00
|
|
|
// Slint config and compilation
|
|
|
|
|
{
|
|
|
|
|
// Don't think this does anything:
|
|
|
|
|
// println!("cargo:rerun-if-env-changed=SLINT_FONT_SIZES");
|
|
|
|
|
|
|
|
|
|
let slint_config = CompilerConfiguration::new()
|
|
|
|
|
// .with_scale_factor(2.0)
|
|
|
|
|
.with_style("cosmic-dark".to_string())
|
|
|
|
|
.embed_resources(EmbedResourcesKind::EmbedForSoftwareRenderer);
|
2026-01-11 22:37:17 +01:00
|
|
|
slint_build::compile_with_config("ui/main.slint", slint_config)
|
|
|
|
|
.expect("Slint build failed");
|
2026-01-11 00:06:35 +01:00
|
|
|
slint_build::print_rustc_flags().unwrap()
|
|
|
|
|
}
|
2025-12-24 02:07:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn generate_vial_config() {
|
|
|
|
|
// Generated vial config file
|
2026-01-02 02:44:00 +01:00
|
|
|
let path = Path::new(&env::var_os("OUT_DIR").unwrap()).join("config_generated.rs");
|
2026-01-06 22:52:41 +01:00
|
|
|
let mut out_file = OpenOptions::new()
|
|
|
|
|
.create(true)
|
|
|
|
|
.write(true)
|
|
|
|
|
.truncate(true)
|
|
|
|
|
.open(path)
|
|
|
|
|
.unwrap();
|
2025-12-24 02:07:21 +01:00
|
|
|
|
|
|
|
|
let p = Path::new("vial.json");
|
|
|
|
|
let mut content = String::new();
|
|
|
|
|
match File::open(p) {
|
|
|
|
|
Ok(mut file) => {
|
2026-01-06 22:52:41 +01:00
|
|
|
file.read_to_string(&mut content)
|
|
|
|
|
.expect("Cannot read vial.json");
|
2025-12-24 02:07:21 +01:00
|
|
|
}
|
|
|
|
|
Err(e) => println!("Cannot find vial.json {p:?}: {e}"),
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-02 02:44:00 +01:00
|
|
|
let vial_cfg = json::parse(&content).unwrap();
|
2025-12-24 02:07:21 +01:00
|
|
|
|
2026-02-12 22:44:13 +01:00
|
|
|
let keyboard_def_compressed: Vec<u8> = {
|
|
|
|
|
let vial_cfg_string = json::stringify(vial_cfg.clone());
|
|
|
|
|
let mut keyboard_def_compressed = Vec::new();
|
|
|
|
|
XzEncoder::new(vial_cfg_string.as_bytes(), 6)
|
|
|
|
|
.read_to_end(&mut keyboard_def_compressed)
|
|
|
|
|
.unwrap();
|
|
|
|
|
keyboard_def_compressed
|
|
|
|
|
};
|
|
|
|
|
// This is a firmware-unique randomly generated ID.
|
|
|
|
|
// If you fork this repo to make your own firmware, you should change this.
|
|
|
|
|
let keyboard_id: Vec<u8> = vec![0x9a, 0x8a, 0x08, 0xae, 0x87, 0xcd, 0xc7, 0xb9];
|
|
|
|
|
let keyboard_name: &str = {
|
|
|
|
|
let JsonValue::Object(vial_cfg) = &vial_cfg else {
|
|
|
|
|
panic!("The root element in `vial.json` is not an object.");
|
|
|
|
|
};
|
|
|
|
|
vial_cfg
|
|
|
|
|
.get("name")
|
|
|
|
|
.expect("No `name` in `vial.json`.")
|
|
|
|
|
.as_str()
|
|
|
|
|
.expect("`name` in `vial.json` is not a string.")
|
|
|
|
|
};
|
|
|
|
|
let vendor_id: u16 = {
|
|
|
|
|
let JsonValue::Object(vial_cfg) = &vial_cfg else {
|
|
|
|
|
panic!("The root element in `vial.json` is not an object.");
|
|
|
|
|
};
|
|
|
|
|
let vendor_id_string = vial_cfg
|
|
|
|
|
.get("vendorId")
|
|
|
|
|
.expect("No `vendorId` in `vial.json`.")
|
|
|
|
|
.as_str()
|
|
|
|
|
.expect("`vendorId` in `vial.json` is not a string.");
|
|
|
|
|
assert!(vendor_id_string.starts_with("0x"));
|
|
|
|
|
u16::from_str_radix(&vendor_id_string[2..], 16).expect("Invalid vendor ID.")
|
|
|
|
|
};
|
|
|
|
|
let product_id: u16 = {
|
|
|
|
|
let JsonValue::Object(vial_cfg) = &vial_cfg else {
|
|
|
|
|
panic!("The root element in `vial.json` is not an object.");
|
|
|
|
|
};
|
|
|
|
|
let product_id_string = vial_cfg
|
|
|
|
|
.get("productId")
|
|
|
|
|
.expect("No `productId` in `vial.json`.")
|
|
|
|
|
.as_str()
|
|
|
|
|
.expect("`productId` in `vial.json` is not a string.");
|
|
|
|
|
assert!(product_id_string.starts_with("0x"));
|
|
|
|
|
u16::from_str_radix(&product_id_string[2..], 16).expect("Invalid product ID.")
|
|
|
|
|
};
|
2025-12-24 02:07:21 +01:00
|
|
|
let const_declarations = [
|
|
|
|
|
const_declaration!(pub VIAL_KEYBOARD_DEF = keyboard_def_compressed),
|
|
|
|
|
const_declaration!(pub VIAL_KEYBOARD_ID = keyboard_id),
|
2026-02-12 22:44:13 +01:00
|
|
|
const_declaration!(pub VIAL_KEYBOARD_NAME = keyboard_name),
|
|
|
|
|
const_declaration!(pub VIAL_VENDOR_ID = vendor_id),
|
|
|
|
|
const_declaration!(pub VIAL_PRODUCT_ID = product_id),
|
2025-12-24 02:07:21 +01:00
|
|
|
]
|
|
|
|
|
.map(|s| "#[allow(clippy::redundant_static_lifetimes)]\n".to_owned() + s.as_str())
|
|
|
|
|
.join("\n");
|
2026-01-02 02:44:00 +01:00
|
|
|
|
|
|
|
|
writeln!(out_file, "{}", const_declarations).unwrap();
|
|
|
|
|
|
2026-01-12 01:03:27 +01:00
|
|
|
writedoc!(
|
|
|
|
|
out_file,
|
|
|
|
|
"
|
2026-02-13 03:00:46 +01:00
|
|
|
#[allow(dead_code, non_camel_case_types, clippy::upper_case_acronyms)]
|
2026-01-12 01:03:27 +01:00
|
|
|
#[repr(u8)]
|
|
|
|
|
pub enum CustomKeycodes {{
|
|
|
|
|
"
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2026-01-02 02:44:00 +01:00
|
|
|
|
2026-01-10 19:21:13 +01:00
|
|
|
// const CUSTOM_KEYCODE_FIRST: u16 = 0x840;
|
2026-01-02 02:44:00 +01:00
|
|
|
|
|
|
|
|
#[allow(clippy::collapsible_if)]
|
|
|
|
|
if let JsonValue::Object(vial_cfg) = vial_cfg {
|
|
|
|
|
if let Some(JsonValue::Array(custom_keycodes)) = vial_cfg.get("customKeycodes") {
|
|
|
|
|
for (index, custom_keycode) in custom_keycodes.iter().enumerate() {
|
|
|
|
|
if let JsonValue::Object(custom_keycode) = custom_keycode {
|
2026-01-06 22:52:41 +01:00
|
|
|
let name = custom_keycode
|
|
|
|
|
.get("name")
|
|
|
|
|
.expect("A custom keycode in vial.json is missing a name.")
|
|
|
|
|
.as_str()
|
|
|
|
|
.expect("A custom keycode's name must be a string.");
|
|
|
|
|
writeln!(
|
|
|
|
|
out_file,
|
|
|
|
|
" {} = {},",
|
|
|
|
|
name,
|
2026-01-10 19:21:13 +01:00
|
|
|
// CUSTOM_KEYCODE_FIRST + index as u16
|
|
|
|
|
index as u8
|
2026-01-06 22:52:41 +01:00
|
|
|
)
|
|
|
|
|
.unwrap();
|
2026-01-02 02:44:00 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeln!(out_file, "}}").unwrap();
|
2025-12-24 02:07:21 +01:00
|
|
|
}
|