acid/firmware2/build.rs
2026-01-19 20:13:25 +01:00

151 lines
5.5 KiB
Rust

use std::env;
use std::fs::{File, OpenOptions};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use const_gen::*;
use indoc::{formatdoc, writedoc};
use json::JsonValue;
use slint_build::{CompilerConfiguration, EmbedResourcesKind};
use xz2::read::XzEncoder;
fn main() {
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
if let Ok(repo) = gix::discover(&manifest_dir) {
let commit_hash = repo.head_commit().unwrap().short_id().unwrap();
println!("cargo:rustc-env=GIT_COMMIT_HASH={}", commit_hash);
println!(
"cargo:rustc-env=GIT_COMMIT={}",
repo.find_tag(repo.head_id().unwrap())
.ok()
.map(|tag| format!("{} ({})", tag.decode().unwrap().name, commit_hash))
.unwrap_or_else(|| commit_hash.to_string())
);
}
// 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");
// TODO: Make it a submodule and use relative paths.
// Link libxkbcommon.
let libxkbcommon_build_dir = env::var("LIBXKBCOMMON_BUILD_DIR")
.expect("The build directory of libxkbcommon must be specified using the `LIBXKBCOMMON_BUILD_DIR` environment variable.");
let libxkbcommon_library_path = PathBuf::from(&libxkbcommon_build_dir).join("libxkbcommon.a");
if !libxkbcommon_library_path.is_file() {
panic!(
"{}",
formatdoc! {"
The compiled libxkbcommon library was not found at path {libxkbcommon_library_path:?}.
Most likely, the library was not compiled. See the `README.md` for compilation instructions.
If the `LIBXKBCOMMON_BUILD_DIR` environment variable is not set to the build directory you want to use, change it.
Currently, `LIBXKBCOMMON_BUILD_DIR` is set to: {libxkbcommon_build_dir}
"}
);
}
println!("cargo:rustc-link-search=native={libxkbcommon_build_dir}");
println!("cargo:rustc-link-lib=static=xkbcommon");
println!("cargo:rerun-if-changed={libxkbcommon_build_dir}/libxkbcommon.a");
println!("cargo:rustc-link-search=native=../../../c/spectre-cli-linux/api/build");
println!("cargo:rustc-link-lib=static=spectre");
println!("cargo:rerun-if-changed=../../../c/spectre-cli-linux/api/build/libspectre.a");
// 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);
slint_build::compile_with_config("ui/main.slint", slint_config)
.expect("Slint build failed");
slint_build::print_rustc_flags().unwrap()
}
}
fn generate_vial_config() {
// Generated vial config file
let path = Path::new(&env::var_os("OUT_DIR").unwrap()).join("config_generated.rs");
let mut out_file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(path)
.unwrap();
let p = Path::new("vial.json");
let mut content = String::new();
match File::open(p) {
Ok(mut file) => {
file.read_to_string(&mut content)
.expect("Cannot read vial.json");
}
Err(e) => println!("Cannot find vial.json {p:?}: {e}"),
};
let vial_cfg = json::parse(&content).unwrap();
let vial_cfg_string = json::stringify(vial_cfg.clone());
let mut keyboard_def_compressed: Vec<u8> = Vec::new();
XzEncoder::new(vial_cfg_string.as_bytes(), 6)
.read_to_end(&mut keyboard_def_compressed)
.unwrap();
let keyboard_id: Vec<u8> = vec![0xB9, 0xBC, 0x09, 0xB2, 0x9D, 0x37, 0x4C, 0xEA];
let const_declarations = [
const_declaration!(pub VIAL_KEYBOARD_DEF = keyboard_def_compressed),
const_declaration!(pub VIAL_KEYBOARD_ID = keyboard_id),
]
.map(|s| "#[allow(clippy::redundant_static_lifetimes)]\n".to_owned() + s.as_str())
.join("\n");
writeln!(out_file, "{}", const_declarations).unwrap();
writedoc!(
out_file,
"
#[allow(dead_code, non_camel_case_types)]
#[repr(u8)]
pub enum CustomKeycodes {{
"
)
.unwrap();
// const CUSTOM_KEYCODE_FIRST: u16 = 0x840;
#[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 {
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,
// CUSTOM_KEYCODE_FIRST + index as u16
index as u8
)
.unwrap();
}
}
}
}
writeln!(out_file, "}}").unwrap();
}