summaryrefslogtreecommitdiffstats
path: root/js/src/wasm/cranelift/build.rs
blob: 3c963b373c2c9fd06598dff9daaa155e734e02b4 (plain)
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
/*
 * Copyright 2018 Mozilla Foundation
 *
 * 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.
 */

//! Build script for the Baldr <-> Cranelift bindings.
//!
//! This file is executed by cargo when this crate is built. It generates the
//! `$OUT_DIR/bindings.rs` file which is then included by `src/bindings/low_level.rs`.

extern crate bindgen;

use std::env;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;

fn main() {
    // Tell Cargo to regenerate the bindings if the header file changes.
    println!("cargo:rerun-if-changed=baldrapi.h");

    let mut generator = bindgen::builder()
        .parse_callbacks(Box::new(bindgen::CargoCallbacks))
        .disable_name_namespacing()
        .size_t_is_usize(true)
        // We whitelist the Baldr C functions and get the associated types for free.
        .whitelist_function("env_.*")
        .whitelist_function("global_.*")
        .whitelist_function("table_.*")
        .whitelist_function("funcType_.*")
        .whitelist_function("stackmaps_.*")
        .whitelist_type("Cranelift.*")
        // The enum classes defined in baldrapi.h and WasmBinaryConstants are all Rust-safe.
        .rustified_enum("BD_.*|Trap|TypeCode|TypeIdDescKind")
        .whitelist_type("BD_.*|Trap|TypeCode|TypeIdDescKind")
        .header("baldrapi.h")
        .clang_args(&[
            "-x",
            "c++",
            "-std=gnu++14",
            "-fno-sized-deallocation",
            "-fno-aligned-new",
            "-DRUST_BINDGEN",
        ])
        .clang_arg("-I../..");

    match env::var_os("MOZ_TOPOBJDIR") {
        Some(path) => {
            let path = PathBuf::from(path).join("js/src/rust/extra-bindgen-flags");

            let mut extra_flags = String::new();
            File::open(&path)
                .expect("Failed to open extra-bindgen-flags file")
                .read_to_string(&mut extra_flags)
                .expect("Failed to read extra-bindgen-flags file");

            let display_path = path.to_str().expect("path is utf8 encoded");
            println!("cargo:rerun-if-changed={}", display_path);

            let extra_flags: Vec<String> = extra_flags
                .split_whitespace()
                .map(|s| s.to_owned())
                .collect();
            for flag in extra_flags {
                generator = generator.clang_arg(flag);
            }
        }
        None => {
            println!("cargo:warning=MOZ_TOPOBJDIR should be set by default, otherwise the build is not guaranted to finish.");
        }
    }

    let command_line_opts = generator.command_line_flags();

    // In case of error, bindgen prints to stderr, and the yielded error is the empty type ().
    let bindings = generator.generate().unwrap_or_else(|_err| {
        panic!(
            r#"Unable to generate baldrapi.h bindings:
- flags: {}
"#,
            command_line_opts.join(" "),
        );
    });

    // Write the bindings to the $OUT_DIR/bindings.rs file.
    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("Couldn't write bindings!");
}