.ycm_extra_conf.py 2.03 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

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

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

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

36
37

def Settings(**kwargs):
38
39
    if kwargs["language"] == "cfamily":
        return FlagsForFile(kwargs["filename"])
40
41
42
43
    # 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 {
44
        "project_directory": ".",
45
    }
46
47


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

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

    # 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
57
    final_flags = [x for x in flag_list if not x.startswith("-march=armv")]
58

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

62
63
    return {"flags": final_flags, "do_cache": True}

64

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