blob: 3e3c464e96238546c82ad322a77b6ff795b4f469 [file] [log] [blame]
Ian Zernyac0b1782022-02-22 14:19:50 +01001#!/usr/bin/env python3
Mads Ager418d1ca2017-05-22 09:35:49 +02002# 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 Ager980ad762019-02-20 11:35:59 +010010import argparse
Mads Ager418d1ca2017-05-22 09:35:49 +020011import os
12import subprocess
13import sys
Ian Zerny5fffb0a2019-02-11 13:54:22 +010014
15import jdk
Mads Ager418d1ca2017-05-22 09:35:49 +020016import utils
17
18GRADLE_DIR = os.path.join(utils.REPO_ROOT, 'third_party', 'gradle')
19GRADLE_SHA1 = os.path.join(GRADLE_DIR, 'gradle.tar.gz.sha1')
Rico Wind51853ee2023-09-06 11:26:42 +020020GRADLE8_SHA1 = os.path.join(GRADLE_DIR, 'gradle-8.3.tar.gz.sha1')
Stephan Herhut928ab0e2017-07-18 10:53:23 +020021GRADLE_TGZ = os.path.join(GRADLE_DIR, 'gradle.tar.gz')
Rico Wind51853ee2023-09-06 11:26:42 +020022GRADLE8_TGZ = os.path.join(GRADLE_DIR, 'gradle-8.3.tar.gz')
Mads Ager1d5ae402017-09-22 12:30:56 +020023
Morten Krogh-Jespersen5a37de82023-03-02 01:42:28 +010024def get_gradle(new_gradle):
Rico Wind51853ee2023-09-06 11:26:42 +020025 gradle_dir = 'gradle-8.3' if new_gradle else 'gradle'
Morten Krogh-Jespersen5a37de82023-03-02 01:42:28 +010026 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 Ager418d1ca2017-05-22 09:35:49 +020030
Ian Zerny5fffb0a2019-02-11 13:54:22 +010031def ParseOptions():
Mads Ager980ad762019-02-20 11:35:59 +010032 parser = argparse.ArgumentParser(description = 'Call gradle.')
Søren Gjesse5ec0a732021-01-28 11:54:00 +010033 parser.add_argument('--exclude-deps', '--exclude_deps',
34 help='Build without internalized dependencies.',
35 default=False, action='store_true')
Morten Krogh-Jespersen5a37de82023-03-02 01:42:28 +010036 parser.add_argument('--new-gradle', '--new_gradle',
37 help='Run with new experimental gradle configuration',
38 default=False, action='store_true')
Rico Wind09570612019-07-04 08:47:21 +020039 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 Ager980ad762019-02-20 11:35:59 +010042 parser.add_argument('--java-home', '--java_home',
Ian Zerny5fffb0a2019-02-11 13:54:22 +010043 help='Use a custom java version to run gradle.')
Morten Krogh-Jespersen29239502019-10-14 09:11:23 +020044 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 Ager980ad762019-02-20 11:35:59 +010049 return parser.parse_known_args()
Ian Zerny5fffb0a2019-02-11 13:54:22 +010050
51def GetJavaEnv(env):
Rico Wind0e0bbd12019-02-27 14:43:15 +010052 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 Zerny9794cfd2020-02-04 07:57:35 +010054 java_env['GRADLE_OPTS'] = '-Xmx1g'
Rico Wind0e0bbd12019-02-27 14:43:15 +010055 return java_env
Ian Zerny5fffb0a2019-02-11 13:54:22 +010056
Mads Ager418d1ca2017-05-22 09:35:49 +020057def PrintCmd(s):
58 if type(s) is list:
59 s = ' '.join(s)
Rico Wind3d369b42021-01-12 10:26:24 +010060 print('Running: %s' % s)
Mads Ager418d1ca2017-05-22 09:35:49 +020061 # I know this will hit os on windows eventually if we don't do this.
62 sys.stdout.flush()
63
Rico Winde6e02632023-03-02 10:29:30 +010064def EnsureGradle():
Ian Zerny5fffb0a2019-02-11 13:54:22 +010065 utils.EnsureDepFromGoogleCloudStorage(
Rico Winde6e02632023-03-02 10:29:30 +010066 get_gradle(False), GRADLE_TGZ, GRADLE_SHA1, 'Gradle binary')
Morten Krogh-Jespersen5cd5eaa2023-03-02 09:44:50 +010067 utils.EnsureDepFromGoogleCloudStorage(
Rico Winde6e02632023-03-02 10:29:30 +010068 get_gradle(True), GRADLE8_TGZ, GRADLE8_SHA1, 'Gradle binary')
Mads Ager418d1ca2017-05-22 09:35:49 +020069
Ian Zerny3f54e222019-02-12 10:51:17 +010070def EnsureJdk():
Rico Wind80af6202023-08-29 12:41:01 +020071 # 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 Zerny3f54e222019-02-12 10:51:17 +010077
Rico Winde6e02632023-03-02 10:29:30 +010078def EnsureDeps():
79 EnsureGradle()
Ian Zerny3f54e222019-02-12 10:51:17 +010080 EnsureJdk()
Mads Agera4911eb2017-11-22 13:19:36 +010081
Morten Krogh-Jespersen5a37de82023-03-02 01:42:28 +010082def RunGradleIn(
83 gradleCmd, args, cwd, throw_on_failure=True, env=None, new_gradle=False):
Rico Winde6e02632023-03-02 10:29:30 +010084 EnsureDeps()
Jean-Marie Henaffe4e36d12018-04-05 10:33:50 +020085 cmd = [gradleCmd]
Morten Krogh-Jespersen51026a42023-08-28 13:34:56 +020086 if new_gradle:
87 args.extend(['--offline', '-c=d8_r8/settings.gradle.kts'])
88 else:
89 args.append('-b=build.gradle')
Mads Ager418d1ca2017-05-22 09:35:49 +020090 cmd.extend(args)
91 utils.PrintCmd(cmd)
Jean-Marie Henaffe4e36d12018-04-05 10:33:50 +020092 with utils.ChangedWorkingDirectory(cwd):
Ian Zerny5fffb0a2019-02-11 13:54:22 +010093 return_value = subprocess.call(cmd, env=GetJavaEnv(env))
Tamas Kenezb787bc82017-06-27 11:32:20 +020094 if throw_on_failure and return_value != 0:
Rico Wind0c2dff92017-11-29 09:27:12 +010095 raise Exception('Failed to execute gradle')
Rico Winda94f01c2017-06-27 10:32:34 +020096 return return_value
Mads Ager418d1ca2017-05-22 09:35:49 +020097
Jean-Marie Henaff889dd942018-04-05 15:56:36 +020098def RunGradleWrapperIn(args, cwd, throw_on_failure=True, env=None):
99 return RunGradleIn('./gradlew', args, cwd, throw_on_failure, env=env)
Jean-Marie Henaffe4e36d12018-04-05 10:33:50 +0200100
Morten Krogh-Jespersen5a37de82023-03-02 01:42:28 +0100101def 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 Henaffe4e36d12018-04-05 10:33:50 +0200109
Rico Winde6e02632023-03-02 10:29:30 +0100110def RunGradleExcludeDeps(args, throw_on_failure=True, env=None):
111 EnsureDeps()
Mads Agera4911eb2017-11-22 13:19:36 +0100112 args.append('-Pexclude_deps')
Jean-Marie Henaff889dd942018-04-05 15:56:36 +0200113 return RunGradle(args, throw_on_failure, env=env)
Mads Agera4911eb2017-11-22 13:19:36 +0100114
Rico Winde6e02632023-03-02 10:29:30 +0100115def RunGradleInGetOutput(gradleCmd, args, cwd, env=None):
116 EnsureDeps()
Jean-Marie Henaffe4e36d12018-04-05 10:33:50 +0200117 cmd = [gradleCmd]
Mads Agera4911eb2017-11-22 13:19:36 +0100118 cmd.extend(args)
119 utils.PrintCmd(cmd)
Jean-Marie Henaffe4e36d12018-04-05 10:33:50 +0200120 with utils.ChangedWorkingDirectory(cwd):
Rico Windfd186372022-02-28 08:55:48 +0100121 return subprocess.check_output(cmd, env=GetJavaEnv(env)).decode('utf-8')
Mads Agera4911eb2017-11-22 13:19:36 +0100122
Jean-Marie Henaff889dd942018-04-05 15:56:36 +0200123def RunGradleWrapperInGetOutput(args, cwd, env=None):
124 return RunGradleInGetOutput('./gradlew', args, cwd, env=env)
Jean-Marie Henaffe4e36d12018-04-05 10:33:50 +0200125
Morten Krogh-Jespersen5a37de82023-03-02 01:42:28 +0100126def RunGradleGetOutput(args, env=None, new_gradle=False):
127 return RunGradleInGetOutput(
128 get_gradle(new_gradle),
129 args,
130 utils.REPO_ROOT,
Rico Winde6e02632023-03-02 10:29:30 +0100131 env=env)
Jean-Marie Henaffe4e36d12018-04-05 10:33:50 +0200132
Mads Ager418d1ca2017-05-22 09:35:49 +0200133def Main():
Ian Zerny5fffb0a2019-02-11 13:54:22 +0100134 (options, args) = ParseOptions()
Ian Zerny5fffb0a2019-02-11 13:54:22 +0100135 if options.java_home:
Mads Ager980ad762019-02-20 11:35:59 +0100136 args.append('-Dorg.gradle.java.home=' + options.java_home)
Rico Wind09570612019-07-04 08:47:21 +0200137 if options.no_internal:
138 args.append('-Pno_internal')
Søren Gjesse5ec0a732021-01-28 11:54:00 +0100139 if options.exclude_deps:
140 args.append('-Pexclude_deps')
Morten Krogh-Jespersen29239502019-10-14 09:11:23 +0200141 if options.worktree:
142 args.append('-g=' + os.path.join(utils.REPO_ROOT, ".gradle_user_home"))
Morten Krogh-Jespersen5a37de82023-03-02 01:42:28 +0100143 return RunGradle(args, new_gradle=options.new_gradle)
Mads Ager418d1ca2017-05-22 09:35:49 +0200144
145if __name__ == '__main__':
146 sys.exit(Main())