-
Notifications
You must be signed in to change notification settings - Fork 118
/
build.rs
546 lines (480 loc) · 16.6 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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use bindgen::Formatter;
use std::env;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str;
use walkdir::WalkDir;
const ENV_VARS: &'static [&'static str] = &[
"AR",
"AS",
"CC",
"CFLAGS",
"CLANGFLAGS",
"CPP",
"CPPFLAGS",
"CXX",
"CXXFLAGS",
"MAKE",
"MOZTOOLS_PATH",
"MOZJS_FORCE_RERUN",
"PYTHON",
"STLPORT_LIBS",
];
const EXTRA_FILES: &'static [&'static str] = &[
"makefile.cargo",
"src/rustfmt.toml",
"src/jsglue.hpp",
"src/jsglue.cpp",
];
/// Which version of moztools we expect
#[cfg(windows)]
const MOZTOOLS_VERSION: &str = "4.0";
fn main() {
// https://github.com/servo/mozjs/issues/113
env::set_var("MOZCONFIG", "");
// https://github.com/servo/servo/issues/14759
env::set_var("MOZ_NO_DEBUG_RTL", "1");
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let build_dir = out_dir.join("build");
// Used by mozjs downstream, don't remove.
println!("cargo:outdir={}", build_dir.display());
fs::create_dir_all(&build_dir).expect("could not create build dir");
build_jsapi(&build_dir);
build_jsglue(&build_dir);
build_jsapi_bindings(&build_dir);
if env::var_os("MOZJS_FORCE_RERUN").is_none() {
for var in ENV_VARS {
println!("cargo:rerun-if-env-changed={}", var);
}
for entry in WalkDir::new("mozjs") {
let entry = entry.unwrap();
let path = entry.path();
if !ignore(path) {
println!("cargo:rerun-if-changed={}", path.display());
}
}
for file in EXTRA_FILES {
println!("cargo:rerun-if-changed={}", file);
}
}
}
#[cfg(not(windows))]
fn find_make() -> OsString {
if let Some(make) = env::var_os("MAKE") {
make
} else {
match Command::new("gmake").status() {
Ok(gmake) => {
if gmake.success() {
OsStr::new("gmake").to_os_string()
} else {
OsStr::new("make").to_os_string()
}
}
Err(_) => OsStr::new("make").to_os_string(),
}
}
}
fn cc_flags() -> Vec<&'static str> {
let mut result = vec!["-DRUST_BINDGEN", "-DSTATIC_JS_API"];
if env::var_os("CARGO_FEATURE_DEBUGMOZJS").is_some() {
result.extend(&["-DJS_GC_ZEAL", "-DDEBUG", "-DJS_DEBUG"]);
}
let target = env::var("TARGET").unwrap();
if target.contains("windows") {
result.extend(&[
"-std=c++17",
"-DWIN32",
// Don't use reinterpret_cast() in offsetof(),
// since it's not a constant expression, so can't
// be used in static_assert().
"-D_CRT_USE_BUILTIN_OFFSETOF",
]);
} else {
result.extend(&[
"-std=gnu++17",
"-fno-sized-deallocation",
"-Wno-unused-parameter",
"-Wno-invalid-offsetof",
"-Wno-unused-private-field",
]);
}
let is_apple = target.contains("apple");
let is_freebsd = target.contains("freebsd");
if is_apple || is_freebsd {
result.push("-stdlib=libc++");
}
result
}
#[cfg(windows)]
fn cargo_target_dir() -> PathBuf {
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let mut dir = out_dir.as_path();
while let Some(target_dir) = dir.parent() {
if target_dir.file_name().unwrap().to_string_lossy() == "target" {
return target_dir.to_path_buf();
}
dir = target_dir;
}
panic!("$OUT_DIR is not in target")
}
#[cfg(windows)]
fn find_moztools() -> Option<PathBuf> {
let cargo_target_dir = cargo_target_dir();
let deps_dir = cargo_target_dir.join("dependencies");
let moztools_path = deps_dir.join("moztools").join(MOZTOOLS_VERSION);
if moztools_path.exists() {
Some(moztools_path)
} else {
None
}
}
fn build_jsapi(build_dir: &Path) {
let target = env::var("TARGET").unwrap();
let make;
#[cfg(windows)]
{
let moztools = if let Some(moztools) = env::var_os("MOZTOOLS_PATH") {
PathBuf::from(moztools)
} else if let Some(moztools) = find_moztools() {
// moztools already in target/dependencies/moztools-*
moztools
} else if let Some(moz_build) = env::var_os("MOZILLABUILD") {
// For now we also support mozilla build
PathBuf::from(moz_build)
} else if let Some(moz_build) = env::var_os("MOZILLA_BUILD") {
// For now we also support mozilla build
PathBuf::from(moz_build)
} else {
panic!(
"MozTools or MozillaBuild not found!\n \
Follow instructions on: https://github.com/servo/mozjs?tab=readme-ov-file#windows"
);
};
let mut paths = Vec::new();
paths.push(moztools.join("msys2").join("usr").join("bin"));
paths.push(moztools.join("bin"));
paths.extend(env::split_paths(&env::var_os("PATH").unwrap()));
env::set_var("PATH", &env::join_paths(paths).unwrap());
// https://searchfox.org/mozilla-esr115/source/python/mozbuild/mozbuild/util.py#1396
env::set_var("MOZILLABUILD", moztools);
make = OsStr::new("mozmake").to_os_string();
}
#[cfg(not(windows))]
{
make = find_make();
}
let mut cmd = Command::new(make.clone());
let encoding_c_mem_include_dir = env::var("DEP_ENCODING_C_MEM_INCLUDE_DIR").unwrap();
let mut cppflags = OsString::from("-I");
cppflags.push(OsString::from(
encoding_c_mem_include_dir.replace("\\", "/"),
));
cppflags.push(" ");
// add zlib from libz-sys to include path
if let Ok(zlib_include_dir) = env::var("DEP_Z_INCLUDE") {
cppflags.push(format!("-I{} ", zlib_include_dir.replace("\\", "/")));
}
// add zlib.pc into pkg-config's search path
// this is only needed when libz-sys builds zlib from source
if let Ok(zlib_root_dir) = env::var("DEP_Z_ROOT") {
let mut pkg_config_path = OsString::from(format!(
"{}/lib/pkgconfig",
zlib_root_dir.replace("\\", "/")
));
if let Some(env_pkg_config_path) = env::var_os("PKG_CONFIG_PATH") {
pkg_config_path.push(":");
pkg_config_path.push(env_pkg_config_path);
}
cmd.env("PKG_CONFIG_PATH", pkg_config_path);
}
cppflags.push(env::var_os("CPPFLAGS").unwrap_or_default());
cmd.env("CPPFLAGS", cppflags);
if let Some(makeflags) = env::var_os("CARGO_MAKEFLAGS") {
cmd.env("MAKEFLAGS", makeflags);
}
if target.contains("apple") || target.contains("freebsd") {
cmd.env("CXXFLAGS", "-stdlib=libc++");
}
let cargo_manifest_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
let result = cmd
.args(&["-R", "-f"])
.arg(cargo_manifest_dir.join("makefile.cargo"))
.current_dir(&build_dir)
.env("SRC_DIR", &cargo_manifest_dir.join("mozjs"))
.env("NO_RUST_PANIC_HOOK", "1")
.status()
.expect(&format!("Failed to run `{:?}`", make));
assert!(result.success());
println!(
"cargo:rustc-link-search=native={}/js/src/build",
build_dir.display()
);
println!("cargo:rustc-link-lib=static=js_static"); // Must come before c++
if target.contains("windows") {
println!(
"cargo:rustc-link-search=native={}/dist/bin",
build_dir.display()
);
println!("cargo:rustc-link-lib=winmm");
println!("cargo:rustc-link-lib=psapi");
println!("cargo:rustc-link-lib=user32");
println!("cargo:rustc-link-lib=Dbghelp");
if target.contains("gnu") {
println!("cargo:rustc-link-lib=stdc++");
}
} else if target.contains("apple") || target.contains("freebsd") {
println!("cargo:rustc-link-lib=c++");
} else {
println!("cargo:rustc-link-lib=stdc++");
}
}
fn build_jsglue(build_dir: &Path) {
let mut build = cc::Build::new();
build.cpp(true);
for flag in cc_flags() {
build.flag_if_supported(flag);
}
let config = format!("{}/js/src/js-confdefs.h", build_dir.display());
if build.get_compiler().is_like_msvc() {
build.flag_if_supported("-std:c++17");
build.flag("-FI");
} else {
build.flag("-std=c++17");
build.flag("-include");
}
build
.flag(&config)
.file("src/jsglue.cpp")
.include(build_dir.join("dist/include"))
.include(build_dir.join("js/src"))
.out_dir(build_dir.join("glue"))
.compile("jsglue");
}
/// Invoke bindgen on the JSAPI headers to produce raw FFI bindings for use from
/// Rust.
///
/// To add or remove which functions, types, and variables get bindings
/// generated, see the `const` configuration variables below.
fn build_jsapi_bindings(build_dir: &Path) {
// By default, constructors, destructors and methods declared in .h files are inlined,
// so their symbols aren't available. Adding the -fkeep-inlined-functions option
// causes the jsapi library to bloat from 500M to 6G, so that's not an option.
let mut config = bindgen::CodegenConfig::all();
config &= !bindgen::CodegenConfig::CONSTRUCTORS;
config &= !bindgen::CodegenConfig::DESTRUCTORS;
config &= !bindgen::CodegenConfig::METHODS;
let mut builder = bindgen::builder()
.rust_target(bindgen::RustTarget::Stable_1_59)
.header("./src/jsglue.hpp")
// Translate every enum with the "rustified enum" strategy. We should
// investigate switching to the "constified module" strategy, which has
// similar ergonomics but avoids some potential Rust UB footguns.
.rustified_enum(".*")
.size_t_is_usize(true)
.enable_cxx_namespaces()
.with_codegen_config(config)
.formatter(Formatter::Rustfmt)
.clang_arg("-I")
.clang_arg(build_dir.join("dist/include").to_str().expect("UTF-8"))
.clang_arg("-I")
.clang_arg(build_dir.join("js/src").to_str().expect("UTF-8"))
.clang_arg("-x")
.clang_arg("c++");
let target = env::var("TARGET").unwrap();
if target.contains("windows") {
builder = builder.clang_arg("-fms-compatibility");
}
if let Ok(flags) = env::var("CXXFLAGS") {
for flag in flags.split_whitespace() {
builder = builder.clang_arg(flag);
}
}
if let Ok(flags) = env::var("CLANGFLAGS") {
for flag in flags.split_whitespace() {
builder = builder.clang_arg(flag);
}
}
for flag in cc_flags() {
builder = builder.clang_arg(flag);
}
builder = builder.clang_arg("-include");
builder = builder.clang_arg(
build_dir
.join("js/src/js-confdefs.h")
.to_str()
.expect("UTF-8"),
);
println!(
"Generating bindings {:?} {}.",
builder.command_line_flags(),
bindgen::clang_version().full
);
for ty in UNSAFE_IMPL_SYNC_TYPES {
builder = builder.raw_line(format!("unsafe impl Sync for root::{} {{}}", ty));
}
for ty in WHITELIST_TYPES {
builder = builder.allowlist_type(ty);
}
for var in WHITELIST_VARS {
builder = builder.allowlist_var(var);
}
for func in WHITELIST_FUNCTIONS {
builder = builder.allowlist_function(func);
}
for func in BLACKLIST_FUNCTIONS {
builder = builder.blocklist_function(func);
}
for ty in OPAQUE_TYPES {
builder = builder.opaque_type(ty);
}
for ty in BLACKLIST_TYPES {
builder = builder.blocklist_type(ty);
}
for &(module, raw_line) in MODULE_RAW_LINES {
builder = builder.module_raw_line(module, raw_line);
}
let bindings = builder
.generate()
.expect("Should generate JSAPI bindings OK");
bindings
.write_to_file(build_dir.join("jsapi.rs"))
.expect("Should write bindings to file OK");
}
/// JSAPI types for which we should implement `Sync`.
const UNSAFE_IMPL_SYNC_TYPES: &'static [&'static str] = &[
"JSClass",
"JSFunctionSpec",
"JSNativeWrapper",
"JSPropertySpec",
"JSTypedMethodJitInfo",
];
/// Types which we want to generate bindings for (and every other type they
/// transitively use).
const WHITELIST_TYPES: &'static [&'static str] = &["JS.*", "js::.*", "mozilla::.*"];
/// Global variables we want to generate bindings to.
const WHITELIST_VARS: &'static [&'static str] = &[
"JS::NullHandleValue",
"JS::TrueHandleValue",
"JS::UndefinedHandleValue",
"JSCLASS_.*",
"JSFUN_.*",
"JSITER_.*",
"JSPROP_.*",
"JSREG_.*",
"JS_.*",
"js::Proxy.*",
];
/// Functions we want to generate bindings to.
const WHITELIST_FUNCTIONS: &'static [&'static str] = &[
"ExceptionStackOrNull",
"glue::.*",
"JS::.*",
"js::.*",
"JS_.*",
".*_TO_JSID",
"JS_DeprecatedStringHasLatin1Chars",
];
/// Functions we do not want to generate bindings to.
const BLACKLIST_FUNCTIONS: &'static [&'static str] = &[
"JS::CopyAsyncStack",
"JS::CreateError",
"JS::DecodeMultiStencilsOffThread",
"JS::DecodeStencilOffThread",
"JS::EncodeStencil",
"JS::FinishDecodeMultiStencilsOffThread",
"JS::FinishIncrementalEncoding",
"JS::FromPropertyDescriptor",
"JS::GetExceptionCause",
"JS::GetOptimizedEncodingBuildId",
"JS::GetScriptTranscodingBuildId",
"JS::dbg::FireOnGarbageCollectionHook",
"JS_EncodeStringToUTF8BufferPartial",
"JS_GetErrorType",
"JS_GetOwnPropertyDescriptorById",
"JS_GetOwnPropertyDescriptor",
"JS_GetOwnUCPropertyDescriptor",
"JS_GetPropertyDescriptorById",
"JS_GetPropertyDescriptor",
"JS_GetUCPropertyDescriptor",
"JS_NewLatin1String",
"JS_NewUCStringDontDeflate",
"JS_NewUCString",
"JS_PCToLineNumber",
"js::AppendUnique",
"js::SetPropertyIgnoringNamedGetter",
"JS::FinishOffThreadStencil",
"std::.*",
];
/// Types that should be treated as an opaque blob of bytes whenever they show
/// up within a whitelisted type.
///
/// These are types which are too tricky for bindgen to handle, and/or use C++
/// features that don't have an equivalent in rust, such as partial template
/// specialization.
const OPAQUE_TYPES: &'static [&'static str] = &[
"JS::Auto.*Impl",
"JS::StackGCVector.*",
"JS::PersistentRooted.*",
"JS::detail::CallArgsBase.*",
"js::detail::UniqueSelector.*",
"mozilla::BufferList",
"mozilla::Maybe.*",
"mozilla::UniquePtr.*",
"mozilla::Variant",
"mozilla::Hash.*",
"mozilla::detail::Hash.*",
"RefPtr_Proxy.*",
"std::.*",
];
/// Types for which we should NEVER generate bindings, even if it is used within
/// a type or function signature that we are generating bindings for.
const BLACKLIST_TYPES: &'static [&'static str] = &[
// We'll be using libc::FILE.
"FILE",
// We provide our own definition because we need to express trait bounds in
// the definition of the struct to make our Drop implementation correct.
"JS::Heap",
// We provide our own definition because SM's use of templates
// is more than bindgen can cope with.
"JS::Rooted",
// We don't need them and bindgen doesn't like them.
"JS::HandleVector",
"JS::MutableHandleVector",
"JS::Rooted.*Vector",
"JS::RootedValueArray",
// Classes we don't use and we cannot generate theri
// types properly from bindgen so we'll skip them for now.
"JS::dbg::Builder",
"JS::dbg::Builder_BuiltThing",
"JS::dbg::Builder_Object",
"JS::dbg::Builder_Object_Base",
"JS::dbg::BuilderOrigin",
];
/// Definitions for types that were blacklisted
const MODULE_RAW_LINES: &'static [(&'static str, &'static str)] = &[
("root", "pub type FILE = ::libc::FILE;"),
("root::JS", "pub type Heap<T> = crate::jsgc::Heap<T>;"),
("root::JS", "pub type Rooted<T> = crate::jsgc::Rooted<T>;"),
];
/// Rerun this build script if files under mozjs/ changed, unless this returns true.
/// Keep this in sync with .gitignore
fn ignore(path: &Path) -> bool {
// Python pollutes a bunch of source directories with pyc and so files,
// making cargo believe that the crate needs a rebuild just because a
// directory's mtime changed.
if path.is_dir() {
return true;
}
let ignored_extensions = ["pyc", "o", "so", "dll", "dylib"];
path.extension().map_or(false, |extension| {
ignored_extensions
.iter()
.any(|&ignored| extension == ignored)
})
}