| #!/usr/bin/env python3 |
| # Copyright 2020 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| """ |
| generate fidl_project.json file declaring FIDL libraries |
| |
| This script reads the all_fidl_generated.json file which contains all fidlgen- & |
| fidlc-generated output, and generates a fidl_project.json file which declares |
| all FIDL libraries along with their constituent files, dependencies, and build |
| artifacts (JSON IR and bindings). This is for use in the FIDL Language Server, |
| which uses fidl_project to do dependency resolution. |
| |
| This script makes the assumption that all FIDL library names are unique, which |
| is currently not the case. However, the exceptions are mainly test libaries, so |
| for now this is left unaddressed. |
| """ |
| import glob |
| import json |
| import os |
| import re |
| import sys |
| from pathlib import Path |
| |
| FUCHSIA_DIR = os.getenv('FUCHSIA_DIR') |
| ALL_FIDL_GENERATED_PATH = 'out/default/all_fidl_generated.json' |
| |
| # schema: |
| # map<string, Library> |
| # where string is library name e.g. 'fuchsia.mem' |
| # and Library is |
| # { |
| # "files": []string, |
| # "json": string, |
| # "deps": []string, |
| # "bindings": { |
| # "hlcpp": {}, |
| # "llcpp": {}, |
| # "rust": {}, |
| # "go": {}, |
| # "dart": {}, |
| # ... |
| # } |
| # } |
| |
| def find_files(artifact): |
| library_name = artifact['library'] |
| pattern = f'^fidling\/gen\/([\w\.\/-]+)\/[\w\-. ]+\.fidl\.json$' |
| result = re.search(pattern, artifact['files'][0]) |
| if not result or not result.group(1): |
| return [] |
| |
| fidl_dir = Path(f'{FUCHSIA_DIR}/{result.group(1)}') |
| globs = [ |
| fidl_dir.glob('*.fidl'), |
| fidl_dir.parent.glob('*.fidl'), |
| ] |
| |
| files = [] |
| for glob in globs: |
| for file in glob: |
| # TODO: read in file |
| # parse `library` decl |
| # check that it matches library name |
| files.append(str(file)) |
| return files |
| |
| def find_deps(artifact): |
| library_json = artifact['files'][0] |
| library_json_path = Path(f'{FUCHSIA_DIR}/out/default/{library_json}') |
| with open(library_json_path, 'r') as f: |
| library = json.load(f) |
| deps = library['library_dependencies'] |
| deps = [dep['name'] for dep in deps] |
| return deps |
| |
| def gen_fidl_project(fidl_project_path): |
| result = {} |
| |
| all_fidl_path = FUCHSIA_DIR + '/' + ALL_FIDL_GENERATED_PATH |
| with open(all_fidl_path, 'r') as f: |
| artifacts = json.load(f) |
| |
| print('have read in all_fidl_generated') |
| print(len(artifacts)) |
| |
| for artifact in artifacts: |
| if artifact['type'] == 'json': |
| result[artifact['library']] = { |
| 'json': f"{FUCHSIA_DIR}/out/default/{artifact['files'][0]}", |
| 'files': find_files(artifact), |
| 'deps': find_deps(artifact), |
| 'bindings': {}, # TODO |
| } |
| |
| print('writing to {}', fidl_project_path) |
| with open(fidl_project_path, 'w') as f: |
| json.dump(result, f, indent=4, sort_keys=True) |
| |
| |
| if __name__ == '__main__': |
| gen_fidl_project(sys.argv[1]) |