-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
221 lines (175 loc) · 6.47 KB
/
build.py
File metadata and controls
221 lines (175 loc) · 6.47 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import os
import re
import yaml
import argparse
import logging
from pathlib import Path
from typing import Dict, List
logging.basicConfig(
format="%(asctime)s, [%(levelname)s]: %(message)s", level=logging.INFO
)
parser = argparse.ArgumentParser(description="Mllm task runner")
parser.add_argument("task_file", type=str, help="Path to task file")
args = parser.parse_args()
task_config = yaml.safe_load(Path(args.task_file).read_text())
PROJECT_ROOT_PATH = Path(os.path.dirname(os.path.abspath(__file__)))
def wildcard_to_regex(pattern):
regex = re.escape(pattern)
return f"^{regex.replace(r'\*', '.*').replace(r'\?', '.')}$"
def filter_files(directory, patterns, ignore_dirs=None, r=True, case_sensitive=True):
root_dir = os.path.abspath(directory)
ignore_abs = set()
if ignore_dirs:
for path in ignore_dirs:
abs_path = os.path.normpath(os.path.join(root_dir, path))
ignore_abs.add(abs_path.lower() if not case_sensitive else abs_path)
flags = 0 if case_sensitive else re.IGNORECASE
if isinstance(patterns, str):
patterns = [patterns]
regexes = [re.compile(wildcard_to_regex(p), flags) for p in patterns]
matched = []
if r:
for root, dirs, files in os.walk(root_dir):
current_path = os.path.normpath(root)
compare_path = current_path.lower() if not case_sensitive else current_path
if any(
compare_path.startswith(ignored + os.sep) or compare_path == ignored
for ignored in ignore_abs
):
dirs[:] = []
continue
dirs[:] = [
d
for d in dirs
if os.path.normpath(os.path.join(current_path, d)) not in ignore_abs
]
for file in files:
if any(rx.match(file) for rx in regexes):
matched.append(os.path.join(root, file))
else:
for entry in os.listdir(root_dir):
full_path = os.path.join(root_dir, entry)
if not os.path.isfile(full_path):
continue
file_path = os.path.normpath(full_path)
compare_path = file_path.lower() if not case_sensitive else file_path
if any(compare_path.startswith(ignored + os.sep) for ignored in ignore_abs):
continue
if any(rx.match(entry) for rx in regexes):
matched.append(full_path)
return matched
class Task:
def __init__(self, config: Dict):
self.config: Dict = config
def make_command_str(self, commands: List) -> str:
return " ".join(commands)
def run(self):
pass
class CMakeConfigTask(Task):
def __init__(self, config):
super().__init__(config)
self.CMAKE_COMMAND = [
"cmake",
"-S",
PROJECT_ROOT_PATH.as_posix(),
]
def run(self):
logging.info("CMake Config Task Start...")
cmake_cfg_path = self.config.get("cmake_cfg_path", "build")
self.CMAKE_COMMAND.extend(
[
"-G",
"Ninja",
"-B",
os.path.join(PROJECT_ROOT_PATH, cmake_cfg_path),
]
)
cmake_build_type = self.config.get("cmake_build_type", "Release")
self.CMAKE_COMMAND.extend(["-DCMAKE_BUILD_TYPE=" + cmake_build_type])
cmake_toolchain_file = self.config.get("cmake_toolchain_file", None)
if cmake_toolchain_file:
self.CMAKE_COMMAND.extend(
[
"-DCMAKE_TOOLCHAIN_FILE=" + cmake_toolchain_file,
]
)
cmake_extra_args = self.config.get("cmake_extra_args", None)
if cmake_extra_args:
self.CMAKE_COMMAND.extend(cmake_extra_args)
commands = self.make_command_str(self.CMAKE_COMMAND)
logging.info(f"{commands}")
os.system(commands)
logging.warning(
f'If you are using vscode to develop. Pls set `"clangd.arguments": ["--compile-commands-dir={os.path.join(PROJECT_ROOT_PATH, cmake_cfg_path)}"]`'
)
logging.info("Finding targets in Ninja Builder:")
os.system(
self.make_command_str(
[
"ninja",
"-C",
os.path.join(PROJECT_ROOT_PATH, cmake_cfg_path),
"-t",
"targets",
]
)
)
class CMakeFormatTask(Task):
def __init__(self, config):
super().__init__(config)
def run(self):
logging.info("CMake Format Task Start...")
ignore_path = self.config.get("ignore_path", [])
cmake_files = filter_files(
PROJECT_ROOT_PATH, ["*.cmake", "CMakeLists.txt"], ignore_path
)
for file in cmake_files:
logging.info(f"cmake-format {file} -o {file}")
os.system(f"cmake-format {file} -o {file}")
class CMakeBuildTask(Task):
def __init__(self, config):
super().__init__(config)
self.CMAKE_COMMAND = [
"cmake",
"--build",
os.path.join(PROJECT_ROOT_PATH, self.config.get("cmake_cfg_path", "build")),
]
def run(self):
logging.info("CMake build Task Start...")
targets = self.config.get("targets", None)
if targets:
for target in targets:
sub_command = self.make_command_str(
self.CMAKE_COMMAND.extend(["--target", target])
)
logging.info(sub_command)
os.system(sub_command)
else:
sub_command = self.make_command_str(self.CMAKE_COMMAND)
logging.info(sub_command)
os.system(sub_command)
class AdbPushTask(Task):
def __init__(self, config):
super().__init__(config)
def run(self):
logging.info("ADB push Task Start...")
files = self.config["files"]
push_path = self.config["to_path"]
for file in files:
command = [
"adb",
"push",
file,
push_path,
]
logging.info(self.make_command_str(command))
os.system(self.make_command_str(command))
TASKS = {
"CMakeConfigTask": CMakeConfigTask,
"CMakeFormatTask": CMakeFormatTask,
"CMakeBuildTask": CMakeBuildTask,
"AdbPushTask": AdbPushTask,
}
if __name__ == "__main__":
for task_name in task_config:
TASKS[task_name](task_config[task_name]).run()