forked from ekacnet/ninja2bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppfileparser.py
More file actions
62 lines (55 loc) · 2.25 KB
/
Copy pathcppfileparser.py
File metadata and controls
62 lines (55 loc) · 2.25 KB
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
import logging
import os
import re
from typing import List
def findAllHeaderFiles(current_dir: str) -> List[str]:
for dirpath, dirname, files in os.walk(current_dir):
for f in files:
if f.endswith(".h") or f.endswith(".hpp"):
yield (f"{dirpath}/{f}")
def parseIncludes(includes: str) -> List[str]:
matches = re.findall(r"-I([^ ](?:[^ ]|(?: (?!(?:-I)|$)))+)", includes)
return set(matches)
def findIncludes(name: str, includes: str) -> List[str]:
if includes is not None:
includes_dirs = parseIncludes(includes)
else:
includes_dirs = []
current_dir = os.path.dirname(os.path.abspath(name))
logging.debug(f"Handling findIncludes {name}")
with open(name, "r") as f:
content = f.readlines()
ret = []
for line in content:
match = re.match(r'#include ((?:<|").*(?:>|"))', line)
if not match:
continue
current_include = match.group(1)
file = current_include[1:-1]
if current_include.startswith('"'):
full_file_name = f"{current_dir}/{file}"
if os.path.exists(full_file_name):
logging.debug(f"Found {file} in the same directory as the looked file")
ret.append(full_file_name)
ret.extend(findIncludes(full_file_name, includes))
else:
# file don't exists in the same directory, let's try to find one
# elsewhere
for d in includes_dirs:
full_file_name = f"{current_dir}/{d}/{file}"
if not os.path.exists(full_file_name):
continue
logging.debug(f"Found {file} in the includes variable")
ret.append(full_file_name)
ret.extend(findIncludes(full_file_name, includes))
break
else:
for d in includes_dirs:
full_file_name = f"{current_dir}/{d}/{file}"
if not os.path.exists(full_file_name):
continue
logging.debug(f"Found {file} in the includes variable")
ret.append(full_file_name)
ret.extend(findIncludes(full_file_name, includes))
break
return ret