diff --git a/.gitmodules b/.gitmodules index 5030c5f..4da0600 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "firmware2/spectre-api-c"] path = firmware/spectre-api-c url = https://github.com/Limeth/spectre-api.git +[submodule "firmware/libsodium"] + path = firmware/libsodium + url = https://github.com/jedisct1/libsodium diff --git a/firmware/Cargo.lock b/firmware/Cargo.lock index 09d29cf..1006a05 100644 --- a/firmware/Cargo.lock +++ b/firmware/Cargo.lock @@ -62,7 +62,7 @@ dependencies = [ "spectre-api-sys", "static_cell", "tinyvec", - "xkbcommon 0.9.0 (git+https://github.com/Limeth/xkbcommon-rs?branch=esp32s3)", + "xkbcommon 0.9.0 (git+https://github.com/Limeth/xkbcommon-rs?rev=d91705a7211e294c09abae5e3e64f1df158bc2c5)", "xz2", ] @@ -3834,7 +3834,7 @@ dependencies = [ "input", "memmap2", "nix", - "xkbcommon 0.9.0", + "xkbcommon 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -6141,6 +6141,7 @@ dependencies = [ [[package]] name = "rmk" version = "0.8.2" +source = "git+https://github.com/Limeth/rmk?rev=1661c55f5c21e7d80ea3f93255df483302c74b84#1661c55f5c21e7d80ea3f93255df483302c74b84" dependencies = [ "bitfield-struct", "bt-hci", @@ -6185,6 +6186,7 @@ dependencies = [ [[package]] name = "rmk-config" version = "0.6.1" +source = "git+https://github.com/Limeth/rmk?rev=1661c55f5c21e7d80ea3f93255df483302c74b84#1661c55f5c21e7d80ea3f93255df483302c74b84" dependencies = [ "cargo_toml", "config", @@ -6201,6 +6203,7 @@ dependencies = [ [[package]] name = "rmk-macro" version = "0.7.1" +source = "git+https://github.com/Limeth/rmk?rev=1661c55f5c21e7d80ea3f93255df483302c74b84#1661c55f5c21e7d80ea3f93255df483302c74b84" dependencies = [ "cargo_toml", "darling 0.20.11", @@ -6222,6 +6225,7 @@ dependencies = [ [[package]] name = "rmk-types" version = "0.2.2" +source = "git+https://github.com/Limeth/rmk?rev=1661c55f5c21e7d80ea3f93255df483302c74b84#1661c55f5c21e7d80ea3f93255df483302c74b84" dependencies = [ "bitfield-struct", "postcard", @@ -6849,6 +6853,7 @@ checksum = "a0f368519fc6c85fc1afdb769fb5a51123f6158013e143656e25a3485a0d401c" [[package]] name = "spectre-api-sys" version = "0.1.0" +source = "git+https://github.com/Limeth/spectre-api-sys#9e844eb056c3dfee8286ac21ec40fa689a8b8aa2" dependencies = [ "bindgen", "cc", @@ -8392,6 +8397,8 @@ dependencies = [ [[package]] name = "xkbcommon" version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a974f48060a14e95705c01f24ad9c3345022f4d97441b8a36beb7ed5c4a02d" dependencies = [ "libc", "memmap2", @@ -8401,7 +8408,7 @@ dependencies = [ [[package]] name = "xkbcommon" version = "0.9.0" -source = "git+https://github.com/Limeth/xkbcommon-rs?branch=esp32s3#d91705a7211e294c09abae5e3e64f1df158bc2c5" +source = "git+https://github.com/Limeth/xkbcommon-rs?rev=d91705a7211e294c09abae5e3e64f1df158bc2c5#d91705a7211e294c09abae5e3e64f1df158bc2c5" dependencies = [ "xkeysym", ] diff --git a/firmware/acid-firmware/.cargo/config.toml b/firmware/acid-firmware/.cargo/config.toml index 933e6fb..d180391 100644 --- a/firmware/acid-firmware/.cargo/config.toml +++ b/firmware/acid-firmware/.cargo/config.toml @@ -18,8 +18,12 @@ rustflags = [ [env] # These must be kept in sync with /.zed/settings.json EXPLICITLY_INCLUDE_DEFAULT_DIRS = "true" -LIBXKBCOMMON_BUILD_DIR = "../libxkbcommon/build" +XKBCOMMON_BUILD_DIR = "../libxkbcommon/build-esp32s3" SPECTRE_API_BUILD_DIR = "../spectre-api-c/build-esp32s3" +SODIUM_INSTALL_DIR = "../libsodium/install" +XKBCOMMON_BUILD_DIR_NAME = "build-esp32s3" +SPECTRE_API_BUILD_DIR_NAME = "build-esp32s3" +SODIUM_INSTALL_DIR_NAME = "install" SPECTRE_API_SYS_CC = "xtensa-esp32s3-elf-cc.exe" ESP_LOG = "warn" ESP_BACKTRACE_CONFIG_BACKTRACE_FRAMES = "20" diff --git a/firmware/acid-firmware/Cargo.toml b/firmware/acid-firmware/Cargo.toml index 7e7d1bd..5b68cc0 100644 --- a/firmware/acid-firmware/Cargo.toml +++ b/firmware/acid-firmware/Cargo.toml @@ -34,7 +34,7 @@ probe = ["limit-fps", "rtt-log", "no-usb", "ble"] format-db = [] [dependencies] -rmk = { version = "0.8.2", default-features = false, features = [ +rmk = { version = "0.8.2", git = "https://github.com/Limeth/rmk", rev = "1661c55f5c21e7d80ea3f93255df483302c74b84", default-features = false, features = [ "log", "storage", "vial", @@ -68,7 +68,7 @@ itertools = { version = "0.14.0", default-features = false } bytemuck = "1.24.0" critical-section = "1.2.0" cfg-if = "1.0.4" -xkbcommon = { git = "https://github.com/Limeth/xkbcommon-rs", branch = "esp32s3", default-features = false, features = ["c-lib-wrap"] } +xkbcommon = { git = "https://github.com/Limeth/xkbcommon-rs", rev = "d91705a7211e294c09abae5e3e64f1df158bc2c5", default-features = false, features = ["c-lib-wrap"] } rtt-target = { version = "0.6.2", features = ["log"], optional = true } panic-rtt-target = { version = "0.2.0", optional = true } enumset = "1.1.10" diff --git a/firmware/acid-firmware/README.md b/firmware/acid-firmware/README.md index fec9b16..d0ded37 100644 --- a/firmware/acid-firmware/README.md +++ b/firmware/acid-firmware/README.md @@ -43,7 +43,7 @@ This replaces the debugging symbols with paths that will be available when debug Then compile the firmware with: ```ps1 -$env:LIBXKBCOMMON_BUILD_DIR="libxkbcommon/build-debug"; cargo build +$env:XKBCOMMON_BUILD_DIR="libxkbcommon/build-debug"; cargo build ``` ### Creating keymaps diff --git a/firmware/acid-firmware/build.rs b/firmware/acid-firmware/build.rs index d67b266..382f391 100644 --- a/firmware/acid-firmware/build.rs +++ b/firmware/acid-firmware/build.rs @@ -4,11 +4,40 @@ use std::io::{Read, Write}; use std::path::{Path, PathBuf}; use const_gen::*; +use embuild::cmd; use indoc::{formatdoc, writedoc}; use json::JsonValue; use slint_build::{CompilerConfiguration, EmbedResourcesKind}; use xz2::read::XzEncoder; +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."); +} + fn main() { let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); if let Ok(repo) = gix::discover(&manifest_dir) { @@ -32,37 +61,56 @@ fn main() { // Set the extra linker script from defmt // println!("cargo:rustc-link-arg=-Tdefmt.x"); - // TODO: Make it a submodule and use relative paths. + #[derive(Debug)] + struct NotBuilt { + lib_build_dir: String, + } - fn link_static_lib(env_var: &str, library: &str) { - let lib_build_dir = env::var(env_var) + fn link_static_lib(env_var: &str, library: &str) -> Result<(), NotBuilt> { + let lib_build_dir_str = env::var(env_var) .unwrap_or_else(|error| panic!("The build directory of lib{library} must be specified using the `{env_var}` environment variable: {error}")); - let lib_build_dir = PathBuf::from(&lib_build_dir).canonicalize().unwrap(); + let lib_build_dir = PathBuf::from(&lib_build_dir_str) + .canonicalize() + .map_err(|_| NotBuilt { + lib_build_dir: lib_build_dir_str.clone(), + })?; let lib_library_path = lib_build_dir .join(format!("lib{library}.a")) .canonicalize() - .unwrap(); + .map_err(|_| NotBuilt { + lib_build_dir: lib_build_dir_str.clone(), + })?; let lib_build_dir = lib_build_dir.display(); if !lib_library_path.is_file() { - panic!( - "{}", - formatdoc! {" - The compiled lib{library} library was not found at path {lib_library_path:?}. - Most likely, the library was not compiled. See the `README.md` for compilation instructions. - If the `{env_var}` environment variable is not set to the build directory you want to use, change it. - Currently, `{env_var}` is set to: {lib_build_dir} - "} - ); + return Err(NotBuilt { + lib_build_dir: lib_build_dir_str, + }); } 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"); + + Ok(()) } - link_static_lib("LIBXKBCOMMON_BUILD_DIR", "xkbcommon"); - link_static_lib("SPECTRE_API_BUILD_DIR", "spectre"); + 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(); + }); // Slint config and compilation { diff --git a/firmware/libsodium b/firmware/libsodium new file mode 160000 index 0000000..d24faf5 --- /dev/null +++ b/firmware/libsodium @@ -0,0 +1 @@ +Subproject commit d24faf56214469b354b01c8ba36257e04737101e diff --git a/firmware/libsodium-compile.sh b/firmware/libsodium-compile.sh index a08e176..29c743c 100644 --- a/firmware/libsodium-compile.sh +++ b/firmware/libsodium-compile.sh @@ -1,2 +1,22 @@ #!/usr/bin/env bash -env CC=xtensa-esp32s3-elf-gcc CFLAGS="-ffreestanding -fno-builtin -mlongcalls" LDFLAGS="-nostdlib -static" ./configure --host xtensa-esp32s3 --disable-shared --enable-static --prefix="$PWD/install" +if [ "$#" -lt 1 ]; then + echo "Usage: $0 " + exit 1 +fi + +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +REPO_DIR="$SCRIPT_DIR/libsodium" +INSTALL_DIR_NAME="$REPO_DIR/$1" + +pushd "$REPO_DIR" >/dev/null + env CC=xtensa-esp32s3-elf-gcc \ + CFLAGS="-ffreestanding -fno-builtin -mlongcalls" \ + LDFLAGS="-nostdlib -static" \ + ./configure \ + --host xtensa-esp32s3 \ + --disable-shared \ + --enable-static \ + --prefix="$INSTALL_DIR_NAME" + make -j + make install +popd >/dev/null diff --git a/firmware/libxkbcommon-compile.sh b/firmware/libxkbcommon-compile.sh index 599d4dc..f8d57e1 100644 --- a/firmware/libxkbcommon-compile.sh +++ b/firmware/libxkbcommon-compile.sh @@ -10,10 +10,8 @@ BUILD_DIR_NAME="$LIBXKBCOMMON_DIR/$1" STATIC_LIB_PATH="$BUILD_DIR_NAME/libxkbcommon.a" SETUP_ARGS=${@:2} -git submodule update --init --recursive pushd "$LIBXKBCOMMON_DIR" >/dev/null meson setup "$BUILD_DIR_NAME" \ - --wipe \ --cross-file "$SCRIPT_DIR/cross-esp32s3.txt" \ -Denable-x11=false \ -Denable-wayland=false \ diff --git a/firmware/spectre-api-compile.sh b/firmware/spectre-api-compile.sh index a43874d..ee522ea 100644 --- a/firmware/spectre-api-compile.sh +++ b/firmware/spectre-api-compile.sh @@ -1,4 +1,16 @@ #!/usr/bin/env bash +if [ "$#" -lt 2 ]; then + echo "Usage: $0 " + exit 1 +fi + SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) -meson setup build --wipe --cross-file "$SCRIPT_DIR/cross-esp32s3.txt" -Dlibsodium-install-dir="$LIBSODIUM_INSTALL_DIR" -meson compile -C build +REPO_DIR="$SCRIPT_DIR/spectre-api-c" +BUILD_DIR_NAME="$REPO_DIR/$1" +STATIC_LIB_PATH="$BUILD_DIR_NAME/libspectre.a" + +pushd "$REPO_DIR" >/dev/null + meson setup "$BUILD_DIR_NAME" --cross-file "$SCRIPT_DIR/cross-esp32s3.txt" -Dlibsodium-install-dir="$2" + meson compile -C "$BUILD_DIR_NAME" + $SCRIPT_DIR/redefine-syms.sh "__spre_" "$STATIC_LIB_PATH" "$STATIC_LIB_PATH" +popd >/dev/null