Bug 1495669: Share bindgen flags globally; r=emilio, r=froydnj

--HG--
extra : rebase_source : 83bfa2a61e523048d0e618ed5920c4aa143a2582
extra : histedit_source : cfc8de67959c5a2dffc5fea3ee5be172bfc370e1%2C53a6b4c0de444b4a7ee04cdd015568069bca893a
This commit is contained in:
Benjamin Bouvier 2018-10-09 15:01:52 +02:00
parent 7ca0f1b58d
commit db0cd81fb4
6 changed files with 137 additions and 196 deletions

View File

@ -319,7 +319,7 @@ imply_option('--with-toolchain-prefix', android_toolchain_prefix,
@depends(extra_toolchain_flags, android_toolchain,
android_toolchain_prefix_base, '--help')
@imports(_from='os.path', _import='isdir')
def bindgen_cflags_defaults(toolchain_flags, toolchain, toolchain_prefix, _):
def bindgen_cflags_android(toolchain_flags, toolchain, toolchain_prefix, _):
if not toolchain_flags:
return
@ -329,10 +329,10 @@ def bindgen_cflags_defaults(toolchain_flags, toolchain, toolchain_prefix, _):
gcc_include = os.path.join(
toolchain, 'lib', 'gcc', toolchain_prefix, '4.9')
cflags_format = "%s -I%s -I%s"
return cflags_format % (' '.join(toolchain_flags),
os.path.join(gcc_include, 'include'),
os.path.join(gcc_include, 'include-fixed'))
return toolchain_flags + [
'-I%s' % os.path.join(gcc_include, 'include'),
'-I%s' % os.path.join(gcc_include, 'include-fixed'),
]
@depends(host, ndk)

View File

@ -212,3 +212,131 @@ def bindgen_config_paths(llvm_config, libclang_path, clang_path,
set_config('MOZ_LIBCLANG_PATH', bindgen_config_paths.libclang_path)
set_config('MOZ_CLANG_PATH', bindgen_config_paths.clang_path)
@depends(host, target, target_is_unix, c_compiler, bindgen_cflags_android)
def basic_bindgen_cflags(host, target, is_unix, compiler_info, android_cflags):
args = [
'-x', 'c++', '-std=gnu++14', '-fno-sized-deallocation',
'-DTRACING=1', '-DIMPL_LIBXUL', '-DMOZILLA_INTERNAL_API',
'-DRUST_BINDGEN'
]
if is_unix:
args += ['-DOS_POSIX=1']
if target.os == 'Android':
args += android_cflags
def handle_cpu(obj):
if 'cpu' in obj and target.cpu in obj['cpu']:
return obj['cpu'][target.cpu]
return []
if target.os == 'WINNT' and host.raw_os.startswith('gnu'):
args += handle_cpu({
'cpu': {
'x86': ['--target=i686-pc-mingw32'],
'x86_64': ['--target=x86_64-w64-mingw32'],
},
})
os_dict = {
'Android': {
'default': ['-DOS_ANDROID=1'],
'cpu': {
'aarch64': ['--target=aarch64-linux-android'],
'arm': ['--target=armv7-linux-androideabi'],
'x86': ['--target=i686-linux-android'],
'x86_64': ['--target=x86_64-linux-android'],
},
},
'DragonFly': {
'default': ['-DOS_BSD=1', '-DOS_DRAGONFLY=1'],
},
'FreeBSD': {
'default': ['-DOS_BSD=1', '-DOS_FREEBSD=1'],
},
'GNU': {
'default': ['-DOS_LINUX=1'],
'cpu': {
'x86': ['-m32'],
'x86_64': ['-m64'],
},
},
'NetBSD': {
'default': ['-DOS_BSD=1', '-DOS_NETBSD=1'],
},
'OpenBSD': {
'default': ['-DOS_BSD=1', '-DOS_OPENBSD=1'],
},
'OSX': {
'default': [
'-DOS_MACOSX=1',
'-stdlib=libc++',
# To disable the fixup bindgen applies which adds search
# paths from clang command line in order to avoid potential
# conflict with -stdlib=libc++.
'--target=x86_64-apple-darwin',
],
},
'SunOS': {
'default': ['-DOS_SOLARIS=1'],
},
'WINNT': {
'default': [
'-DOS_WIN=1',
'-DWIN32=1',
],
'compiler': {
'msvc': {
'default': [
# For compatibility with MSVC 2015
'-fms-compatibility-version=19',
# To enable the builtin __builtin_offsetof so that CRT wouldn't
# use reinterpret_cast in offsetof() which is not allowed inside
# static_assert().
'-D_CRT_USE_BUILTIN_OFFSETOF',
# Enable hidden attribute (which is not supported by MSVC and
# thus not enabled by default with a MSVC-compatibile build)
# to exclude hidden symbols from the generated file.
'-DHAVE_VISIBILITY_HIDDEN_ATTRIBUTE=1',
],
'cpu': {
'x86': ['--target=i686-pc-win32'],
'x86_64': ['--target=x86_64-pc-win32'],
'aarch64': ['--target=aarch64-pc-windows-msvc'],
},
},
},
},
}.get(target.os, {})
if 'default' in os_dict:
args += os_dict['default']
args += handle_cpu(os_dict)
if 'compiler' in os_dict and compiler_info and compiler_info in os_dict['compiler']:
compiler_dict = os_dict['compiler'].get(compiler_info)
if 'default' in compiler_dict:
args += compiler_dict['default']
args += handle_cpu(compiler_dict)
return args
js_option(env='BINDGEN_CFLAGS',
nargs=1,
help='Options bindgen should pass to the C/C++ parser')
@depends(basic_bindgen_cflags, 'BINDGEN_CFLAGS')
@checking('bindgen cflags', lambda s: s if s else 'no')
def bindgen_cflags(base_flags, extra_flags):
flags = base_flags
if extra_flags and len(extra_flags):
flags += extra_flags[0].split()
return ' '.join(flags)
add_old_configure_assignment('_BINDGEN_CFLAGS', bindgen_cflags)

View File

@ -1126,22 +1126,6 @@ def check_have_64_bit(have_64_bit, compiler_have_64_bit):
'about the target bitness.')
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()
add_old_configure_assignment('_BINDGEN_CFLAGS', bindgen_cflags)
@depends(c_compiler)
def default_debug_flags(compiler_info):
# Debug info is ON by default.

View File

@ -26,13 +26,6 @@ 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");
@ -52,69 +45,6 @@ fn main() {
.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") => Some(Arch::Aarch64),
Ok("arm") => Some(Arch::Arm),
Ok("x86") => Some(Arch::X86),
Ok("x86_64") => Some(Arch::X64),
_ => None
}
};
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.expect("unknown android architecture") {
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("ios") => {
// Nothing to do in particular for these OSes, until proven the contrary.
}
Ok("macos") => {
bindings = bindings.clang_arg("-DOS_MACOSX=1");
bindings = bindings.clang_arg("-stdlib=libc++");
bindings = bindings.clang_arg("--target=x86_64-apple-darwin");
}
Ok("windows") => {
let arch = arch.expect("unknown Windows architecture");
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();

View File

@ -1,61 +1,3 @@
[build]
args = [
"-x", "c++", "-std=gnu++14", "-fno-sized-deallocation",
"-DTRACING=1", "-DIMPL_LIBXUL", "-DMOZ_STYLO_BINDINGS=1",
"-DMOZILLA_INTERNAL_API", "-DRUST_BINDGEN"
]
"family=unix" = ["-DOS_POSIX=1"]
"os=solaris" = ["-DOS_SOLARIS=1"]
"os=dragonfly" = ["-DOS_BSD=1", "-DOS_DRAGONFLY=1"]
"os=freebsd" = ["-DOS_BSD=1", "-DOS_FREEBSD=1"]
"os=netbsd" = ["-DOS_BSD=1", "-DOS_NETBSD=1"]
"os=openbsd" = ["-DOS_BSD=1", "-DOS_OPENBSD=1"]
"os=macos" = [
"-DOS_MACOSX=1", "-stdlib=libc++",
# To disable the fixup bindgen applies which adds search
# paths from clang command line in order to avoid potential
# conflict with -stdlib=libc++.
"--target=x86_64-apple-darwin",
]
[build."os=linux"]
args = ["-DOS_LINUX=1"]
"arch=x86" = ["-m32"]
"arch=x86_64" = ["-m64"]
[build."os=android"]
args = ["-DOS_ANDROID=1"]
"arch=aarch64" = ["--target=aarch64-linux-android"]
"arch=arm" = ["--target=armv7-linux-androideabi"]
"arch=x86" = ["--target=i686-linux-android"]
"arch=x86_64" = ["--target=x86_64-linux-android"]
[build."os=windows"]
args = [
"-DOS_WIN=1", "-DWIN32=1",
]
[build."os=windows"."env=msvc"]
args = [
# For compatibility with MSVC 2015
"-fms-compatibility-version=19",
# To enable the builtin __builtin_offsetof so that CRT wouldn't
# use reinterpret_cast in offsetof() which is not allowed inside
# static_assert().
"-D_CRT_USE_BUILTIN_OFFSETOF",
# Enable hidden attribute (which is not supported by MSVC and
# thus not enabled by default with a MSVC-compatibile build)
# to exclude hidden symbols from the generated file.
"-DHAVE_VISIBILITY_HIDDEN_ATTRIBUTE=1",
]
"arch=x86" = ["--target=i686-pc-win32"]
"arch=x86_64" = ["--target=x86_64-pc-win32"]
"arch=aarch64" = ["--target=aarch64-pc-windows-msvc"]
[build."os=windows"."env=gnu"]
"arch=x86" = ["--target=i686-pc-mingw32"]
"arch=x86_64" = ["--target=x86_64-w64-mingw32"]
[structs]
headers = [
"nsStyleStruct.h",

View File

@ -58,22 +58,10 @@ mod bindings {
};
static ref BUILD_CONFIG: Table = {
// Load build-specific config overrides.
// FIXME: We should merge with CONFIG above instead of
// forcing callers to do it.
let path = PathBuf::from(env::var_os("MOZ_TOPOBJDIR").unwrap())
.join("layout/style/bindgen.toml");
read_config(&path)
};
static ref TARGET_INFO: HashMap<String, String> = {
const TARGET_PREFIX: &'static str = "CARGO_CFG_TARGET_";
let mut result = HashMap::new();
for (k, v) in env::vars() {
if k.starts_with(TARGET_PREFIX) {
result.insert(k[TARGET_PREFIX.len()..].to_lowercase(), v);
}
}
result
};
static ref INCLUDE_RE: Regex = Regex::new(r#"#include\s*"(.+?)""#).unwrap();
static ref DISTDIR_PATH: PathBuf = {
let path = PathBuf::from(env::var_os("MOZ_DIST").unwrap());
@ -145,35 +133,6 @@ mod bindings {
fn mutable_borrowed_type(self, ty: &str) -> Builder;
}
fn add_clang_args(mut builder: Builder, config: &Table, matched_os: &mut bool) -> Builder {
fn add_args(mut builder: Builder, values: &[toml::Value]) -> Builder {
for item in values.iter() {
builder = builder.clang_arg(item.as_str().expect("Expect string in list"));
}
builder
}
for (k, v) in config.iter() {
if k == "args" {
builder = add_args(builder, v.as_array().unwrap().as_slice());
continue;
}
let equal_idx = k.find('=').expect(&format!("Invalid key: {}", k));
let (target_type, target_value) = k.split_at(equal_idx);
if TARGET_INFO[target_type] != target_value[1..] {
continue;
}
if target_type == "os" {
*matched_os = true;
}
builder = match *v {
toml::Value::Table(ref table) => add_clang_args(builder, table, matched_os),
toml::Value::Array(ref array) => add_args(builder, array),
_ => panic!("Unknown type"),
};
}
builder
}
impl BuilderExt for Builder {
fn get_initial_builder() -> Builder {
use bindgen::RustTarget;
@ -207,16 +166,14 @@ mod bindings {
builder = builder.clang_arg("-DDEBUG=1").clang_arg("-DJS_DEBUG=1");
}
let mut matched_os = false;
let build_config = CONFIG["build"].as_table().expect("Malformed config file");
builder = add_clang_args(builder, build_config, &mut matched_os);
let build_config = BUILD_CONFIG["build"]
.as_table()
.expect("Malformed config file");
builder = add_clang_args(builder, build_config, &mut matched_os);
if !matched_os {
panic!("Unknown platform");
let extra_bindgen_flags = build_config["args"].as_array().unwrap().as_slice();
for item in extra_bindgen_flags.iter() {
builder = builder.clang_arg(item.as_str().expect("Expect string in list"));
}
builder
}
fn include<T: Into<String>>(self, file: T) -> Builder {