| #!/usr/bin/env python |
| # Copyright 2018 The Chromium Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| # |
| # Generates a single BUILD.gn file with build targets generated using the |
| # manifest files in the SDK. |
| |
| import json |
| import os |
| import subprocess |
| import sys |
| |
| |
| # Inserted at the top of the generated BUILD.gn file. |
| _GENERATED_PREAMBLE = """# DO NOT EDIT! This file was generated by |
| # //third_party/fuchsia-sdk/gen_build_defs.py. |
| # Any changes made to this file will be discarded. |
| |
| import("//third_party/fuchsia-sdk/fuchsia_sdk_pkg.gni") |
| |
| """ |
| |
| |
| def SerializeListOfStrings(strings): |
| """Outputs a list of strings in GN-friendly, double-quoted format.""" |
| |
| return '[\n' + ''.join([' "{}",\n'.format(s) for s in strings]) + ' ]' |
| |
| |
| def ConvertFidlLibrary(json): |
| """Massages the manifest data to match the format required by the GN build |
| and outputs a GN target for the FIDL library |
| |
| Arguments: |
| json: The parsed manifest JSON. |
| Returns: |
| The GN target definition, represented as a string.""" |
| |
| json['deps'] = SerializeListOfStrings( |
| [':' + dep.split('.')[-1] for dep in json['deps']]) |
| json['sources'] = SerializeListOfStrings(json['sources']) |
| |
| name_tokenized = json['name'].split('.') |
| json['shortname'] = name_tokenized[-1] |
| json['namespace'] = '.'.join(name_tokenized[:-1]) |
| |
| return """fuchsia_sdk_fidl_pkg("{shortname}") {{ |
| namespace = "{namespace}" |
| public_deps = {deps} |
| sources = {sources} |
| }}\n\n""".format(**json) |
| |
| |
| def ConvertNoOp(json): |
| """Null implementation of a conversion function. No output is generated.""" |
| |
| return "" |
| |
| |
| """Maps manifest type strings to relevant conversion functions.""" |
| _CONVERSION_FUNCTION_MAP = { |
| 'fidl_library': ConvertFidlLibrary, |
| |
| # TODO(888753): Add conversion routines for these manifest types and migrate |
| # existing dependents of //third_party/fuchsia_sdk/BUILD.gn to use the |
| # generated targets instead. The migration can be executed incrementally. |
| 'cc_source_library': ConvertNoOp, |
| 'cc_prebuilt_library': ConvertNoOp, |
| 'host_tool': ConvertNoOp, |
| 'image': ConvertNoOp, |
| 'loadable_module': ConvertNoOp, |
| 'sysroot': ConvertNoOp, |
| } |
| |
| |
| def ConvertSdkManifests(): |
| sdk_base_dir = os.path.join(os.path.dirname(__file__), 'sdk') |
| toplevel_meta = json.load(open(os.path.join(sdk_base_dir, 'meta', |
| 'manifest.json'))) |
| |
| build_output_path = os.path.join(sdk_base_dir, 'BUILD.gn') |
| with open(build_output_path, 'w') as buildfile: |
| buildfile.write(_GENERATED_PREAMBLE) |
| |
| for next_part in toplevel_meta['parts']: |
| parsed = json.load(open(os.path.join(sdk_base_dir, next_part))) |
| if 'type' not in parsed: |
| raise Exception("Couldn't find 'type' node in %s." % next_part) |
| |
| convert_function = _CONVERSION_FUNCTION_MAP.get(parsed['type']) |
| if convert_function is None: |
| raise Exception('Unexpected SDK artifact type %s in %s.' % |
| (parsed['type'], next_part)) |
| buildfile.write(convert_function(parsed)) |
| |
| |
| if __name__ == '__main__': |
| sys.exit(ConvertSdkManifests()) |