1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this
3# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5import imp
6import os
7import shlex
8import sys
9try:
10    from StringIO import StringIO
11except ImportError:
12    from io import StringIO
13
14old_bytecode = sys.dont_write_bytecode
15sys.dont_write_bytecode = True
16
17path = os.path.join(os.path.dirname(__file__), 'mach')
18
19if not os.path.exists(path):
20    path = os.path.join(os.path.dirname(__file__), 'config.status')
21    config = imp.load_module('_buildconfig', open(path), path, ('', 'r', imp.PY_SOURCE))
22    path = os.path.join(config.topsrcdir, 'mach')
23mach_module = imp.load_module('_mach', open(path), path, ('', 'r', imp.PY_SOURCE))
24
25sys.dont_write_bytecode = old_bytecode
26
27def FlagsForFile(filename):
28    mach = mach_module.get_mach()
29    out = StringIO()
30
31    # Mach calls sys.stdout.fileno(), so we need to fake it when capturing it.
32    # Returning an invalid file descriptor does the trick.
33    out.fileno = lambda: -1
34    out.encoding = None
35    mach.run(['compileflags', filename], stdout=out, stderr=out)
36
37    flag_list = shlex.split(out.getvalue())
38
39    # This flag is added by Fennec for android build and causes ycmd to fail to parse the file.
40    # Removing this flag is a workaround until ycmd starts to handle this flag properly.
41    # https://github.com/Valloric/YouCompleteMe/issues/1490
42    final_flags = [x for x in flag_list if not x.startswith('-march=armv')]
43
44    return {
45        'flags': final_flags,
46        'do_cache': True
47    }
48