Bug 1524467 - Fix and refactor basic_bindgen_cflags. r=froydnj

basic_bindgen_cflags's function is to set some flags for use with
rust-bindgen through clang/libclang for C++ code. Part of the flags it
sets are for the C++ standard, and the target.

Unfortunately, some of the logic wrt target-specific flags is currently
broken. It wants to apply per-compiler flags on Windows, but fails to do
so. First, because the condition test is wrong, and second, because it
only cares about msvc and not clang-cl.

OTOH, we already have those flags when the compiler is clang or
clang-cl. And we already have code to get the right flags for a given
compiler. So when the compiler is not clang or clang-cl, we can use that
to get the right flags for the clang we're going to use for bindgen.

Depends on D18316

Differential Revision: https://phabricator.services.mozilla.com/D18317

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Mike Hommey 2019-02-05 00:27:33 +00:00
parent 33a69b7eea
commit c5bfd96206

View file

@ -243,10 +243,11 @@ set_config('MOZ_LIBCLANG_PATH', bindgen_config_paths.libclang_path)
set_config('MOZ_CLANG_PATH', bindgen_config_paths.clang_path) set_config('MOZ_CLANG_PATH', bindgen_config_paths.clang_path)
@depends(host, target, target_is_unix, c_compiler, bindgen_cflags_android) @depends(target, target_is_unix, cxx_compiler, bindgen_cflags_android,
def basic_bindgen_cflags(host, target, is_unix, compiler_info, android_cflags): bindgen_config_paths.clang_path)
def basic_bindgen_cflags(target, is_unix, compiler_info, android_cflags, clang_path):
args = [ args = [
'-x', 'c++', '-std=gnu++14', '-fno-sized-deallocation', '-x', 'c++', '-fno-sized-deallocation',
'-DTRACING=1', '-DIMPL_LIBXUL', '-DMOZILLA_INTERNAL_API', '-DTRACING=1', '-DIMPL_LIBXUL', '-DMOZILLA_INTERNAL_API',
'-DRUST_BINDGEN' '-DRUST_BINDGEN'
] ]
@ -257,97 +258,43 @@ def basic_bindgen_cflags(host, target, is_unix, compiler_info, android_cflags):
if target.os == 'Android': if target.os == 'Android':
args += android_cflags args += android_cflags
def handle_cpu(obj): args += {
if 'cpu' in obj and target.cpu in obj['cpu']: 'Android': ['-DOS_ANDROID=1'],
return obj['cpu'][target.cpu] 'DragonFly': ['-DOS_BSD=1', '-DOS_DRAGONFLY=1'],
return [] 'FreeBSD': ['-DOS_BSD=1', '-DOS_FREEBSD=1'],
'GNU': ['-DOS_LINUX=1'],
'NetBSD': ['-DOS_BSD=1', '-DOS_NETBSD=1'],
'OpenBSD': ['-DOS_BSD=1', '-DOS_OPENBSD=1'],
'OSX': ['-DOS_MACOSX=1', '-stdlib=libc++'],
'SunOS': ['-DOS_SOLARIS=1'],
'WINNT': [
'-DOS_WIN=1',
'-DWIN32=1',
],
}.get(target.os, [])
if target.os == 'WINNT' and host.raw_os.startswith('gnu'): if compiler_info.type in ('msvc', 'clang-cl'):
args += handle_cpu({ args += [
'cpu': { # To enable the builtin __builtin_offsetof so that CRT wouldn't
'x86': ['--target=i686-pc-mingw32'], # use reinterpret_cast in offsetof() which is not allowed inside
'x86_64': ['--target=x86_64-w64-mingw32'], # 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',
]
os_dict = { # We want to pass the same base flags as we'd pass clang.
'Android': { # check_compiler from toolchain.configure gives us that, but we don't need
'default': ['-DOS_ANDROID=1'], # to use that when the compiler used for the build is already clang-based,
'cpu': { # in which case we can use the same flags.
'aarch64': ['--target=aarch64-linux-android'], if compiler_info.type in ('clang-cl', 'clang'):
'arm': ['--target=armv7-linux-androideabi'], info = compiler_info
'x86': ['--target=i686-linux-android'], else:
'x86_64': ['--target=x86_64-linux-android'], info = check_compiler([clang_path], 'C++', target)
},
},
'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': [
# 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 += info.flags
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 return args