forked from google/shaderc-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
277 lines (249 loc) · 10.7 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
// 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: &str = "libshaderc_combined.a";
static SHADERC_STATIC_LIB_FILE_MSVC: &str = "shaderc_combined.lib";
fn build_shaderc(shaderc_dir: &PathBuf, use_ninja: bool) -> PathBuf {
let mut config = cmake::Config::new(shaderc_dir);
config
.profile("Release")
.define("CMAKE_POSITION_INDEPENDENT_CODE", "ON")
.define("SPIRV_SKIP_EXECUTABLES", "ON")
.define("SPIRV_WERROR", "OFF")
.define("SHADERC_SKIP_TESTS", "ON")
.define("CMAKE_INSTALL_LIBDIR", "lib");
if use_ninja {
config.generator("Ninja");
}
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")
.define("CMAKE_POSITION_INDEPENDENT_CODE", "ON")
.define("SPIRV_SKIP_EXECUTABLES", "ON")
.define("SPIRV_WERROR", "OFF")
.define("SHADERC_SKIP_TESTS", "ON")
.define("CMAKE_INSTALL_LIBDIR", "lib")
.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 probe_linux_lib_kind(lib_name: &str, search_dir: &PathBuf) -> Option<&'static str> {
if search_dir.join(&format!("lib{}.so", lib_name)).exists() {
Some("dylib")
} else if search_dir.join(&format!("lib{}.a", lib_name)).exists() {
Some("static")
} else {
None
}
}
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 explicit_lib_dir_set = env::var("SHADERC_LIB_DIR").is_ok();
// Initialize explicit libshaderc search directory first
let mut search_dir = if let Ok(lib_dir) = env::var("SHADERC_LIB_DIR") {
println!(
"cargo:warning=Specified {} to search for shaderc libraries",
lib_dir
);
Some(lib_dir)
} else {
None
};
// If no explicit path is set and no explicit request is made to build from
// source, check known locations before falling back to from-source-build
if search_dir.is_none() && target_os == "linux" && !config_build_from_source {
println!(
"cargo:warning=Checking for system installed libraries. \
Use --features = build-from-source to disable this behavior"
);
search_dir = Some("/usr/lib/".to_owned());
}
// Try to build with the static lib if a path was received or chosen
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=Provided path {:?} is relative. Path must be \
relative to shaderc-sys crate, possibly not your current \
working directory",
&path
);
} else if !path.is_dir() {
println!("cargo:warning=Provided path {:?} is not a directory", &path);
}
if (cannonical.is_err()) && explicit_lib_dir_set {
println!("cargo:warning={:?}", cannonical.err().unwrap());
println!(
"cargo:warning=Provided path {:?} could not be canonicallized",
&path
);
None
} else {
cannonical.ok()
}
} else {
None
};
if let Some(search_dir) = search_dir {
let search_dir_str = search_dir.to_string_lossy();
let combined_lib_path =
search_dir.join(if target_os == "windows" && target_env == "msvc" {
SHADERC_STATIC_LIB_FILE_MSVC
} else {
SHADERC_STATIC_LIB_FILE
});
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, kind)) = {
if combined_lib_path.exists() {
Some((SHADERC_STATIC_LIB, "static"))
} else if dylib_path.exists() {
Some((SHADERC_SHARED_LIB, "dylib"))
} else {
None
}
} {
match (target_os.as_str(), target_env.as_str()) {
("linux", _) => {
println!("cargo:rustc-link-search=native={}", search_dir_str);
// Libraries from the SPIRV-Tools project
let spirv_tools_lib = probe_linux_lib_kind("SPIRV-Tools", &search_dir);
let spirv_tools_opt_lib = probe_linux_lib_kind("SPIRV-Tools-opt", &search_dir);
// Libraries from the Glslang project
let spirv_lib = probe_linux_lib_kind("SPIRV", &search_dir);
let glslang_lib = probe_linux_lib_kind("glslang", &search_dir);
match (spirv_tools_lib, spirv_tools_opt_lib, spirv_lib, glslang_lib) {
(Some(spirv_tools), Some(spirv_tools_opt), Some(spirv), Some(glslang)) => {
println!("cargo:warning=Found and linking system installed Glslang and SPIRV-Tools libraries.");
println!("cargo:rustc-link-lib={}=glslang", glslang);
println!("cargo:rustc-link-lib={}=SPIRV", spirv);
println!(
"cargo:rustc-link-lib={}=SPIRV-Tools",
spirv_tools
);
println!(
"cargo:rustc-link-lib={}=SPIRV-Tools-opt",
spirv_tools_opt
);
}
_ => {
println!(
"cargo:warning=Only shaderc library found. \
Assuming libraries it depends on are built into \
the shaderc library."
);
}
}
println!("cargo:rustc-link-lib={}={}", kind, lib_name);
println!("cargo:rustc-link-lib=dylib=stdc++");
return;
}
("windows", "msvc") => {
println!("cargo:warning=Windows MSVC static builds experimental");
println!("cargo:rustc-link-search=native={}", search_dir_str);
println!("cargo:rustc-link-lib={}={}", kind, lib_name);
return;
}
("windows", "gnu") => {
println!("cargo:warning=Windows MinGW static builds experimental");
println!("cargo:rustc-link-search=native={}", search_dir_str);
println!("cargo:rustc-link-lib={}={}", kind, lib_name);
println!("cargo:rustc-link-lib=dylib=stdc++");
return;
}
("macos", _) => {
println!("cargo:warning=MacOS static builds experimental");
println!("cargo:rustc-link-search=native={}", search_dir_str);
println!("cargo:rustc-link-lib={}={}", kind, lib_name);
println!("cargo:rustc-link-lib=dylib=c++");
return;
}
(_, _) => {
println!("cargo:warning=Platform unsupported for linking against system installed shaderc libraries");
}
}
}
}
if config_build_from_source {
println!("cargo:warning=Requested to build from source");
} else {
println!(
"cargo:warning=System installed library not found. Falling back \
to build from source"
);
}
let mut finder = cmd_finder::CommandFinder::new();
finder.must_have("cmake");
finder.must_have("git");
finder.must_have("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(&shaderc_dir, has_ninja)
};
lib_path.push("lib");
println!("cargo:rustc-link-search=native={}", lib_path.display());
println!("cargo:rustc-link-lib=static=shaderc_combined");
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++"),
_ => {}
}
}