-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_kernel_toolchain.py
executable file
·230 lines (204 loc) · 8.78 KB
/
update_kernel_toolchain.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
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
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python3
#
# Copyright (C) 2019 The Android Open Source Project
#
# 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.
#
# pylint: disable=not-callable, relative-import, line-too-long, no-else-return
import argparse
import fileinput
import os.path as path
import re
import subprocess
import xml.etree.ElementTree as ET
def green_print(to_print):
print("\033[92m" + to_print + "\033[0m")
def is_clang_project(element):
return element.tag == "project" and \
element.get("path") == "prebuilts-master/clang/host/linux-x86"
class KernelToolchainUpdater():
def __init__(self):
self.parse_args()
if self.clang_version_arg:
self.clang_revision, self.clang_version = self.clang_version_arg.split(':')
else:
self.get_clang_versions()
self.kernel_dir = path.normpath(path.join(self.repo_checkout, self.kernel_relpath))
self.repo_dir = path.join(self.repo_checkout, ".repo", "manifests")
self.topic = path.basename(self.repo_checkout) + "_" + self.clang_revision
if path.basename(self.kernel_dir) != 'common':
self.resync_tree()
self.get_clang_sha()
self.update_sha()
self.commit_sha()
self.push_manifest_change()
self.resync_tree()
self.update_kernel_toolchain()
self.commit_kernel_toolchain()
self.push_kernel_change()
def parse_args(self):
parser = argparse.ArgumentParser()
# TODO: some validation of the command line args would be nice.
parser.add_argument(
"repo_checkout",
help="Source directory of the repo checkout of the kernel.")
parser.add_argument(
"kernel_relpath",
help="Path relative to repo checkout containing kernel source tree.")
parser.add_argument(
"clang_bin", help="Path to the new clang binary in AOSP.")
parser.add_argument(
"bug_number",
help="The bug number to be included in the commit message.")
parser.add_argument(
"-d", action="store_true", help="Dry run and debug.")
parser.add_argument(
"-n", action="store_true", help="No push (but do local changes)")
parser.add_argument(
"--no_topic",
action="store_true",
help="Do not set topic on uploaded changes.")
parser.add_argument(
"--wip",
action="store_true",
help="Mark change as WIP.")
parser.add_argument(
"--hashtag",
metavar="HASHTAG",
help="Hashtags (comma-separated) to apply to pushed changes.")
parser.add_argument(
"--clang_version",
metavar="REVISION:VERSION",
help="REVISION (e.g. r1234) and VERSION (e.g. 10.0.0) to use " +\
"instead of fetching this from clang_bin.")
args = parser.parse_args()
self.bug_number = args.bug_number
self.clang_bin = args.clang_bin
self.repo_checkout = args.repo_checkout
self.kernel_relpath = args.kernel_relpath
self.dry_run = args.d
self.no_push = args.n
self.no_topic = args.no_topic
self.wip = args.wip
self.hashtag = args.hashtag
self.clang_version_arg = args.clang_version
def get_clang_versions(self):
output = subprocess.check_output([self.clang_bin, "--version"]).decode("utf-8")
self.clang_revision = re.search("based on (r[0-9]+[a-z]?)",
output).groups()[0]
self.clang_version = re.search("clang version ([0-9\.]+)",
output).groups()[0]
def get_clang_sha(self):
clang_dir = path.dirname(self.clang_bin)
output = subprocess.check_output(
["git", "--no-pager", "log", "-n", "1"], cwd=clang_dir).decode("utf-8")
self.clang_sha = re.search("commit ([0-9a-z]+)", output).groups()[0]
def update_sha(self):
green_print("Updating SHA")
xml_path = path.join(self.repo_dir, "default.xml")
if self.dry_run:
print("Updating %s to use\n%s." % (xml_path, self.clang_sha))
return
# It would be great to just use the builtin XML serializer/deserializer
# in a sane way; unfortunately this will end up reformatting
# indentation and reordering element attributes.
for line in fileinput.input(xml_path, inplace=True):
try:
element = ET.fromstring(line)
if is_clang_project(element):
line = re.sub("revision=\"[0-9a-z]+\"",
"revision=\"%s\"" % self.clang_sha, line)
except ET.ParseError:
pass
finally:
print(line, end="")
def commit_sha(self):
green_print("Committing SHA")
message = """
Update Clang to %s based on %s
Bug: %s
""".strip() % (self.clang_version, self.clang_revision, self.bug_number)
command = "git commit -asm"
if self.dry_run:
print(command + " \"" + message + "\"")
return
subprocess.check_output(
command.split(" ") + [message], cwd=self.repo_dir)
def push_manifest_change(self):
green_print("Pushing manifest change")
output = subprocess.check_output(["repo", "--no-pager", "info"],
cwd=self.repo_dir).decode("utf-8")
repo_branch = output.split("\n")[1].split(" ")[3].split("/")[2]
command = "git push origin HEAD:refs/for/" + repo_branch
if self.wip:
command += '%wip' # note: no preceding space needed here.
if not self.no_topic:
command += " -o topic=" + self.topic
if self.hashtag:
command += " -o hashtag=" + self.hashtag
if self.dry_run or self.no_push:
print(command)
return
subprocess.check_output(command.split(" "), cwd=self.repo_dir)
def resync_tree(self):
green_print("Syncing kernel tree")
command = "repo sync -c --no-tags -q -n -j 71"
if self.dry_run:
print(command)
return
subprocess.check_output(command.split(" "), cwd=self.repo_dir)
def update_kernel_toolchain(self):
green_print("Updating kernel toolchain")
config_path = path.join(self.kernel_dir, "build.config.constants")
if self.dry_run:
print("Updating %s to use %s." % (config_path, self.clang_revision))
return
for line in fileinput.input(config_path, inplace=True):
line = re.sub("CLANG_VERSION=r[0-9a-z]+",
"CLANG_VERSION=" + self.clang_revision, line)
print(line, end="")
def commit_kernel_toolchain(self):
green_print("Committing kernel toolchain")
message = """
ANDROID: clang: update to %s
Bug: %s
""".strip() % (self.clang_version, self.bug_number)
command = "git commit -asm"
if self.dry_run:
print(command + " \"" + message + "\"")
return
# TODO: it might be nice to `git checkout -b <topic>` before committing.
subprocess.check_output(
command.split(" ") + [message], cwd=self.kernel_dir)
def push_kernel_change(self):
green_print("Pushing kernel change")
xml_path = path.join(self.repo_dir, "default.xml")
remote = subprocess.check_output(["git", "--no-pager", "remote"],
cwd=self.kernel_dir).decode("utf-8").strip()
for project in ET.parse(xml_path).iter("project"):
if (project.get("path") == self.kernel_relpath):
command = "git push %s HEAD:refs/for/%s" % (
remote, project.get("revision"))
if self.wip:
command += '%wip' # note: no preceding space needed here.
if not self.no_topic:
command += " -o topic=" + self.topic
if self.hashtag:
command += " -o hashtag=" + self.hashtag
if self.dry_run or self.no_push:
print(command)
return
subprocess.check_output(command.split(" "), cwd=self.kernel_dir)
break
if __name__ == "__main__":
KernelToolchainUpdater()