-
Notifications
You must be signed in to change notification settings - Fork 64
/
build.rs
402 lines (364 loc) · 15 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
mod cmd_finder;
use std::env;
use std::env::consts;
use std::fs;
use std::path::{Path, PathBuf};
static SHADERC_STATIC_LIB: &str = "shaderc_combined";
static SHADERC_SHARED_LIB: &str = "shaderc_shared";
static SHADERC_STATIC_LIB_FILE_UNIX: &str = "libshaderc_combined.a";
static SHADERC_STATIC_LIB_FILE_WIN: &str = "shaderc_combined.lib";
static MIN_VULKAN_SDK_VERSION: u32 = 182;
fn get_apple_sdk_path() -> Option<PathBuf> {
let target = std::env::var("TARGET").unwrap();
use std::process::Command;
// tvOS (and the simulator) could be added here in the future.
let sdk = if target == "x86_64-apple-ios"
|| target == "i386-apple-ios"
|| target == "aarch64-apple-ios-sim"
{
"iphonesimulator"
} else if target == "aarch64-apple-ios"
|| target == "armv7-apple-ios"
|| target == "armv7s-apple-ios"
{
"iphoneos"
} else {
return None;
};
let output = if let Ok(out) = Command::new("xcrun")
.args(["--sdk", sdk, "--show-sdk-path"])
.output()
{
out.stdout
} else {
return None;
};
let prefix_str = std::str::from_utf8(&output).expect("invalid output from `xcrun`");
Some(PathBuf::from(prefix_str.trim_end().to_string()))
}
fn build_shaderc_unix(shaderc_dir: &PathBuf, use_ninja: bool, target_os: String) -> PathBuf {
let mut config = cmake::Config::new(shaderc_dir);
config
.profile("Release")
// CMake options
.define("CMAKE_INSTALL_LIBDIR", "lib")
.define("CMAKE_POSITION_INDEPENDENT_CODE", "ON")
// Glslang options
.define("ENABLE_SPVREMAPPER", "OFF")
.define("ENABLE_GLSLANG_BINARIES", "OFF")
// Shaderc options
.define("SHADERC_SKIP_TESTS", "ON")
// SPIRV-Tools options
.define("SPIRV_SKIP_EXECUTABLES", "ON")
.define("SPIRV_WERROR", "OFF");
if use_ninja {
config.generator("Ninja");
}
if target_os == "ios" {
if let Some(path) = get_apple_sdk_path() {
config.define("CMAKE_OSX_SYSROOT", path);
}
}
config.build()
}
fn build_shaderc_msvc(shaderc_dir: &PathBuf) -> PathBuf {
let linkage = env::var("CARGO_CFG_TARGET_FEATURE").unwrap_or_default();
let mut config = cmake::Config::new(shaderc_dir);
config
.profile("Release")
// CMake options
.define("CMAKE_INSTALL_LIBDIR", "lib")
.define("CMAKE_POSITION_INDEPENDENT_CODE", "ON")
// Glslang options
.define("ENABLE_SPVREMAPPER", "OFF")
.define("ENABLE_GLSLANG_BINARIES", "OFF")
// Shaderc options
.define("SHADERC_SKIP_TESTS", "ON")
// SPIRV-Tools options
.define("SPIRV_SKIP_EXECUTABLES", "ON")
.define("SPIRV_WERROR", "OFF")
.generator("Ninja");
// cmake-rs tries to be clever on Windows by injecting several
// C/C++ flags, which causes problems. So I have to manually
// define CMAKE_*_FLAGS_* here to suppress that.
let config = if linkage.contains("crt-static") {
// statically-linked CRT
config
.define("CMAKE_C_FLAGS", " /nologo /EHsc /MT")
.define("CMAKE_CXX_FLAGS", " /nologo /EHsc /MT")
.define("CMAKE_C_FLAGS_RELEASE", " /nologo /EHsc /MT")
.define("CMAKE_CXX_FLAGS_RELEASE", " /nologo /EHsc /MT")
} else {
// dynamically-linked CRT
config
.define("CMAKE_C_FLAGS", " /nologo /EHsc /MD")
.define("CMAKE_CXX_FLAGS", " /nologo /EHsc /MD")
.define("CMAKE_C_FLAGS_RELEASE", " /nologo /EHsc /MD")
.define("CMAKE_CXX_FLAGS_RELEASE", " /nologo /EHsc /MD")
// prevent shaderc's cmake script messes with crt flags
.define("SHADERC_ENABLE_SHARED_CRT", "ON")
};
config.build()
}
fn check_vulkan_sdk_version(path: &Path) -> Result<(), Box<dyn std::error::Error>> {
let xml = std::fs::read_to_string(
path.join("share")
.join("vulkan")
.join("registry")
.join("vk.xml"),
)
.map_err(|error| format!("could not read vk.xml in $VULKAN_SDK: {error}"))?;
let tree = roxmltree::Document::parse(&xml)
.map_err(|error| format!("vk.xml in $VULKAN_SDK is not a valid XML document: {error}"))?;
let version = tree
.root()
.descendants()
.find(|node| node.has_tag_name("types"))
.ok_or("invalid vk.xml in $VULKAN_SDK is invalid: missing <types> node")?
.descendants()
.find(|node| node.text() == Some("VK_HEADER_VERSION"))
.ok_or("invalid vk.xml in $VULKAN_SDK is invalid: missing VK_HEADER_VERSION node")?
.tail()
.ok_or("invalid vk.xml in $VULKAN_SDK: no vesion string")?
.trim()
.parse::<u32>()?;
if version < MIN_VULKAN_SDK_VERSION {
return Err(Box::from(format!(
"requires Vulkan SDK patch version to be at least {MIN_VULKAN_SDK_VERSION}"
)));
}
Ok(())
}
fn host_target() -> String {
let output = std::process::Command::new("rustc")
.arg("-vV")
.output()
.expect("failed to invoke rustc");
std::str::from_utf8(&output.stdout)
.expect("rustc didn't return valid UTF-8")
.lines()
.find_map(|l| l.strip_prefix("host: "))
.expect("failed to query rustc for the host target triple")
.to_owned()
}
fn main() {
// Don't attempt to build shaderc native library on docs.rs when cross-compiling.
if env::var("DOCS_RS").is_ok() {
let is_cross_compiling = env::var("TARGET").unwrap() != host_target();
if is_cross_compiling {
println!(
"cargo:warning=shaderc: docs.rs cross-compilation detected, will not attempt to \
link against shaderc",
);
return;
}
}
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.
let mut search_dir = if let Ok(lib_dir) = env::var("SHADERC_LIB_DIR") {
println!("cargo:warning=shaderc: searching native shaderc libraries in '{lib_dir}'");
Some(lib_dir)
} else {
None
};
// Try to find native shaderc library from Vulkan SDK if possible.
if search_dir.is_none() {
search_dir = if let Ok(sdk_dir) = env::var("VULKAN_SDK") {
check_vulkan_sdk_version(Path::new(&sdk_dir)).unwrap();
println!("cargo:warning=shaderc: searching native shaderc libraries in Vulkan SDK '{sdk_dir}/lib'");
Some(format!("{sdk_dir}/lib/"))
} else {
None
};
}
// If no explicit path is set and no explicit request is made to build from
// source, check known system locations before falling back to build from source.
// This set `search_dir` for later usage.
if search_dir.is_none() && !config_build_from_source {
println!(
"cargo:warning=shaderc: searching for native shaderc libraries on system; \
use '--features build-from-source' to force building from source code"
);
if target_os == "macos" {
// Vulkan SDK is installed in `/usr/local/` by default on macOS
let macos_path = "/usr/local/lib/";
if Path::new(macos_path).exists() {
search_dir = Some(macos_path.to_owned());
}
} else if target_os == "linux" {
// https://wiki.ubuntu.com/MultiarchSpec
// https://wiki.debian.org/Multiarch/Implementation
let debian_arch = match env::var("CARGO_CFG_TARGET_ARCH").unwrap() {
arch if arch == "x86" => "i386".to_owned(),
arch => arch,
};
let debian_triple_path = format!("/usr/lib/{debian_arch}-linux-gnu/");
search_dir = if Path::new(&debian_triple_path).exists() {
// Debian, Ubuntu and their derivatives.
Some(debian_triple_path)
} else if env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "x86_64"
&& Path::new("/usr/lib64/").exists()
{
// Other distributions running on x86_64 usually use this path.
Some("/usr/lib64/".to_owned())
} else {
// Other distributions, not x86_64.
Some("/usr/lib/".to_owned())
};
}
}
// Canonicalize the search directory first.
let search_dir = if let Some(search_dir) = search_dir {
let path = Path::new(&search_dir);
let cannonical = fs::canonicalize(path);
if path.is_relative() {
println!(
"cargo:warning=shaderc: the given search path '{path:?}' is relative; \
path must be relative to shaderc-sys crate, \
likely not your current working directory"
);
} else if !path.is_dir() {
println!("cargo:warning=shaderc: the given search path '{path:?}' is not a directory");
}
if (cannonical.is_err()) && has_explicit_set_search_dir {
println!("cargo:warning=shaderc: {:?}", cannonical.err().unwrap());
println!(
"cargo:warning=shaderc: failed to canonicalize the given search path '{path:?}'"
);
None
} else {
cannonical.ok()
}
} else {
None
};
// Try to build with the dynamic or static library if a path was explicit set
// or implicitly chosen.
if let Some(search_dir) = search_dir {
let search_dir_str = search_dir.to_string_lossy();
let static_lib_path = search_dir.join(if target_os == "windows" && target_env == "msvc" {
SHADERC_STATIC_LIB_FILE_WIN
} else {
SHADERC_STATIC_LIB_FILE_UNIX
});
let dylib_name = format!(
"{}{}{}",
consts::DLL_PREFIX,
SHADERC_SHARED_LIB,
consts::DLL_SUFFIX
);
let dylib_path = search_dir.join(dylib_name);
if let Some((lib_name, lib_kind)) = {
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()) {
("linux", _) => {
println!("cargo:rustc-link-search=native={search_dir_str}");
println!("cargo:rustc-link-lib={lib_kind}={lib_name}");
println!("cargo:rustc-link-lib=dylib=stdc++");
return;
}
("windows", "msvc") => {
println!("cargo:warning=shaderc: Windows MSVC static build is experimental");
println!("cargo:rustc-link-search=native={search_dir_str}");
println!("cargo:rustc-link-lib={lib_kind}={lib_name}");
return;
}
("windows", "gnu") => {
println!("cargo:warning=shaderc: Windows MinGW static build is experimental");
println!("cargo:rustc-link-search=native={search_dir_str}");
println!("cargo:rustc-link-lib={lib_kind}={lib_name}");
println!("cargo:rustc-link-lib=dylib=stdc++");
return;
}
("macos", _) => {
println!("cargo:warning=shaderc: macOS static build is experimental");
println!("cargo:rustc-link-search=native={search_dir_str}");
println!("cargo:rustc-link-lib={lib_kind}={lib_name}");
println!("cargo:rustc-link-lib=dylib=c++");
return;
}
("ios", _) => {
println!("cargo:warning=shaderc: macOS static build is experimental");
println!("cargo:rustc-link-search=native={search_dir_str}");
println!("cargo:rustc-link-lib={lib_kind}={lib_name}");
println!("cargo:rustc-link-lib=dylib=c++");
return;
}
(_, _) => {
println!(
"cargo:warning=shaderc: unsupported platform for linking against \
native shaderc libraries installed on system"
);
}
}
}
}
if config_build_from_source {
println!("cargo:warning=shaderc: requested to build from source");
} else {
println!(
"cargo:warning=shaderc: cannot find native shaderc library on system; \
falling back to build from source"
);
}
let mut finder = cmd_finder::CommandFinder::new();
finder.must_have("cmake");
finder.must_have("git");
finder
.maybe_have("python3")
.or(finder.maybe_have("python"))
.unwrap_or_else(|| {
panic!("Build requires one of `python3` or `python`");
});
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let shaderc_dir = Path::new(&manifest_dir).join("build");
let mut lib_path = if target_env == "msvc" {
finder.must_have("ninja");
build_shaderc_msvc(&shaderc_dir)
} else {
let has_ninja = finder.maybe_have("ninja").is_some();
build_shaderc_unix(&shaderc_dir, has_ninja, target_os)
};
lib_path.push("lib");
println!("cargo:rustc-link-search=native={}", lib_path.display());
println!("cargo:rustc-link-lib=static={SHADERC_STATIC_LIB}");
emit_std_cpp_link();
}
fn emit_std_cpp_link() {
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
match (target_os.as_str(), target_env.as_str()) {
("linux", _) | ("windows", "gnu") => println!("cargo:rustc-link-lib=dylib=stdc++"),
("macos", _) => println!("cargo:rustc-link-lib=dylib=c++"),
_ => {}
}
}