blob: c7b46771c89ca36f0d13c1b11e9a59aff4c2a71e [file] [log] [blame]
Haibo Huang993a2a52020-08-12 15:27:30 -07001#!/usr/bin/env python3
Yi Konga8e8ffb2019-05-20 16:37:24 -07002#
3# Copyright (C) 2019 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17# pylint: disable=not-callable, relative-import
18
19""" Update symlinks for binutils """
20
21import argparse
Pirama Arumuga Nainar5529b422020-10-07 13:49:09 -070022import logging
Yabin Cui8a7b4df2022-12-02 10:26:57 -080023from pathlib import Path
24import shutil
Yi Konga8e8ffb2019-05-20 16:37:24 -070025import subprocess
Yabin Cui8a7b4df2022-12-02 10:26:57 -080026from typing import Optional
27
Matthew Maurer0848aa62024-09-24 16:28:42 +000028import context
29from llvm_android import paths, utils
Yi Konga8e8ffb2019-05-20 16:37:24 -070030
Yabin Cui8a7b4df2022-12-02 10:26:57 -080031
Yi Konga8e8ffb2019-05-20 16:37:24 -070032class ArgParser(argparse.ArgumentParser):
33 def __init__(self):
Yabin Cui8a7b4df2022-12-02 10:26:57 -080034 super().__init__(description=__doc__)
Yi Konga8e8ffb2019-05-20 16:37:24 -070035
36 self.add_argument(
37 'version', metavar='VERSION',
38 help='Version of binutil prebuilt updating to (e.g. r123456a).')
39
40 self.add_argument(
41 '-b', '--bug', type=int,
42 help='Bug to reference in commit message.')
43
44 self.add_argument(
45 '--use-current-branch', action='store_true',
46 help='Do not repo start a new branch for the update.')
47
48
Yabin Cui8a7b4df2022-12-02 10:26:57 -080049def update_binutils_symlink(prebuilt_dir: Path, version: str, use_cbr: bool):
50 if not use_cbr:
51 utils.unchecked_call(['repo', 'abandon', 'update-binutils-' + version, prebuilt_dir],
52 stderr=subprocess.DEVNULL)
53 utils.check_call(['repo', 'start', 'update-binutils-' + version, prebuilt_dir])
54
55 binutils_dir = prebuilt_dir / 'llvm-binutils-stable'
56 binutils = [p.name for p in binutils_dir.iterdir()]
57 shutil.rmtree(binutils_dir)
58 binutils_dir.mkdir()
Yi Konga8e8ffb2019-05-20 16:37:24 -070059
60 for b in binutils:
Yabin Cui8a7b4df2022-12-02 10:26:57 -080061 symlink_path = binutils_dir / b
62 util_rela_path = Path('..') / ('clang-' + version) / 'bin' / b
63 symlink_path.symlink_to(util_rela_path)
Yi Konga8e8ffb2019-05-20 16:37:24 -070064
Yabin Cui8a7b4df2022-12-02 10:26:57 -080065 if not symlink_path.exists():
Pirama Arumuga Nainara67e11b2021-04-20 13:52:06 -070066 # check that the created link is valid
67 raise RuntimeError(f'Created symlink, {symlink_path}, is broken')
68
Yi Konga8e8ffb2019-05-20 16:37:24 -070069
Yabin Cui8a7b4df2022-12-02 10:26:57 -080070def do_commit(prebuilt_dir: Path, version: str, bug_id: Optional[str]):
71 utils.check_call(['git', 'add', 'llvm-binutils-stable'], cwd=prebuilt_dir)
Yi Konga8e8ffb2019-05-20 16:37:24 -070072
73 message_lines = []
Yabin Cui8a7b4df2022-12-02 10:26:57 -080074 message_lines.append(f'Update LLVM binutils to {version}.')
Yi Konga8e8ffb2019-05-20 16:37:24 -070075 message_lines.append('')
76 message_lines.append('Test: N/A')
77 if bug_id is not None:
Yabin Cui8a7b4df2022-12-02 10:26:57 -080078 message_lines.append(f'Bug: http://b/{bug_id}')
Yi Konga8e8ffb2019-05-20 16:37:24 -070079 message = '\n'.join(message_lines)
Yabin Cui8a7b4df2022-12-02 10:26:57 -080080 utils.check_call(['git', 'commit', '-m', message], cwd=prebuilt_dir)
Yi Konga8e8ffb2019-05-20 16:37:24 -070081
82
83def main():
Pirama Arumuga Nainar426eb2a2020-10-28 16:13:47 -070084 logging.basicConfig(level=logging.DEBUG)
Yi Konga8e8ffb2019-05-20 16:37:24 -070085 args = ArgParser().parse_args()
86 bug_id = args.bug
87 use_cbr = args.use_current_branch
88 version = args.version
89
90 hosts = ['darwin-x86', 'linux-x86']
91
92 for host in hosts:
Pirama Arumuga Nainar5529b422020-10-07 13:49:09 -070093 prebuilt_dir = paths.PREBUILTS_DIR / 'clang' / 'host' / host
Yabin Cui8a7b4df2022-12-02 10:26:57 -080094 update_binutils_symlink(prebuilt_dir, version, use_cbr)
95 do_commit(prebuilt_dir, version, bug_id)
Yi Konga8e8ffb2019-05-20 16:37:24 -070096
97 return 0
98
99
100if __name__ == '__main__':
101 main()