-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.rs
93 lines (85 loc) · 2.8 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// SPDX-FileCopyrightText: © The `magic-sys` Rust crate authors
// SPDX-License-Identifier: MIT OR Apache-2.0
#[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(_) => 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) => {
// workaround, see https://github.com/robo9k/rust-magic-sys/pull/16#issuecomment-949094327
println!("cargo:rustc-link-lib=shlwapi");
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);
}
LibraryResult::Success(lib) => {
println!("pkg-config success: {:?}", lib);
return;
}
}
}
#[cfg(not(feature = "pkg-config"))]
println!("pkg-config feature disabled");
#[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);
}
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"
);
panic!("could not link to `libmagic`");
}