Lalit Maganti | bf99dd3 | 2023-02-07 23:50:48 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Lalit Maganti | ba9e1bf | 2023-02-13 16:40:46 +0000 | [diff] [blame] | 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 | |
Lalit Maganti | bf99dd3 | 2023-02-07 23:50:48 +0000 | [diff] [blame] | 16 | import json |
| 17 | import os |
| 18 | import subprocess |
| 19 | import sys |
| 20 | from typing import Any, Dict, List |
| 21 | import yaml |
| 22 | |
| 23 | ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 24 | |
Lalit Maganti | bf99dd3 | 2023-02-07 23:50:48 +0000 | [diff] [blame] | 25 | GRPC_GN_HEADER = ''' |
| 26 | # |
| 27 | # DO NOT EDIT. AUTOGENERATED file |
| 28 | # |
| 29 | # This file is generated with the command: |
| 30 | # tools/gen_grpc_build_gn.py > buildtools/grpc/BUILD.gn |
| 31 | # |
| 32 | |
Lalit Maganti | ba9e1bf | 2023-02-13 16:40:46 +0000 | [diff] [blame] | 33 | import("../../gn/perfetto.gni") |
| 34 | |
Lalit Maganti | bf99dd3 | 2023-02-07 23:50:48 +0000 | [diff] [blame] | 35 | # Prevent the gRPC from being depended upon without explicitly being opted in. |
| 36 | assert(enable_perfetto_grpc) |
Lalit Maganti | ba9e1bf | 2023-02-13 16:40:46 +0000 | [diff] [blame] | 37 | |
| 38 | # BoringSSL has assembly code which is tied to platform-specific. For now, we |
| 39 | # only care about Linux x64 so assert this as the case. |
| 40 | assert(is_linux && current_cpu == "x64") |
Lalit Maganti | bf99dd3 | 2023-02-07 23:50:48 +0000 | [diff] [blame] | 41 | ''' |
| 42 | |
Lalit Maganti | ba9e1bf | 2023-02-13 16:40:46 +0000 | [diff] [blame] | 43 | TARGET_TEMPLATE = """ |
| 44 | {target_type}("{name}") {{ |
| 45 | sources = {srcs} |
| 46 | public_deps = {deps} |
| 47 | public_configs = ["..:{config_name}"] |
| 48 | configs -= [ "//gn/standalone:extra_warnings" ] |
| 49 | }}""" |
| 50 | |
| 51 | LIBRARY_IGNORE_LIST = set([ |
| 52 | 'grpcpp_channelz', |
| 53 | 'grpc++_reflection', |
| 54 | 'benchmark_helpers', |
| 55 | 'boringssl_test_util', |
| 56 | ]) |
| 57 | |
| 58 | TARGET_ALLOW_LIST = set([ |
| 59 | 'grpc_cpp_plugin', |
| 60 | ]) |
| 61 | |
| 62 | STATIC_LIBRARY_TARGETS = set([ |
| 63 | 'upb', |
| 64 | 're2', |
| 65 | 'boringssl', |
| 66 | 'grpc++', |
| 67 | ]) |
| 68 | |
| 69 | |
| 70 | def grpc_relpath(*segments: str) -> str: |
| 71 | '''From path segments to GRPC root, returns the absolute path.''' |
| 72 | return os.path.join(ROOT_DIR, 'buildtools', 'grpc', 'src', *segments) |
| 73 | |
| 74 | |
Lalit Maganti | bf99dd3 | 2023-02-07 23:50:48 +0000 | [diff] [blame] | 75 | GRPC_BUILD_YAML = grpc_relpath('build_autogenerated.yaml') |
| 76 | ABSL_GEN_BUILD_YAML = grpc_relpath('src', 'abseil-cpp', 'gen_build_yaml.py') |
| 77 | UPB_GEN_BUILD_YAML = grpc_relpath('src', 'upb', 'gen_build_yaml.py') |
Lalit Maganti | ba9e1bf | 2023-02-13 16:40:46 +0000 | [diff] [blame] | 78 | RE2_GEN_BUILD_YAML = grpc_relpath('src', 're2', 'gen_build_yaml.py') |
Lalit Maganti | bf99dd3 | 2023-02-07 23:50:48 +0000 | [diff] [blame] | 79 | BSSL_GEN_BUILD_YAML = grpc_relpath('src', 'boringssl', 'gen_build_yaml.py') |
| 80 | |
| 81 | |
Lalit Maganti | bf99dd3 | 2023-02-07 23:50:48 +0000 | [diff] [blame] | 82 | def gen_grpc_dep_yaml(gen_path: str) -> Dict[str, Any]: |
| 83 | '''Invokes a gen_build_yaml.py file for creating YAML for gRPC deps.''' |
| 84 | return yaml.safe_load(subprocess.check_output(['python3', gen_path])) |
| 85 | |
| 86 | |
Lalit Maganti | ba9e1bf | 2023-02-13 16:40:46 +0000 | [diff] [blame] | 87 | def bazel_label_to_gn_target(dep: str) -> str: |
| 88 | '''Converts a Bazel label name into a gn target name.''' |
| 89 | if dep == 'libssl': |
| 90 | return 'boringssl' |
| 91 | return dep.replace('/', '_').replace(':', '_') |
| 92 | |
| 93 | |
| 94 | def get_deps_for_target(target: str) -> List[str]: |
| 95 | if target == 'grpc_plugin_support': |
| 96 | return ['..:protoc_lib'] |
Lalit Maganti | 07ecf77 | 2023-03-02 03:12:53 +0000 | [diff] [blame] | 97 | if target == 'grpc': |
| 98 | return [':re2', '../../gn:zlib'] |
| 99 | if target == 'grpc_authorization_provider': |
| 100 | return [':re2'] |
Lalit Maganti | ba9e1bf | 2023-02-13 16:40:46 +0000 | [diff] [blame] | 101 | return [] |
| 102 | |
| 103 | |
| 104 | def get_library_target_type(target: str) -> str: |
| 105 | if target in STATIC_LIBRARY_TARGETS: |
| 106 | return 'static_library' |
| 107 | return 'source_set' |
| 108 | |
| 109 | |
| 110 | def yaml_to_gn_targets(desc: Dict[str, Any], build_types: list[str], |
| 111 | config_name: str) -> List[str]: |
Lalit Maganti | bf99dd3 | 2023-02-07 23:50:48 +0000 | [diff] [blame] | 112 | '''Given a gRPC YAML description of the build graph, generates GN targets.''' |
Lalit Maganti | ba9e1bf | 2023-02-13 16:40:46 +0000 | [diff] [blame] | 113 | out = [] |
Lalit Maganti | bf99dd3 | 2023-02-07 23:50:48 +0000 | [diff] [blame] | 114 | for lib in desc['libs']: |
Lalit Maganti | ba9e1bf | 2023-02-13 16:40:46 +0000 | [diff] [blame] | 115 | if lib['build'] not in build_types: |
Lalit Maganti | bf99dd3 | 2023-02-07 23:50:48 +0000 | [diff] [blame] | 116 | continue |
Lalit Maganti | ba9e1bf | 2023-02-13 16:40:46 +0000 | [diff] [blame] | 117 | if lib['name'] in LIBRARY_IGNORE_LIST: |
Lalit Maganti | bf99dd3 | 2023-02-07 23:50:48 +0000 | [diff] [blame] | 118 | continue |
Lalit Maganti | ba9e1bf | 2023-02-13 16:40:46 +0000 | [diff] [blame] | 119 | srcs = [f'src/{file}' for file in lib['src'] + lib['headers']] |
| 120 | if 'asm_src' in lib: |
| 121 | srcs += [f'src/{file}' for file in lib['asm_src']['crypto_linux_x86_64']] |
| 122 | deps = [f':{bazel_label_to_gn_target(dep)}' for dep in lib.get('deps', []) |
| 123 | ] + get_deps_for_target(lib['name']) |
| 124 | library_target = TARGET_TEMPLATE.format( |
Lalit Maganti | bf99dd3 | 2023-02-07 23:50:48 +0000 | [diff] [blame] | 125 | name=bazel_label_to_gn_target(lib['name']), |
| 126 | config_name=config_name, |
Lalit Maganti | ba9e1bf | 2023-02-13 16:40:46 +0000 | [diff] [blame] | 127 | srcs=json.dumps(srcs), |
| 128 | deps=json.dumps(deps), |
| 129 | target_type=get_library_target_type(lib['name'])) |
| 130 | out.append(library_target) |
| 131 | |
| 132 | for bin in desc.get('targets', []): |
| 133 | if bin['build'] not in build_types: |
| 134 | continue |
| 135 | if bin['name'] not in TARGET_ALLOW_LIST: |
| 136 | continue |
| 137 | srcs = json.dumps([f'src/{file}' for file in bin['src'] + bin['headers']]) |
| 138 | deps = [f':{bazel_label_to_gn_target(dep)}' for dep in bin.get('deps', []) |
| 139 | ] + get_deps_for_target(bin['name']) |
| 140 | binary_target = TARGET_TEMPLATE.format( |
| 141 | name=bazel_label_to_gn_target(bin['name']), |
| 142 | config_name=config_name, |
Lalit Maganti | bf99dd3 | 2023-02-07 23:50:48 +0000 | [diff] [blame] | 143 | srcs=srcs, |
Lalit Maganti | ba9e1bf | 2023-02-13 16:40:46 +0000 | [diff] [blame] | 144 | deps=json.dumps(deps), |
| 145 | target_type='executable') |
| 146 | out.append(binary_target) |
| 147 | return out |
Lalit Maganti | bf99dd3 | 2023-02-07 23:50:48 +0000 | [diff] [blame] | 148 | |
| 149 | |
| 150 | def main(): |
| 151 | out: List[str] = [] |
| 152 | |
| 153 | # Generate absl rules |
| 154 | absl_yaml = gen_grpc_dep_yaml(ABSL_GEN_BUILD_YAML) |
Lalit Maganti | ba9e1bf | 2023-02-13 16:40:46 +0000 | [diff] [blame] | 155 | out.extend(yaml_to_gn_targets(absl_yaml, ['private'], 'grpc_absl_config')) |
Lalit Maganti | bf99dd3 | 2023-02-07 23:50:48 +0000 | [diff] [blame] | 156 | |
| 157 | # Generate upb rules |
| 158 | upb_yaml = gen_grpc_dep_yaml(UPB_GEN_BUILD_YAML) |
Lalit Maganti | ba9e1bf | 2023-02-13 16:40:46 +0000 | [diff] [blame] | 159 | out.extend(yaml_to_gn_targets(upb_yaml, ['all'], 'grpc_upb_config')) |
| 160 | |
| 161 | # Generate re2 rules |
| 162 | re2_yaml = gen_grpc_dep_yaml(RE2_GEN_BUILD_YAML) |
| 163 | out.extend(yaml_to_gn_targets(re2_yaml, ['private'], 'grpc_re2_config')) |
Lalit Maganti | bf99dd3 | 2023-02-07 23:50:48 +0000 | [diff] [blame] | 164 | |
| 165 | # Generate boringssl rules |
| 166 | boringssl_yaml = gen_grpc_dep_yaml(BSSL_GEN_BUILD_YAML) |
Lalit Maganti | ba9e1bf | 2023-02-13 16:40:46 +0000 | [diff] [blame] | 167 | out.extend( |
| 168 | yaml_to_gn_targets(boringssl_yaml, ['private'], 'grpc_boringssl_config')) |
Lalit Maganti | bf99dd3 | 2023-02-07 23:50:48 +0000 | [diff] [blame] | 169 | |
| 170 | # Generate grpc rules |
| 171 | with open(GRPC_BUILD_YAML, 'r', encoding='utf-8') as f: |
| 172 | grpc_yaml = yaml.safe_load(f.read()) |
Lalit Maganti | ba9e1bf | 2023-02-13 16:40:46 +0000 | [diff] [blame] | 173 | out.extend( |
| 174 | yaml_to_gn_targets(grpc_yaml, ['all', 'protoc'], 'grpc_internal_config')) |
Lalit Maganti | bf99dd3 | 2023-02-07 23:50:48 +0000 | [diff] [blame] | 175 | |
| 176 | print(GRPC_GN_HEADER) |
| 177 | print('\n'.join(out)) |
| 178 | return 0 |
| 179 | |
| 180 | |
| 181 | if __name__ == '__main__': |
| 182 | sys.exit(main()) |