sandboxed-api/sandboxed_api/tools/generator2/sapi_generator.py
Maciej Szawłowski edd6b437ae Filter functions based on files we scan.
Currently we extract all functions from the compilation unit - it doesn't really make sense as it will try to process functions in included files too.
As we require sapi_in flag, we have information which files are of interest.

This change stores the top path in _TranslationUnit and uses it when looking for function definitions to filter only functions from the path we provided.

PiperOrigin-RevId: 297342507
Change-Id: Ie411321d375168f413f9f153a606c1113f55e79a
2020-02-26 06:02:15 -08:00

74 lines
2.4 KiB
Python

# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""SAPI interface header generator.
Parses headers to extract type information from functions and generate a SAPI
interface wrapper.
"""
import sys
from absl import app
from absl import flags
from absl import logging
import code
FLAGS = flags.FLAGS
flags.DEFINE_string('sapi_name', None, 'library name')
flags.DEFINE_string('sapi_out', '', 'output header file')
flags.DEFINE_string('sapi_ns', '', 'namespace')
flags.DEFINE_string('sapi_isystem', '', 'system includes')
flags.DEFINE_list('sapi_functions', [], 'function list to analyze')
flags.DEFINE_list('sapi_in', None, 'input files to analyze')
flags.DEFINE_string('sapi_embed_dir', None, 'directory with embed includes')
flags.DEFINE_string('sapi_embed_name', None, 'name of the embed object')
flags.DEFINE_bool(
'sapi_limit_scan_depth', False,
'scan only functions from top level file in compilation unit')
def extract_includes(path, array):
try:
with open(path, 'r') as f:
for line in f:
array.append('-isystem')
array.append(line.strip())
except IOError:
pass
return array
def main(c_flags):
# remove path to current binary
c_flags.pop(0)
logging.debug(FLAGS.sapi_functions)
extract_includes(FLAGS.sapi_isystem, c_flags)
tus = code.Analyzer.process_files(FLAGS.sapi_in, c_flags,
FLAGS.sapi_limit_scan_depth)
generator = code.Generator(tus)
result = generator.generate(FLAGS.sapi_name, FLAGS.sapi_functions,
FLAGS.sapi_ns, FLAGS.sapi_out,
FLAGS.sapi_embed_dir, FLAGS.sapi_embed_name)
if FLAGS.sapi_out:
with open(FLAGS.sapi_out, 'w') as out_file:
out_file.write(result)
else:
sys.stdout.write(result)
if __name__ == '__main__':
flags.mark_flags_as_required(['sapi_name', 'sapi_in'])
app.run(main)