From be5d33e5c1f56ff2e8e503496abbe4ea3526f8d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kav=C3=ADk?= Date: Sat, 1 Jun 2024 23:29:56 +0200 Subject: [PATCH] build.rs instead of frontend crate features --- Makefile.toml | 16 +++++++++------- frontend/Cargo.toml | 5 ----- frontend/build.rs | 22 ++++++++++++++++++++++ frontend/src/controls_panel.rs | 6 ++---- frontend/src/platform.rs | 14 +++++++------- frontend/src/platform/browser.rs | 4 ++-- src-tauri/src/lib.rs | 2 +- 7 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 frontend/build.rs diff --git a/Makefile.toml b/Makefile.toml index 8a3f03b..006f4ea 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -29,11 +29,11 @@ run_task = { fork = true, parallel = true, name = [ "watch_tauri_glue", ]} -[tasks.start_web] +[tasks.start_browser] description = "Run without Tauri in the browser & watch Typescript and Rust in the debug mode" dependencies = ["store_current_process_id"] run_task = { fork = true, parallel = true, name = [ - "tauri_dev_with_cleanup", + "mzoon_start_with_cleanup", "watch_pixi_canvas", "watch_tauri_glue", ]} @@ -56,12 +56,14 @@ command = "tauri/bin/cargo-tauri" args = ["${@}"] [tasks.mzoon_for_tauri] -description = "Run locally installed mzoon with enabled `frontend` feature `platform_tauri" -extend = "mzoon_with_disabled_frontend_features" +description = "Run locally installed mzoon with env var FASTWAVE_PLATFORM=TAURI" +extend = "mzoon_without_env_var_fastwave_platform" +env = { FASTWAVE_PLATFORM = "TAURI" } [tasks.mzoon] -description = "Run locally installed mzoon with enabled `frontend` feature `platform_browser" -extend = "mzoon_with_disabled_frontend_features" +description = "Run locally installed mzoon with env var FASTWAVE_PLATFORM=BROWSER" +extend = "mzoon_without_env_var_fastwave_platform" +env = { FASTWAVE_PLATFORM = "BROWSER" } # [tasks.mzoon] # description = "Run mzoon from a cloned MoonZoon repo" @@ -70,7 +72,7 @@ extend = "mzoon_with_disabled_frontend_features" ###### HELPER TASKS ###### -[tasks.mzoon_with_disabled_frontend_features] +[tasks.mzoon_without_env_var_fastwave_platform] description = "Run locally installed mzoo" command = "mzoon/bin/mzoon" args = ["${@}"] diff --git a/frontend/Cargo.toml b/frontend/Cargo.toml index 206f980..4bb8e99 100644 --- a/frontend/Cargo.toml +++ b/frontend/Cargo.toml @@ -14,8 +14,3 @@ wasm-bindgen-test = "0.3.19" shared.workspace = true zoon.workspace = true wellen.workspace = true - -[features] -platform_browser = [] -platform_tauri = [] - diff --git a/frontend/build.rs b/frontend/build.rs new file mode 100644 index 0000000..39935d2 --- /dev/null +++ b/frontend/build.rs @@ -0,0 +1,22 @@ +use std::env; + +// https://doc.rust-lang.org/cargo/reference/build-scripts.html + +macro_rules! instruction { + ($($arg: tt)*) => { + println!($($arg)*) + } +} + +// https://github.com/rust-lang/cargo/issues/985 +// macro_rules! warning { +// ($($arg: tt)*) => { +// instruction!("cargo:warning={}", format!($($arg)*)) +// } +// } + +fn main() { + let default_platform = "BROWSER"; + let platform = env::var("FASTWAVE_PLATFORM").unwrap_or_else(|_| default_platform.to_owned()); + instruction!("cargo:rustc-cfg=FASTWAVE_PLATFORM=\"{platform}\""); +} diff --git a/frontend/src/controls_panel.rs b/frontend/src/controls_panel.rs index 3a33f27..cf6e6c4 100644 --- a/frontend/src/controls_panel.rs +++ b/frontend/src/controls_panel.rs @@ -143,10 +143,8 @@ impl ControlsPanel { let hierarchy_and_time_table = hierarchy_and_time_table.clone(); Task::start(async move { platform::load_waveform(test_file_name).await; - let (hierarchy, time_table) = join!( - platform::get_hierarchy(), - platform::get_time_table() - ); + let (hierarchy, time_table) = + join!(platform::get_hierarchy(), platform::get_time_table()); hierarchy_and_time_table.set(Some((Rc::new(hierarchy), Rc::new(time_table)))) }) }) diff --git a/frontend/src/platform.rs b/frontend/src/platform.rs index 9e3104d..84e0465 100644 --- a/frontend/src/platform.rs +++ b/frontend/src/platform.rs @@ -1,16 +1,16 @@ -#[cfg(feature = "platform_tauri")] +// @TODO maybe rewrite `FASTWAVE_PLATFORM` to features once it's possible to set them through env vars: +// https://github.com/rust-lang/cargo/issues/4829 + +#[cfg(FASTWAVE_PLATFORM = "TAURI")] mod tauri; -#[cfg(feature = "platform_tauri")] +#[cfg(FASTWAVE_PLATFORM = "TAURI")] use tauri as platform; -#[cfg(feature = "platform_browser")] +#[cfg(FASTWAVE_PLATFORM = "BROWSER")] mod browser; -#[cfg(feature = "platform_browser")] +#[cfg(FASTWAVE_PLATFORM = "BROWSER")] use browser as platform; -#[cfg(all(feature = "platform_tauri", feature = "platform_browser"))] -compile_error!("feature \"foo\" and feature \"bar\" cannot be enabled at the same time"); - pub async fn show_window() { platform::show_window().await } diff --git a/frontend/src/platform/browser.rs b/frontend/src/platform/browser.rs index 1245fae..a48d078 100644 --- a/frontend/src/platform/browser.rs +++ b/frontend/src/platform/browser.rs @@ -1,7 +1,7 @@ -use zoon::*; +use shared::wellen_helpers; use std::sync::Mutex; use wellen::simple::Waveform; -use shared::wellen_helpers; +use zoon::*; #[derive(Default)] struct Store { diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index f316644..e21a8e3 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,7 +1,7 @@ +use shared::wellen_helpers; use std::rc::Rc; use std::sync::Mutex; use wellen::simple::Waveform; -use shared::wellen_helpers; #[derive(Default)] struct Store {