js/src/wasm/cranelift/build.rs
author Benjamin Bouvier <benj@benj.me>
Tue, 09 Oct 2018 15:01:52 +0200
changeset 441237 1f5478a63db8a1f606ad2fd6ead34b5d91712357
parent 439147 231b53d76122680bb682eb74cc73615a9a554f31
child 445108 35187257bbd8780da15a24d8d04e167c4ddf4caa
permissions -rw-r--r--
Bug 1495669: Share bindgen flags globally; r=emilio, r=froydnj

/*
 * 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/baldrapi.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 bindings = bindgen::builder()
        .disable_name_namespacing()
        // 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_type("Cranelift.*")
        // The enum classes defined in baldrapi.h and WasmBinaryConstants are all Rust-safe.
        .rustified_enum("BD_.*|Trap|TypeCode|FuncTypeIdDescKind")
        .whitelist_type("BD_.*|Trap|TypeCode|FuncTypeIdDescKind")
        .header("baldrapi.h")
        .clang_args(&["-x", "c++", "-std=gnu++14", "-fno-sized-deallocation", "-DRUST_BINDGEN"])
        .clang_arg("-I../..");

    let path = PathBuf::from(env::var_os("MOZ_TOPOBJDIR").unwrap()).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 {
        bindings = bindings.clang_arg(flag);
    }

    let bindings = bindings
        .generate()
        .expect("Unable to generate baldrapi.h bindings");

    // 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!");
}