1#!/usr/bin/env python3 2# Copyright (C) 2023 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 16import argparse 17import filecmp 18import os 19import pathlib 20import shutil 21import subprocess 22import sys 23import tempfile 24 25SOURCE_FILES = [ 26 { 27 'files': [ 28 'protos/perfetto/common/builtin_clock.proto', 29 'protos/perfetto/common/data_source_descriptor.proto', 30 'protos/perfetto/config/data_source_config.proto', 31 'protos/perfetto/config/trace_config.proto', 32 'protos/perfetto/config/track_event/track_event_config.proto', 33 'protos/perfetto/trace/clock_snapshot.proto', 34 'protos/perfetto/trace/interned_data/interned_data.proto', 35 'protos/perfetto/trace/test_event.proto', 36 'protos/perfetto/trace/trace.proto', 37 'protos/perfetto/trace/trace_packet.proto', 38 'protos/perfetto/trace/track_event/counter_descriptor.proto', 39 'protos/perfetto/trace/track_event/debug_annotation.proto', 40 'protos/perfetto/trace/track_event/track_descriptor.proto', 41 'protos/perfetto/trace/track_event/track_event.proto', 42 ], 43 'guard_strip_prefix': 'PROTOS_PERFETTO_', 44 'guard_add_prefix':'INCLUDE_PERFETTO_PUBLIC_PROTOS_', 45 'path_strip_prefix': 'protos/perfetto', 46 'path_add_prefix': 'perfetto/public/protos', 47 'include_prefix': 'include/', 48 }, 49 { 50 'files': [ 51 'src/protozero/test/example_proto/library.proto', 52 'src/protozero/test/example_proto/library_internals/galaxies.proto', 53 'src/protozero/test/example_proto/other_package/test_messages.proto', 54 'src/protozero/test/example_proto/subpackage/test_messages.proto', 55 'src/protozero/test/example_proto/test_messages.proto', 56 'src/protozero/test/example_proto/upper_import.proto', 57 ], 58 'guard_strip_prefix': 'SRC_PROTOZERO_TEST_EXAMPLE_PROTO_', 59 'guard_add_prefix':'SRC_SHARED_LIB_TEST_PROTOS_', 60 'path_strip_prefix': 'src/protozero/test/example_proto', 61 'path_add_prefix': 'src/shared_lib/test/protos', 62 }, 63] 64 65ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) 66IS_WIN = sys.platform.startswith('win') 67 68SCRIPT_PATH = 'tools/gen_c_protos' 69 70 71def protozero_c_plugin_path(out_directory): 72 path = os.path.join(out_directory, 73 'protozero_c_plugin') + ('.exe' if IS_WIN else '') 74 assert os.path.isfile(path) 75 return path 76 77 78def protoc_path(out_directory): 79 path = os.path.join(out_directory, 'protoc') + ('.exe' if IS_WIN else '') 80 assert os.path.isfile(path) 81 return path 82 83 84def call(cmd, *args): 85 path = os.path.join('tools', cmd) 86 command = ['python3', path] + list(args) 87 print('Running', ' '.join(command)) 88 try: 89 subprocess.check_call(command, cwd=ROOT_DIR) 90 except subprocess.CalledProcessError as e: 91 assert False, 'Command: {} failed'.format(' '.join(command)) 92 93 94# Reformats filename 95def clang_format(filename): 96 path = os.path.join(ROOT_DIR, 'third_party', 'clang-format', 97 'clang-format') + ('.exe' if IS_WIN else '') 98 assert os.path.isfile( 99 path), "clang-format not found. Run tools/install-build-deps" 100 subprocess.check_call([ 101 path, '--style=file:{}'.format(os.path.join(ROOT_DIR, '.clang-format')), 102 '-i', filename 103 ], 104 cwd=ROOT_DIR) 105 106 107# Transforms filename extension like the ProtoZero C plugin 108def transform_extension(filename): 109 old_suffix = ".proto" 110 new_suffix = ".pzc.h" 111 if filename.endswith(old_suffix): 112 return filename[:-len(old_suffix)] + new_suffix 113 return filename 114 115 116def generate(source, outdir, protoc_path, protozero_c_plugin_path, guard_strip_prefix, guard_add_prefix, path_strip_prefix, path_add_prefix): 117 options = { 118 'guard_strip_prefix': guard_strip_prefix, 119 'guard_add_prefix': guard_add_prefix, 120 'path_strip_prefix': path_strip_prefix, 121 'path_add_prefix': path_add_prefix, 122 'invoker': SCRIPT_PATH, 123 } 124 serialized_options = ','.join( 125 ['{}={}'.format(name, value) for name, value in options.items()]) 126 subprocess.check_call([ 127 protoc_path, 128 '--proto_path=.', 129 '--plugin=protoc-gen-plugin={}'.format(protozero_c_plugin_path), 130 '--plugin_out={}:{}'.format(serialized_options, outdir), 131 source, 132 ], 133 cwd=ROOT_DIR) 134 135 136# Given filename, the path of a header generated by the ProtoZero C plugin, 137# returns the path where the header should go in the public include directory. 138# Example 139# 140# include_path_for("protos/perfetto/trace/trace.pzc.h") == 141# "include/perfetto/public/protos/trace/trace.pzc.h" 142def include_path_for(filename): 143 return os.path.join('include', 'perfetto', 'public', 'protos', 144 *pathlib.Path(transform_extension(filename)).parts[2:]) 145 146 147def main(): 148 parser = argparse.ArgumentParser() 149 parser.add_argument('--check-only', action='store_true') 150 parser.add_argument('OUT') 151 args = parser.parse_args() 152 out = args.OUT 153 154 call('ninja', '-C', out, 'protoc', 'protozero_c_plugin') 155 156 try: 157 with tempfile.TemporaryDirectory() as tmpdirname: 158 for sources in SOURCE_FILES: 159 for source in sources['files']: 160 generate(source, tmpdirname, protoc_path(out), protozero_c_plugin_path(out), 161 guard_strip_prefix=sources['guard_strip_prefix'], 162 guard_add_prefix=sources['guard_add_prefix'], 163 path_strip_prefix=sources['path_strip_prefix'], 164 path_add_prefix=sources['path_add_prefix'], 165 ) 166 167 tmpfilename = os.path.join(tmpdirname, transform_extension(source)) 168 clang_format(tmpfilename) 169 if source.startswith(sources['path_strip_prefix']): 170 targetfilename = source[len(sources['path_strip_prefix']):] 171 else: 172 targetfilename = source 173 174 targetfilename = sources['path_add_prefix'] + targetfilename 175 176 if 'include_prefix' in sources: 177 targetfilename = os.path.join(sources['include_prefix'], targetfilename) 178 targetfilename = transform_extension(targetfilename) 179 180 if args.check_only: 181 if not filecmp.cmp(tmpfilename, targetfilename): 182 raise AssertionError('Target {} does not match', targetfilename) 183 else: 184 os.makedirs(os.path.dirname(targetfilename), exist_ok=True) 185 shutil.copyfile(tmpfilename, targetfilename) 186 187 except AssertionError as e: 188 if not str(e): 189 raise 190 print('Error: {}'.format(e)) 191 return 1 192 193 194if __name__ == '__main__': 195 exit(main()) 196