forked from Deniskore/opus-codec
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
191 lines (164 loc) · 5.83 KB
/
build.rs
File metadata and controls
191 lines (164 loc) · 5.83 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use std::env;
fn main() {
emit_rerun_directives();
let opts = BuildOptions::from_env();
if opts.use_system_lib {
handle_system_lib(&opts);
} else {
build_bundled_and_link(&opts);
}
generate_bindings();
}
struct BuildOptions {
use_system_lib: bool,
dred_enabled: bool,
presume_avx: bool,
target_arch: String,
avx_allowed: bool,
msvc_static_crt: bool,
}
impl BuildOptions {
fn from_env() -> Self {
let use_system_lib = env::var("CARGO_FEATURE_SYSTEM_LIB").is_ok();
let dred_enabled = env::var("CARGO_FEATURE_DRED").is_ok();
let presume_avx = env::var("CARGO_FEATURE_PRESUME_AVX2").is_ok();
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
let avx_allowed = presume_avx && matches!(target_arch.as_str(), "x86" | "x86_64");
let msvc_static_crt = is_windows_msvc_target()
&& env::var("CARGO_CFG_TARGET_FEATURE")
.is_ok_and(|features| features.split(',').any(|feature| feature == "crt-static"));
Self {
use_system_lib,
dred_enabled,
presume_avx,
target_arch,
avx_allowed,
msvc_static_crt,
}
}
}
fn emit_rerun_directives() {
println!("cargo:rerun-if-changed=opus/include/opus.h");
println!("cargo:rerun-if-changed=opus/include/opus_defines.h");
println!("cargo:rerun-if-changed=opus/include/opus_types.h");
println!("cargo:rerun-if-changed=opus/include/opus_multistream.h");
println!("cargo:rerun-if-changed=opus/include/opus_projection.h");
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=opus/dnn/download_model.sh");
println!("cargo:rerun-if-env-changed=CARGO_FEATURE_SYSTEM_LIB");
println!("cargo:rerun-if-env-changed=CARGO_FEATURE_PRESUME_AVX2");
println!("cargo:rerun-if-env-changed=CARGO_CFG_TARGET_ENV");
println!("cargo:rerun-if-env-changed=CARGO_CFG_TARGET_FAMILY");
println!("cargo:rerun-if-env-changed=CARGO_CFG_TARGET_FEATURE");
}
fn handle_system_lib(opts: &BuildOptions) {
if opts.dred_enabled {
println!(
"cargo:warning=system-lib feature enabled; ensure the system libopus includes DRED support"
);
}
if opts.presume_avx {
println!(
"cargo:warning=presume-avx2 feature enabled; ensure the system libopus was built with OPUS_X86_PRESUME_AVX2"
);
}
link_system_lib();
}
fn build_bundled_and_link(opts: &BuildOptions) {
if opts.dred_enabled {
ensure_dred_assets();
}
if opts.presume_avx && !opts.avx_allowed {
println!(
"cargo:warning=presume-avx2 feature only applies to x86/x86_64 targets; ignoring for {}",
opts.target_arch
);
}
let dst = build_bundled(opts.dred_enabled, opts.avx_allowed, opts.msvc_static_crt);
println!("cargo:rustc-link-search=native={}/lib", dst.display());
println!("cargo:rustc-link-lib=static=opus");
}
fn build_bundled(
dred_enabled: bool,
presume_avx: bool,
msvc_static_crt: bool,
) -> std::path::PathBuf {
let mut config = cmake::Config::new("opus");
config.profile("Release");
config.static_crt(msvc_static_crt);
config
.define("OPUS_BUILD_SHARED_LIBRARY", "OFF")
.define("OPUS_BUILD_TESTING", "OFF")
.define("OPUS_BUILD_PROGRAMS", "OFF")
.define("OPUS_DRED", if dred_enabled { "ON" } else { "OFF" })
.define(
"OPUS_STATIC_RUNTIME",
if msvc_static_crt { "ON" } else { "OFF" },
)
.define("BUILD_SHARED_LIBS", "OFF")
.define("OPUS_DISABLE_INTRINSICS", "OFF")
.define("CMAKE_POSITION_INDEPENDENT_CODE", "ON");
if presume_avx {
config
.define("OPUS_X86_PRESUME_AVX2", "ON")
.define("OPUS_X86_MAY_HAVE_AVX2", "ON");
}
config.build()
}
fn link_system_lib() {
pkg_config::Config::new()
.atleast_version("1.5.2")
.probe("opus")
.expect("system-lib feature requested but pkg-config couldn't find libopus");
}
fn generate_bindings() {
let bindings_path = std::path::Path::new("src/bindings.rs");
if bindings_path.exists() {
println!(
"cargo:warning=Using existing src/bindings.rs. Delete this file to force regeneration."
);
return;
}
let bindings = bindgen::Builder::default()
.header("opus/include/opus.h")
.header("opus/include/opus_defines.h")
.header("opus/include/opus_types.h")
.header("opus/include/opus_multistream.h")
.header("opus/include/opus_projection.h")
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate bindings");
bindings
.write_to_file(bindings_path)
.expect("Couldn't write bindings!");
}
fn is_windows_msvc_target() -> bool {
matches!(
env::var("CARGO_CFG_TARGET_FAMILY").as_deref(),
Ok("windows")
) && matches!(env::var("CARGO_CFG_TARGET_ENV").as_deref(), Ok("msvc"))
}
fn ensure_dred_assets() {
use std::path::Path;
use std::process::Command;
const REQUIRED_FILE: &str = "opus/dnn/fargan_data.h";
if Path::new(REQUIRED_FILE).exists() {
return;
}
let script = Path::new("opus/dnn/download_model.sh");
if !script.exists() {
panic!("DRED feature requires {script:?}, but it was not found");
}
let status = Command::new("sh")
.arg("dnn/download_model.sh")
.arg("735117b")
.current_dir("opus")
.status()
.expect("failed to spawn DRED model download script");
if !status.success() {
panic!("downloading DRED model assets failed (exit status: {status})");
}
if !Path::new(REQUIRED_FILE).exists() {
panic!("DRED model download completed but {REQUIRED_FILE} is still missing");
}
}