Bug 1490948: Add support for extra bindgen flags when compiling Cranelift; r=froydnj
authorBenjamin Bouvier <benj@benj.me>
Tue, 25 Sep 2018 15:31:22 +0200
changeset 438677 2225cbe1f042a75fc02177dd4daa86c3208997d7
parent 438676 537457dc2f47fa9436c5abb6b27977ae9727e059
child 438678 e58d578f13d990ac2064d38692b2440bc96f9e2f
push id34733
push usertoros@mozilla.com
push dateFri, 28 Sep 2018 15:39:37 +0000
treeherdermozilla-central@156f9442db84 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1490948
milestone64.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1490948: Add support for extra bindgen flags when compiling Cranelift; r=froydnj Some build flags are being passed by the build system: they're passed in a text file called extra-bindgen-flags.in that's filled at configure time. Other flags have to be inferred from the current target/host combination, in Cranelift's build script directly. This is mostly cargo-culted from the ServoBindings.toml file, and should probably be merged in the build system at some point. Some Windows-specific adjustments were needed to provide access to libclang for bindgen support, by adding clang-cl to the plain Spidermonkey Windows builds.
build/moz.configure/toolchain.configure
js/src/old-configure.in
js/src/rust/extra-bindgen-flags.in
js/src/rust/moz.build
js/src/wasm/cranelift/build.rs
layout/style/bindgen.toml.in
old-configure.in
taskcluster/ci/spidermonkey/windows.yml
--- a/build/moz.configure/toolchain.configure
+++ b/build/moz.configure/toolchain.configure
@@ -1111,20 +1111,20 @@ include('compile-checks.configure')
          try_compile(body='static_assert(sizeof(void *) == 8, "")',
                      check_msg='for 64-bit OS'))
 def check_have_64_bit(have_64_bit, compiler_have_64_bit):
     if have_64_bit != compiler_have_64_bit:
         configure_error('The target compiler does not agree with configure '
                         'about the target bitness.')
 
 
-option(env='BINDGEN_CFLAGS',
-       nargs=1,
-       default=bindgen_cflags_defaults,
-       help='Options bindgen should pass to the C/C++ parser')
+js_option(env='BINDGEN_CFLAGS',
+          nargs=1,
+          default=bindgen_cflags_defaults,
+          help='Options bindgen should pass to the C/C++ parser')
 
 
 @depends('BINDGEN_CFLAGS')
 @checking('bindgen cflags', lambda s: s if s else 'no')
 def bindgen_cflags(value):
     if value and len(value):
         return value[0].split()
 
--- a/js/src/old-configure.in
+++ b/js/src/old-configure.in
@@ -1815,16 +1815,19 @@ fi
 if test "$BUILDING_RELEASE"; then
   # Override value in defines.sh, if any
   EARLY_BETA_OR_EARLIER=
 elif test "$EARLY_BETA_OR_EARLIER"; then
   AC_DEFINE(EARLY_BETA_OR_EARLIER)
 fi
 AC_SUBST(EARLY_BETA_OR_EARLIER)
 
+BINDGEN_SYSTEM_FLAGS="$_BINDGEN_CFLAGS $NSPR_CFLAGS"
+AC_SUBST(BINDGEN_SYSTEM_FLAGS)
+
 dnl ========================================================
 dnl JavaScript shell
 dnl ========================================================
 
 MOZ_CHECK_ALLOCATOR
 
 AC_CHECK_FUNCS(localeconv)
 
new file mode 100644
--- /dev/null
+++ b/js/src/rust/extra-bindgen-flags.in
@@ -0,0 +1,1 @@
+@BINDGEN_SYSTEM_FLAGS@
--- a/js/src/rust/moz.build
+++ b/js/src/rust/moz.build
@@ -1,16 +1,18 @@
 # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # 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/.
 
 RustLibrary('jsrust')
 
+CONFIGURE_SUBST_FILES += ['extra-bindgen-flags']
+
 if CONFIG['JS_SHARED_LIBRARY']:
     FINAL_LIBRARY = 'js'
 
 if CONFIG['OS_ARCH'] == 'Darwin':
     # The Rust standard library references libresolv on macOS, so we need to
     # link it as a workaround. See also bug 1367932.
     OS_LIBS += ['-lresolv']
 elif CONFIG['OS_ARCH'] == 'WINNT':
--- a/js/src/wasm/cranelift/build.rs
+++ b/js/src/wasm/cranelift/build.rs
@@ -17,36 +17,116 @@
 //! 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;
 
+enum Arch {
+    X86,
+    X64,
+    Arm,
+    Aarch64
+}
+
 fn main() {
     // Tell Cargo to regenerate the bindings if the header file changes.
     println!("cargo:rerun-if-changed=baldrapi.h");
 
-    let bindings = bindgen::builder()
+    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=c++14"])
-        .clang_arg("-I../..")
+        .clang_args(&["-x", "c++", "-std=gnu++14", "-fno-sized-deallocation", "-DRUST_BINDGEN"])
+        .clang_arg("-I../..");
+
+    let arch = {
+        let target_arch = env::var("CARGO_CFG_TARGET_ARCH");
+        match target_arch.as_ref().map(|x| x.as_str()) {
+            Ok("aarch64") => Arch::Aarch64,
+            Ok("arm") => Arch::Arm,
+            Ok("x86") => Arch::X86,
+            Ok("x86_64") => Arch::X64,
+            _ => panic!("unknown arch")
+        }
+    };
+
+    match env::var("CARGO_CFG_TARGET_OS").as_ref().map(|x| x.as_str()) {
+        Ok("android") => {
+            bindings = bindings.clang_arg("-DOS_ANDROID=1");
+            bindings = match arch {
+                Arch::Aarch64 => { bindings.clang_arg("--target=aarch64-linux-android") }
+                Arch::Arm => { bindings.clang_arg("--target=armv7-linux-androideabi") }
+                Arch::X86 => { bindings.clang_arg("--target=i686-linux-android") }
+                Arch::X64 => { bindings.clang_arg("--target=x86_64-linux-android") }
+            };
+        }
+        Ok("linux") | Ok("freebsd") | Ok("dragonfly") | Ok("openbsd") | Ok("bitrig") | Ok("netbsd")
+            | Ok("macos") | Ok("ios") => {
+            // Nothing to do in particular for these OSes, until proven the contrary.
+        }
+        Ok("windows") => {
+            bindings = bindings.clang_arg("-DOS_WIN=1")
+                .clang_arg("-DWIN32=1");
+            bindings = match env::var("CARGO_CFG_TARGET_ENV").as_ref().map(|x| x.as_str()) {
+                Ok("msvc") => {
+                    bindings = bindings.clang_arg("-fms-compatibility-version=19");
+                    bindings = bindings.clang_arg("-D_CRT_USE_BUILTIN_OFFSETOF");
+                    bindings = bindings.clang_arg("-DHAVE_VISIBILITY_HIDDEN_ATTRIBUTE=1");
+                    match arch {
+                        Arch::X86 => { bindings.clang_arg("--target=i686-pc-win32") },
+                        Arch::X64 => { bindings.clang_arg("--target=x86_64-pc-win32") },
+                        Arch::Aarch64 => { bindings.clang_arg("--target=aarch64-pc-windows-msvc") }
+                        _ => panic!("unknown Windows architecture for msvc build")
+                    }
+                }
+                Ok("gnu") => {
+                    match arch {
+                        Arch::X86 => { bindings.clang_arg("--target=i686-pc-mingw32") },
+                        Arch::X64 => { bindings.clang_arg("--target=x86_64-w64-mingw32") },
+                        _ => panic!("unknown Windows architecture for gnu build")
+                    }
+                }
+                _ => panic!("unknown Windows build environment")
+            };
+        }
+        os => panic!("unknown target os {:?}!", os)
+    }
+
+    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!");
--- a/layout/style/bindgen.toml.in
+++ b/layout/style/bindgen.toml.in
@@ -1,4 +1,4 @@
 [build]
 args = [
-    @BINDGEN_SYSTEM_FLAGS@
+    @BINDGEN_SYSTEM_TOML_FLAGS@
 ]
--- a/old-configure.in
+++ b/old-configure.in
@@ -3941,17 +3941,19 @@ esac
 
 AC_SUBST(MOZ_TREE_CAIRO)
 AC_SUBST_LIST(MOZ_CAIRO_CFLAGS)
 AC_SUBST_LIST(MOZ_CAIRO_LIBS)
 AC_SUBST_LIST(MOZ_CAIRO_OSLIBS)
 AC_SUBST(MOZ_TREE_PIXMAN)
 
 BINDGEN_SYSTEM_FLAGS="$_BINDGEN_CFLAGS $NSPR_CFLAGS $NSS_CFLAGS $MOZ_PIXMAN_CFLAGS $MOZ_CAIRO_CFLAGS"
-AC_SUBST_TOML_LIST(BINDGEN_SYSTEM_FLAGS)
+AC_SUBST(BINDGEN_SYSTEM_FLAGS)
+BINDGEN_SYSTEM_TOML_FLAGS="$BINDGEN_SYSTEM_FLAGS"
+AC_SUBST_TOML_LIST(BINDGEN_SYSTEM_TOML_FLAGS)
 
 dnl ========================================================
 dnl disable xul
 dnl ========================================================
 MOZ_ARG_DISABLE_BOOL(xul,
 [  --disable-xul           Disable XUL],
     MOZ_XUL= )
 if test "$MOZ_XUL"; then
--- a/taskcluster/ci/spidermonkey/windows.yml
+++ b/taskcluster/ci/spidermonkey/windows.yml
@@ -14,33 +14,36 @@ sm-plain-win32/debug:
     index:
         job-name: sm-plain-win32-debug
     treeherder:
         platform: windows2012-32/debug
         symbol: SM(p)
     run:
         spidermonkey-variant: plaindebug
     toolchains:
+        - win64-clang-cl
         - win64-rust
 
 sm-plain-win32/opt:
     description: "Spidermonkey Plain"
     index:
         job-name: sm-plain-win32-opt
     treeherder:
         platform: windows2012-32/opt
         symbol: SM(p)
     run:
         spidermonkey-variant: plain
     toolchains:
+        - win64-clang-cl
         - win64-rust
 
 sm-compacting-win32/debug:
     description: "Spidermonkey Compacting"
     index:
         job-name: sm-compacting-win32-debug
     treeherder:
         platform: windows2012-32/debug
         symbol: SM(cgc)
     run:
         spidermonkey-variant: compacting
     toolchains:
+        - win64-clang-cl
         - win64-rust