blob: 3c11c8bf8c8f0ce84f2f1b42b949cda9c4766085 [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 Tucci3faad742018-05-16 19:30:48 +010022 x, white_list=".*", black_list=['Android[.]bp', '.*[.]json$'])
Hector Dearman534765e2017-11-01 11:17:38 +000023 results = []
24 results += input.canned_checks.CheckDoNotSubmit(input, output)
25 results += input.canned_checks.CheckChangeHasNoTabs(input, output)
Sami Kyostilab27619f2017-12-13 19:22:16 +000026 results += input.canned_checks.CheckLongLines(
27 input, output, 80, source_file_filter=long_line_sources)
Deepanjan Roya752b462018-07-04 01:24:31 +010028 results += input.canned_checks.CheckPatchFormatted(
29 input, output, check_js=True)
Hector Dearman534765e2017-11-01 11:17:38 +000030 results += input.canned_checks.CheckGNFormatted(input, output)
Primiano Tucci40e98722018-02-16 11:50:17 +000031 results += CheckIncludeGuards(input, output)
Sami Kyostilab27619f2017-12-13 19:22:16 +000032 results += CheckAndroidBlueprint(input, output)
Hector Dearman1e269142018-11-14 13:53:08 +000033 results += CheckBinaryDescriptors(input, output)
Primiano Tuccic5010802018-01-19 17:13:21 +000034 results += CheckMergedTraceConfigProto(input, output)
Florian Mayer640b7ae2018-05-09 15:28:32 +010035 results += CheckWhitelist(input, output)
Hector Dearman534765e2017-11-01 11:17:38 +000036 return results
37
Sami Kyostilab27619f2017-12-13 19:22:16 +000038
Hector Dearman534765e2017-11-01 11:17:38 +000039def CheckChangeOnUpload(input_api, output_api):
40 return CheckChange(input_api, output_api)
41
Sami Kyostilab27619f2017-12-13 19:22:16 +000042
Hector Dearman534765e2017-11-01 11:17:38 +000043def CheckChangeOnCommit(input_api, output_api):
44 return CheckChange(input_api, output_api)
45
Sami Kyostilab27619f2017-12-13 19:22:16 +000046
47def CheckAndroidBlueprint(input_api, output_api):
48 # If no GN files were modified, bail out.
49 build_file_filter = lambda x: input_api.FilterSourceFile(
50 x,
51 white_list=('.*BUILD[.]gn$', '.*[.]gni$', 'tools/gen_android_bp'))
52 if not input_api.AffectedSourceFiles(build_file_filter):
53 return []
54
55 with open('Android.bp') as f:
56 current_blueprint = f.read()
Primiano Tucci40e98722018-02-16 11:50:17 +000057
Sami Kyostilab27619f2017-12-13 19:22:16 +000058 new_blueprint = subprocess.check_output(
59 ['tools/gen_android_bp', '--output', '/dev/stdout'])
60
61 if current_blueprint != new_blueprint:
62 return [
63 output_api.PresubmitError(
64 'Android.bp is out of date. Please run tools/gen_android_bp '
65 'to update it.')
66 ]
67 return []
Primiano Tuccic5010802018-01-19 17:13:21 +000068
69
Primiano Tucci40e98722018-02-16 11:50:17 +000070def CheckIncludeGuards(input_api, output_api):
71 tool = 'tools/fix_include_guards'
72 file_filter = lambda x: input_api.FilterSourceFile(
73 x,
74 white_list=('.*[.]cc$', '.*[.]h$', tool))
75 if not input_api.AffectedSourceFiles(file_filter):
76 return []
77 if subprocess.call([tool, '--check-only']):
78 return [
79 output_api.PresubmitError(
80 'Please run ' + tool + ' to fix include guards.')
81 ]
82 return []
83
84
Hector Dearmanb7fa5442018-11-08 18:39:32 +000085def CheckBinaryDescriptors(input_api, output_api):
86 tool = 'tools/gen_binary_descriptors'
87 file_filter = lambda x: input_api.FilterSourceFile(
88 x,
Hector Dearman1e269142018-11-14 13:53:08 +000089 white_list=('protos/perfetto/.*[.]proto$', '.*[.]h', tool))
Hector Dearmanb7fa5442018-11-08 18:39:32 +000090 if not input_api.AffectedSourceFiles(file_filter):
91 return []
92 if subprocess.call([tool, '--check-only']):
93 return [
94 output_api.PresubmitError(
95 'Please run ' + tool + ' to update binary descriptors.')
96 ]
97 return []
98
99
Primiano Tuccic5010802018-01-19 17:13:21 +0000100def CheckMergedTraceConfigProto(input_api, output_api):
Hector Dearman55ef3e02018-04-11 17:28:55 +0100101 tool = 'tools/gen_merged_protos'
Primiano Tuccic5010802018-01-19 17:13:21 +0000102 build_file_filter = lambda x: input_api.FilterSourceFile(
103 x,
Hector Dearman55ef3e02018-04-11 17:28:55 +0100104 white_list=('protos/perfetto/.*[.]proto$', tool))
Primiano Tuccic5010802018-01-19 17:13:21 +0000105 if not input_api.AffectedSourceFiles(build_file_filter):
106 return []
107 if subprocess.call([tool, '--check-only']):
108 return [
109 output_api.PresubmitError(
Hector Dearman55ef3e02018-04-11 17:28:55 +0100110 'perfetto_config.proto or perfetto_trace.proto is out of ' +
111 'date. Please run ' + tool + ' to update it.')
Primiano Tuccic5010802018-01-19 17:13:21 +0000112 ]
113 return []
Florian Mayer640b7ae2018-05-09 15:28:32 +0100114
115
116# Prevent removing or changing lines in event_whitelist.
117def CheckWhitelist(input_api, output_api):
118 for f in input_api.AffectedFiles():
119 if f.LocalPath() != 'tools/ftrace_proto_gen/event_whitelist':
120 continue
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000121 if any((not new_line.startswith('removed'))
Isabelle Taylor8d49fa02018-10-26 13:23:00 +0100122 and new_line != old_line for old_line, new_line
Florian Mayer640b7ae2018-05-09 15:28:32 +0100123 in itertools.izip(f.OldContents(), f.NewContents())):
124 return [
125 output_api.PresubmitError(
126 'event_whitelist only has two supported changes: '
127 'appending a new line, and replacing a line with removed.'
128 )
129 ]
130 return []