blob: 8e1d1bdfd7b778ef45b12fa9aea74e2a2e3c728e [file] [log] [blame]
Rico Wind62d03202018-11-30 13:43:49 +01001# Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file
2# for details. All rights reserved. Use of this source code is governed by a
3# BSD-style license that can be found in the LICENSE file.
4
Ian Zernydaac9c52020-03-03 10:57:17 +01005from os import path
Morten Krogh-Jespersenf9977862021-09-14 12:36:28 +02006import datetime
Christoffer Quist Adamsen4411f072020-03-05 13:28:31 +01007from subprocess import check_output, Popen, PIPE, STDOUT
Morten Krogh-Jespersen145c0ea2023-06-27 13:43:49 +02008import inspect
Rico Wind141383f2023-07-03 13:25:50 +02009import os
10import sys
11# Add both current path to allow us to package import utils and the tools
12# dir to allow transitive (for utils) dependendies to be loaded.
Morten Krogh-Jespersen145c0ea2023-06-27 13:43:49 +020013sys.path.append(path.dirname(inspect.getfile(lambda: None)))
Rico Wind141383f2023-07-03 13:25:50 +020014sys.path.append(os.path.join(
15 path.dirname(inspect.getfile(lambda: None)), 'tools'))
Morten Krogh-Jespersen145c0ea2023-06-27 13:43:49 +020016from tools.utils import EnsureDepFromGoogleCloudStorage
Søren Gjesseb5ae53d2025-02-19 09:31:52 +010017from tools.jdk import GetJavaExecutable
18
19
20KOTLIN_FMT_JAR = path.join(
21 'third_party',
22 'google',
23 'google-kotlin-format',
24 '0.54',
25 'ktfmt-0.54-jar-with-dependencies.jar')
26
27KOTLIN_FMT_SHA1 = path.join(
28 'third_party', 'google', 'google-kotlin-format', '0.54.tar.gz.sha1')
29KOTLIN_FMT_TGZ = path.join(
30 'third_party', 'google', 'google-kotlin-format', '0.54.tar.gz.sha1')
Søren Gjesseae696a12025-02-19 11:48:28 +010031KOTLIN_FMT_IGNORE = {
32 'src/test/java/com/android/tools/r8/kotlin/metadata/inline_class_fun_descriptor_classes_app/main.kt'
33}
Søren Gjesse853e8632025-02-19 12:37:50 +010034KOTLIN_FMT_BATCH_SIZE = 100
Ian Zernydaac9c52020-03-03 10:57:17 +010035
36FMT_CMD = path.join(
37 'third_party',
Christoffer Quist Adamsenbfe52fd2022-02-15 14:32:52 +010038 'google',
Ian Zernydaac9c52020-03-03 10:57:17 +010039 'google-java-format',
Søren Gjesseb557f922024-11-22 09:31:01 +010040 '1.24.0',
41 'google-java-format-1.24.0',
Ian Zernydaac9c52020-03-03 10:57:17 +010042 'scripts',
43 'google-java-format-diff.py')
44
Søren Gjesseffc06192022-06-03 11:11:27 +020045FMT_CMD_JDK17 = path.join('tools','google-java-format-diff.py')
Morten Krogh-Jespersen145c0ea2023-06-27 13:43:49 +020046FMT_SHA1 = path.join(
Søren Gjesseb557f922024-11-22 09:31:01 +010047 'third_party', 'google', 'google-java-format', '1.24.0.tar.gz.sha1')
Morten Krogh-Jespersen145c0ea2023-06-27 13:43:49 +020048FMT_TGZ = path.join(
Søren Gjesseb557f922024-11-22 09:31:01 +010049 'third_party', 'google', 'google-java-format', '1.24.0.tar.gz')
Søren Gjesseffc06192022-06-03 11:11:27 +020050
Rico Wind62d03202018-11-30 13:43:49 +010051def CheckDoNotMerge(input_api, output_api):
52 for l in input_api.change.FullDescriptionText().splitlines():
53 if l.lower().startswith('do not merge'):
54 msg = 'Your cl contains: \'Do not merge\' - this will break WIP bots'
55 return [output_api.PresubmitPromptWarning(msg, [])]
56 return []
57
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +020058def CheckFormatting(input_api, output_api, branch):
Søren Gjesse853e8632025-02-19 12:37:50 +010059 seen_kotlin_error = False
60 seen_java_error = False
61 pending_kotlin_files = []
Søren Gjesseb5ae53d2025-02-19 09:31:52 +010062 EnsureDepFromGoogleCloudStorage(
63 KOTLIN_FMT_JAR, KOTLIN_FMT_TGZ, KOTLIN_FMT_SHA1, 'google-kotlin-format')
64 EnsureDepFromGoogleCloudStorage(
65 FMT_CMD, FMT_TGZ, FMT_SHA1, 'google-java-format')
Rico Wind62d03202018-11-30 13:43:49 +010066 results = []
Ian Zernydaac9c52020-03-03 10:57:17 +010067 for f in input_api.AffectedFiles():
68 path = f.LocalPath()
Søren Gjesseb5ae53d2025-02-19 09:31:52 +010069 if not path.endswith('.java') and not path.endswith('.kt'):
Ian Zernydaac9c52020-03-03 10:57:17 +010070 continue
Søren Gjessee86a4a32025-02-19 12:41:34 +010071 if path.endswith('.kt'):
72 if path in KOTLIN_FMT_IGNORE:
73 continue
Søren Gjesse853e8632025-02-19 12:37:50 +010074 pending_kotlin_files.append(path)
75 if len(pending_kotlin_files) == KOTLIN_FMT_BATCH_SIZE:
Rico Windf387fb22025-02-20 09:01:53 +010076 seen_kotlin_error = (CheckKotlinFormatting(pending_kotlin_files, output_api, results)
77 or seen_kotlin_error)
Søren Gjesse853e8632025-02-19 12:37:50 +010078 pending_kotlin_files = []
Søren Gjessee86a4a32025-02-19 12:41:34 +010079 else:
Rico Windf387fb22025-02-20 09:01:53 +010080 seen_java_error = (CheckJavaFormatting(path, branch, output_api, results)
81 or seen_java_error)
Søren Gjessee86a4a32025-02-19 12:41:34 +010082 # Check remaining Kotlin files if any.
Søren Gjesse853e8632025-02-19 12:37:50 +010083 if len(pending_kotlin_files) > 0:
Rico Windf387fb22025-02-20 09:01:53 +010084 seen_kotlin_error = (CheckKotlinFormatting(pending_kotlin_files, output_api, results)
85 or seen_kotlin_error)
Søren Gjessee86a4a32025-02-19 12:41:34 +010086 # Provide the reformatting commands if needed.
87 if seen_kotlin_error:
88 results.append(output_api.PresubmitError(KotlinFormatPresubmitMessage()))
89 if seen_java_error:
90 results.append(output_api.PresubmitError(JavaFormatPresubMessage()))
91
Søren Gjesse853e8632025-02-19 12:37:50 +010092 # Comment this out to easily presumbit changes
93 # results.append(output_api.PresubmitError("TESTING"))
94 return results
95
96
97def CheckKotlinFormatting(paths, output_api, results):
98 cmd = [GetJavaExecutable(), '-jar', KOTLIN_FMT_JAR, '--google-style', '-n']
99 cmd.extend(paths)
100 result = check_output(cmd)
101 if len(result) > 0:
Søren Gjessee86a4a32025-02-19 12:41:34 +0100102 with_format_error = result.splitlines()
103 for path in with_format_error:
104 results.append(
105 output_api.PresubmitError(
106 "File {path} needs formatting".format(path=path.decode('utf-8'))))
Søren Gjesse853e8632025-02-19 12:37:50 +0100107 return len(result) > 0
108
109
110def KotlinFormatPresubmitMessage():
111 return """Please fix the Kotlin formatting by running:
Ian Zerny67f49452023-05-25 13:12:45 +0200112
Søren Gjesseb5ae53d2025-02-19 09:31:52 +0100113 git diff $(git cl upstream) --name-only "*.kt" | xargs {java} -jar {fmt_jar} --google-style
114
115or fix formatting, commit and upload:
116
117 git diff $(git cl upstream) --name-only "*.kt" | xargs {java} -jar {fmt_jar} --google-style && git commit -a --amend --no-edit && git cl upload
118
119or bypass the checks with:
120
121 git cl upload --bypass-hooks
Søren Gjesse853e8632025-02-19 12:37:50 +0100122 """.format(java=GetJavaExecutable(), fmt_jar=KOTLIN_FMT_JAR)
Søren Gjesseb5ae53d2025-02-19 09:31:52 +0100123
Søren Gjesse853e8632025-02-19 12:37:50 +0100124
125def CheckJavaFormatting(path, branch, output_api, results):
126 diff = check_output(
127 ['git', 'diff', '--no-prefix', '-U0', branch, '--', path])
128
129 proc = Popen(FMT_CMD, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
130 (stdout, stderr) = proc.communicate(input=diff)
131 if len(stdout) > 0:
132 results.append(output_api.PresubmitError(stdout.decode('utf-8')))
Søren Gjesse853e8632025-02-19 12:37:50 +0100133 return len(stdout) > 0
134
135
136def JavaFormatPresubMessage():
137 return """Please fix the Java formatting by running:
Ian Zernydaac9c52020-03-03 10:57:17 +0100138
139 git diff -U0 $(git cl upstream) | %s -p1 -i
140
Søren Gjesse641b9ab2020-10-08 13:28:37 +0200141or fix formatting, commit and upload:
142
143 git diff -U0 $(git cl upstream) | %s -p1 -i && git commit -a --amend --no-edit && git cl upload
144
Ian Zernydaac9c52020-03-03 10:57:17 +0100145or bypass the checks with:
146
Søren Gjesse641b9ab2020-10-08 13:28:37 +0200147 git cl upload --bypass-hooks
Søren Gjesseffc06192022-06-03 11:11:27 +0200148
149If formatting fails with 'No enum constant javax.lang.model.element.Modifier.SEALED' try
150
151 git diff -U0 $(git cl upstream) | %s %s %s -p1 -i && git commit -a --amend --no-edit && git cl upload
152 """ % (
153 FMT_CMD,
154 FMT_CMD,
155 FMT_CMD_JDK17,
156 '--google-java-format-jar',
Søren Gjesse853e8632025-02-19 12:37:50 +0100157 'third_party/google/google-java-format/1.24.0/google-java-format-1.24.0-all-deps.jar')
158
Ian Zernydaac9c52020-03-03 10:57:17 +0100159
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +0200160def CheckDeterministicDebuggingChanged(input_api, output_api, branch):
Morten Krogh-Jespersen30d1f1b2020-03-26 11:39:19 +0100161 for f in input_api.AffectedFiles():
162 path = f.LocalPath()
163 if not path.endswith('InternalOptions.java'):
164 continue
Morten Krogh-Jespersen30d1f1b2020-03-26 11:39:19 +0100165 diff = check_output(
Ian Zerny67f49452023-05-25 13:12:45 +0200166 ['git', 'diff', '--no-prefix', '-U0', branch, '--', path]).decode('utf-8')
Morten Krogh-Jespersen30d1f1b2020-03-26 11:39:19 +0100167 if 'DETERMINISTIC_DEBUGGING' in diff:
168 return [output_api.PresubmitError(diff)]
169 return []
170
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200171def CheckForAddedDisassemble(input_api, output_api):
Ian Zernydaac9c52020-03-03 10:57:17 +0100172 results = []
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200173 for (file, line_nr, line) in input_api.RightHandSideLines():
Ian Zernya84bc1b2020-08-11 09:47:00 +0200174 if file.LocalPath().endswith('.java') and '.disassemble()' in line:
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200175 results.append(
176 output_api.PresubmitError(
Ian Zernya84bc1b2020-08-11 09:47:00 +0200177 'Test call to disassemble\n%s:%s %s' % (file.LocalPath(), line_nr, line)))
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +0200178 return results
179
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200180def CheckForCopyRight(input_api, output_api, branch):
181 results = []
182 for f in input_api.AffectedSourceFiles(None):
183 # Check if it is a new file.
184 if f.OldContents():
185 continue
186 contents = f.NewContents()
187 if (not contents) or (len(contents) == 0):
188 continue
Christoffer Quist Adamsenc1e18d62020-04-15 08:45:01 +0200189 if not CopyRightInContents(f, contents):
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200190 results.append(
Morten Krogh-Jespersenf9977862021-09-14 12:36:28 +0200191 output_api.PresubmitError('Could not find correctly formatted '
192 'copyright in file: %s' % f))
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200193 return results
194
Christoffer Quist Adamsenc1e18d62020-04-15 08:45:01 +0200195def CopyRightInContents(f, contents):
Søren Gjesse09c3dd02021-06-17 08:21:51 +0200196 expected = '//'
197 if f.LocalPath().endswith('.py') or f.LocalPath().endswith('.sh'):
198 expected = '#'
Morten Krogh-Jespersenf9977862021-09-14 12:36:28 +0200199 expected = expected + ' Copyright (c) ' + str(datetime.datetime.now().year)
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200200 for content_line in contents:
Christoffer Quist Adamsenc1e18d62020-04-15 08:45:01 +0200201 if expected in content_line:
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200202 return True
203 return False
204
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +0200205def CheckChange(input_api, output_api):
206 branch = (
207 check_output(['git', 'cl', 'upstream'])
Ian Zerny4eff3b62023-05-15 17:09:03 +0200208 .decode('utf-8')
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +0200209 .strip()
210 .replace('refs/heads/', ''))
211 results = []
Rico Wind62d03202018-11-30 13:43:49 +0100212 results.extend(CheckDoNotMerge(input_api, output_api))
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +0200213 results.extend(CheckFormatting(input_api, output_api, branch))
214 results.extend(
215 CheckDeterministicDebuggingChanged(input_api, output_api, branch))
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200216 results.extend(CheckForAddedDisassemble(input_api, output_api))
217 results.extend(CheckForCopyRight(input_api, output_api, branch))
Rico Wind62d03202018-11-30 13:43:49 +0100218 return results
Ian Zernydaac9c52020-03-03 10:57:17 +0100219
220def CheckChangeOnCommit(input_api, output_api):
221 return CheckChange(input_api, output_api)
222
223def CheckChangeOnUpload(input_api, output_api):
224 return CheckChange(input_api, output_api)