From 7cb7c9c102d652c1c99c9bb29f2bf8f28d3d51ba Mon Sep 17 00:00:00 2001 From: Dominick Schroer Date: Sun, 28 May 2023 12:31:01 -0600 Subject: [PATCH] Add dynamic linking support for faster builds --- crates/opencascade-sys/Cargo.toml | 3 + crates/opencascade-sys/build.rs | 97 ++++++++++++++++++------------ crates/opencascade-sys/src/main.rs | 6 ++ 3 files changed, 68 insertions(+), 38 deletions(-) create mode 100644 crates/opencascade-sys/src/main.rs diff --git a/crates/opencascade-sys/Cargo.toml b/crates/opencascade-sys/Cargo.toml index 9434c8fd..caeea2f1 100644 --- a/crates/opencascade-sys/Cargo.toml +++ b/crates/opencascade-sys/Cargo.toml @@ -46,6 +46,9 @@ exclude = [ "OCCT/tools/*", ] +[features] +dynamic = [] + [dependencies] cxx = "1" diff --git a/crates/opencascade-sys/build.rs b/crates/opencascade-sys/build.rs index 430b353a..caa8d878 100644 --- a/crates/opencascade-sys/build.rs +++ b/crates/opencascade-sys/build.rs @@ -1,44 +1,29 @@ +const LIBS: [&'static str; 18] = [ + "TKMath", + "TKMath", + "TKernel", + "TKGeomBase", + "TKG2d", + "TKG3d", + "TKTopAlgo", + "TKGeomAlgo", + "TKGeomBase", + "TKBRep", + "TKPrim", + "TKSTL", + "TKMesh", + "TKShHealing", + "TKFillet", + "TKBool", + "TKBO", + "TKOffset", +]; + fn main() { let target = std::env::var("TARGET").expect("No TARGET environment variable defined"); let is_windows = target.to_lowercase().contains("windows"); let is_windows_gnu = target.to_lowercase().contains("windows-gnu"); - - let dst = cmake::Config::new("OCCT") - .define("BUILD_LIBRARY_TYPE", "Static") - .define("BUILD_MODULE_Draw", "FALSE") - .define("USE_OPENGL", "FALSE") - .define("USE_GLES2", "FALSE") - .define("USE_D3D", "FALSE") - .define("USE_VTK", "FALSE") - .define("USE_TCL", "FALSE") - .define("USE_XLIB", "FALSE") - .define("USE_FREETYPE", "FALSE") - .define("USE_FREEIMAGE", "FALSE") - .define("USE_OPENVR", "FALSE") - .define("USE_FFMPEG", "FALSE") - .define("INSTALL_DIR_LIB", "lib") - .define("INSTALL_DIR_INCLUDE", "include") - .build(); - - println!("cargo:rustc-link-search=native={}", dst.join("lib").display()); - println!("cargo:rustc-link-lib=static=TKMath"); - println!("cargo:rustc-link-lib=static=TKernel"); - println!("cargo:rustc-link-lib=static=TKFeat"); - println!("cargo:rustc-link-lib=static=TKGeomBase"); - println!("cargo:rustc-link-lib=static=TKG2d"); - println!("cargo:rustc-link-lib=static=TKG3d"); - println!("cargo:rustc-link-lib=static=TKTopAlgo"); - println!("cargo:rustc-link-lib=static=TKGeomAlgo"); - println!("cargo:rustc-link-lib=static=TKGeomBase"); - println!("cargo:rustc-link-lib=static=TKBRep"); - println!("cargo:rustc-link-lib=static=TKPrim"); - println!("cargo:rustc-link-lib=static=TKSTL"); - println!("cargo:rustc-link-lib=static=TKMesh"); - println!("cargo:rustc-link-lib=static=TKShHealing"); - println!("cargo:rustc-link-lib=static=TKFillet"); - println!("cargo:rustc-link-lib=static=TKBool"); - println!("cargo:rustc-link-lib=static=TKBO"); - println!("cargo:rustc-link-lib=static=TKOffset"); + let is_dynamic = std::env::var("CARGO_FEATURE_DYNAMIC").is_ok(); if is_windows { println!("cargo:rustc-link-lib=dylib=user32"); @@ -50,11 +35,24 @@ fn main() { build.define("OCC_CONVERT_SIGNALS", "TRUE"); } + if !is_dynamic { + let opencascade_include = build_opencascade(); + build.include(opencascade_include); + + for lib in LIBS { + println!("cargo:rustc-link-lib=static={lib}"); + } + } else { + for lib in LIBS { + println!("cargo:rustc-link-lib=dylib={lib}"); + } + build.include("/usr/include/opencascade"); + } + build .cpp(true) .flag_if_supported("-std=c++11") .define("_USE_MATH_DEFINES", "TRUE") - .include(format!("{}", dst.join("include").display())) .include("include") .compile("wrapper"); @@ -63,3 +61,26 @@ fn main() { println!("cargo:rerun-if-changed=src/lib.rs"); println!("cargo:rerun-if-changed=include/wrapper.hxx"); } + +fn build_opencascade() -> String { + let dst = cmake::Config::new("OCCT") + .define("BUILD_LIBRARY_TYPE", "Static") + .define("BUILD_MODULE_Draw", "FALSE") + .define("USE_OPENGL", "FALSE") + .define("USE_GLES2", "FALSE") + .define("USE_D3D", "FALSE") + .define("USE_VTK", "FALSE") + .define("USE_TCL", "FALSE") + .define("USE_XLIB", "FALSE") + .define("USE_FREETYPE", "FALSE") + .define("USE_FREEIMAGE", "FALSE") + .define("USE_OPENVR", "FALSE") + .define("USE_FFMPEG", "FALSE") + .define("INSTALL_DIR_LIB", "lib") + .define("INSTALL_DIR_INCLUDE", "include") + .build(); + + println!("cargo:rustc-link-search=native={}", dst.join("lib").display()); + + format!("{}", dst.join("include").display()) +} diff --git a/crates/opencascade-sys/src/main.rs b/crates/opencascade-sys/src/main.rs new file mode 100644 index 00000000..a8b20852 --- /dev/null +++ b/crates/opencascade-sys/src/main.rs @@ -0,0 +1,6 @@ +use opencascade_sys::ffi::new_point; + +fn main() { + let point = new_point(0.0, 0.0, 0.0); + println!("x: {}, y: {}, z: {}", point.X(), point.Y(), point.Z()); +} \ No newline at end of file