| 1 |
# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*- |
| 2 |
# vim: set filetype=python: |
| 3 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
|
| 8 |
# Generates a test program and attempts to compile it. In case of failure, the |
| 9 |
# resulting check will return None. If the test program succeeds, it will return |
| 10 |
# the output of the test program. |
| 11 |
# - `includes` are the includes (as file names) that will appear at the top of |
| 12 |
# the generated test program. |
| 13 |
# - `body` is the code that will appear in the main function of the generated |
| 14 |
# test program. `return 0;` is appended to the function body automatically. |
| 15 |
# - `language` is the language selection, so that the appropriate compiler is |
| 16 |
# used. |
| 17 |
# - `flags` are the flags to be passed to the compiler, in addition to `-c`. |
| 18 |
# - `check_msg` is the message to be printed to accompany compiling the test |
| 19 |
# program. |
| 20 |
@template |
| 21 |
def try_compile( |
| 22 |
includes=None, |
| 23 |
body="", |
| 24 |
language="C++", |
| 25 |
flags=None, |
| 26 |
check_msg=None, |
| 27 |
when=None, |
| 28 |
onerror=lambda: None, |
| 29 |
): |
| 30 |
compiler = { |
| 31 |
"C": c_compiler, |
| 32 |
"C++": cxx_compiler, |
| 33 |
}[language] |
| 34 |
|
| 35 |
return compiler.try_compile( |
| 36 |
includes, body, flags, check_msg, when=when, onerror=onerror |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
# Generates a test program and attempts to link it. In case of failure, the |
| 41 |
# resulting check will return None. If the link succeeds, it will return |
| 42 |
# True |
| 43 |
# - `includes` are the includes (as file names) that will appear at the top of |
| 44 |
# the generated test program. |
| 45 |
# - `body` is the code that will appear in the main function of the generated |
| 46 |
# test program. `return 0;` is appended to the function body automatically. |
| 47 |
# - `language` is the language selection, so that the appropriate compiler is |
| 48 |
# used. |
| 49 |
# - `flags` are the flags to be passed to the compiler. |
| 50 |
# - `check_msg` is the message to be printed to accompany compiling the test |
| 51 |
# program. |
| 52 |
@template |
| 53 |
def try_link( |
| 54 |
includes=None, |
| 55 |
body="", |
| 56 |
language="C++", |
| 57 |
flags=None, |
| 58 |
check_msg=None, |
| 59 |
when=None, |
| 60 |
onerror=lambda: None, |
| 61 |
): |
| 62 |
compiler = { |
| 63 |
"C": c_compiler, |
| 64 |
"C++": cxx_compiler, |
| 65 |
}[language] |
| 66 |
|
| 67 |
return compiler.try_link( |
| 68 |
linker_ldflags, |
| 69 |
includes, |
| 70 |
body, |
| 71 |
flags, |
| 72 |
check_msg, |
| 73 |
when=when, |
| 74 |
onerror=onerror, |
| 75 |
) |
| 76 |
|
| 77 |
|
| 78 |
# Checks for the presence of the given header on the target system by compiling |
| 79 |
# a test program including that header. The return value of the template is a |
| 80 |
# check function returning True if the header is present, and None if it is not. |
| 81 |
# The value of this check function is also used to set a variable (with set_define) |
| 82 |
# corresponding to the checked header. For instance, HAVE_MALLOC_H will be set in |
| 83 |
# defines if check_header if called with 'malloc.h' as input and malloc.h is |
| 84 |
# present on the target. |
| 85 |
# - `header` is the header, as a file name, to check for. |
| 86 |
# - `language` is the language selection, so that the appropriate compiler is |
| 87 |
# used. |
| 88 |
# - `flags` are the flags to be passed to the compiler, in addition to `-c`. |
| 89 |
# - `includes` are additional includes, as file names, to appear before the |
| 90 |
# header checked for. |
| 91 |
# - `when` is a depends function that if present will make performing the check |
| 92 |
# conditional on the value of that function. |
| 93 |
@template |
| 94 |
def check_header( |
| 95 |
header, language="C++", flags=None, includes=None, when=None, onerror=lambda: None |
| 96 |
): |
| 97 |
if when is None: |
| 98 |
when = always |
| 99 |
|
| 100 |
if includes: |
| 101 |
includes = includes[:] |
| 102 |
else: |
| 103 |
includes = [] |
| 104 |
includes.append(header) |
| 105 |
|
| 106 |
have_header = try_compile( |
| 107 |
includes=includes, |
| 108 |
language=language, |
| 109 |
flags=flags, |
| 110 |
check_msg="for %s" % header, |
| 111 |
when=when, |
| 112 |
onerror=onerror, |
| 113 |
) |
| 114 |
header_var = "HAVE_%s" % ( |
| 115 |
header.upper().replace("-", "_").replace("/", "_").replace(".", "_") |
| 116 |
) |
| 117 |
set_define(header_var, have_header) |
| 118 |
return have_header |
| 119 |
|
| 120 |
|
| 121 |
# A convenience wrapper for check_header for checking multiple headers. |
| 122 |
# returns an array of the resulting checks in order corresponding to the |
| 123 |
# provided headers. |
| 124 |
# - `headers` are the headers to be checked. |
| 125 |
# - `kwargs` are keyword arguments passed verbatim to check_header. |
| 126 |
|
| 127 |
|
| 128 |
@template |
| 129 |
def check_headers(*headers, **kwargs): |
| 130 |
checks = [] |
| 131 |
for header in headers: |
| 132 |
checks.append(check_header(header, **kwargs)) |
| 133 |
return checks |
| 134 |
|
| 135 |
|
| 136 |
@depends(linker_ldflags, target.kernel) |
| 137 |
def check_symbol_flags(linker_ldflags, kernel): |
| 138 |
if kernel == "WINNT": |
| 139 |
# The build doesn't use the compiler to link things as of writing, |
| 140 |
# but some compilation checks do. When using clang-cl, the only |
| 141 |
# linker we really support is lld.link, but clang-cl defaults to |
| 142 |
# link.exe (even when cross-compiling). So we force the use of |
| 143 |
# lld.link for the linkage checks. |
| 144 |
return ["-fuse-ld=lld"] |
| 145 |
return linker_ldflags |
| 146 |
|
| 147 |
|
| 148 |
# Checks for the presence of the given symbol on the target system by compiling |
| 149 |
# a test program. The return value of the template is a check function |
| 150 |
# returning True if the symbol can be found, and None if it is not. |
| 151 |
@template |
| 152 |
def check_symbol( |
| 153 |
symbol, language="C", flags=None, msg_extra="", when=None, onerror=lambda: None |
| 154 |
): |
| 155 |
if when is None: |
| 156 |
when = always |
| 157 |
|
| 158 |
compiler, extern_c = { |
| 159 |
"C": (c_compiler, ""), |
| 160 |
"C++": (cxx_compiler, 'extern "C" '), |
| 161 |
}[language] |
| 162 |
|
| 163 |
# Stolen from autoconf 2.13 ; might be irrelevant now, but it doesn't hurt to |
| 164 |
# keep using a char return type. |
| 165 |
comment = [ |
| 166 |
"/* Override any gcc2 internal prototype to avoid an error. */", |
| 167 |
"/* We use char because int might match the return type of a gcc2", |
| 168 |
" builtin and then its argument prototype would still apply. */", |
| 169 |
] |
| 170 |
|
| 171 |
if flags: |
| 172 |
|
| 173 |
@depends(check_symbol_flags, dependable(flags)) |
| 174 |
def flags(base_flags, extra_flags): |
| 175 |
if base_flags and extra_flags: |
| 176 |
return base_flags + list(extra_flags) |
| 177 |
if extra_flags: |
| 178 |
return extra_flags |
| 179 |
return base_flags |
| 180 |
|
| 181 |
else: |
| 182 |
flags = check_symbol_flags |
| 183 |
|
| 184 |
return compiler.try_run( |
| 185 |
header=comment + ["%schar %s();" % (extern_c, symbol)], |
| 186 |
body="%s();" % symbol, |
| 187 |
flags=flags, |
| 188 |
check_msg="for %s%s" % (symbol, msg_extra), |
| 189 |
when=when, |
| 190 |
onerror=onerror, |
| 191 |
) |
| 192 |
|
| 193 |
|
| 194 |
# Checks for the presence of the given symbol in the given library on the |
| 195 |
# target system by compiling a test program. The return value of the template |
| 196 |
# is a check function returning True if the symbol can be found, and None if it |
| 197 |
# is not. |
| 198 |
@template |
| 199 |
def check_symbol_in_lib(libname, symbol, language="C", when=None, onerror=lambda: None): |
| 200 |
flag = f"-l{libname}" |
| 201 |
have_symbol = check_symbol( |
| 202 |
symbol, |
| 203 |
flags=[flag], |
| 204 |
msg_extra=f" in {flag}", |
| 205 |
language=language, |
| 206 |
when=when, |
| 207 |
onerror=onerror, |
| 208 |
) |
| 209 |
|
| 210 |
return have_symbol |
| 211 |
|
| 212 |
|
| 213 |
# Same as check_symbol_in_lib but iteratively try libraries in the given libnames until it |
| 214 |
# finds one that contains the given symbol. |
| 215 |
# Returns None if not found, empty tuple if None is in the given libnames and |
| 216 |
# the symbol is present without library linked, a singleton containing the |
| 217 |
# library name if found in that library. |
| 218 |
@template |
| 219 |
def check_symbol_in_libs( |
| 220 |
libnames, symbol, language="C", when=None, onerror=lambda: None |
| 221 |
): |
| 222 |
have_symbol = never |
| 223 |
|
| 224 |
found_lib = dependable(namespace(found=False, lib=None)) |
| 225 |
|
| 226 |
kwargs = { |
| 227 |
"symbol": symbol, |
| 228 |
"language": language, |
| 229 |
"when": when or always, |
| 230 |
"onerror": onerror, |
| 231 |
} |
| 232 |
|
| 233 |
for libname in libnames: |
| 234 |
kwargs["when"] &= ~have_symbol |
| 235 |
if libname: |
| 236 |
have_symbol = check_symbol_in_lib(libname, **kwargs) |
| 237 |
else: |
| 238 |
have_symbol = check_symbol(**kwargs) |
| 239 |
|
| 240 |
add_lib = namespace(found=True, lib=libname) |
| 241 |
found_lib = depends(have_symbol, found_lib)(lambda h, f: add_lib if h else f) |
| 242 |
|
| 243 |
return found_lib |
| 244 |
|
| 245 |
|
| 246 |
# Determine whether to add a given flag to the given lists of flags for C or |
| 247 |
# C++ compilation. |
| 248 |
# - `flag` is the flag to test |
| 249 |
# - `flags_collection` is a @depends function for a namespace of lists of |
| 250 |
# C/C++ compiler flags to add to. |
| 251 |
# - `test_flags` is a list of flags to pass to the compiler instead of merely |
| 252 |
# passing `flag`. This is especially useful for checking warning flags. If |
| 253 |
# this list is empty, `flag` will be passed on its own. |
| 254 |
# - `compiler` (optional) is the compiler to test against (c_compiler or |
| 255 |
# cxx_compiler, from toolchain.configure). When omitted, both compilers |
| 256 |
# are tested; the list of flags added to is dependent on the compiler tested. |
| 257 |
# - `when` (optional) is a @depends function or option name conditioning |
| 258 |
# when the warning flag is wanted. |
| 259 |
# - `check`, when not set, skips checking whether the flag is supported and |
| 260 |
# adds it to the list of flags unconditionally. |
| 261 |
# - `mode`, can be "compile", "link" or "assemble" |
| 262 |
@template |
| 263 |
def check_and_add_flags( |
| 264 |
flag, |
| 265 |
flags_collection, |
| 266 |
test_flags=(), |
| 267 |
compiler=None, |
| 268 |
when=None, |
| 269 |
check=True, |
| 270 |
mode="compile", |
| 271 |
): |
| 272 |
assert mode in ("compile", "link", "assemble") |
| 273 |
|
| 274 |
if compiler is not None: |
| 275 |
compilers = (compiler,) |
| 276 |
elif mode in ("link", "assemble"): |
| 277 |
compilers = (c_compiler,) |
| 278 |
else: |
| 279 |
compilers = (c_compiler, cxx_compiler) |
| 280 |
|
| 281 |
if when is None: |
| 282 |
when = always |
| 283 |
|
| 284 |
results = [] |
| 285 |
|
| 286 |
if test_flags: |
| 287 |
flags = test_flags |
| 288 |
else: |
| 289 |
flags = [flag] |
| 290 |
|
| 291 |
for c in compilers: |
| 292 |
assert c in {c_compiler, cxx_compiler, host_c_compiler, host_cxx_compiler} |
| 293 |
if mode == "compile": |
| 294 |
lang, list_of_flags = { |
| 295 |
c_compiler: ("C", flags_collection.cflags), |
| 296 |
cxx_compiler: ("C++", flags_collection.cxxflags), |
| 297 |
host_c_compiler: ("host C", flags_collection.host_cflags), |
| 298 |
host_cxx_compiler: ("host C++", flags_collection.host_cxxflags), |
| 299 |
}[c] |
| 300 |
elif mode == "assemble": |
| 301 |
lang, list_of_flags = { |
| 302 |
c_compiler: ("C", flags_collection.asflags), |
| 303 |
}[c] |
| 304 |
elif mode == "link": |
| 305 |
lang, list_of_flags = { |
| 306 |
c_compiler: ("C", flags_collection.ldflags), |
| 307 |
cxx_compiler: ("C++", flags_collection.ldflags), |
| 308 |
host_c_compiler: ("host C", flags_collection.host_ldflags), |
| 309 |
host_cxx_compiler: ("host C++", flags_collection.host_ldflags), |
| 310 |
}[c] |
| 311 |
|
| 312 |
result = when |
| 313 |
|
| 314 |
if check: |
| 315 |
|
| 316 |
@depends(c, dependable(flags)) |
| 317 |
def flags(c, flags): |
| 318 |
# Don't error out just because clang complains about other things. |
| 319 |
if c.type in ("clang", "clang-cl"): |
| 320 |
flags += ["-Wno-error=unused-command-line-argument"] |
| 321 |
|
| 322 |
return flags |
| 323 |
|
| 324 |
if mode == "link": |
| 325 |
|
| 326 |
def runner(*args, **kwargs): |
| 327 |
if c in (c_compiler, cxx_compiler): |
| 328 |
return c.try_link(linker_ldflags, *args, **kwargs) |
| 329 |
else: |
| 330 |
return c.try_link(host_linker_ldflags, *args, **kwargs) |
| 331 |
|
| 332 |
tool = "linker" |
| 333 |
elif mode == "assemble": |
| 334 |
runner = c.try_compile |
| 335 |
tool = "assembler" |
| 336 |
else: |
| 337 |
runner = c.try_compile |
| 338 |
tool = "compiler" |
| 339 |
|
| 340 |
result = runner( |
| 341 |
flags=flags, |
| 342 |
when=result, |
| 343 |
check_msg="whether the %s %s supports %s" % (lang, tool, flag), |
| 344 |
) |
| 345 |
|
| 346 |
@depends(result, list_of_flags) |
| 347 |
def maybe_add_flag(result, list_of_flags): |
| 348 |
if result: |
| 349 |
list_of_flags.append(flag) |
| 350 |
|
| 351 |
results.append(result) |
| 352 |
|
| 353 |
return tuple(results) |
| 354 |
|
| 355 |
|
| 356 |
@dependable |
| 357 |
def warnings_flags(): |
| 358 |
return namespace(cflags=[], cxxflags=[], host_cflags=[], host_cxxflags=[]) |
| 359 |
|
| 360 |
|
| 361 |
# Tests whether GCC or clang support the given warning flag, and if it is, |
| 362 |
# add it to the list of warning flags for the build. |
| 363 |
# - `warning` is the warning flag (e.g. -Wfoo) |
| 364 |
# - `compiler` (optional) is the compiler to test against (c_compiler or |
| 365 |
# cxx_compiler, from toolchain.configure). When omitted, both compilers |
| 366 |
# are tested. |
| 367 |
# - `when` (optional) is a @depends function or option name conditioning |
| 368 |
# when the warning flag is wanted. |
| 369 |
# - `check`, when not set, skips checking whether the flag is supported and |
| 370 |
# adds it to the list of warning flags unconditionally. This is only meant |
| 371 |
# for add_warning(). |
| 372 |
@template |
| 373 |
def check_and_add_warning(warning, compiler=None, when=None, check=True): |
| 374 |
# GCC and clang will fail if given an unknown warning option like |
| 375 |
# -Wfoobar. But later versions won't fail if given an unknown negated |
| 376 |
# warning option like -Wno-foobar. So when we are checking for support |
| 377 |
# of a negated warning option, we actually test the positive form, but |
| 378 |
# add the negated form to the flags variable. |
| 379 |
if warning.startswith("-Wno-") and not warning.startswith("-Wno-error="): |
| 380 |
flags = ["-Werror", "-W" + warning[5:]] |
| 381 |
elif warning.startswith("-Werror="): |
| 382 |
flags = [warning] |
| 383 |
else: |
| 384 |
flags = ["-Werror", warning] |
| 385 |
|
| 386 |
return check_and_add_flags( |
| 387 |
warning, warnings_flags, flags, compiler=compiler, when=when, check=check |
| 388 |
) |
| 389 |
|
| 390 |
|
| 391 |
# Add the given warning to the list of warning flags for the build. |
| 392 |
# - `warning` is the warning flag (e.g. -Wfoo) |
| 393 |
# - `compiler` (optional) is the compiler to add the flag for (c_compiler or |
| 394 |
# cxx_compiler, from toolchain.configure). When omitted, the warning flag |
| 395 |
# is added for both compilers. |
| 396 |
# - `when` (optional) is a @depends function or option name conditioning |
| 397 |
# when the warning flag is wanted. |
| 398 |
|
| 399 |
|
| 400 |
@template |
| 401 |
def add_warning(warning, compiler=None, when=None): |
| 402 |
check_and_add_warning(warning, compiler, when, check=False) |
| 403 |
|
| 404 |
|
| 405 |
# Like the warning checks above, but for general compilation flags. |
| 406 |
@dependable |
| 407 |
def compilation_flags(): |
| 408 |
return namespace( |
| 409 |
cflags=[], |
| 410 |
cxxflags=[], |
| 411 |
host_cflags=[], |
| 412 |
host_cxxflags=[], |
| 413 |
cmflags=[], |
| 414 |
cmmflags=[], |
| 415 |
) |
| 416 |
|
| 417 |
|
| 418 |
# Tests whether GCC or clang support the given compilation flag; if the flag |
| 419 |
# is supported, add it to the list of compilation flags for the build. |
| 420 |
# - `flag` is the flag to test |
| 421 |
# - `compiler` (optional) is the compiler to test against (c_compiler or |
| 422 |
# cxx_compiler, from toolchain.configure). When omitted, both compilers |
| 423 |
# are tested. |
| 424 |
# - `when` (optional) is a @depends function or option name conditioning |
| 425 |
# when the warning flag is wanted. |
| 426 |
# - `check`, when not set, skips checking whether the flag is supported and |
| 427 |
# adds it to the list of flags unconditionally. This is only meant for |
| 428 |
# add_flag(). |
| 429 |
@template |
| 430 |
def check_and_add_flag(flag, compiler=None, when=None, check=True): |
| 431 |
flags = ["-Werror", flag] |
| 432 |
|
| 433 |
return check_and_add_flags( |
| 434 |
flag, compilation_flags, flags, compiler=compiler, when=when, check=check |
| 435 |
) |
| 436 |
|
| 437 |
|
| 438 |
# Add the given flag to the list of flags for the build. |
| 439 |
# - `flag` is the flag (e.g. -fno-sized-deallocation) |
| 440 |
# - `compiler` (optional) is the compiler to add the flag for (c_compiler or |
| 441 |
# cxx_compiler, from toolchain.configure). When omitted, the flag is added |
| 442 |
# for both compilers. |
| 443 |
# - `when` (optional) is a @depends function or option name conditioning |
| 444 |
# when the flag is wanted. |
| 445 |
@template |
| 446 |
def add_flag(warning, compiler=None, when=None): |
| 447 |
check_and_add_flag(warning, compiler, when, check=False) |
| 448 |
|
| 449 |
|
| 450 |
# Like the compilation checks above, but for asm flags. |
| 451 |
@dependable |
| 452 |
def asm_flags(): |
| 453 |
return namespace(asflags=[]) |
| 454 |
|
| 455 |
|
| 456 |
# Tests the given assembler flag is supported; if the flag |
| 457 |
# is supported, add it to the list of compilation flags for the build. |
| 458 |
# - `flag` is the flag to test |
| 459 |
# - `when` (optional) is a @depends function or option name conditioning |
| 460 |
# when the warning flag is wanted. |
| 461 |
# - `check`, when not set, skips checking whether the flag is supported and |
| 462 |
# adds it to the list of flags unconditionally. This is only meant for |
| 463 |
# add_flag(). |
| 464 |
@template |
| 465 |
def check_and_add_asm_flag(flag, when=None, check=True): |
| 466 |
return check_and_add_flags( |
| 467 |
flag, |
| 468 |
asm_flags, |
| 469 |
[flag], |
| 470 |
when=when, |
| 471 |
check=check, |
| 472 |
mode="assemble", |
| 473 |
) |
| 474 |
|
| 475 |
|
| 476 |
# Like the compilation checks above, but for linker flags. |
| 477 |
@dependable |
| 478 |
def linker_flags(): |
| 479 |
return namespace(ldflags=[], host_ldflags=[]) |
| 480 |
|
| 481 |
|
| 482 |
@dependable |
| 483 |
def dso_flags(): |
| 484 |
return namespace(ldopts=[]) |
| 485 |
|
| 486 |
|
| 487 |
# Tests the given linker flag is supported; if the flag |
| 488 |
# is supported, add it to the list of compilation flags for the build. |
| 489 |
# - `flag` is the flag to test |
| 490 |
# - `when` (optional) is a @depends function or option name conditioning |
| 491 |
# when the warning flag is wanted. |
| 492 |
# - `check`, when not set, skips checking whether the flag is supported and |
| 493 |
# adds it to the list of flags unconditionally. This is only meant for |
| 494 |
# add_flag(). |
| 495 |
@template |
| 496 |
def check_and_add_linker_flag(flag, compiler=None, when=None, check=True): |
| 497 |
return check_and_add_flags( |
| 498 |
flag, |
| 499 |
linker_flags, |
| 500 |
[flag], |
| 501 |
when=when, |
| 502 |
check=check, |
| 503 |
mode="link", |
| 504 |
) |
| 505 |
|
| 506 |
|
| 507 |
# Like the compilation checks above, but for linker optimization flags. |
| 508 |
@dependable |
| 509 |
def linker_optimize_flags(): |
| 510 |
return namespace(ldflags=[]) |
| 511 |
|
| 512 |
|
| 513 |
@template |
| 514 |
def check_and_add_linker_optimize_flag(flag, compiler=None, when=None, check=True): |
| 515 |
return check_and_add_flags( |
| 516 |
flag, |
| 517 |
linker_optimize_flags, |
| 518 |
[flag], |
| 519 |
when=when, |
| 520 |
check=check, |
| 521 |
mode="link", |
| 522 |
) |
| 523 |
|
| 524 |
|
| 525 |
# Add the given flag to the list of linker flags for the build. |
| 526 |
# - `flag` is the flag (e.g. -fno-sized-deallocation) |
| 527 |
# - `when` (optional) is a @depends function or option name conditioning |
| 528 |
# when the flag is wanted. |
| 529 |
@template |
| 530 |
def add_linker_flag(flag, compiler=None, when=None): |
| 531 |
check_and_add_linker_flag(flag, compiler, when, check=False) |