Skip to content

Commit

Permalink
feat: migration to conan for dotlottie-rs (#225)
Browse files Browse the repository at this point in the history
* feat: migration to conan for dotlottie-rs

---------

Co-authored-by: Muhmud Ahmad <[email protected]>
  • Loading branch information
muhmud and Muhmud Ahmad authored Sep 6, 2024
1 parent dcc2e27 commit 83763fc
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 35 deletions.
1 change: 1 addition & 0 deletions .github/workflows/check-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
with:
xcode-version: "13.3.1"
- uses: ningenMe/[email protected]
- uses: turtlebrowser/get-conan@main
- name: Install Make
run: brew install make
- name: Build Setup
Expand Down
5 changes: 3 additions & 2 deletions .mac-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ brew install android-ndk \
ninja \
pkg-config \
ktlint \
swiftformat
swiftformat \
conan

rustup component add rust-src

Expand All @@ -76,7 +77,7 @@ rustup component add rust-src --toolchain nightly
rustup target add wasm32-unknown-emscripten --toolchain nightly

echo
echo "Install cargo dependencies"
echo "Installing cargo dependencies"
cargo install uniffi-bindgen-cpp \
--git https://github.com/NordSecurity/uniffi-bindgen-cpp \
--tag "${UNIFFI_BINDGEN_CPP_VERSION}"
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -892,11 +892,11 @@ $(RUNTIME_FFI)/$(APPLE_BUILD)/$(MODULE_MAP): $(RUNTIME_FFI)/$(RUNTIME_FFI_UNIFFI
$(CREATE_OUTPUT_FILE)

.PHONY: demo-player
demo-player: $(LOCAL_ARCH_LIB_DIR)/$(THORVG_LIB)
demo-player:
cargo build --manifest-path examples/demo-player/Cargo.toml

.PHONY: demo-state-machine
demo-state-machine: $(LOCAL_ARCH_LIB_DIR)/$(THORVG_LIB)
demo-state-machine:
cargo build --manifest-path examples/demo-state-machine/Cargo.toml

.PHONY: $(ANDROID)
Expand Down
1 change: 1 addition & 0 deletions dotlottie-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ serde = { version = "1.0.188", features = ["derive"] }
[build-dependencies]
bindgen = "0.69.1"
lazy_static = "1.4"
conan2 = "0.1"

[dev-dependencies]
criterion = "0.5.1"
Expand Down
93 changes: 63 additions & 30 deletions dotlottie-rs/build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use conan2::{ConanInstall, ConanVerbosity};
use lazy_static::lazy_static;
use std::env;
use std::path::{Path, PathBuf};

// Path for all default artifacts
const DEFAULT_ARTIFACTS_DIR: &str = "../deps/artifacts/local-arch/usr";
// Artifacts environment variables
const ARTIFACTS_INCLUDE_DIR: &str = "ARTIFACTS_INCLUDE_DIR";
const ARTIFACTS_LIB_DIR: &str = "ARTIFACTS_LIB_DIR";
const ARTIFACTS_LIB64_DIR: &str = "ARTIFACTS_LIB64_DIR";

// Target triple for WASM
const WASM32_UNKNOWN_EMSCRIPTEN: &str = "wasm32-unknown-emscripten";
Expand All @@ -15,45 +18,55 @@ struct BuildSettings {
link_args: Vec<String>,
}

fn is_artifacts_provided() -> bool {
std::env::var(ARTIFACTS_INCLUDE_DIR).is_ok() && std::env::var(ARTIFACTS_LIB_DIR).is_ok()
}

fn is_wasm_build() -> bool {
match std::env::var("TARGET") {
Ok(target) => target == WASM32_UNKNOWN_EMSCRIPTEN,
Err(_) => panic!("TARGET environment variable not set"),
}
}

fn platform_libs() -> Vec<String> {
match env::var("CARGO_CFG_TARGET_VENDOR") {
Ok(platform) if platform == "apple" => vec![String::from("c++")],
Ok(_) if std::env::var("CARGO_CFG_UNIX").is_ok() => vec![String::from("stdc++")],
Ok(_) => vec![],
Err(_) => panic!("CARGO_CFG_TARGET_VENDOR environment variable not set"),
}
}

lazy_static! {
// The project root directory
static ref PROJECT_DIR: PathBuf = PathBuf::from(env!("CARGO_MANIFEST_DIR"));

// Default artifacts directories
static ref DEFAULT_INCLUDE_DIR: PathBuf = PathBuf::from(format!("{DEFAULT_ARTIFACTS_DIR}/include"));
static ref DEFAULT_LIB_DIR: PathBuf = PathBuf::from(&format!("{DEFAULT_ARTIFACTS_DIR}/lib"));
static ref DEFAULT_LIB64_DIR: PathBuf = PathBuf::from(&format!("{DEFAULT_ARTIFACTS_DIR}/lib64"));

// Native library dependencies
static ref TARGET_BUILD_SETTINGS: BuildSettings = match is_wasm_build() {
true => BuildSettings{
static ref TARGET_BUILD_SETTINGS: BuildSettings = match is_artifacts_provided() {
true if is_wasm_build() => BuildSettings{
static_libs: vec![String::from("thorvg")],
dynamic_libs: vec![],
link_args: vec![String::from("--no-entry")],
},
_ => BuildSettings{
true => BuildSettings{
static_libs: vec![String::from("thorvg"), String::from("turbojpeg"), String::from("png"), String::from("z"), String::from("webp")],
dynamic_libs: vec![String::from("c++")],
dynamic_libs: platform_libs(),
link_args: vec![],
},
// Conan build
_ => BuildSettings{
static_libs: vec![],
dynamic_libs: platform_libs(),
link_args: vec![],
}
};
}

fn find_path(var: &str, default: &Path, required: bool) -> PathBuf {
Some(
env::var(var)
.map(|v| PROJECT_DIR.join(v))
.unwrap_or_else(|_| PROJECT_DIR.join(default)),
)
.filter(|p| !required || p.exists())
.unwrap()
fn find_path(var: &str, required: bool) -> PathBuf {
Some(env::var(var).map(|v| PROJECT_DIR.join(v)).unwrap())
.filter(|p| !required || p.exists())
.unwrap()
}

fn register_link_path(lib_path: &Path) {
Expand Down Expand Up @@ -84,22 +97,42 @@ fn apply_build_settings(build_settings: &BuildSettings) {
}

fn main() {
let include_dir = find_path("ARTIFACTS_INCLUDE_DIR", &DEFAULT_INCLUDE_DIR, true);
let lib_dir = find_path("ARTIFACTS_LIB_DIR", &DEFAULT_LIB_DIR, true);
let lib64_dir = find_path("ARTIFACTS_LIB64_DIR", &DEFAULT_LIB64_DIR, false);
let bindings_output_path = env::var("OUT_DIR").map(PathBuf::from).unwrap();

// Add artifacts library directories
register_link_path(&lib_dir);
register_link_path(&lib64_dir);
let mut builder = bindgen::Builder::default().header("wrapper.h");
if is_artifacts_provided() {
let include_dir = find_path(ARTIFACTS_INCLUDE_DIR, true);
let lib_dir = find_path(ARTIFACTS_LIB_DIR, true);
let lib64_dir = find_path(ARTIFACTS_LIB64_DIR, false);

// Add artifacts library directories
register_link_path(&lib_dir);
register_link_path(&lib64_dir);

// Update bindings builder
builder = builder.clang_arg(format!("-I{}", include_dir.display()))
} else {
// Conan build
let cargo_instructions = ConanInstall::new()
.detect_profile()
.build("missing")
.verbosity(ConanVerbosity::Error)
.run()
.parse();

// Emit instructions to cargo
cargo_instructions.emit();

// Update bindings builder
for path in cargo_instructions.include_paths() {
builder = builder.clang_arg(format!("-I{}", path.display()))
}
}

// Apply build settings
apply_build_settings(&TARGET_BUILD_SETTINGS);

println!("cargo:rerun-if-changed=wrapper.h");
let bindings = bindgen::Builder::default()
.header("wrapper.h")
.clang_arg(format!("-I{}", include_dir.display()))
let bindings_output_path = env::var("OUT_DIR").map(PathBuf::from).unwrap();
let bindings = builder
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate bindings");
Expand Down
2 changes: 2 additions & 0 deletions dotlottie-rs/conanfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[requires]
thorvg/0.14.6
2 changes: 1 addition & 1 deletion dotlottie-rs/src/lottie_renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl LottieRenderer {
return Ok(());
}

if width.max(0) == 0 || height.max(0) == 0 {
if width == 0 || height == 0 {
return Err(LottieRendererError::InvalidArgument(
"Width and height must be greater than 0".to_string(),
));
Expand Down
1 change: 1 addition & 0 deletions dotlottie-rs/tests/frame_interpolation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mod tests {
assert!(!player.config().use_frame_interpolation);
}

#[ignore = "flaky test"]
#[test]
fn test_disable_frame_interpolation() {
let player = DotLottiePlayer::new(Config {
Expand Down

0 comments on commit 83763fc

Please sign in to comment.