blob: b2d38a6588dfb6c0ef2cf88d43bfac7f23f013fe [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)
Primiano Tuccic5010802018-01-19 17:13:21 +000033 results += CheckMergedTraceConfigProto(input, output)
Florian Mayer640b7ae2018-05-09 15:28:32 +010034 results += CheckWhitelist(input, output)
Hector Dearman534765e2017-11-01 11:17:38 +000035 return results
36
Sami Kyostilab27619f2017-12-13 19:22:16 +000037
Hector Dearman534765e2017-11-01 11:17:38 +000038def CheckChangeOnUpload(input_api, output_api):
39 return CheckChange(input_api, output_api)
40
Sami Kyostilab27619f2017-12-13 19:22:16 +000041
Hector Dearman534765e2017-11-01 11:17:38 +000042def CheckChangeOnCommit(input_api, output_api):
43 return CheckChange(input_api, output_api)
44
Sami Kyostilab27619f2017-12-13 19:22:16 +000045
46def CheckAndroidBlueprint(input_api, output_api):
47 # If no GN files were modified, bail out.
48 build_file_filter = lambda x: input_api.FilterSourceFile(
49 x,
50 white_list=('.*BUILD[.]gn$', '.*[.]gni$', 'tools/gen_android_bp'))
51 if not input_api.AffectedSourceFiles(build_file_filter):
52 return []
53
54 with open('Android.bp') as f:
55 current_blueprint = f.read()
Primiano Tucci40e98722018-02-16 11:50:17 +000056
Sami Kyostilab27619f2017-12-13 19:22:16 +000057 new_blueprint = subprocess.check_output(
58 ['tools/gen_android_bp', '--output', '/dev/stdout'])
59
60 if current_blueprint != new_blueprint:
61 return [
62 output_api.PresubmitError(
63 'Android.bp is out of date. Please run tools/gen_android_bp '
64 'to update it.')
65 ]
66 return []
Primiano Tuccic5010802018-01-19 17:13:21 +000067
68
Primiano Tucci40e98722018-02-16 11:50:17 +000069def CheckIncludeGuards(input_api, output_api):
70 tool = 'tools/fix_include_guards'
71 file_filter = lambda x: input_api.FilterSourceFile(
72 x,
73 white_list=('.*[.]cc$', '.*[.]h$', tool))
74 if not input_api.AffectedSourceFiles(file_filter):
75 return []
76 if subprocess.call([tool, '--check-only']):
77 return [
78 output_api.PresubmitError(
79 'Please run ' + tool + ' to fix include guards.')
80 ]
81 return []
82
83
Primiano Tuccic5010802018-01-19 17:13:21 +000084def CheckMergedTraceConfigProto(input_api, output_api):
Hector Dearman55ef3e02018-04-11 17:28:55 +010085 tool = 'tools/gen_merged_protos'
Primiano Tuccic5010802018-01-19 17:13:21 +000086 build_file_filter = lambda x: input_api.FilterSourceFile(
87 x,
Hector Dearman55ef3e02018-04-11 17:28:55 +010088 white_list=('protos/perfetto/.*[.]proto$', tool))
Primiano Tuccic5010802018-01-19 17:13:21 +000089 if not input_api.AffectedSourceFiles(build_file_filter):
90 return []
91 if subprocess.call([tool, '--check-only']):
92 return [
93 output_api.PresubmitError(
Hector Dearman55ef3e02018-04-11 17:28:55 +010094 'perfetto_config.proto or perfetto_trace.proto is out of ' +
95 'date. Please run ' + tool + ' to update it.')
Primiano Tuccic5010802018-01-19 17:13:21 +000096 ]
97 return []
Florian Mayer640b7ae2018-05-09 15:28:32 +010098
99
100# Prevent removing or changing lines in event_whitelist.
101def CheckWhitelist(input_api, output_api):
102 for f in input_api.AffectedFiles():
103 if f.LocalPath() != 'tools/ftrace_proto_gen/event_whitelist':
104 continue
105 if any(new_line != 'removed' and new_line != old_line for old_line, new_line
106 in itertools.izip(f.OldContents(), f.NewContents())):
107 return [
108 output_api.PresubmitError(
109 'event_whitelist only has two supported changes: '
110 'appending a new line, and replacing a line with removed.'
111 )
112 ]
113 return []