Ian Zerny | dcb172e | 2022-02-22 15:36:45 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 2 | # Copyright (c) 2017, 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 | |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 6 | import argparse |
Mads Ager | a4911eb | 2017-11-22 13:19:36 +0100 | [diff] [blame] | 7 | import gradle |
| 8 | import hashlib |
Søren Gjesse | a70d3bd | 2019-09-24 15:07:00 +0200 | [diff] [blame] | 9 | import jdk |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 10 | import json |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 11 | from os import makedirs |
Rico Wind | cdc39b6 | 2022-04-08 12:37:57 +0200 | [diff] [blame] | 12 | from os.path import join, basename |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 13 | from shutil import copyfile, make_archive, move, rmtree |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 14 | import subprocess |
| 15 | import sys |
| 16 | from string import Template |
| 17 | import tempfile |
Mads Ager | 0fab491 | 2017-11-20 13:52:48 +0100 | [diff] [blame] | 18 | import utils |
Søren Gjesse | 17fc67d | 2019-12-04 14:50:17 +0100 | [diff] [blame] | 19 | import zipfile |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 20 | |
Mads Ager | a4911eb | 2017-11-22 13:19:36 +0100 | [diff] [blame] | 21 | DEPENDENCYTEMPLATE = Template( |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 22 | """ |
Mads Ager | a4911eb | 2017-11-22 13:19:36 +0100 | [diff] [blame] | 23 | <dependency> |
| 24 | <groupId>$group</groupId> |
| 25 | <artifactId>$artifact</artifactId> |
| 26 | <version>$version</version> |
| 27 | </dependency>""") |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 28 | |
Søren Gjesse | c425e6a | 2019-06-28 11:41:14 +0200 | [diff] [blame] | 29 | LICENSETEMPLATE = Template( |
| 30 | """ |
| 31 | <license> |
| 32 | <name>$name</name> |
| 33 | <url>$url</url> |
| 34 | <distribution>repo</distribution> |
| 35 | </license>""") |
| 36 | |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 37 | R8_POMTEMPLATE = Template( |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 38 | """<project |
| 39 | xmlns="http://maven.apache.org/POM/4.0.0" |
| 40 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 41 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 42 | <modelVersion>4.0.0</modelVersion> |
| 43 | <groupId>com.android.tools</groupId> |
| 44 | <artifactId>r8</artifactId> |
| 45 | <version>$version</version> |
| 46 | <name>D8 dexer and R8 shrinker</name> |
| 47 | <description> |
| 48 | D8 dexer and R8 shrinker. |
| 49 | </description> |
| 50 | <url>http://r8.googlesource.com/r8</url> |
| 51 | <inceptionYear>2016</inceptionYear> |
| 52 | <licenses> |
| 53 | <license> |
| 54 | <name>BSD-3-Clause</name> |
| 55 | <url>https://opensource.org/licenses/BSD-3-Clause</url> |
| 56 | <distribution>repo</distribution> |
Søren Gjesse | c425e6a | 2019-06-28 11:41:14 +0200 | [diff] [blame] | 57 | </license>$library_licenses |
Mads Ager | 7346019 | 2017-11-08 10:02:50 +0100 | [diff] [blame] | 58 | </licenses> |
Mads Ager | a4911eb | 2017-11-22 13:19:36 +0100 | [diff] [blame] | 59 | <dependencies>$dependencies |
| 60 | </dependencies> |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 61 | <developers> |
| 62 | <developer> |
| 63 | <name>The Android Open Source Project</name> |
| 64 | </developer> |
| 65 | </developers> |
| 66 | <scm> |
| 67 | <connection> |
| 68 | https://r8.googlesource.com/r8.git |
| 69 | </connection> |
| 70 | <url> |
| 71 | https://r8.googlesource.com/r8 |
| 72 | </url> |
| 73 | </scm> |
| 74 | </project> |
| 75 | """) |
| 76 | |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 77 | DESUGAR_CONFIGUATION_POMTEMPLATE = Template( |
| 78 | """<project |
| 79 | xmlns="http://maven.apache.org/POM/4.0.0" |
| 80 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 81 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 82 | <modelVersion>4.0.0</modelVersion> |
| 83 | <groupId>com.android.tools</groupId> |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 84 | <artifactId>$artifactId</artifactId> |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 85 | <version>$version</version> |
| 86 | <name>D8 configuration to desugar desugar_jdk_libs</name> |
| 87 | <description> |
| 88 | D8 configuration to desugar desugar_jdk_libs. |
| 89 | </description> |
| 90 | <url>http://r8.googlesource.com/r8</url> |
| 91 | <inceptionYear>2019</inceptionYear> |
| 92 | <licenses> |
| 93 | <license> |
| 94 | <name>BSD-3-Clause</name> |
| 95 | <url>https://opensource.org/licenses/BSD-3-Clause</url> |
| 96 | <distribution>repo</distribution> |
| 97 | </license> |
| 98 | </licenses> |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 99 | <developers> |
| 100 | <developer> |
| 101 | <name>The Android Open Source Project</name> |
| 102 | </developer> |
| 103 | </developers> |
| 104 | <scm> |
| 105 | <connection> |
| 106 | https://r8.googlesource.com/r8.git |
| 107 | </connection> |
| 108 | <url> |
| 109 | https://r8.googlesource.com/r8 |
| 110 | </url> |
| 111 | </scm> |
| 112 | </project> |
| 113 | """) |
| 114 | |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 115 | def parse_options(argv): |
| 116 | result = argparse.ArgumentParser() |
Rico Wind | 8fc8bfa | 2019-03-22 09:57:36 +0100 | [diff] [blame] | 117 | result.add_argument('--out', help='The zip file to output') |
Søren Gjesse | 705a3b1 | 2022-03-17 11:37:30 +0100 | [diff] [blame] | 118 | group = result.add_mutually_exclusive_group() |
| 119 | group.add_argument('--r8lib', action='store_true', |
Rico Wind | 8fc8bfa | 2019-03-22 09:57:36 +0100 | [diff] [blame] | 120 | help='Build r8 with dependencies included shrunken') |
Søren Gjesse | 705a3b1 | 2022-03-17 11:37:30 +0100 | [diff] [blame] | 121 | group.add_argument('--desugar-configuration', action='store_true', |
| 122 | help='Build desugar library configuration (original JDK-8)') |
| 123 | group.add_argument('--desugar-configuration-jdk8', action='store_true', |
| 124 | help='Build desugar library configuration (original JDK-8)') |
| 125 | group.add_argument('--desugar-configuration-jdk11-legacy', action='store_true', |
| 126 | help='Build desugar library configuration (JDK-11 legacy)') |
Søren Gjesse | 1a8ccad | 2023-06-06 15:00:46 +0200 | [diff] [blame] | 127 | group.add_argument('--desugar-configuration-jdk11-minimal', action='store_true', |
| 128 | help='Build desugar library configuration (JDK-11 minimal)') |
| 129 | group.add_argument('--desugar-configuration-jdk11', action='store_true', |
| 130 | help='Build desugar library configuration (JDK-11)') |
| 131 | group.add_argument('--desugar-configuration-jdk11-nio', action='store_true', |
| 132 | help='Build desugar library configuration (JDK-11 nio)') |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 133 | return result.parse_args(argv) |
| 134 | |
Mads Ager | a4911eb | 2017-11-22 13:19:36 +0100 | [diff] [blame] | 135 | def determine_version(): |
| 136 | version_file = join( |
| 137 | utils.SRC_ROOT, 'com', 'android', 'tools', 'r8', 'Version.java') |
| 138 | with open(version_file, 'r') as file: |
| 139 | for line in file: |
| 140 | if 'final String LABEL ' in line: |
Rico Wind | 6feccf2 | 2018-05-08 11:18:22 +0200 | [diff] [blame] | 141 | result = line[line.find('"') + 1:] |
Mads Ager | a4911eb | 2017-11-22 13:19:36 +0100 | [diff] [blame] | 142 | result = result[:result.find('"')] |
| 143 | return result |
| 144 | raise Exception('Unable to determine version.') |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 145 | |
| 146 | def generate_library_licenses(): |
Søren Gjesse | c425e6a | 2019-06-28 11:41:14 +0200 | [diff] [blame] | 147 | artifact_prefix = '- artifact: ' |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 148 | license_prefix = 'license: ' |
| 149 | licenses = [] |
| 150 | license_url_prefix = 'licenseUrl: ' |
| 151 | license_urls = [] |
Søren Gjesse | ea67830 | 2019-08-27 14:41:03 +0200 | [diff] [blame] | 152 | # The ./LIBRARY-LICENSE file is a simple yaml file, which for each dependency |
| 153 | # has the following information: |
| 154 | # |
| 155 | # - artifact: <maven artifact> // in the form <group-id>:<artifact-id>:+ |
| 156 | # name: <name of dependency> |
| 157 | # copyrightHolder: <name of copyright holder> |
| 158 | # license: <license name> |
| 159 | # licenseUrl: <url to license test> |
| 160 | # |
| 161 | # E.g. for Guava: |
| 162 | # |
| 163 | # - artifact: com.google.guava:guava:+ |
| 164 | # name: Guava Google Core Libraries for Java |
| 165 | # copyrightHolder: The Guava Authors |
| 166 | # license: The Apache Software License, Version 2.0 |
| 167 | # licenseUrl: http://www.apache.org/licenses/LICENSE-2.0.txt |
| 168 | # |
| 169 | # This file should always be up to date as the build will fail if it |
| 170 | # is does not have information for all dependencies. |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 171 | with open('LIBRARY-LICENSE', 'r') as file: |
Søren Gjesse | c425e6a | 2019-06-28 11:41:14 +0200 | [diff] [blame] | 172 | name = None |
| 173 | url = None |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 174 | for line in file: |
| 175 | trimmed = line.strip() |
Søren Gjesse | c425e6a | 2019-06-28 11:41:14 +0200 | [diff] [blame] | 176 | # Collect license name and url for each artifact. They must come in |
| 177 | # pairs for each artifact. |
| 178 | if trimmed.startswith(artifact_prefix): |
| 179 | assert not name |
| 180 | assert not url |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 181 | if trimmed.startswith(license_prefix): |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 182 | name = trimmed[len(license_prefix):] |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 183 | if trimmed.startswith(license_url_prefix): |
| 184 | url = trimmed[len(license_url_prefix):] |
Søren Gjesse | c425e6a | 2019-06-28 11:41:14 +0200 | [diff] [blame] | 185 | # Licenses come in name/url pairs. When both are present add pair |
| 186 | # to collected licenses if either name or url has not been recorded yet, |
| 187 | # as some licenses with slightly different names point to the same url. |
| 188 | if name and url: |
| 189 | if (not name in licenses) or (not url in license_urls): |
| 190 | licenses.append(name) |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 191 | license_urls.append(url) |
Søren Gjesse | c425e6a | 2019-06-28 11:41:14 +0200 | [diff] [blame] | 192 | name = None |
| 193 | url = None |
| 194 | assert len(licenses) == len(license_urls) |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 195 | result = '' |
| 196 | for i in range(len(licenses)): |
| 197 | name = licenses[i] |
| 198 | url = license_urls[i] |
| 199 | result += LICENSETEMPLATE.substitute(name=name, url=url) |
| 200 | return result |
| 201 | |
Mads Ager | a4911eb | 2017-11-22 13:19:36 +0100 | [diff] [blame] | 202 | |
| 203 | # Generate the dependencies block for the pom file. |
| 204 | # |
| 205 | # We ask gradle to list all dependencies. In that output |
| 206 | # we locate the runtimeClasspath block for 'main' which |
| 207 | # looks something like: |
| 208 | # |
| 209 | # runtimeClasspath - Runtime classpath of source set 'main'. |
| 210 | # +--- net.sf.jopt-simple:jopt-simple:4.6 |
| 211 | # +--- com.googlecode.json-simple:json-simple:1.1 |
| 212 | # +--- com.google.guava:guava:23.0 |
| 213 | # +--- it.unimi.dsi:fastutil:7.2.0 |
| 214 | # +--- org.ow2.asm:asm:6.0 |
| 215 | # +--- org.ow2.asm:asm-commons:6.0 |
| 216 | # | \--- org.ow2.asm:asm-tree:6.0 |
| 217 | # | \--- org.ow2.asm:asm:6.0 |
| 218 | # +--- org.ow2.asm:asm-tree:6.0 (*) |
| 219 | # +--- org.ow2.asm:asm-analysis:6.0 |
| 220 | # | \--- org.ow2.asm:asm-tree:6.0 (*) |
| 221 | # \--- org.ow2.asm:asm-util:6.0 |
| 222 | # \--- org.ow2.asm:asm-tree:6.0 (*) |
| 223 | # |
| 224 | # We filter out the repeats that are marked by '(*)'. |
| 225 | # |
| 226 | # For each remaining line, we remove the junk at the start |
| 227 | # in chunks. As an example: |
| 228 | # |
| 229 | # ' | \--- org.ow2.asm:asm-tree:6.0 ' --strip--> |
| 230 | # '| \--- org.ow2.asm:asm-tree:6.0' --> |
| 231 | # '\--- org.ow2.asm:asm-tree:6.0' --> |
| 232 | # 'org.ow2.asm:asm-tree:6.0' |
| 233 | # |
| 234 | # The end result is the dependency we are looking for: |
| 235 | # |
| 236 | # groupId: org.ow2.asm |
| 237 | # artifact: asm-tree |
| 238 | # version: 6.0 |
| 239 | def generate_dependencies(): |
| 240 | dependencies = gradle.RunGradleGetOutput(['dependencies']) |
| 241 | dependency_lines = [] |
| 242 | collect = False |
| 243 | for line in dependencies.splitlines(): |
Rico Wind | 5f2d5a7 | 2022-02-22 14:23:37 +0100 | [diff] [blame] | 244 | if line and 'runtimeClasspath' in line and "'main'" in line: |
Mads Ager | a4911eb | 2017-11-22 13:19:36 +0100 | [diff] [blame] | 245 | collect = True |
| 246 | continue |
| 247 | if collect: |
| 248 | if not len(line) == 0: |
| 249 | if not '(*)' in line: |
| 250 | trimmed = line.strip() |
| 251 | while trimmed.find(' ') != -1: |
| 252 | trimmed = trimmed[trimmed.find(' ') + 1:].strip() |
| 253 | if not trimmed in dependency_lines: |
| 254 | dependency_lines.append(trimmed) |
| 255 | else: |
| 256 | break |
| 257 | result = '' |
| 258 | for dep in dependency_lines: |
| 259 | components = dep.split(':') |
| 260 | assert len(components) == 3 |
| 261 | group = components[0] |
| 262 | artifact = components[1] |
| 263 | version = components[2] |
| 264 | result += DEPENDENCYTEMPLATE.substitute( |
| 265 | group=group, artifact=artifact, version=version) |
| 266 | return result |
| 267 | |
Rico Wind | 257044c | 2019-11-22 08:21:21 +0100 | [diff] [blame] | 268 | def write_default_r8_pom_file(pom_file, version): |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 269 | write_pom_file(R8_POMTEMPLATE, pom_file, version, dependencies=generate_dependencies()) |
Rico Wind | 257044c | 2019-11-22 08:21:21 +0100 | [diff] [blame] | 270 | |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 271 | def write_pom_file( |
| 272 | template, pom_file, version, artifact_id=None, dependencies='', library_licenses=''): |
| 273 | version_pom = ( |
| 274 | template.substitute( |
| 275 | artifactId=artifact_id, |
| 276 | version=version, |
| 277 | dependencies=dependencies, |
| 278 | library_licenses=library_licenses) |
| 279 | if artifact_id else |
| 280 | template.substitute( |
| 281 | version=version, dependencies=dependencies, library_licenses=library_licenses)) |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 282 | with open(pom_file, 'w') as file: |
| 283 | file.write(version_pom) |
| 284 | |
| 285 | def hash_for(file, hash): |
| 286 | with open(file, 'rb') as f: |
| 287 | while True: |
| 288 | # Read chunks of 1MB |
| 289 | chunk = f.read(2 ** 20) |
| 290 | if not chunk: |
| 291 | break |
| 292 | hash.update(chunk) |
| 293 | return hash.hexdigest() |
| 294 | |
| 295 | def write_md5_for(file): |
| 296 | hexdigest = hash_for(file, hashlib.md5()) |
| 297 | with (open(file + '.md5', 'w')) as file: |
| 298 | file.write(hexdigest) |
| 299 | |
| 300 | def write_sha1_for(file): |
| 301 | hexdigest = hash_for(file, hashlib.sha1()) |
| 302 | with (open(file + '.sha1', 'w')) as file: |
| 303 | file.write(hexdigest) |
| 304 | |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 305 | def generate_maven_zip(name, version, pom_file, jar_file, out): |
Mads Ager | 14d9b07 | 2017-11-20 13:42:55 +0100 | [diff] [blame] | 306 | with utils.TempDir() as tmp_dir: |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 307 | # Create the base maven version directory |
| 308 | version_dir = join(tmp_dir, utils.get_maven_path(name, version)) |
Mads Ager | 14d9b07 | 2017-11-20 13:42:55 +0100 | [diff] [blame] | 309 | makedirs(version_dir) |
| 310 | # Write the pom file. |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 311 | pom_file_location = join(version_dir, name + '-' + version + '.pom') |
| 312 | copyfile(pom_file, pom_file_location) |
| 313 | # Write the jar file. |
| 314 | jar_file_location = join(version_dir, name + '-' + version + '.jar') |
| 315 | copyfile(jar_file, jar_file_location) |
Mads Ager | 14d9b07 | 2017-11-20 13:42:55 +0100 | [diff] [blame] | 316 | # Create check sums. |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 317 | write_md5_for(jar_file_location) |
| 318 | write_md5_for(pom_file_location) |
| 319 | write_sha1_for(jar_file_location) |
| 320 | write_sha1_for(pom_file_location) |
Rico Wind | 8fc8bfa | 2019-03-22 09:57:36 +0100 | [diff] [blame] | 321 | # Zip it up - make_archive will append zip to the file, so remove. |
| 322 | assert out.endswith('.zip') |
| 323 | base_no_zip = out[0:len(out)-4] |
| 324 | make_archive(base_no_zip, 'zip', tmp_dir) |
| 325 | |
Søren Gjesse | 1b035b3 | 2022-08-19 08:53:57 +0200 | [diff] [blame] | 326 | def generate_r8_maven_zip(out, is_r8lib=False, version_file=None, skip_gradle_build=False): |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 327 | # Build the R8 no deps artifact. |
Søren Gjesse | 1b035b3 | 2022-08-19 08:53:57 +0200 | [diff] [blame] | 328 | if not skip_gradle_build: |
| 329 | if not is_r8lib: |
| 330 | gradle.RunGradleExcludeDeps([utils.R8]) |
| 331 | else: |
| 332 | gradle.RunGradle([utils.R8LIB, '-Pno_internal']) |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 333 | |
| 334 | version = determine_version() |
Rico Wind | 52ece1b | 2020-08-25 14:33:17 +0200 | [diff] [blame] | 335 | with utils.TempDir() as tmp_dir: |
Rico Wind | cdc39b6 | 2022-04-08 12:37:57 +0200 | [diff] [blame] | 336 | file_copy = join(tmp_dir, 'copy_of_jar.jar') |
| 337 | copyfile(utils.R8LIB_JAR if is_r8lib else utils.R8_JAR, file_copy) |
| 338 | |
| 339 | if version_file: |
| 340 | with zipfile.ZipFile(file_copy, 'a') as zip: |
| 341 | zip.write(version_file, basename(version_file)) |
| 342 | |
Rico Wind | 52ece1b | 2020-08-25 14:33:17 +0200 | [diff] [blame] | 343 | # Generate the pom file. |
| 344 | pom_file = join(tmp_dir, 'r8.pom') |
| 345 | write_pom_file( |
| 346 | R8_POMTEMPLATE, |
| 347 | pom_file, |
| 348 | version, |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 349 | dependencies='' if is_r8lib else generate_dependencies(), |
| 350 | library_licenses=generate_library_licenses() if is_r8lib else '') |
Rico Wind | 52ece1b | 2020-08-25 14:33:17 +0200 | [diff] [blame] | 351 | # Write the maven zip file. |
| 352 | generate_maven_zip( |
| 353 | 'r8', |
| 354 | version, |
| 355 | pom_file, |
Rico Wind | cdc39b6 | 2022-04-08 12:37:57 +0200 | [diff] [blame] | 356 | file_copy, |
Rico Wind | 52ece1b | 2020-08-25 14:33:17 +0200 | [diff] [blame] | 357 | out) |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 358 | |
| 359 | # Write the desugaring configuration of a jar file with the following content: |
Søren Gjesse | 17fc67d | 2019-12-04 14:50:17 +0100 | [diff] [blame] | 360 | # java/ |
| 361 | # util/ |
| 362 | # <java.util conversions classes> |
| 363 | # time/ |
| 364 | # <java.time conversions classes> |
| 365 | # META-INF/ |
| 366 | # desugar/ |
| 367 | # d8/ |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 368 | # desugar.json |
Søren Gjesse | 17fc67d | 2019-12-04 14:50:17 +0100 | [diff] [blame] | 369 | # lint/ |
| 370 | # <lint files> |
Søren Gjesse | d98f24c | 2020-10-30 12:42:31 +0100 | [diff] [blame] | 371 | def generate_jar_with_desugar_configuration( |
| 372 | configuration, implementation, conversions, destination): |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 373 | with utils.TempDir() as tmp_dir: |
Søren Gjesse | 17fc67d | 2019-12-04 14:50:17 +0100 | [diff] [blame] | 374 | # Add conversion classes. |
| 375 | with zipfile.ZipFile(conversions, 'r') as conversions_zip: |
| 376 | conversions_zip.extractall(tmp_dir) |
| 377 | |
Søren Gjesse | 1a8ccad | 2023-06-06 15:00:46 +0200 | [diff] [blame] | 378 | # Add configuration. |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 379 | configuration_dir = join(tmp_dir, 'META-INF', 'desugar', 'd8') |
| 380 | makedirs(configuration_dir) |
| 381 | copyfile(configuration, join(configuration_dir, 'desugar.json')) |
Søren Gjesse | a70d3bd | 2019-09-24 15:07:00 +0200 | [diff] [blame] | 382 | |
Søren Gjesse | 17fc67d | 2019-12-04 14:50:17 +0100 | [diff] [blame] | 383 | # Add lint configuartion. |
Søren Gjesse | a70d3bd | 2019-09-24 15:07:00 +0200 | [diff] [blame] | 384 | lint_dir = join(configuration_dir, 'lint') |
| 385 | makedirs(lint_dir) |
| 386 | cmd = [ |
| 387 | jdk.GetJavaExecutable(), |
| 388 | '-cp', |
| 389 | utils.R8_JAR, |
Clément Béra | 9ef8f84 | 2023-05-01 09:16:50 +0200 | [diff] [blame] | 390 | 'com.android.tools.r8.ir.desugar.desugaredlibrary.lint.GenerateDesugaredLibraryLintFiles', |
Søren Gjesse | a70d3bd | 2019-09-24 15:07:00 +0200 | [diff] [blame] | 391 | configuration, |
Søren Gjesse | d98f24c | 2020-10-30 12:42:31 +0100 | [diff] [blame] | 392 | implementation, |
Søren Gjesse | a70d3bd | 2019-09-24 15:07:00 +0200 | [diff] [blame] | 393 | lint_dir] |
| 394 | utils.PrintCmd(cmd) |
| 395 | subprocess.check_call(cmd) |
| 396 | |
Søren Gjesse | 1a8ccad | 2023-06-06 15:00:46 +0200 | [diff] [blame] | 397 | # Add LICENSE file. |
| 398 | copyfile(join(utils.REPO_ROOT, 'LICENSE'), join(tmp_dir, 'LICENSE')) |
| 399 | |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 400 | make_archive(destination, 'zip', tmp_dir) |
| 401 | move(destination + '.zip', destination) |
| 402 | |
Søren Gjesse | 7929810 | 2022-08-23 16:25:41 +0200 | [diff] [blame] | 403 | def convert_desugar_configuration( |
| 404 | configuration, conversions, implementation, machine_configuration): |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 405 | cmd = [jdk.GetJavaExecutable(), |
| 406 | '-cp', |
| 407 | utils.R8_JAR, |
| 408 | 'com.android.tools.r8.ir.desugar.desugaredlibrary.specificationconversion.DesugaredLibraryConverter', |
| 409 | configuration, |
Søren Gjesse | 7929810 | 2022-08-23 16:25:41 +0200 | [diff] [blame] | 410 | implementation, |
| 411 | conversions, |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 412 | utils.get_android_jar(33), |
| 413 | machine_configuration] |
| 414 | subprocess.check_call(cmd) |
| 415 | |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 416 | # Generate the maven zip for the configuration to desugar desugar_jdk_libs. |
Søren Gjesse | e18fa6e | 2022-06-24 15:14:53 +0200 | [diff] [blame] | 417 | def generate_desugar_configuration_maven_zip( |
| 418 | out, configuration, implementation, conversions): |
Rico Wind | 52ece1b | 2020-08-25 14:33:17 +0200 | [diff] [blame] | 419 | with utils.TempDir() as tmp_dir: |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 420 | (name, version) = utils.desugar_configuration_name_and_version(configuration, False) |
| 421 | |
| 422 | if (not version.startswith("1.")): |
| 423 | machine_configuration = join(tmp_dir, "machine.json") |
Søren Gjesse | 7929810 | 2022-08-23 16:25:41 +0200 | [diff] [blame] | 424 | convert_desugar_configuration(configuration, conversions, implementation, machine_configuration) |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 425 | configuration = machine_configuration |
| 426 | |
Rico Wind | 52ece1b | 2020-08-25 14:33:17 +0200 | [diff] [blame] | 427 | # Generate the pom file. |
| 428 | pom_file = join(tmp_dir, 'desugar_configuration.pom') |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 429 | write_pom_file(DESUGAR_CONFIGUATION_POMTEMPLATE, pom_file, version, artifact_id=name) |
Rico Wind | 52ece1b | 2020-08-25 14:33:17 +0200 | [diff] [blame] | 430 | # Generate the jar with the configuration file. |
| 431 | jar_file = join(tmp_dir, 'desugar_configuration.jar') |
| 432 | generate_jar_with_desugar_configuration( |
Søren Gjesse | 705a3b1 | 2022-03-17 11:37:30 +0100 | [diff] [blame] | 433 | configuration, |
| 434 | implementation, |
Søren Gjesse | e18fa6e | 2022-06-24 15:14:53 +0200 | [diff] [blame] | 435 | conversions, |
Rico Wind | 52ece1b | 2020-08-25 14:33:17 +0200 | [diff] [blame] | 436 | jar_file) |
| 437 | # Write the maven zip file. |
Søren Gjesse | 2b04769 | 2022-08-19 16:34:38 +0200 | [diff] [blame] | 438 | generate_maven_zip(name, version, pom_file, jar_file, out) |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 439 | |
Rico Wind | 8fc8bfa | 2019-03-22 09:57:36 +0100 | [diff] [blame] | 440 | def main(argv): |
| 441 | options = parse_options(argv) |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 442 | if options.out == None: |
| 443 | raise Exception( |
| 444 | 'Need to supply output zip with --out.') |
Søren Gjesse | 705a3b1 | 2022-03-17 11:37:30 +0100 | [diff] [blame] | 445 | if options.desugar_configuration or options.desugar_configuration_jdk8: |
| 446 | generate_desugar_configuration_maven_zip( |
Søren Gjesse | 1a8ccad | 2023-06-06 15:00:46 +0200 | [diff] [blame] | 447 | options.out, |
| 448 | utils.DESUGAR_CONFIGURATION, |
| 449 | utils.DESUGAR_IMPLEMENTATION, |
| 450 | utils.LIBRARY_DESUGAR_CONVERSIONS_LEGACY_ZIP) |
Søren Gjesse | 705a3b1 | 2022-03-17 11:37:30 +0100 | [diff] [blame] | 451 | elif options.desugar_configuration_jdk11_legacy: |
| 452 | generate_desugar_configuration_maven_zip( |
Søren Gjesse | 1a8ccad | 2023-06-06 15:00:46 +0200 | [diff] [blame] | 453 | options.out, |
| 454 | utils.DESUGAR_CONFIGURATION_JDK11_LEGACY, |
| 455 | utils.DESUGAR_IMPLEMENTATION_JDK11, |
| 456 | utils.LIBRARY_DESUGAR_CONVERSIONS_LEGACY_ZIP) |
| 457 | elif options.desugar_configuration_jdk11_minimal: |
| 458 | generate_desugar_configuration_maven_zip( |
| 459 | options.out, |
| 460 | utils.DESUGAR_CONFIGURATION_JDK11_MINIMAL, |
| 461 | utils.DESUGAR_IMPLEMENTATION_JDK11, |
| 462 | utils.LIBRARY_DESUGAR_CONVERSIONS_ZIP) |
| 463 | elif options.desugar_configuration_jdk11: |
| 464 | generate_desugar_configuration_maven_zip( |
| 465 | options.out, |
| 466 | utils.DESUGAR_CONFIGURATION_JDK11, |
| 467 | utils.DESUGAR_IMPLEMENTATION_JDK11, |
| 468 | utils.LIBRARY_DESUGAR_CONVERSIONS_ZIP) |
| 469 | elif options.desugar_configuration_jdk11_nio: |
| 470 | generate_desugar_configuration_maven_zip( |
| 471 | options.out, |
| 472 | utils.DESUGAR_CONFIGURATION_JDK11_NIO, |
| 473 | utils.DESUGAR_IMPLEMENTATION_JDK11, |
| 474 | utils.LIBRARY_DESUGAR_CONVERSIONS_ZIP) |
Søren Gjesse | 6e5e584 | 2019-09-03 08:48:30 +0200 | [diff] [blame] | 475 | else: |
| 476 | generate_r8_maven_zip(options.out, options.r8lib) |
Mads Ager | a974561 | 2017-11-02 12:42:15 +0100 | [diff] [blame] | 477 | |
| 478 | if __name__ == "__main__": |
| 479 | exit(main(sys.argv[1:])) |