Ian Zerny | ac0b178 | 2022-02-22 14:19:50 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 2 | # Copyright (c) 2016, 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 | |
| 6 | # Wrapper script for running gradle. |
| 7 | # Will make sure we pulled down gradle before running, and will use the pulled |
| 8 | # down version to have a consistent developer experience. |
| 9 | |
Mads Ager | 980ad76 | 2019-02-20 11:35:59 +0100 | [diff] [blame] | 10 | import argparse |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 11 | import os |
| 12 | import subprocess |
| 13 | import sys |
Ian Zerny | 5fffb0a | 2019-02-11 13:54:22 +0100 | [diff] [blame] | 14 | |
| 15 | import jdk |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 16 | import utils |
| 17 | |
| 18 | GRADLE_DIR = os.path.join(utils.REPO_ROOT, 'third_party', 'gradle') |
| 19 | GRADLE_SHA1 = os.path.join(GRADLE_DIR, 'gradle.tar.gz.sha1') |
Rico Wind | 51853ee | 2023-09-06 11:26:42 +0200 | [diff] [blame] | 20 | GRADLE8_SHA1 = os.path.join(GRADLE_DIR, 'gradle-8.3.tar.gz.sha1') |
Stephan Herhut | 928ab0e | 2017-07-18 10:53:23 +0200 | [diff] [blame] | 21 | GRADLE_TGZ = os.path.join(GRADLE_DIR, 'gradle.tar.gz') |
Rico Wind | 51853ee | 2023-09-06 11:26:42 +0200 | [diff] [blame] | 22 | GRADLE8_TGZ = os.path.join(GRADLE_DIR, 'gradle-8.3.tar.gz') |
Mads Ager | 1d5ae40 | 2017-09-22 12:30:56 +0200 | [diff] [blame] | 23 | |
Morten Krogh-Jespersen | 5a37de8 | 2023-03-02 01:42:28 +0100 | [diff] [blame] | 24 | def get_gradle(new_gradle): |
Rico Wind | 51853ee | 2023-09-06 11:26:42 +0200 | [diff] [blame] | 25 | gradle_dir = 'gradle-8.3' if new_gradle else 'gradle' |
Morten Krogh-Jespersen | 5a37de8 | 2023-03-02 01:42:28 +0100 | [diff] [blame] | 26 | if utils.IsWindows(): |
| 27 | return os.path.join(GRADLE_DIR, gradle_dir, 'bin', 'gradle.bat') |
| 28 | else: |
| 29 | return os.path.join(GRADLE_DIR, gradle_dir, 'bin', 'gradle') |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 30 | |
Ian Zerny | 5fffb0a | 2019-02-11 13:54:22 +0100 | [diff] [blame] | 31 | def ParseOptions(): |
Mads Ager | 980ad76 | 2019-02-20 11:35:59 +0100 | [diff] [blame] | 32 | parser = argparse.ArgumentParser(description = 'Call gradle.') |
Søren Gjesse | 5ec0a73 | 2021-01-28 11:54:00 +0100 | [diff] [blame] | 33 | parser.add_argument('--exclude-deps', '--exclude_deps', |
| 34 | help='Build without internalized dependencies.', |
| 35 | default=False, action='store_true') |
Morten Krogh-Jespersen | 5a37de8 | 2023-03-02 01:42:28 +0100 | [diff] [blame] | 36 | parser.add_argument('--new-gradle', '--new_gradle', |
| 37 | help='Run with new experimental gradle configuration', |
| 38 | default=False, action='store_true') |
Rico Wind | 0957061 | 2019-07-04 08:47:21 +0200 | [diff] [blame] | 39 | parser.add_argument('--no-internal', '--no_internal', |
| 40 | help='Do not build with support for Google internal tests.', |
| 41 | default=False, action='store_true') |
Mads Ager | 980ad76 | 2019-02-20 11:35:59 +0100 | [diff] [blame] | 42 | parser.add_argument('--java-home', '--java_home', |
Ian Zerny | 5fffb0a | 2019-02-11 13:54:22 +0100 | [diff] [blame] | 43 | help='Use a custom java version to run gradle.') |
Morten Krogh-Jespersen | 2923950 | 2019-10-14 09:11:23 +0200 | [diff] [blame] | 44 | parser.add_argument('--worktree', |
| 45 | help='Gradle is running in a worktree and may lock up ' |
| 46 | 'the gradle caches.', |
| 47 | action='store_true', |
| 48 | default=False) |
Mads Ager | 980ad76 | 2019-02-20 11:35:59 +0100 | [diff] [blame] | 49 | return parser.parse_known_args() |
Ian Zerny | 5fffb0a | 2019-02-11 13:54:22 +0100 | [diff] [blame] | 50 | |
| 51 | def GetJavaEnv(env): |
Rico Wind | 0e0bbd1 | 2019-02-27 14:43:15 +0100 | [diff] [blame] | 52 | java_env = dict(env if env else os.environ, JAVA_HOME = jdk.GetJdkHome()) |
| 53 | java_env['PATH'] = java_env['PATH'] + os.pathsep + os.path.join(jdk.GetJdkHome(), 'bin') |
Ian Zerny | 9794cfd | 2020-02-04 07:57:35 +0100 | [diff] [blame] | 54 | java_env['GRADLE_OPTS'] = '-Xmx1g' |
Rico Wind | 0e0bbd1 | 2019-02-27 14:43:15 +0100 | [diff] [blame] | 55 | return java_env |
Ian Zerny | 5fffb0a | 2019-02-11 13:54:22 +0100 | [diff] [blame] | 56 | |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 57 | def PrintCmd(s): |
| 58 | if type(s) is list: |
| 59 | s = ' '.join(s) |
Rico Wind | 3d369b4 | 2021-01-12 10:26:24 +0100 | [diff] [blame] | 60 | print('Running: %s' % s) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 61 | # I know this will hit os on windows eventually if we don't do this. |
| 62 | sys.stdout.flush() |
| 63 | |
Rico Wind | e6e0263 | 2023-03-02 10:29:30 +0100 | [diff] [blame] | 64 | def EnsureGradle(): |
Ian Zerny | 5fffb0a | 2019-02-11 13:54:22 +0100 | [diff] [blame] | 65 | utils.EnsureDepFromGoogleCloudStorage( |
Rico Wind | e6e0263 | 2023-03-02 10:29:30 +0100 | [diff] [blame] | 66 | get_gradle(False), GRADLE_TGZ, GRADLE_SHA1, 'Gradle binary') |
Morten Krogh-Jespersen | 5cd5eaa | 2023-03-02 09:44:50 +0100 | [diff] [blame] | 67 | utils.EnsureDepFromGoogleCloudStorage( |
Rico Wind | e6e0263 | 2023-03-02 10:29:30 +0100 | [diff] [blame] | 68 | get_gradle(True), GRADLE8_TGZ, GRADLE8_SHA1, 'Gradle binary') |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 69 | |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 70 | def EnsureJdk(): |
Rico Wind | 80af620 | 2023-08-29 12:41:01 +0200 | [diff] [blame] | 71 | # Gradle in the new setup will use the jdks in the evaluation - fetch |
| 72 | # all beforehand. |
| 73 | for root in jdk.GetAllJdkDirs(): |
| 74 | jdkTgz = root + '.tar.gz' |
| 75 | jdkSha1 = jdkTgz + '.sha1' |
| 76 | utils.EnsureDepFromGoogleCloudStorage(root, jdkTgz, jdkSha1, root) |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 77 | |
Rico Wind | e6e0263 | 2023-03-02 10:29:30 +0100 | [diff] [blame] | 78 | def EnsureDeps(): |
| 79 | EnsureGradle() |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 80 | EnsureJdk() |
Mads Ager | a4911eb | 2017-11-22 13:19:36 +0100 | [diff] [blame] | 81 | |
Morten Krogh-Jespersen | 5a37de8 | 2023-03-02 01:42:28 +0100 | [diff] [blame] | 82 | def RunGradleIn( |
| 83 | gradleCmd, args, cwd, throw_on_failure=True, env=None, new_gradle=False): |
Rico Wind | e6e0263 | 2023-03-02 10:29:30 +0100 | [diff] [blame] | 84 | EnsureDeps() |
Jean-Marie Henaff | e4e36d1 | 2018-04-05 10:33:50 +0200 | [diff] [blame] | 85 | cmd = [gradleCmd] |
Morten Krogh-Jespersen | 51026a4 | 2023-08-28 13:34:56 +0200 | [diff] [blame] | 86 | if new_gradle: |
| 87 | args.extend(['--offline', '-c=d8_r8/settings.gradle.kts']) |
| 88 | else: |
| 89 | args.append('-b=build.gradle') |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 90 | cmd.extend(args) |
| 91 | utils.PrintCmd(cmd) |
Jean-Marie Henaff | e4e36d1 | 2018-04-05 10:33:50 +0200 | [diff] [blame] | 92 | with utils.ChangedWorkingDirectory(cwd): |
Ian Zerny | 5fffb0a | 2019-02-11 13:54:22 +0100 | [diff] [blame] | 93 | return_value = subprocess.call(cmd, env=GetJavaEnv(env)) |
Tamas Kenez | b787bc8 | 2017-06-27 11:32:20 +0200 | [diff] [blame] | 94 | if throw_on_failure and return_value != 0: |
Rico Wind | 0c2dff9 | 2017-11-29 09:27:12 +0100 | [diff] [blame] | 95 | raise Exception('Failed to execute gradle') |
Rico Wind | a94f01c | 2017-06-27 10:32:34 +0200 | [diff] [blame] | 96 | return return_value |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 97 | |
Jean-Marie Henaff | 889dd94 | 2018-04-05 15:56:36 +0200 | [diff] [blame] | 98 | def RunGradleWrapperIn(args, cwd, throw_on_failure=True, env=None): |
| 99 | return RunGradleIn('./gradlew', args, cwd, throw_on_failure, env=env) |
Jean-Marie Henaff | e4e36d1 | 2018-04-05 10:33:50 +0200 | [diff] [blame] | 100 | |
Morten Krogh-Jespersen | 5a37de8 | 2023-03-02 01:42:28 +0100 | [diff] [blame] | 101 | def RunGradle(args, throw_on_failure=True, env=None, new_gradle=False): |
| 102 | return RunGradleIn( |
| 103 | get_gradle(new_gradle), |
| 104 | args, |
| 105 | utils.REPO_ROOT, |
| 106 | throw_on_failure, |
| 107 | env=env, |
| 108 | new_gradle=new_gradle) |
Jean-Marie Henaff | e4e36d1 | 2018-04-05 10:33:50 +0200 | [diff] [blame] | 109 | |
Rico Wind | e6e0263 | 2023-03-02 10:29:30 +0100 | [diff] [blame] | 110 | def RunGradleExcludeDeps(args, throw_on_failure=True, env=None): |
| 111 | EnsureDeps() |
Mads Ager | a4911eb | 2017-11-22 13:19:36 +0100 | [diff] [blame] | 112 | args.append('-Pexclude_deps') |
Jean-Marie Henaff | 889dd94 | 2018-04-05 15:56:36 +0200 | [diff] [blame] | 113 | return RunGradle(args, throw_on_failure, env=env) |
Mads Ager | a4911eb | 2017-11-22 13:19:36 +0100 | [diff] [blame] | 114 | |
Rico Wind | e6e0263 | 2023-03-02 10:29:30 +0100 | [diff] [blame] | 115 | def RunGradleInGetOutput(gradleCmd, args, cwd, env=None): |
| 116 | EnsureDeps() |
Jean-Marie Henaff | e4e36d1 | 2018-04-05 10:33:50 +0200 | [diff] [blame] | 117 | cmd = [gradleCmd] |
Mads Ager | a4911eb | 2017-11-22 13:19:36 +0100 | [diff] [blame] | 118 | cmd.extend(args) |
| 119 | utils.PrintCmd(cmd) |
Jean-Marie Henaff | e4e36d1 | 2018-04-05 10:33:50 +0200 | [diff] [blame] | 120 | with utils.ChangedWorkingDirectory(cwd): |
Rico Wind | fd18637 | 2022-02-28 08:55:48 +0100 | [diff] [blame] | 121 | return subprocess.check_output(cmd, env=GetJavaEnv(env)).decode('utf-8') |
Mads Ager | a4911eb | 2017-11-22 13:19:36 +0100 | [diff] [blame] | 122 | |
Jean-Marie Henaff | 889dd94 | 2018-04-05 15:56:36 +0200 | [diff] [blame] | 123 | def RunGradleWrapperInGetOutput(args, cwd, env=None): |
| 124 | return RunGradleInGetOutput('./gradlew', args, cwd, env=env) |
Jean-Marie Henaff | e4e36d1 | 2018-04-05 10:33:50 +0200 | [diff] [blame] | 125 | |
Morten Krogh-Jespersen | 5a37de8 | 2023-03-02 01:42:28 +0100 | [diff] [blame] | 126 | def RunGradleGetOutput(args, env=None, new_gradle=False): |
| 127 | return RunGradleInGetOutput( |
| 128 | get_gradle(new_gradle), |
| 129 | args, |
| 130 | utils.REPO_ROOT, |
Rico Wind | e6e0263 | 2023-03-02 10:29:30 +0100 | [diff] [blame] | 131 | env=env) |
Jean-Marie Henaff | e4e36d1 | 2018-04-05 10:33:50 +0200 | [diff] [blame] | 132 | |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 133 | def Main(): |
Ian Zerny | 5fffb0a | 2019-02-11 13:54:22 +0100 | [diff] [blame] | 134 | (options, args) = ParseOptions() |
Ian Zerny | 5fffb0a | 2019-02-11 13:54:22 +0100 | [diff] [blame] | 135 | if options.java_home: |
Mads Ager | 980ad76 | 2019-02-20 11:35:59 +0100 | [diff] [blame] | 136 | args.append('-Dorg.gradle.java.home=' + options.java_home) |
Rico Wind | 0957061 | 2019-07-04 08:47:21 +0200 | [diff] [blame] | 137 | if options.no_internal: |
| 138 | args.append('-Pno_internal') |
Søren Gjesse | 5ec0a73 | 2021-01-28 11:54:00 +0100 | [diff] [blame] | 139 | if options.exclude_deps: |
| 140 | args.append('-Pexclude_deps') |
Morten Krogh-Jespersen | 2923950 | 2019-10-14 09:11:23 +0200 | [diff] [blame] | 141 | if options.worktree: |
| 142 | args.append('-g=' + os.path.join(utils.REPO_ROOT, ".gradle_user_home")) |
Morten Krogh-Jespersen | 5a37de8 | 2023-03-02 01:42:28 +0100 | [diff] [blame] | 143 | return RunGradle(args, new_gradle=options.new_gradle) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 144 | |
| 145 | if __name__ == '__main__': |
| 146 | sys.exit(Main()) |