Skip to content
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 option to prefer static linking #143

Merged
merged 3 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading