-
Notifications
You must be signed in to change notification settings - Fork 21
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
Add dynamic linking support for faster builds #26
Changes from all commits
7cb7c9c
cf0fc6a
1900b2b
95bb105
e594009
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,9 @@ exclude = [ | |
"OCCT/tools/*", | ||
] | ||
|
||
[features] | ||
dynamic = [] | ||
|
||
[dependencies] | ||
cxx = "1" | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,44 +1,29 @@ | ||||||
const LIBS: [&str; 18] = [ | ||||||
"TKMath", | ||||||
"TKernel", | ||||||
"TKFeat", | ||||||
"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(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should be able to use the cfg macro or a cfg attribute. if cfg!(feature = "dynamic") {
...
} or something like #[cfg(feature = "dynamic")]
fn is_dynamic() {
true
}
#[cfg(not(feature = "dynamic"))]
fn is_dynamic() {
false
} In this case the |
||||||
|
||||||
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"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure this is very portable, for example if I install opencascade with homebrew on macos, it'll be in
I have an example build.rs for another project which has a feature flag for dynamic vs. static linking, as well as usage of pkgconfig. It'll increase the complexity of build.rs, but it makes the library more likely to build without errors when dynamically linking. |
||||||
} | ||||||
|
||||||
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 { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be
Suggested change
|
||||||
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()) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then this return value can just be
Suggested change
|
||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can avoid having to specify the array length: