blob: d5282c72a71e5dc03752ab53befaa8734005cc8d [file] [log] [blame]
Søren Gjesse85c37922022-09-01 16:58:07 +02001#!/usr/bin/env python3
2# Copyright (c) 2022, the R8 project authors. Please see the AUTHORS file
3# for details. All rights reserved. Use of this source code is governed by a
4# BSD-style license that can be found in the LICENSE file.
5
Søren Gjesse0ea3f292023-02-02 14:43:48 +01006from datetime import datetime
Søren Gjesse85c37922022-09-01 16:58:07 +02007import argparse
8import os
9from os.path import join
Søren Gjesse0ea3f292023-02-02 14:43:48 +010010import re
Søren Gjesse85c37922022-09-01 16:58:07 +020011import shutil
12import subprocess
13import sys
14
15import utils
16
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020017
Søren Gjesse0ea3f292023-02-02 14:43:48 +010018def sed(pattern, replace, path):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020019 with open(path, "r") as sources:
20 lines = sources.readlines()
21 with open(path, "w") as sources:
22 for line in lines:
23 sources.write(re.sub(pattern, replace, line))
24
Søren Gjesse0ea3f292023-02-02 14:43:48 +010025
Søren Gjesse85c37922022-09-01 16:58:07 +020026def GetGitHash(checkout_dir):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020027 return subprocess.check_output(
28 ['git', '-C', checkout_dir, 'rev-parse',
29 'HEAD']).decode('utf-8').strip()
30
Søren Gjesse85c37922022-09-01 16:58:07 +020031
32def run(args):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020033 with utils.TempDir() as tmp_dir:
34 use_existing_checkout = args.desugar_jdk_libs_checkout != None
35 checkout_dir = (args.desugar_jdk_libs_checkout if use_existing_checkout
36 else join(tmp_dir, 'desugar_jdk_libs'))
37 if (not use_existing_checkout):
38 subprocess.check_call([
39 'git', 'clone',
40 'https://github.com/google/desugar_jdk_libs.git', checkout_dir
41 ])
42 if (args.desugar_jdk_libs_revision):
43 subprocess.check_call([
44 'git', '-C', checkout_dir, 'checkout',
45 args.desugar_jdk_libs_revision
46 ])
47 print("Hack to workaround b/256723819")
48 os.remove(
49 join(checkout_dir, "jdk11", "src", "java.base", "share", "classes",
50 "java", "time", "format",
51 "DesugarDateTimeFormatterBuilder.java"))
52 print("Building desugared library")
53 bazel = os.path.join(utils.BAZEL_TOOL, 'lib', 'bazel', 'bin', 'bazel')
54 with utils.ChangedWorkingDirectory(checkout_dir):
Søren Gjessed10fec92024-08-19 11:27:07 +020055 subprocess.check_call([
56 bazel, '--bazelrc=/dev/null', 'build',
57 '--spawn_strategy=local', '--verbose_failures', ':desugar_jdk_libs_jdk11'
58 ])
Søren Gjesse85c37922022-09-01 16:58:07 +020059
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020060 openjdk_dir = join('third_party', 'openjdk')
61 openjdk_subdir = 'desugar_jdk_libs_11'
62 dest_dir = join(openjdk_dir, openjdk_subdir)
63 src_dir = join(checkout_dir, 'bazel-bin', 'jdk11', 'src')
Søren Gjesse85c37922022-09-01 16:58:07 +020064
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020065 metadata_files = ('LICENSE', 'README.google')
66 for f in metadata_files:
67 shutil.copyfile(join(dest_dir, f), join(tmp_dir, f))
68 shutil.rmtree(dest_dir)
69 os.remove(join(openjdk_dir, openjdk_subdir + '.tar.gz'))
70 os.remove(join(openjdk_dir, openjdk_subdir + '.tar.gz.sha1'))
71 os.mkdir(dest_dir)
Søren Gjessed10fec92024-08-19 11:27:07 +020072 shutil.copyfile(
73 join(src_dir, 'd8_java_base_selected_with_addon.jar'),
74 join(dest_dir, 'desugar_jdk_libs.jar'))
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020075 for f in metadata_files:
76 shutil.copyfile(join(tmp_dir, f), join(dest_dir, f))
77 desugar_jdk_libs_hash = os.path.join(dest_dir, 'desugar_jdk_libs_hash')
78 with open(desugar_jdk_libs_hash, 'w') as desugar_jdk_libs_hash_writer:
79 desugar_jdk_libs_hash_writer.write(GetGitHash(checkout_dir))
80 sed('^Version: [0-9a-f]{40}$', 'Version: %s' % GetGitHash(checkout_dir),
81 join(dest_dir, 'README.google'))
82 sed('^Date: .*$', 'Date: %s' % datetime.today().strftime('%Y-%m-%d'),
83 join(dest_dir, 'README.google'))
Søren Gjesse85c37922022-09-01 16:58:07 +020084
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020085 print('Now run')
86 print(' (cd %s; upload_to_google_storage.py -a --bucket r8-deps %s)' %
87 (openjdk_dir, openjdk_subdir))
Søren Gjesse85c37922022-09-01 16:58:07 +020088
89
90def main():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020091 args = parse_options()
92 run(args)
93
Søren Gjesse85c37922022-09-01 16:58:07 +020094
95def parse_options():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020096 parser = argparse.ArgumentParser(
97 description='Script for updating third_party/openjdk/desugar_jdk_libs*')
98 parser.add_argument(
99 '--desugar-jdk-libs-checkout',
100 '--desugar_jdk_libs_checkout',
101 default=None,
102 metavar=('<path>'),
103 help='Use existing checkout of github.com/google/desugar_jdk_libs.')
104 parser.add_argument(
105 '--desugar-jdk-libs-revision',
106 '--desugar_jdk_libs_revision',
107 default=None,
108 metavar=('<revision>'),
109 help='Revision of github.com/google/desugar_jdk_libs to use.')
110 args = parser.parse_args()
111 return args
Søren Gjesse85c37922022-09-01 16:58:07 +0200112
113
114if __name__ == '__main__':
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200115 sys.exit(main())