blob: bcf6bd39a0eabb0077c393ae113048ee3ab7b381 [file] [log] [blame]
Primiano Tucci34bc5592021-02-19 17:53:36 +01001#!/usr/bin/env python3
Hector Dearman7e079772018-11-15 16:08:12 +00002# Copyright (C) 2018 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +010016from __future__ import print_function
Primiano Tuccieac5d712021-05-18 20:45:05 +010017
Hector Dearman7e079772018-11-15 16:08:12 +000018import os
19import argparse
20import subprocess
Primiano Tuccieac5d712021-05-18 20:45:05 +010021import sys
Hector Dearman7e079772018-11-15 16:08:12 +000022
23ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
Primiano Tuccieac5d712021-05-18 20:45:05 +010024IS_WIN = sys.platform.startswith('win')
Hector Dearman7e079772018-11-15 16:08:12 +000025
26
27def protoc_path(out_directory):
Primiano Tuccieac5d712021-05-18 20:45:05 +010028 path = os.path.join(out_directory, 'protoc') + ('.exe' if IS_WIN else '')
Hector Dearman7e079772018-11-15 16:08:12 +000029 assert os.path.isfile(path)
30 return path
31
32
Sami Kyostilaf15f3f62019-08-09 11:00:29 +010033def call(cmd, *args):
Hector Dearman7e079772018-11-15 16:08:12 +000034 path = os.path.join('tools', cmd)
Primiano Tuccieac5d712021-05-18 20:45:05 +010035 command = ['python3', path] + list(args)
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +010036 print('Running', ' '.join(command))
Hector Dearman7e079772018-11-15 16:08:12 +000037 try:
Primiano Tucci2854a0a2019-06-03 14:51:18 +010038 subprocess.check_call(command, cwd=ROOT_DIR)
Hector Dearman7e079772018-11-15 16:08:12 +000039 except subprocess.CalledProcessError as e:
40 assert False, 'Command: {} failed'.format(' '.join(command))
41
42
43def main():
44 parser = argparse.ArgumentParser()
Primiano Tucci561ba7e2019-10-10 21:23:11 +010045 parser.add_argument('--check-only', default=False, action='store_true')
Hector Dearman7e079772018-11-15 16:08:12 +000046 parser.add_argument('OUT')
47 args = parser.parse_args()
48 out = args.OUT
49
50 try:
51 assert os.path.isdir(out), \
52 'Output directory "{}" is not a directory'.format(out)
Primiano Tucci561ba7e2019-10-10 21:23:11 +010053 check_only = ['--check-only'] if args.check_only else []
54 call('check_include_violations')
Anindita Ghosh2aa4e422020-06-26 15:48:32 +010055 call('check_proto_comments')
Primiano Tucci561ba7e2019-10-10 21:23:11 +010056 call('fix_include_guards', *check_only)
Primiano Tuccieac5d712021-05-18 20:45:05 +010057 if not IS_WIN:
58 call('gen_bazel', *check_only)
59 call('gen_android_bp', *check_only)
Primiano Tucci561ba7e2019-10-10 21:23:11 +010060 call('gen_merged_protos', *check_only)
Primiano Tuccif33540e2019-09-19 12:35:57 +010061 call('ninja', '-C', out, 'protoc')
Primiano Tucci561ba7e2019-10-10 21:23:11 +010062 call('gen_binary_descriptors', '--protoc', protoc_path(out), *check_only)
63
Primiano Tuccieac5d712021-05-18 20:45:05 +010064 if IS_WIN:
65 print('WARNING: Cannot generate BUILD / Android.bp from Windows. ' +
66 'They might be left stale and fail in the CI if you edited any ' +
67 'BUILD.gn file')
68
Hector Dearman7e079772018-11-15 16:08:12 +000069 except AssertionError as e:
70 if not str(e):
71 raise
72 print('Error: {}'.format(e))
73 return 1
74
75 return 0
76
Primiano Tucci834fdc72019-10-04 11:33:44 +010077
Hector Dearman7e079772018-11-15 16:08:12 +000078if __name__ == '__main__':
79 exit(main())