.ycm_extra_conf.py 2.05 KB
Newer Older
1
2
3
4
# 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/.

5
6
from __future__ import absolute_import, print_function
import json
7
8
import os
import shlex
9
import subprocess
10
11
12
13
import sys

old_bytecode = sys.dont_write_bytecode
sys.dont_write_bytecode = True
14

15
path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'mach'))
16

17
# If mach is not here, we're on the objdir go to the srcdir.
18
if not os.path.exists(path):
19
    with open(os.path.join(os.path.dirname(__file__), 'mozinfo.json')) as info:
20
        config = json.loads(info.read())
21
    path = os.path.join(config['topsrcdir'], 'mach')
22

23
24
sys.dont_write_bytecode = old_bytecode

25
def _is_likely_cpp_header(filename):
26
    if not filename.endswith('.h'):
27
28
        return False

29
    if filename.endswith('Inlines.h') or filename.endswith('-inl.h'):
30
31
        return True

32
    cpp_file = filename[:-1] + 'cpp'
33
34
    return os.path.exists(cpp_file)

35
36

def Settings(**kwargs):
37
38
    if kwargs[ 'language' ] == 'cfamily':
        return FlagsForFile(kwargs['filename'])
39
40
41
42
    # This is useful for generic language server protocols, like rust-analyzer,
    # to discover the right project root instead of guessing based on where the
    # closest Cargo.toml is.
    return {
43
      'project_directory': '.',
44
    }
45
46


47
def FlagsForFile(filename):
48
49
    output = subprocess.check_output([path, 'compileflags', filename])
    output = output.decode('utf-8')
50

51
    flag_list = shlex.split(output)
52
53
54
55

    # This flag is added by Fennec for android build and causes ycmd to fail to parse the file.
    # Removing this flag is a workaround until ycmd starts to handle this flag properly.
    # https://github.com/Valloric/YouCompleteMe/issues/1490
56
    final_flags = [x for x in flag_list if not x.startswith('-march=armv')]
57

58
59
60
    if _is_likely_cpp_header(filename):
        final_flags += ["-x", "c++"]

61
62
63
64
    return {
        'flags': final_flags,
        'do_cache': True
    }
65

66
if __name__ == '__main__':
67
    print(FlagsForFile(sys.argv[1]))