1 # Copyright (c) 2012 The Native Client Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 # Documentation on PRESUBMIT.py can be found at:
6 # http://www.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 from __future__ import print_function
14 # List of directories to not apply presubmit project checks, relative
15 # to the NaCl top directory
16 EXCLUDE_PROJECT_CHECKS = [
17 # The following contain test data (including automatically generated),
18 # and do not follow our conventions.
19 'src/trusted/validator_ragel/testdata/32/',
20 'src/trusted/validator_ragel/testdata/64/',
21 'src/trusted/validator_x86/testdata/32/',
22 'src/trusted/validator_x86/testdata/64/',
23 'src/trusted/validator/x86/decoder/generator/testdata/32/',
24 'src/trusted/validator/x86/decoder/generator/testdata/64/',
25 # The following directories contains automatically generated source,
26 # which may not follow our conventions.
27 'src/trusted/validator_x86/gen/',
28 'src/trusted/validator/x86/decoder/gen/',
29 'src/trusted/validator/x86/decoder/generator/gen/',
30 'src/trusted/validator/x86/ncval_seg_sfi/gen/',
31 'src/trusted/validator_arm/gen/',
32 'src/trusted/validator_ragel/gen/',
33 # The following files contain code from outside native_client (e.g. newlib)
34 # or are known the contain style violations.
35 'src/trusted/service_runtime/include/sys/',
36 'src/include/win/port_win.h',
37 'src/trusted/service_runtime/include/machine/_types.h'
40 NACL_TOP_DIR = os.getcwd()
41 while not os.path.isfile(os.path.join(NACL_TOP_DIR, 'PRESUBMIT.py')):
42 NACL_TOP_DIR = os.path.dirname(NACL_TOP_DIR)
43 assert len(NACL_TOP_DIR) >= 3, "Could not find NaClTopDir"
47 p = subprocess.Popen("git branch -vv", shell=True,
48 stdout=subprocess.PIPE)
49 output, _ = p.communicate()
50 lines = output.split('\n')
52 # output format for checked-out branch should be
53 # * branchname hash [TrackedBranchName ...
55 if '*' not in toks[0]:
57 if not ('origin/master' in toks[3] or
58 'origin/refs/heads/master' in toks[3]):
59 warning = 'Warning: your current branch:\n' + line
60 warning += '\nis not tracking origin/master. git cl push may silently '
61 warning += 'fail to push your change. To fix this, do\n'
62 warning += 'git branch -u origin/master'
65 print('Warning: presubmit check could not determine local git branch')
69 def _CommonChecks(input_api, output_api):
70 """Checks for both upload and commit."""
73 results.extend(input_api.canned_checks.PanProjectChecks(
74 input_api, output_api, project_name='Native Client',
75 excluded_paths=tuple(EXCLUDE_PROJECT_CHECKS)))
77 # The commit queue assumes PRESUBMIT.py is standalone.
78 # TODO(bradnelson): Migrate code_hygiene to a common location so that
79 # it can be used by the commit queue.
80 old_sys_path = list(sys.path)
82 sys.path.append(os.path.join(NACL_TOP_DIR, 'tools'))
83 sys.path.append(os.path.join(NACL_TOP_DIR, 'build'))
86 sys.path = old_sys_path
89 affected_files = input_api.AffectedFiles(include_deletes=False)
90 exclude_dirs = [ NACL_TOP_DIR + '/' + x for x in EXCLUDE_PROJECT_CHECKS ]
91 for filename in affected_files:
92 filename = filename.AbsoluteLocalPath()
93 if filename in exclude_dirs:
95 if not IsFileInDirectories(filename, exclude_dirs):
96 errors, warnings = code_hygiene.CheckFile(filename, False)
98 results.append(output_api.PresubmitError(e, items=errors[e]))
100 results.append(output_api.PresubmitPromptWarning(w, items=warnings[w]))
105 def IsFileInDirectories(f, dirs):
106 """ Returns true if f is in list of directories"""
108 if d is os.path.commonprefix([f, d]):
113 def CheckChangeOnUpload(input_api, output_api):
114 """Verifies all changes in all files.
116 input_api: the limited set of input modules allowed in presubmit.
117 output_api: the limited set of output modules allowed in presubmit.
120 report.extend(_CommonChecks(input_api, output_api))
122 branch_warning = CheckGitBranch()
124 report.append(output_api.PresubmitPromptWarning(branch_warning))
129 def CheckChangeOnCommit(input_api, output_api):
130 """Verifies all changes in all files and verifies that the
131 tree is open and can accept a commit.
133 input_api: the limited set of input modules allowed in presubmit.
134 output_api: the limited set of output modules allowed in presubmit.
137 report.extend(_CommonChecks(input_api, output_api))
138 report.extend(input_api.canned_checks.CheckTreeIsOpen(
139 input_api, output_api,
140 json_url='http://nativeclient-status.appspot.com/current?format=json'))
144 # Note that this list is duplicated in the Commit Queue. If you change
145 # this list, you should also update the CQ's list in infra/config/cq.cfg
146 # (see https://crbug.com/399059).
148 'nacl-precise32_newlib_dbg',
149 'nacl-precise32_newlib_opt',
150 'nacl-precise32_glibc_opt',
151 'nacl-precise64_newlib_dbg',
152 'nacl-precise64_newlib_opt',
153 'nacl-precise64_glibc_opt',
154 'nacl-mac_newlib_dbg',
155 'nacl-mac_newlib_opt',
156 'nacl-mac_glibc_dbg',
157 'nacl-mac_glibc_opt',
158 'nacl-win32_newlib_opt',
159 'nacl-win32_glibc_opt',
160 'nacl-win64_newlib_dbg',
161 'nacl-win64_newlib_opt',
162 'nacl-win64_glibc_opt',
163 'nacl-win8-64_newlib_dbg',
164 'nacl-win8-64_newlib_opt',
165 'nacl-arm_opt_panda',
167 'nacl-win7_64_arm_newlib_opt',
168 'nacl-mac_arm_newlib_opt',
169 'nacl-precise64_arm_newlib_opt',
170 'nacl-precise64_arm_glibc_opt',
172 'nacl-precise_64-newlib-arm_qemu-pnacl',
173 'nacl-precise_64-newlib-x86_32-pnacl',
174 'nacl-precise_64-newlib-x86_64-pnacl',
175 'nacl-mac_newlib_opt_pnacl',
176 'nacl-win7_64_newlib_opt_pnacl',
178 'nacl-arm_perf_panda',
179 'nacl-precise_64-newlib-x86_32-pnacl-spec',
180 'nacl-precise_64-newlib-x86_64-pnacl-spec',
183 PNACL_TOOLCHAIN_TRYBOTS = [
184 'nacl-toolchain-linux-pnacl-x86_64',
185 'nacl-toolchain-linux-pnacl-x86_32',
186 'nacl-toolchain-mac-pnacl-x86_32',
187 'nacl-toolchain-win7-pnacl-x86_64',
190 TOOLCHAIN_BUILD_TRYBOTS = [
191 'nacl-toolchain-precise64-newlib-arm',
192 'nacl-toolchain-mac-newlib-arm',
193 ] + PNACL_TOOLCHAIN_TRYBOTS