2016-07-27 01:27:19 +03:00
|
|
|
# -*- Mode: python; c-basic-offset: 4; 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/.
|
|
|
|
|
|
|
|
|
|
|
|
# Generates a test program and attempts to compile it. In case of failure, the
|
|
|
|
# resulting check will return None. If the test program succeeds, it will return
|
|
|
|
# the output of the test program.
|
|
|
|
# - `includes` are the includes (as file names) that will appear at the top of
|
|
|
|
# the generated test program.
|
|
|
|
# - `body` is the code that will appear in the main function of the generated
|
|
|
|
# test program. `return 0;` is appended to the function body automatically.
|
|
|
|
# - `language` is the language selection, so that the appropriate compiler is
|
|
|
|
# used.
|
|
|
|
# - `flags` are the flags to be passed to the compiler, in addition to `-c`.
|
|
|
|
# - `check_msg` is the message to be printed to accompany compiling the test
|
|
|
|
# program.
|
|
|
|
@template
|
2016-09-22 08:02:43 +03:00
|
|
|
def try_compile(includes=None, body='', language='C++', flags=None, check_msg=None,
|
2017-03-28 00:12:56 +03:00
|
|
|
when=None, onerror=lambda: None):
|
2016-08-04 09:51:47 +03:00
|
|
|
compiler = {
|
|
|
|
'C': c_compiler,
|
|
|
|
'C++': cxx_compiler,
|
|
|
|
}[language]
|
2016-07-27 01:27:19 +03:00
|
|
|
|
2017-03-28 00:12:56 +03:00
|
|
|
return compiler.try_compile(includes, body, flags, check_msg, when=when, onerror=onerror)
|
2016-07-27 01:27:19 +03:00
|
|
|
|
2016-07-27 01:27:19 +03:00
|
|
|
|
|
|
|
# Checks for the presence of the given header on the target system by compiling
|
|
|
|
# a test program including that header. The return value of the template is a
|
|
|
|
# check function returning True if the header is present, and None if it is not.
|
|
|
|
# The value of this check function is also used to set a variable (with set_define)
|
|
|
|
# corresponding to the checked header. For instance, HAVE_MALLOC_H will be set in
|
|
|
|
# defines if check_header if called with 'malloc.h' as input and malloc.h is
|
|
|
|
# present on the target.
|
|
|
|
# - `header` is the header, as a file name, to check for.
|
|
|
|
# - `language` is the language selection, so that the appropriate compiler is
|
|
|
|
# used.
|
|
|
|
# - `flags` are the flags to be passed to the compiler, in addition to `-c`.
|
|
|
|
# - `includes` are additional includes, as file names, to appear before the
|
|
|
|
# header checked for.
|
|
|
|
# - `when` is a depends function that if present will make performing the check
|
|
|
|
# conditional on the value of that function.
|
|
|
|
@template
|
|
|
|
def check_header(header, language='C++', flags=None, includes=None, when=None):
|
2017-05-10 05:35:22 +03:00
|
|
|
if when is None:
|
|
|
|
when = always
|
2016-07-27 01:27:19 +03:00
|
|
|
|
|
|
|
if includes:
|
|
|
|
includes = includes[:]
|
|
|
|
else:
|
|
|
|
includes = []
|
|
|
|
includes.append(header)
|
|
|
|
|
2016-09-22 08:02:43 +03:00
|
|
|
have_header = try_compile(includes=includes, language=language, flags=flags,
|
|
|
|
check_msg='for %s' % header, when=when)
|
2016-07-27 01:27:19 +03:00
|
|
|
header_var = 'HAVE_%s' % (header.upper()
|
|
|
|
.replace('-', '_')
|
|
|
|
.replace('/', '_')
|
|
|
|
.replace('.', '_'))
|
|
|
|
set_define(header_var, have_header)
|
|
|
|
return have_header
|
|
|
|
|
|
|
|
# A convenience wrapper for check_header for checking multiple headers.
|
|
|
|
# returns an array of the resulting checks in order corresponding to the
|
|
|
|
# provided headers.
|
|
|
|
# - `headers` are the headers to be checked.
|
|
|
|
# - `kwargs` are keyword arguments passed verbatim to check_header.
|
2017-10-12 16:22:59 +03:00
|
|
|
|
|
|
|
|
2016-07-27 01:27:19 +03:00
|
|
|
@template
|
|
|
|
def check_headers(*headers, **kwargs):
|
|
|
|
checks = []
|
|
|
|
for header in headers:
|
|
|
|
checks.append(check_header(header, **kwargs))
|
|
|
|
return checks
|
2016-08-05 11:13:48 +03:00
|
|
|
|
|
|
|
|
2017-11-15 21:53:16 +03:00
|
|
|
# Determine whether to add a given flag to the given lists of flags for C or
|
|
|
|
# C++ compilation.
|
|
|
|
# - `flag` is the flag to test
|
2017-12-21 05:11:22 +03:00
|
|
|
# - `flags_collection` is a @depends function for a namespace of lists of
|
|
|
|
# C/C++ compiler flags to add to.
|
2017-11-15 21:53:16 +03:00
|
|
|
# - `test_flags` is a list of flags to pass to the compiler instead of merely
|
|
|
|
# passing `flag`. This is especially useful for checking warning flags. If
|
|
|
|
# this list is empty, `flag` will be passed on its own.
|
2016-08-05 11:13:48 +03:00
|
|
|
# - `compiler` (optional) is the compiler to test against (c_compiler or
|
|
|
|
# cxx_compiler, from toolchain.configure). When omitted, both compilers
|
2017-11-15 21:53:16 +03:00
|
|
|
# are tested; the list of flags added to is dependent on the compiler tested.
|
2016-08-05 11:13:48 +03:00
|
|
|
# - `when` (optional) is a @depends function or option name conditioning
|
|
|
|
# when the warning flag is wanted.
|
|
|
|
# - `check`, when not set, skips checking whether the flag is supported and
|
2017-11-15 21:53:16 +03:00
|
|
|
# adds it to the list of flags unconditionally.
|
2016-08-05 11:13:48 +03:00
|
|
|
@template
|
2017-12-21 05:11:22 +03:00
|
|
|
def check_and_add_flags(flag, flags_collection, test_flags,
|
2017-11-15 21:53:16 +03:00
|
|
|
compiler=None, when=None, check=True):
|
2017-05-10 05:35:22 +03:00
|
|
|
if compiler is not None:
|
2016-08-05 11:13:48 +03:00
|
|
|
compilers = (compiler,)
|
|
|
|
else:
|
|
|
|
compilers = (c_compiler, cxx_compiler)
|
|
|
|
|
2017-05-10 05:35:22 +03:00
|
|
|
if when is None:
|
|
|
|
when = always
|
2016-08-05 11:13:48 +03:00
|
|
|
|
2017-11-16 21:36:33 +03:00
|
|
|
results = []
|
|
|
|
|
2017-11-15 21:53:16 +03:00
|
|
|
if test_flags:
|
|
|
|
flags = test_flags
|
|
|
|
else:
|
|
|
|
flags = [flag]
|
|
|
|
|
2016-08-05 11:13:48 +03:00
|
|
|
for c in compilers:
|
2018-10-12 16:44:08 +03:00
|
|
|
assert c in {c_compiler, cxx_compiler,
|
|
|
|
host_c_compiler, host_cxx_compiler}
|
2017-11-15 21:53:16 +03:00
|
|
|
lang, list_of_flags = {
|
2017-12-21 05:11:22 +03:00
|
|
|
c_compiler: ('C', flags_collection.cflags),
|
|
|
|
cxx_compiler: ('C++', flags_collection.cxxflags),
|
|
|
|
host_c_compiler: ('host C', flags_collection.host_cflags),
|
|
|
|
host_cxx_compiler: ('host C++', flags_collection.host_cxxflags),
|
2016-08-05 11:13:48 +03:00
|
|
|
}[c]
|
|
|
|
|
|
|
|
@depends(c, when)
|
|
|
|
def result(c, when):
|
|
|
|
if when and c.type in ('clang', 'gcc'):
|
|
|
|
return True
|
|
|
|
|
|
|
|
if check:
|
|
|
|
result = c.try_compile(
|
|
|
|
flags=flags, when=result,
|
2017-11-15 21:53:16 +03:00
|
|
|
check_msg='whether the %s compiler supports %s' % (lang, flag))
|
2016-08-05 11:13:48 +03:00
|
|
|
|
2017-11-15 21:53:16 +03:00
|
|
|
@depends(result, list_of_flags)
|
|
|
|
def maybe_add_flag(result, list_of_flags):
|
2016-08-10 04:00:43 +03:00
|
|
|
if result:
|
2017-11-15 21:53:16 +03:00
|
|
|
list_of_flags.append(flag)
|
|
|
|
|
2017-11-16 21:36:33 +03:00
|
|
|
results.append(result)
|
|
|
|
|
|
|
|
return tuple(results)
|
|
|
|
|
2017-11-15 21:53:16 +03:00
|
|
|
|
|
|
|
@dependable
|
2017-12-21 05:11:22 +03:00
|
|
|
def warnings_flags():
|
|
|
|
return namespace(cflags=[], cxxflags=[], host_cflags=[], host_cxxflags=[])
|
2017-11-15 21:53:16 +03:00
|
|
|
|
|
|
|
|
|
|
|
# Tests whether GCC or clang support the given warning flag, and if it is,
|
|
|
|
# add it to the list of warning flags for the build.
|
|
|
|
# - `warning` is the warning flag (e.g. -Wfoo)
|
|
|
|
# - `compiler` (optional) is the compiler to test against (c_compiler or
|
|
|
|
# cxx_compiler, from toolchain.configure). When omitted, both compilers
|
|
|
|
# are tested.
|
|
|
|
# - `when` (optional) is a @depends function or option name conditioning
|
|
|
|
# when the warning flag is wanted.
|
|
|
|
# - `check`, when not set, skips checking whether the flag is supported and
|
|
|
|
# adds it to the list of warning flags unconditionally. This is only meant
|
|
|
|
# for add_gcc_warning().
|
|
|
|
@template
|
|
|
|
def check_and_add_gcc_warning(warning, compiler=None, when=None, check=True):
|
|
|
|
# GCC and clang will fail if given an unknown warning option like
|
|
|
|
# -Wfoobar. But later versions won't fail if given an unknown negated
|
|
|
|
# warning option like -Wno-foobar. So when we are checking for support
|
|
|
|
# of a negated warning option, we actually test the positive form, but
|
|
|
|
# add the negated form to the flags variable.
|
|
|
|
if warning.startswith('-Wno-') and not warning.startswith('-Wno-error='):
|
|
|
|
flags = ['-Werror', '-W' + warning[5:]]
|
|
|
|
elif warning.startswith('-Werror='):
|
|
|
|
flags = [warning]
|
|
|
|
else:
|
|
|
|
flags = ['-Werror', warning]
|
|
|
|
|
2017-12-21 05:11:22 +03:00
|
|
|
return check_and_add_flags(warning, warnings_flags, flags,
|
|
|
|
compiler=compiler, when=when, check=check)
|
2017-11-15 21:53:16 +03:00
|
|
|
|
2016-08-05 11:13:48 +03:00
|
|
|
|
|
|
|
# Add the given warning to the list of warning flags for the build.
|
|
|
|
# - `warning` is the warning flag (e.g. -Wfoo)
|
|
|
|
# - `compiler` (optional) is the compiler to add the flag for (c_compiler or
|
|
|
|
# cxx_compiler, from toolchain.configure). When omitted, the warning flag
|
|
|
|
# is added for both compilers.
|
|
|
|
# - `when` (optional) is a @depends function or option name conditioning
|
|
|
|
# when the warning flag is wanted.
|
2017-10-12 16:22:59 +03:00
|
|
|
|
|
|
|
|
2016-08-05 11:13:48 +03:00
|
|
|
@template
|
|
|
|
def add_gcc_warning(warning, compiler=None, when=None):
|
|
|
|
check_and_add_gcc_warning(warning, compiler, when, check=False)
|
2017-11-15 21:53:16 +03:00
|
|
|
|
|
|
|
|
|
|
|
# Like the warning checks above, but for general compilation flags.
|
|
|
|
@dependable
|
2017-12-21 05:11:22 +03:00
|
|
|
def compilation_flags():
|
|
|
|
return namespace(cflags=[], cxxflags=[], host_cflags=[], host_cxxflags=[])
|
2017-11-15 21:53:16 +03:00
|
|
|
|
|
|
|
|
|
|
|
# Tests whether GCC or clang support the given compilation flag; if the flag
|
|
|
|
# is supported, add it to the list of compilation flags for the build.
|
|
|
|
# - `flag` is the flag to test
|
|
|
|
# - `compiler` (optional) is the compiler to test against (c_compiler or
|
|
|
|
# cxx_compiler, from toolchain.configure). When omitted, both compilers
|
|
|
|
# are tested.
|
|
|
|
# - `when` (optional) is a @depends function or option name conditioning
|
|
|
|
# when the warning flag is wanted.
|
|
|
|
# - `check`, when not set, skips checking whether the flag is supported and
|
|
|
|
# adds it to the list of flags unconditionally. This is only meant for
|
|
|
|
# add_gcc_flag().
|
|
|
|
@template
|
|
|
|
def check_and_add_gcc_flag(flag, compiler=None, when=None, check=True):
|
|
|
|
flags = ['-Werror', flag]
|
|
|
|
|
2017-12-21 05:11:22 +03:00
|
|
|
return check_and_add_flags(flag, compilation_flags, flags,
|
|
|
|
compiler=compiler, when=when, check=check)
|
2017-11-15 21:53:16 +03:00
|
|
|
|
|
|
|
|
|
|
|
# Add the given flag to the list of flags for the build.
|
|
|
|
# - `flag` is the flag (e.g. -fno-sized-deallocation)
|
|
|
|
# - `compiler` (optional) is the compiler to add the flag for (c_compiler or
|
|
|
|
# cxx_compiler, from toolchain.configure). When omitted, the flag is added
|
|
|
|
# for both compilers.
|
|
|
|
# - `when` (optional) is a @depends function or option name conditioning
|
|
|
|
# when the flag is wanted.
|
|
|
|
@template
|
|
|
|
def add_gcc_flag(warning, compiler=None, when=None):
|
|
|
|
check_and_add_gcc_flag(warning, compiler, when, check=False)
|