Skip to content

Commit

Permalink
Add option to prefer static linking (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
acid1103 authored Nov 27, 2023
1 parent d3c65e4 commit 7bc44b7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ are passed through to shaderc-sys when building shaderc-rs:
1. Building from source, if the native shaderc library is not found via the
above steps.

For each library directory, the build script will try to fine and link to the
For each library directory, the build script will try to find and link to the
dynamic native shaderc library `shaderc_shared` first and the static native
shaderc library `shaderc_combined` next.
shaderc library `shaderc_combined` next. To prefer searching for the static
library first and the dynamic library next, the option
`--features prefer-static-linking` may be used.

Building from Source
--------------------
Expand Down
1 change: 1 addition & 0 deletions shaderc-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ assert_matches = "1.5"

[features]
build-from-source = ["shaderc-sys/build-from-source"]
prefer-static-linking = ["shaderc-sys/prefer-static-linking"]
1 change: 1 addition & 0 deletions shaderc-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ name = "shaderc_sys"

[features]
build-from-source = []
prefer-static-linking = []

[dependencies]
libc = "0.2"
Expand Down
18 changes: 12 additions & 6 deletions shaderc-sys/build/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ fn main() {
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
let config_build_from_source = env::var("CARGO_FEATURE_BUILD_FROM_SOURCE").is_ok();
let config_prefer_static_linking = env::var("CARGO_FEATURE_PREFER_STATIC_LINKING").is_ok();
let has_explicit_set_search_dir = env::var("SHADERC_LIB_DIR").is_ok();

// Initialize explicit shaderc search directory first.
Expand Down Expand Up @@ -275,12 +276,17 @@ fn main() {
let dylib_path = search_dir.join(dylib_name);

if let Some((lib_name, lib_kind)) = {
if dylib_path.exists() {
Some((SHADERC_SHARED_LIB, "dylib"))
} else if static_lib_path.exists() {
Some((SHADERC_STATIC_LIB, "static"))
} else {
None
match (
dylib_path.exists(),
static_lib_path.exists(),
config_prefer_static_linking,
) {
// If dylib not exist OR prefer static lib and static lib exist, static.
(false, true, _) | (_, true, true) => Some((SHADERC_STATIC_LIB, "static")),
// Otherwise, if dylib exist, dynamic.
(true, _, _) => Some((SHADERC_SHARED_LIB, "dylib")),
// Neither dylib nor static lib exist.
_ => None,
}
} {
match (target_os.as_str(), target_env.as_str()) {
Expand Down

0 comments on commit 7bc44b7

Please sign in to comment.