Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm as platform dependency #139

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ julia = ["sdp", "dep:libc", "dep:num-derive", "serde", "faer-sparse"]
# enable a blas/lapack source package
python = ["sdp", "dep:libc", "dep:pyo3", "dep:num-derive", "serde", "faer-sparse"]

wasm = ["dep:web-time"]

#compile with faer supernodal solver option
faer-sparse = ["dep:faer", "dep:faer-entity"]

Expand Down Expand Up @@ -200,9 +198,10 @@ features = ["sdp","sdp-mkl"]
# wasm compatibility
# ------------------------------

[dependencies.web-time]
optional = true
version = "0.2.3"
[target.'cfg(target_family = "wasm")'.dependencies]
web-time = "0.2.3"
[target.'cfg(target_family = "wasm")'.dev-dependencies]
wasm-bindgen-test = "0.3"

# ------------------------------
# testing, benchmarking etc
Expand Down
3 changes: 1 addition & 2 deletions src/timers/timers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ use std::collections::HashMap;
use std::ops::{Deref, DerefMut};

cfg_if::cfg_if! {
if #[cfg(feature="wasm")] {
if #[cfg(target_family = "wasm")] {
use web_time::{Duration, Instant};
}
else {
use std::time::{Duration, Instant};
}
}
Comment on lines 4 to 11
Copy link

@lovasoa lovasoa Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@goulart-paul Is there a reason to use cfg-if here ? (Instead of just adding the #cfg attribute directly ?

#[cfg(target_family = "wasm")]
use web_time::{Duration, Instant};

#[cfg(not(target_family = "wasm"))]
use std::time::{Duration, Instant};

https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think either way works. Maybe the current way is slightly preferable since it presents the cases as mutually exclusive options?

We already depend on cfg-if anyway since it is used when configuring basic types with/without BLAS support, and there cfg-if is quite useful since the else blocks can have (marginally) more substance.



#[derive(Debug, Default)]
struct InnerTimer {
Expand Down
11 changes: 11 additions & 0 deletions tests/basic_qp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#![allow(non_snake_case)]
#[cfg(target_family = "wasm")]
use wasm_bindgen_test::*;

use clarabel::{algebra::*, solver::*};

Expand Down Expand Up @@ -173,3 +175,12 @@ fn test_qp_dual_infeasible_ill_cond() {
assert!(solver.solution.obj_val.is_nan());
assert!(solver.solution.obj_val_dual.is_nan());
}

// a minimal test to check that the wasm build is working

#[cfg(target_family = "wasm")]
#[wasm_bindgen_test]
fn test_qp_feasible_wasm() {
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
test_qp_feasible();
}
Loading