2020-01-17 21:05:03 +08:00
|
|
|
# Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
#
|
|
|
|
# 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')
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
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)
|