-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Make
pkg-config
and vcpkg
default crate features
Signed-off-by: robot9001 <robo9k@symlink.io>
- Loading branch information
Showing
4 changed files
with
89 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,88 @@ | ||
// SPDX-FileCopyrightText: © The `magic-sys` Rust crate authors | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
fn main() { | ||
#[cfg(any(feature = "pkg-config", feature = "vcpkg"))] | ||
enum LibraryResult<E, L> { | ||
Skipped(E), | ||
Failed(E), | ||
Success(L), | ||
} | ||
|
||
#[cfg(feature = "pkg-config")] | ||
fn try_pkgconfig() -> LibraryResult<pkg_config::Error, pkg_config::Library> { | ||
let lib = pkg_config::Config::new() | ||
.atleast_version("5.39") | ||
.probe("libmagic"); | ||
match lib { | ||
Err(err) => match err { | ||
pkg_config::Error::EnvNoPkgConfig(_) => { | ||
pkg_config::Error::EnvNoPkgConfig(_) => LibraryResult::Skipped(err), | ||
_ => LibraryResult::Failed(err), | ||
}, | ||
Ok(lib) => LibraryResult::Success(lib), | ||
} | ||
} | ||
|
||
#[cfg(feature = "vcpkg")] | ||
fn try_vcpkg() -> LibraryResult<vcpkg::Error, vcpkg::Library> { | ||
let lib = vcpkg::find_package("libmagic"); | ||
match lib { | ||
Err(err) => match err { | ||
vcpkg::Error::DisabledByEnv(_) => LibraryResult::Skipped(err), | ||
_ => LibraryResult::Failed(err), | ||
}, | ||
Ok(lib) => LibraryResult::Success(lib), | ||
} | ||
} | ||
|
||
fn main() { | ||
#[cfg(feature = "pkg-config")] | ||
{ | ||
let lib = try_pkgconfig(); | ||
match lib { | ||
LibraryResult::Skipped(err) => { | ||
println!("pkg-config skipped: {}", err); | ||
} | ||
_ => { | ||
LibraryResult::Failed(err) => { | ||
println!("cargo:warning=pkg-config failed: {}", err); | ||
} | ||
}, | ||
Ok(lib) => { | ||
println!("pkg-config success: {:?}", lib); | ||
return; | ||
LibraryResult::Success(lib) => { | ||
println!("pkg-config success: {:?}", lib); | ||
return; | ||
} | ||
} | ||
} | ||
#[cfg(not(feature = "pkg-config"))] | ||
println!("pkg-config feature disabled"); | ||
|
||
let lib = vcpkg::find_package("libmagic"); | ||
match lib { | ||
Err(err) => match err { | ||
vcpkg::Error::DisabledByEnv(_) => { | ||
#[cfg(feature = "vcpkg")] | ||
{ | ||
let lib = try_vcpkg(); | ||
match lib { | ||
LibraryResult::Skipped(err) => { | ||
println!("vcpkg skipped: {}", err); | ||
} | ||
_ => { | ||
LibraryResult::Failed(err) => { | ||
println!("cargo:warning=vcpkg failed: {}", err); | ||
} | ||
}, | ||
Ok(lib) => { | ||
println!("vcpkg success: {:?}", lib); | ||
return; | ||
LibraryResult::Success(lib) => { | ||
println!("vcpkg success: {:?}", lib); | ||
return; | ||
} | ||
} | ||
} | ||
#[cfg(not(feature = "vcpkg"))] | ||
println!("vcpkg feature disabled"); | ||
|
||
// if we're reach here, this means that either both pkg-config and vcpkg | ||
// got skipped or | ||
// both failed or | ||
// both features are disabled | ||
#[cfg(not(any(feature = "pkg-config", feature = "vcpkg")))] | ||
println!( | ||
"the pkg-config and vcpkg features are both disabled, \ | ||
this configuration requires you to override the build script: \ | ||
https://crates.io/crates/magic#override" | ||
); | ||
|
||
// if we're reach here, this means that either both pkg-config and vcpkg got skipped or both failed | ||
panic!("could not link to `libmagic` with neither `pkg-config` nor `vcpkg`"); | ||
panic!("could not link to `libmagic`"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters