summaryrefslogtreecommitdiff
path: root/ycm_extra_conf.py
blob: e4bde94f429251f4bd7aedb06d423cd1319fd322 (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
import os
import string
import re
import subprocess
import ycm_core

def IsHeaderFile( filename ):
  extension = os.path.splitext( filename )[ 1 ]
  return extension in [ '.h', '.hxx', '.hpp', '.hh' ]

def ContainsJamfile(path):
  for file in os.listdir(path):
    if file == 'Jamfile.jam' or file == 'Jamroot.jam':
      return True
  return False

def FindOwningJamfile(filename):
  path = os.path.dirname(filename)
  while (not ContainsJamfile(path)) or (path == '/'):
    path = os.path.dirname(path)
  return path

def ExpandBackTicks(line):
  bts = re.search("^([^`]*)`([^`]*)`(.*)$", line)
  if bts and (len(bts.groups()) >= 3):
    p = subprocess.Popen(bts.group(2), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    line = bts.group(1) + p.stdout.read() + bts.group(3);
    return ExpandBackTicks(line)
  return line

def GetBJamCommandFlags(rtarget, target):
  flags = []
  p = subprocess.Popen(['b2', '-and2', rtarget], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  for lineBytes in p.stdout.readlines():
    line = lineBytes.decode("utf-8")
    if str.lstrip(line).startswith('"g++"') and str.find(line, '"'+target+'"') > 0:
      flags = []
      line = ExpandBackTicks(line)
      args = str.split(line)
      idx = 1
      while args[idx] != '-c':
        flags.append(args[idx])
        idx += 1
  return flags

def ApplyFlagQuirks(flags):
  newflags = []
  for flag in flags:
    qs = re.search('([^"]+)"([^"]*)"$', flag)
    if qs:
      flag = qs.group(1)
      value = qs.group(2)
      newflags.append(flag)
      if (flag == '-I'):
        newflags.append(os.path.abspath(value))
      else:
        newflags.append(value)
    elif flag != '-Werror':
      newflags.append(flag)
  return newflags

def GuessAnIncluder(filename):
  cpp = str.replace(filename, ".h", ".cpp")
  if os.path.exists(cpp):
    return cpp

  filename = os.path.abspath(filename)
  basename = os.path.basename(filename)
  dirname = os.path.dirname(filename)
  p = subprocess.Popen(['grep', '-Ilrw', basename, dirname], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  f = p.stdout.readlines()[0].strip()
  if IsHeaderFile(f):
    return GuessAnIncluder(f)
  return f

def FlagsForFile( filename, **kwargs ):
  if (IsHeaderFile(filename)):
    filename = GuessAnIncluder(filename)

  filename = os.path.abspath(filename)
  rootDir = FindOwningJamfile(filename)
  target = str.replace(filename, rootDir + "/", "")
  rtarget = str.replace(target, ".cpp", ".o")

  cwd = os.getcwd()
  os.chdir(rootDir)

  flags = GetBJamCommandFlags(rtarget, target)
  flags = ApplyFlagQuirks(flags)

  # Fall back to . for finding precompiled headers
  flags.append("-x")
  flags.append("c++");
  flags.append("-I")
  flags.append(os.path.dirname(filename))

  os.chdir(cwd)

  return {
    'flags': flags,
    'do_cache': False
  }