-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgenerate_compile_commands.py
More file actions
97 lines (79 loc) · 2.83 KB
/
Copy pathgenerate_compile_commands.py
File metadata and controls
97 lines (79 loc) · 2.83 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
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
#!/usr/bin/env python3
"""Generate compile_commands.json for clangd."""
import json
import os
import subprocess
from pathlib import Path
def get_bazel_external_path():
"""Get the path to Bazel's external directory."""
result = subprocess.run(
["bazel", "info", "output_base"],
capture_output=True,
text=True,
check=True,
)
return Path(result.stdout.strip()) / "external"
def main():
workspace_root = Path(__file__).parent
bazel_external = get_bazel_external_path()
# Find nlohmann_json include path
nlohmann_path = None
for p in bazel_external.iterdir():
if p.name.startswith("nlohmann_json"):
include_path = p / "include"
if include_path.exists():
nlohmann_path = include_path
break
if nlohmann_path is None:
print("Warning: nlohmann_json not found in Bazel external")
nlohmann_path = workspace_root # fallback
# Build include flags
include_flags = [
f"-I{workspace_root / 'cpp'}",
f"-I{workspace_root / 'cpp' / 'common'}",
f"-I{workspace_root / 'cpp' / 'models'}",
f"-I{workspace_root}",
f"-I{nlohmann_path}",
]
compile_flags = [
"-std=c++23",
"-Wall",
"-Wno-unused-variable",
"-Wno-unused-parameter",
"-D_GLIBCXX_USE_CXX11_ABI=1",
*include_flags,
]
compile_commands = []
# Add entries for all Solution.cpp files
for cpp_file in workspace_root.rglob("Solution.cpp"):
# Skip premiums folder if needed
if "premiums" in str(cpp_file):
continue
entry = {
"directory": str(workspace_root),
"command": f"clang++ {' '.join(compile_flags)} -c {cpp_file.relative_to(workspace_root)}",
"file": str(cpp_file.relative_to(workspace_root)),
}
compile_commands.append(entry)
# Add entries for cpp/*.cpp files
for cpp_file in (workspace_root / "cpp").rglob("*.cpp"):
entry = {
"directory": str(workspace_root),
"command": f"clang++ {' '.join(compile_flags)} -c {cpp_file.relative_to(workspace_root)}",
"file": str(cpp_file.relative_to(workspace_root)),
}
compile_commands.append(entry)
# Add a catch-all entry for header files
header_entry = {
"directory": str(workspace_root),
"command": f"clang++ {' '.join(compile_flags)} -c dummy.cpp",
"file": "cpp/common/Solution.h",
}
compile_commands.append(header_entry)
output_path = workspace_root / "compile_commands.json"
with open(output_path, "w") as f:
json.dump(compile_commands, f, indent=2)
print(f"Generated {output_path} with {len(compile_commands)} entries")
print(f"nlohmann_json path: {nlohmann_path}")
if __name__ == "__main__":
main()