blob: c5c1e8853a2570fb7b54d47315fda3efb6ce0da4 [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):
Søren Gjesseb2f03c72025-03-19 15:10:23 +010098 paths_to_format = {
99 '--kotlinlang-style': [path for path in paths if path.startswith('src/keepanno/')],
100 '--google-style': [path for path in paths if not path.startswith('src/keepanno/')]
101 }
102 needs_formatting_count = 0
103 for format in ['--kotlinlang-style', '--google-style']:
104 cmd = [GetJavaExecutable(), '-jar', KOTLIN_FMT_JAR, format, '-n']
105 to_format = paths_to_format[format]
106 if len(to_format) > 0:
107 cmd.extend(to_format)
108 result = check_output(cmd)
109 if len(result) > 0:
110 with_format_error = result.splitlines()
111 for path in with_format_error:
112 results.append(
113 output_api.PresubmitError(
114 "File {path} needs formatting".format(path=path.decode('utf-8'))))
115 needs_formatting_count += len(result)
116 return needs_formatting_count > 0
Søren Gjesse853e8632025-02-19 12:37:50 +0100117
118
119def KotlinFormatPresubmitMessage():
120 return """Please fix the Kotlin formatting by running:
Ian Zerny67f49452023-05-25 13:12:45 +0200121
Søren Gjesseb2f03c72025-03-19 15:10:23 +0100122 git diff $(git cl upstream) --name-only "*.kt" | grep -v "^src/keepanno/" | xargs {java} -jar {fmt_jar} --google-style
123 git diff $(git cl upstream) --name-only "*.kt" | grep "^src/keepanno/" | xargs {java} -jar {fmt_jar} --kotlinlang-style
Søren Gjesseb5ae53d2025-02-19 09:31:52 +0100124
125or fix formatting, commit and upload:
126
Søren Gjesseb2f03c72025-03-19 15:10:23 +0100127 git diff $(git cl upstream) --name-only "*.kt" | grep -v "^src/keepanno/" | xargs {java} -jar {fmt_jar} --google-style && git commit -a --amend --no-edit && git cl upload
128 git diff $(git cl upstream) --name-only "*.kt" | grep "^src/keepanno/" | xargs {java} -jar {fmt_jar} --kotlinlang-style && git commit -a --amend --no-edit && git cl upload
Søren Gjesseb5ae53d2025-02-19 09:31:52 +0100129
130or bypass the checks with:
131
132 git cl upload --bypass-hooks
Søren Gjesse853e8632025-02-19 12:37:50 +0100133 """.format(java=GetJavaExecutable(), fmt_jar=KOTLIN_FMT_JAR)
Søren Gjesseb5ae53d2025-02-19 09:31:52 +0100134
Søren Gjesse853e8632025-02-19 12:37:50 +0100135
136def CheckJavaFormatting(path, branch, output_api, results):
137 diff = check_output(
138 ['git', 'diff', '--no-prefix', '-U0', branch, '--', path])
139
140 proc = Popen(FMT_CMD, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
141 (stdout, stderr) = proc.communicate(input=diff)
142 if len(stdout) > 0:
143 results.append(output_api.PresubmitError(stdout.decode('utf-8')))
Søren Gjesse853e8632025-02-19 12:37:50 +0100144 return len(stdout) > 0
145
146
147def JavaFormatPresubMessage():
148 return """Please fix the Java formatting by running:
Ian Zernydaac9c52020-03-03 10:57:17 +0100149
150 git diff -U0 $(git cl upstream) | %s -p1 -i
151
Søren Gjesse641b9ab2020-10-08 13:28:37 +0200152or fix formatting, commit and upload:
153
154 git diff -U0 $(git cl upstream) | %s -p1 -i && git commit -a --amend --no-edit && git cl upload
155
Ian Zernydaac9c52020-03-03 10:57:17 +0100156or bypass the checks with:
157
Søren Gjesse641b9ab2020-10-08 13:28:37 +0200158 git cl upload --bypass-hooks
Søren Gjesseffc06192022-06-03 11:11:27 +0200159
160If formatting fails with 'No enum constant javax.lang.model.element.Modifier.SEALED' try
161
162 git diff -U0 $(git cl upstream) | %s %s %s -p1 -i && git commit -a --amend --no-edit && git cl upload
163 """ % (
164 FMT_CMD,
165 FMT_CMD,
166 FMT_CMD_JDK17,
167 '--google-java-format-jar',
Søren Gjesse853e8632025-02-19 12:37:50 +0100168 'third_party/google/google-java-format/1.24.0/google-java-format-1.24.0-all-deps.jar')
169
Ian Zernydaac9c52020-03-03 10:57:17 +0100170
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +0200171def CheckDeterministicDebuggingChanged(input_api, output_api, branch):
Morten Krogh-Jespersen30d1f1b2020-03-26 11:39:19 +0100172 for f in input_api.AffectedFiles():
173 path = f.LocalPath()
174 if not path.endswith('InternalOptions.java'):
175 continue
Morten Krogh-Jespersen30d1f1b2020-03-26 11:39:19 +0100176 diff = check_output(
Ian Zerny67f49452023-05-25 13:12:45 +0200177 ['git', 'diff', '--no-prefix', '-U0', branch, '--', path]).decode('utf-8')
Morten Krogh-Jespersen30d1f1b2020-03-26 11:39:19 +0100178 if 'DETERMINISTIC_DEBUGGING' in diff:
179 return [output_api.PresubmitError(diff)]
180 return []
181
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200182def CheckForAddedDisassemble(input_api, output_api):
Ian Zernydaac9c52020-03-03 10:57:17 +0100183 results = []
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200184 for (file, line_nr, line) in input_api.RightHandSideLines():
Ian Zernya84bc1b2020-08-11 09:47:00 +0200185 if file.LocalPath().endswith('.java') and '.disassemble()' in line:
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200186 results.append(
187 output_api.PresubmitError(
Ian Zernya84bc1b2020-08-11 09:47:00 +0200188 'Test call to disassemble\n%s:%s %s' % (file.LocalPath(), line_nr, line)))
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +0200189 return results
190
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200191def CheckForCopyRight(input_api, output_api, branch):
192 results = []
193 for f in input_api.AffectedSourceFiles(None):
194 # Check if it is a new file.
195 if f.OldContents():
196 continue
197 contents = f.NewContents()
198 if (not contents) or (len(contents) == 0):
199 continue
Christoffer Quist Adamsenc1e18d62020-04-15 08:45:01 +0200200 if not CopyRightInContents(f, contents):
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200201 results.append(
Morten Krogh-Jespersenf9977862021-09-14 12:36:28 +0200202 output_api.PresubmitError('Could not find correctly formatted '
203 'copyright in file: %s' % f))
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200204 return results
205
Christoffer Quist Adamsenc1e18d62020-04-15 08:45:01 +0200206def CopyRightInContents(f, contents):
Søren Gjesse09c3dd02021-06-17 08:21:51 +0200207 expected = '//'
208 if f.LocalPath().endswith('.py') or f.LocalPath().endswith('.sh'):
209 expected = '#'
Morten Krogh-Jespersenf9977862021-09-14 12:36:28 +0200210 expected = expected + ' Copyright (c) ' + str(datetime.datetime.now().year)
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200211 for content_line in contents:
Christoffer Quist Adamsenc1e18d62020-04-15 08:45:01 +0200212 if expected in content_line:
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200213 return True
214 return False
215
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +0200216def CheckChange(input_api, output_api):
217 branch = (
218 check_output(['git', 'cl', 'upstream'])
Ian Zerny4eff3b62023-05-15 17:09:03 +0200219 .decode('utf-8')
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +0200220 .strip()
221 .replace('refs/heads/', ''))
222 results = []
Rico Wind62d03202018-11-30 13:43:49 +0100223 results.extend(CheckDoNotMerge(input_api, output_api))
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +0200224 results.extend(CheckFormatting(input_api, output_api, branch))
225 results.extend(
226 CheckDeterministicDebuggingChanged(input_api, output_api, branch))
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200227 results.extend(CheckForAddedDisassemble(input_api, output_api))
228 results.extend(CheckForCopyRight(input_api, output_api, branch))
Rico Wind62d03202018-11-30 13:43:49 +0100229 return results
Ian Zernydaac9c52020-03-03 10:57:17 +0100230
231def CheckChangeOnCommit(input_api, output_api):
232 return CheckChange(input_api, output_api)
233
234def CheckChangeOnUpload(input_api, output_api):
235 return CheckChange(input_api, output_api)