1# Copyright 2019 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""SAPI interface header generator. 15 16Parses headers to extract type information from functions and generate a SAPI 17interface wrapper. 18""" 19import sys 20 21from absl import app 22from absl import flags 23from absl import logging 24try: 25 from com_google_sandboxed_api.sandboxed_api.tools.generator2 import code 26except: 27 import code 28 29FLAGS = flags.FLAGS 30 31flags.DEFINE_string('sapi_name', None, 'library name') 32flags.DEFINE_string('sapi_out', '', 'output header file') 33flags.DEFINE_string('sapi_ns', '', 'namespace') 34flags.DEFINE_string('sapi_isystem', '', 'system includes') 35flags.DEFINE_list('sapi_functions', [], 'function list to analyze') 36flags.DEFINE_list('sapi_in', None, 'input files to analyze') 37flags.DEFINE_string('sapi_embed_dir', '', 'directory with embed includes') 38flags.DEFINE_string('sapi_embed_name', '', 'name of the embed object') 39flags.DEFINE_bool( 40 'sapi_limit_scan_depth', False, 41 'scan only functions from top level file in compilation unit') 42 43 44def extract_includes(path, array): 45 try: 46 with open(path, 'r') as f: 47 for line in f: 48 array.append('-isystem') 49 array.append(line.strip()) 50 except IOError: 51 pass 52 return array 53 54 55def main(c_flags): 56 # remove path to current binary 57 c_flags.pop(0) 58 logging.debug(FLAGS.sapi_functions) 59 extract_includes(FLAGS.sapi_isystem, c_flags) 60 tus = code.Analyzer.process_files(FLAGS.sapi_in, c_flags, 61 FLAGS.sapi_limit_scan_depth) 62 generator = code.Generator(tus) 63 result = generator.generate(FLAGS.sapi_name, FLAGS.sapi_functions, 64 FLAGS.sapi_ns, FLAGS.sapi_out, 65 FLAGS.sapi_embed_dir, FLAGS.sapi_embed_name) 66 67 if FLAGS.sapi_out: 68 with open(FLAGS.sapi_out, 'w') as out_file: 69 out_file.write(result) 70 else: 71 sys.stdout.write(result) 72 73 74if __name__ == '__main__': 75 flags.mark_flags_as_required(['sapi_name', 'sapi_in']) 76 app.run(main) 77