Skip to content

Commit

Permalink
Add dynamic linking support for faster builds
Browse files Browse the repository at this point in the history
  • Loading branch information
DSchroer committed May 28, 2023
1 parent d24b4d8 commit 1abdf54
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 37 deletions.
3 changes: 3 additions & 0 deletions crates/opencascade-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ exclude = [
"OCCT/tools/*",
]

[features]
dynamic = []

[dependencies]
cxx = "1"

Expand Down
95 changes: 58 additions & 37 deletions crates/opencascade-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,43 +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=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");
Expand All @@ -49,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");

Expand All @@ -62,3 +61,25 @@ 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())
}
6 changes: 6 additions & 0 deletions crates/opencascade-sys/src/main.rs
Original file line number Diff line number Diff line change
@@ -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());
}

0 comments on commit 1abdf54

Please sign in to comment.