-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrefresh-compile-command-bazel-module.py
executable file
·143 lines (110 loc) · 3.65 KB
/
refresh-compile-command-bazel-module.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#! /usr/bin/env python3
# Copyright 2024 Ant Group Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import pathlib
import shutil
import argparse
from utils import run_shell_command_with_live_output
module_file_content = """
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
git_override(
module_name = "hedron_compile_commands",
# TODO: use the main branch commit if the following PR merged.
# https://github.com/hedronvision/bazel-compile-commands-extractor/pull/219
commit = "f5fbd4cee671d8d908f37c83abaf70fba5928fc7",
remote = "https://github.com/hedronvision/bazel-compile-commands-extractor.git",
)
"""
def _build_file_content(content, targets):
return f"""
load("@hedron_compile_commands//:refresh_compile_commands.bzl", "refresh_compile_commands")
{content}
refresh_compile_commands(
name = "refresh_compile_commands",
exclude_external_sources = True,
exclude_headers = "external",
{targets}
)
"""
def _rm(path: str):
p = pathlib.Path(path)
if os.path.isfile:
p.unlink()
else:
p.rmdir()
def _backup_file(path: str):
shutil.copy(src=path, dst=f"{path}_bak")
def _restore_file(path: str):
backup = f"{path}_bak"
shutil.copy(src=backup, dst=path)
_rm(backup)
class BzlModule(object):
def __init__(self):
self.module_file = 'MODULE.bazel'
if os.path.isfile('BUILD.bazel'):
self.build_file = 'BUILD.bazel'
else:
self.build_file = 'BUILD'
self.has_build = os.path.exists(self.build_file)
def __enter__(self):
if self.has_build:
_backup_file(self.build_file)
else:
# Create empty file
with open(self.build_file, 'w') as fp:
pass
_backup_file(self.module_file)
return self
def __exit__(self, *args):
# Restore files
_restore_file(self.module_file)
if self.has_build:
_restore_file(self.build_file)
else:
_rm(self.build_file)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="(Re)generate compile commands for Bazel project"
)
parser.add_argument(
"--targets",
metavar="bazel targets",
type=str,
help="bazel build targets, comma separated",
)
args = parser.parse_args()
with BzlModule() as ws:
# Add hedron_compile_commands to BzlModule
with open(ws.module_file, "a+") as wf:
wf.write(module_file_content)
# Build targets
targets = args.targets
t_str = ""
if targets:
for t in targets.split(','):
t_str += f' "{t}": "",\n'
if t_str:
t_str = f"targets = {{\n{t_str} }}"
# Add build rule
with open(ws.build_file, "r+") as bf:
content = bf.read()
bf.seek(0)
# append load at beginning
content = _build_file_content(content, targets=t_str)
bf.write(content)
# Run
run_shell_command_with_live_output(
'bazel run -s :refresh_compile_commands', cwd=os.getcwd(), shell=True
)