blob: fa136fc9ce8845133a101f9d97be830af7e368d7 [file] [log] [blame]
Hector Dearman534765e2017-11-01 11:17:38 +00001# Copyright (C) 2017 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Florian Mayer640b7ae2018-05-09 15:28:32 +010015import itertools
Sami Kyostilab27619f2017-12-13 19:22:16 +000016import subprocess
17
Hector Dearman534765e2017-11-01 11:17:38 +000018def CheckChange(input, output):
Sami Kyostilab27619f2017-12-13 19:22:16 +000019 # There apparently is no way to wrap strings in blueprints, so ignore long
20 # lines in them.
21 long_line_sources = lambda x: input.FilterSourceFile(
Primiano Tucci2c761ef2019-01-07 20:20:46 +000022 x, white_list=".*",
Primiano Tuccif7793ef2019-01-10 21:32:45 +000023 black_list=['Android[.]bp', '.*[.]json$', '.*[.]sql$', '.*[.]out$',
Lalit Maganti279ecde2019-04-01 16:57:12 +010024 'test/trace_processor/index$', 'BUILD$', 'protos/BUILD$'])
Hector Dearman534765e2017-11-01 11:17:38 +000025 results = []
26 results += input.canned_checks.CheckDoNotSubmit(input, output)
27 results += input.canned_checks.CheckChangeHasNoTabs(input, output)
Sami Kyostilab27619f2017-12-13 19:22:16 +000028 results += input.canned_checks.CheckLongLines(
29 input, output, 80, source_file_filter=long_line_sources)
Deepanjan Roya752b462018-07-04 01:24:31 +010030 results += input.canned_checks.CheckPatchFormatted(
31 input, output, check_js=True)
Hector Dearman534765e2017-11-01 11:17:38 +000032 results += input.canned_checks.CheckGNFormatted(input, output)
Primiano Tucci40e98722018-02-16 11:50:17 +000033 results += CheckIncludeGuards(input, output)
Lalit Maganti279ecde2019-04-01 16:57:12 +010034 results += CheckBuild(input, output)
Sami Kyostilab27619f2017-12-13 19:22:16 +000035 results += CheckAndroidBlueprint(input, output)
Hector Dearman1e269142018-11-14 13:53:08 +000036 results += CheckBinaryDescriptors(input, output)
Primiano Tuccic5010802018-01-19 17:13:21 +000037 results += CheckMergedTraceConfigProto(input, output)
Florian Mayer640b7ae2018-05-09 15:28:32 +010038 results += CheckWhitelist(input, output)
Hector Dearman534765e2017-11-01 11:17:38 +000039 return results
40
Sami Kyostilab27619f2017-12-13 19:22:16 +000041
Hector Dearman534765e2017-11-01 11:17:38 +000042def CheckChangeOnUpload(input_api, output_api):
43 return CheckChange(input_api, output_api)
44
Sami Kyostilab27619f2017-12-13 19:22:16 +000045
Hector Dearman534765e2017-11-01 11:17:38 +000046def CheckChangeOnCommit(input_api, output_api):
47 return CheckChange(input_api, output_api)
48
Sami Kyostilab27619f2017-12-13 19:22:16 +000049
Lalit Maganti279ecde2019-04-01 16:57:12 +010050def CheckBuild(input_api, output_api):
51 # If no GN files were modified, bail out.
52 def build_file_filter(x): return input_api.FilterSourceFile(
53 x,
Sami Kyostila3c88a1d2019-05-22 18:29:42 +010054 white_list=('.*BUILD[.]gn$', '.*[.]gni$', 'tools/gen_bazel'))
Lalit Maganti279ecde2019-04-01 16:57:12 +010055 if not input_api.AffectedSourceFiles(build_file_filter):
56 return []
57
58 with open('BUILD') as f:
59 current_build = f.read()
60
61 new_build = subprocess.check_output(
Sami Kyostila3c88a1d2019-05-22 18:29:42 +010062 ['tools/gen_bazel', '--output', '/dev/stdout', '--output-proto', '/dev/null'])
Lalit Maganti279ecde2019-04-01 16:57:12 +010063
64 with open('protos/BUILD') as f:
65 current_proto_build = f.read()
66
67 new_proto_build = subprocess.check_output(
Sami Kyostila3c88a1d2019-05-22 18:29:42 +010068 ['tools/gen_bazel', '--output', '/dev/null', '--output-proto', '/dev/stdout'])
Lalit Maganti279ecde2019-04-01 16:57:12 +010069
70 if current_build != new_build or current_proto_build != new_proto_build:
71 return [
72 output_api.PresubmitError(
Sami Kyostila3c88a1d2019-05-22 18:29:42 +010073 'BUILD and/or protos/BUILD is out of date. Please run tools/gen_bazel '
Lalit Maganti279ecde2019-04-01 16:57:12 +010074 'to update it.')
75 ]
76 return []
77
78
Sami Kyostilab27619f2017-12-13 19:22:16 +000079def CheckAndroidBlueprint(input_api, output_api):
80 # If no GN files were modified, bail out.
81 build_file_filter = lambda x: input_api.FilterSourceFile(
82 x,
83 white_list=('.*BUILD[.]gn$', '.*[.]gni$', 'tools/gen_android_bp'))
84 if not input_api.AffectedSourceFiles(build_file_filter):
85 return []
86
87 with open('Android.bp') as f:
88 current_blueprint = f.read()
Primiano Tucci40e98722018-02-16 11:50:17 +000089
Sami Kyostilab27619f2017-12-13 19:22:16 +000090 new_blueprint = subprocess.check_output(
91 ['tools/gen_android_bp', '--output', '/dev/stdout'])
92
93 if current_blueprint != new_blueprint:
94 return [
95 output_api.PresubmitError(
96 'Android.bp is out of date. Please run tools/gen_android_bp '
97 'to update it.')
98 ]
99 return []
Primiano Tuccic5010802018-01-19 17:13:21 +0000100
101
Primiano Tucci40e98722018-02-16 11:50:17 +0000102def CheckIncludeGuards(input_api, output_api):
103 tool = 'tools/fix_include_guards'
104 file_filter = lambda x: input_api.FilterSourceFile(
105 x,
106 white_list=('.*[.]cc$', '.*[.]h$', tool))
107 if not input_api.AffectedSourceFiles(file_filter):
108 return []
109 if subprocess.call([tool, '--check-only']):
110 return [
111 output_api.PresubmitError(
112 'Please run ' + tool + ' to fix include guards.')
113 ]
114 return []
115
116
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000117def CheckBinaryDescriptors(input_api, output_api):
118 tool = 'tools/gen_binary_descriptors'
119 file_filter = lambda x: input_api.FilterSourceFile(
120 x,
Hector Dearman1e269142018-11-14 13:53:08 +0000121 white_list=('protos/perfetto/.*[.]proto$', '.*[.]h', tool))
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000122 if not input_api.AffectedSourceFiles(file_filter):
123 return []
124 if subprocess.call([tool, '--check-only']):
125 return [
126 output_api.PresubmitError(
127 'Please run ' + tool + ' to update binary descriptors.')
128 ]
129 return []
130
131
Primiano Tuccic5010802018-01-19 17:13:21 +0000132def CheckMergedTraceConfigProto(input_api, output_api):
Hector Dearman55ef3e02018-04-11 17:28:55 +0100133 tool = 'tools/gen_merged_protos'
Primiano Tuccic5010802018-01-19 17:13:21 +0000134 build_file_filter = lambda x: input_api.FilterSourceFile(
135 x,
Hector Dearman55ef3e02018-04-11 17:28:55 +0100136 white_list=('protos/perfetto/.*[.]proto$', tool))
Primiano Tuccic5010802018-01-19 17:13:21 +0000137 if not input_api.AffectedSourceFiles(build_file_filter):
138 return []
139 if subprocess.call([tool, '--check-only']):
140 return [
141 output_api.PresubmitError(
Hector Dearman55ef3e02018-04-11 17:28:55 +0100142 'perfetto_config.proto or perfetto_trace.proto is out of ' +
143 'date. Please run ' + tool + ' to update it.')
Primiano Tuccic5010802018-01-19 17:13:21 +0000144 ]
145 return []
Florian Mayer640b7ae2018-05-09 15:28:32 +0100146
147
148# Prevent removing or changing lines in event_whitelist.
149def CheckWhitelist(input_api, output_api):
150 for f in input_api.AffectedFiles():
151 if f.LocalPath() != 'tools/ftrace_proto_gen/event_whitelist':
152 continue
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000153 if any((not new_line.startswith('removed'))
Isabelle Taylor8d49fa02018-10-26 13:23:00 +0100154 and new_line != old_line for old_line, new_line
Florian Mayer640b7ae2018-05-09 15:28:32 +0100155 in itertools.izip(f.OldContents(), f.NewContents())):
156 return [
157 output_api.PresubmitError(
158 'event_whitelist only has two supported changes: '
159 'appending a new line, and replacing a line with removed.'
160 )
161 ]
162 return []