summaryrefslogtreecommitdiffstats
path: root/build/moz.configure/compile-checks.configure
blob: a639a012f12f6ccc15cb5beff28e03c886049f5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# -*- 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
def try_compile(
    includes=None,
    body="",
    language="C++",
    flags=None,
    check_msg=None,
    when=None,
    onerror=lambda: None,
):
    compiler = {
        "C": c_compiler,
        "C++": cxx_compiler,
    }[language]

    return compiler.try_compile(
        includes, body, flags, check_msg, when=when, onerror=onerror
    )


# Generates a test program and attempts to link it. In case of failure, the
# resulting check will return None. If the link succeeds, it will return
# True
# - `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.
# - `check_msg` is the message to be printed to accompany compiling the test
#   program.
@template
def try_link(
    includes=None,
    body="",
    language="C++",
    flags=None,
    check_msg=None,
    when=None,
    onerror=lambda: None,
):
    compiler = {
        "C": c_compiler,
        "C++": cxx_compiler,
    }[language]

    return compiler.try_link(
        linker_ldflags,
        includes,
        body,
        flags,
        check_msg,
        when=when,
        onerror=onerror,
    )


# 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, onerror=lambda: None
):
    if when is None:
        when = always

    if includes:
        includes = includes[:]
    else:
        includes = []
    includes.append(header)

    have_header = try_compile(
        includes=includes,
        language=language,
        flags=flags,
        check_msg="for %s" % header,
        when=when,
        onerror=onerror,
    )
    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.


@template
def check_headers(*headers, **kwargs):
    checks = []
    for header in headers:
        checks.append(check_header(header, **kwargs))
    return checks


@depends(linker_ldflags, target.kernel)
def check_symbol_flags(linker_ldflags, kernel):
    if kernel == "WINNT":
        # The build doesn't use the compiler to link things as of writing,
        # but some compilation checks do. When using clang-cl, the only
        # linker we really support is lld.link, but clang-cl defaults to
        # link.exe (even when cross-compiling). So we force the use of
        # lld.link for the linkage checks.
        return ["-fuse-ld=lld"]
    return linker_ldflags


# Checks for the presence of the given symbol on the target system by compiling
# a test program. The return value of the template is a check function
# returning True if the symbol can be found, and None if it is not.
@template
def check_symbol(symbol, language="C", flags=None, when=None, onerror=lambda: None):
    if when is None:
        when = always

    compiler, extern_c = {
        "C": (c_compiler, ""),
        "C++": (cxx_compiler, 'extern "C" '),
    }[language]

    # Stolen from autoconf 2.13 ; might be irrelevant now, but it doesn't hurt to
    # keep using a char return type.
    comment = [
        "/* Override any gcc2 internal prototype to avoid an error.  */",
        "/* We use char because int might match the return type of a gcc2",
        "    builtin and then its argument prototype would still apply.  */",
    ]

    if flags:

        @depends(check_symbol_flags, dependable(flags))
        def flags(base_flags, extra_flags):
            if base_flags and extra_flags:
                return base_flags + list(extra_flags)
            if extra_flags:
                return extra_flags
            return base_flags

    else:
        flags = check_symbol_flags

    return compiler.try_run(
        header=comment + ["%schar %s();" % (extern_c, symbol)],
        body="%s();" % symbol,
        flags=flags,
        check_msg="for %s" % symbol,
        when=when,
        onerror=onerror,
    )


# Determine whether to add a given flag to the given lists of flags for C or
# C++ compilation.
# - `flag` is the flag to test
# - `flags_collection` is a @depends function for a namespace of lists of
#    C/C++ compiler flags to add to.
# - `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.
# - `compiler` (optional) is the compiler to test against (c_compiler or
#   cxx_compiler, from toolchain.configure). When omitted, both compilers
#   are tested; the list of flags added to is dependent on the compiler 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.
# - `mode`, can be "compile", "link" or "assemble"
@template
def check_and_add_flags(
    flag,
    flags_collection,
    test_flags=(),
    compiler=None,
    when=None,
    check=True,
    mode="compile",
):
    assert mode in ("compile", "link", "assemble")

    if compiler is not None:
        compilers = (compiler,)
    elif mode in ("link", "assemble"):
        compilers = (c_compiler,)
    else:
        compilers = (c_compiler, cxx_compiler)

    if when is None:
        when = always

    results = []

    if test_flags:
        flags = test_flags
    else:
        flags = [flag]

    for c in compilers:
        assert c in {c_compiler, cxx_compiler, host_c_compiler, host_cxx_compiler}
        if mode == "compile":
            lang, list_of_flags = {
                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),
            }[c]
        elif mode == "assemble":
            lang, list_of_flags = {
                c_compiler: ("C", flags_collection.asflags),
                host_c_compiler: ("host C", flags_collection.host_asflags),
            }[c]
        elif mode == "link":
            lang, list_of_flags = {
                c_compiler: ("C", flags_collection.ldflags),
                cxx_compiler: ("C++", flags_collection.ldflags),
                host_c_compiler: ("host C", flags_collection.host_ldflags),
                host_cxx_compiler: ("host C++", flags_collection.host_ldflags),
            }[c]

        result = when

        if check:

            @depends(c, dependable(flags))
            def flags(c, flags):
                # Don't error out just because clang complains about other things.
                if c.type in ("clang", "clang-cl"):
                    flags += ["-Wno-error=unused-command-line-argument"]

                return flags

            if mode == "link":

                def runner(*args, **kwargs):
                    if c in (c_compiler, cxx_compiler):
                        return c.try_link(linker_ldflags, *args, **kwargs)
                    else:
                        return c.try_link(host_linker_ldflags, *args, **kwargs)

                tool = "linker"
            else:
                runner = c.try_compile
                tool = "compiler"

            result = runner(
                flags=flags,
                when=result,
                check_msg="whether the %s %s supports %s" % (lang, tool, flag),
            )

        @depends(result, list_of_flags)
        def maybe_add_flag(result, list_of_flags):
            if result:
                list_of_flags.append(flag)

        results.append(result)

    return tuple(results)


@dependable
def warnings_flags():
    return namespace(cflags=[], cxxflags=[], host_cflags=[], host_cxxflags=[])


# 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_warning().
@template
def check_and_add_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]

    return check_and_add_flags(
        warning, warnings_flags, flags, compiler=compiler, when=when, check=check
    )


# 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.


@template
def add_warning(warning, compiler=None, when=None):
    check_and_add_warning(warning, compiler, when, check=False)


# Like the warning checks above, but for general compilation flags.
@dependable
def compilation_flags():
    return namespace(cflags=[], cxxflags=[], host_cflags=[], host_cxxflags=[])


# 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_flag().
@template
def check_and_add_flag(flag, compiler=None, when=None, check=True):
    flags = ["-Werror", flag]

    return check_and_add_flags(
        flag, compilation_flags, flags, compiler=compiler, when=when, check=check
    )


# 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_flag(warning, compiler=None, when=None):
    check_and_add_flag(warning, compiler, when, check=False)


# Like the compilation checks above, but for asm flags.
@dependable
def asm_flags():
    return namespace(asflags=[], host_asflags=[])


# Tests the given assembler flag is supported; if the flag
# is supported, add it to the list of compilation flags for the build.
# - `flag` is the flag to test
# - `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_flag().
@template
def check_and_add_asm_flag(flag, when=None, check=True):
    return check_and_add_flags(
        flag,
        asm_flags,
        [flag],
        when=when,
        check=check,
        mode="assemble",
    )


# Like the compilation checks above, but for linker flags.
@dependable
def linker_flags():
    return namespace(ldflags=[], host_ldflags=[])


# Tests the given linker flag is supported; if the flag
# is supported, add it to the list of compilation flags for the build.
# - `flag` is the flag to test
# - `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_flag().
@template
def check_and_add_linker_flag(flag, compiler=None, when=None, check=True):
    return check_and_add_flags(
        flag,
        linker_flags,
        [flag],
        when=when,
        check=check,
        mode="link",
    )


# Add the given flag to the list of linker flags for the build.
# - `flag` is the flag (e.g. -fno-sized-deallocation)
# - `when` (optional) is a @depends function or option name conditioning
#   when the flag is wanted.
@template
def add_linker_flag(flag, compiler=None, when=None):
    check_and_add_linker_flag(flag, compiler, when, check=False)