summaryrefslogtreecommitdiffstats
path: root/python/mozbuild/mozbuild/compilation/util.py
blob: fc06382a3bf8e2880cdd959d3a7cc59d25a098a5 (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
# 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/.

import os


def check_top_objdir(topobjdir):
    top_make = os.path.join(topobjdir, "Makefile")
    if not os.path.exists(top_make):
        print(
            "Your tree has not been built yet. Please run "
            "|mach build| with no arguments."
        )
        return False
    return True


def get_build_vars(directory, cmd):
    build_vars = {}

    def on_line(line):
        elements = [s.strip() for s in line.split("=", 1)]

        if len(elements) != 2:
            return

        build_vars[elements[0]] = elements[1]

    try:
        old_logger = cmd.log_manager.replace_terminal_handler(None)
        cmd._run_make(
            directory=directory,
            target="showbuild",
            log=False,
            print_directory=False,
            num_jobs=1,
            silent=True,
            line_handler=on_line,
        )
    finally:
        cmd.log_manager.replace_terminal_handler(old_logger)

    return build_vars


def sanitize_cflags(flags):
    # We filter out -Xclang arguments as clang based tools typically choke on
    # passing these flags down to the clang driver.  -Xclang tells the clang
    # driver driver to pass whatever comes after it down to clang cc1, which is
    # why we skip -Xclang and the argument immediately after it.  Here is an
    # example: the following two invocations pass |-foo -bar -baz| to cc1:
    # clang -cc1 -foo -bar -baz
    # clang -Xclang -foo -Xclang -bar -Xclang -baz
    sanitized = []
    saw_xclang = False
    for flag in flags:
        if flag == "-Xclang":
            saw_xclang = True
        elif saw_xclang:
            saw_xclang = False
        else:
            sanitized.append(flag)
    return sanitized