1#!/usr/bin/python3 2# 3# Copyright (C) 2020 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18import argparse 19import os 20import json 21import subprocess 22 23from api_parser import ApiParser 24from chpp_code_generator import CodeGenerator 25from utils import system_chre_abs_path 26 27 28def run(args): 29 with open(system_chre_abs_path() + '/api_parser/chre_api_annotations.json') as f: 30 js = json.load(f) 31 32 commit_hash = subprocess.getoutput( 33 "git describe --always --long --dirty --exclude '*'") 34 for file in js: 35 if args.file_filter: 36 matched = False 37 for matcher in args.file_filter: 38 if matcher in file['filename']: 39 matched = True 40 break 41 if not matched: 42 print("Skipping {} - doesn't match filter(s) {}".format(file['filename'], 43 args.file_filter)) 44 continue 45 print('Parsing {} ... '.format(file['filename']), end='', flush=True) 46 api_parser = ApiParser(file) 47 code_gen = CodeGenerator(api_parser, commit_hash) 48 print('done') 49 code_gen.generate_header_file(args.dry_run, args.skip_clang_format) 50 code_gen.generate_conversion_file(args.dry_run, args.skip_clang_format) 51 52 53def main(): 54 parser = argparse.ArgumentParser( 55 description='Generate CHPP serialization code from CHRE APIs.') 56 parser.add_argument('-n', dest='dry_run', action='store_true', 57 help='Print the output instead of writing to a file') 58 parser.add_argument('--skip-clang-format', dest='skip_clang_format', action='store_true', 59 help='Skip running clang-format on the output files (doesn\'t apply to dry ' 60 'runs)') 61 parser.add_argument('file_filter', nargs='*', 62 help='Filters the input files (filename field in the JSON) to generate a ' 63 'subset of the typical output, e.g. "wifi" to just generate conversion' 64 ' routines for wifi.h') 65 args = parser.parse_args() 66 run(args) 67 68 69if __name__ == '__main__': 70 main() 71