2020-06-23 00:10:31 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
"""Generates source for stub shared libraries for the NDK."""
|
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import logging
|
2020-07-30 23:32:55 +02:00
|
|
|
from pathlib import Path
|
2020-06-23 00:10:31 +02:00
|
|
|
import sys
|
2020-06-23 20:21:21 +02:00
|
|
|
from typing import Iterable, TextIO
|
2020-06-23 00:10:31 +02:00
|
|
|
|
|
|
|
import symbolfile
|
2020-06-23 20:21:21 +02:00
|
|
|
from symbolfile import Arch, Version
|
2020-06-23 00:10:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Generator:
|
|
|
|
"""Output generator that writes stub source files and version scripts."""
|
2020-07-30 23:32:55 +02:00
|
|
|
def __init__(self, src_file: TextIO, version_script: TextIO,
|
2022-07-16 16:30:09 +02:00
|
|
|
symbol_list: TextIO, filt: symbolfile.Filter) -> None:
|
2020-06-23 00:10:31 +02:00
|
|
|
self.src_file = src_file
|
|
|
|
self.version_script = version_script
|
2020-07-30 23:32:55 +02:00
|
|
|
self.symbol_list = symbol_list
|
2022-07-16 16:30:09 +02:00
|
|
|
self.filter = filt
|
|
|
|
self.api = filt.api
|
2020-06-23 00:10:31 +02:00
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
def write(self, versions: Iterable[Version]) -> None:
|
2020-06-23 00:10:31 +02:00
|
|
|
"""Writes all symbol data to the output files."""
|
2020-07-30 23:32:55 +02:00
|
|
|
self.symbol_list.write('[abi_symbol_list]\n')
|
2020-06-23 00:10:31 +02:00
|
|
|
for version in versions:
|
|
|
|
self.write_version(version)
|
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
def write_version(self, version: Version) -> None:
|
2020-06-23 00:10:31 +02:00
|
|
|
"""Writes a single version block's data to the output files."""
|
2022-07-16 16:30:09 +02:00
|
|
|
if self.filter.should_omit_version(version):
|
2020-06-23 00:10:31 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
section_versioned = symbolfile.symbol_versioned_in_api(
|
|
|
|
version.tags, self.api)
|
|
|
|
version_empty = True
|
|
|
|
pruned_symbols = []
|
|
|
|
for symbol in version.symbols:
|
2022-07-16 16:30:09 +02:00
|
|
|
if self.filter.should_omit_symbol(symbol):
|
2020-06-23 00:10:31 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
if symbolfile.symbol_versioned_in_api(symbol.tags, self.api):
|
|
|
|
version_empty = False
|
|
|
|
pruned_symbols.append(symbol)
|
|
|
|
|
|
|
|
if len(pruned_symbols) > 0:
|
|
|
|
if not version_empty and section_versioned:
|
|
|
|
self.version_script.write(version.name + ' {\n')
|
|
|
|
self.version_script.write(' global:\n')
|
|
|
|
for symbol in pruned_symbols:
|
|
|
|
emit_version = symbolfile.symbol_versioned_in_api(
|
|
|
|
symbol.tags, self.api)
|
|
|
|
if section_versioned and emit_version:
|
|
|
|
self.version_script.write(' ' + symbol.name + ';\n')
|
|
|
|
|
|
|
|
weak = ''
|
|
|
|
if 'weak' in symbol.tags:
|
|
|
|
weak = '__attribute__((weak)) '
|
|
|
|
|
|
|
|
if 'var' in symbol.tags:
|
2020-07-30 23:32:55 +02:00
|
|
|
self.src_file.write(f'{weak}int {symbol.name} = 0;\n')
|
2020-06-23 00:10:31 +02:00
|
|
|
else:
|
2020-07-30 23:32:55 +02:00
|
|
|
self.src_file.write(f'{weak}void {symbol.name}() {{}}\n')
|
|
|
|
|
|
|
|
self.symbol_list.write(f'{symbol.name}\n')
|
2020-06-23 00:10:31 +02:00
|
|
|
|
|
|
|
if not version_empty and section_versioned:
|
|
|
|
base = '' if version.base is None else ' ' + version.base
|
|
|
|
self.version_script.write('}' + base + ';\n')
|
|
|
|
|
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
def parse_args() -> argparse.Namespace:
|
2020-06-23 00:10:31 +02:00
|
|
|
"""Parses and returns command line arguments."""
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
2020-07-30 23:32:55 +02:00
|
|
|
def resolved_path(raw: str) -> Path:
|
|
|
|
"""Returns a resolved Path for the given string."""
|
|
|
|
return Path(raw).resolve()
|
|
|
|
|
2020-06-23 00:10:31 +02:00
|
|
|
parser.add_argument('-v', '--verbose', action='count', default=0)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'--api', required=True, help='API level being targeted.')
|
|
|
|
parser.add_argument(
|
|
|
|
'--arch', choices=symbolfile.ALL_ARCHITECTURES, required=True,
|
|
|
|
help='Architecture being targeted.')
|
|
|
|
parser.add_argument(
|
|
|
|
'--llndk', action='store_true', help='Use the LLNDK variant.')
|
|
|
|
parser.add_argument(
|
2021-06-04 23:31:58 +02:00
|
|
|
'--apex',
|
|
|
|
action='store_true',
|
|
|
|
help='Use the APEX variant. Note: equivalent to --system-api.')
|
|
|
|
parser.add_argument(
|
|
|
|
'--system-api',
|
|
|
|
action='store_true',
|
|
|
|
dest='apex',
|
|
|
|
help='Use the SystemAPI variant. Note: equivalent to --apex.')
|
2020-06-23 00:10:31 +02:00
|
|
|
|
2020-07-30 23:32:55 +02:00
|
|
|
parser.add_argument('--api-map',
|
|
|
|
type=resolved_path,
|
|
|
|
required=True,
|
|
|
|
help='Path to the API level map JSON file.')
|
|
|
|
|
|
|
|
parser.add_argument('symbol_file',
|
|
|
|
type=resolved_path,
|
|
|
|
help='Path to symbol file.')
|
|
|
|
parser.add_argument('stub_src',
|
|
|
|
type=resolved_path,
|
|
|
|
help='Path to output stub source file.')
|
|
|
|
parser.add_argument('version_script',
|
|
|
|
type=resolved_path,
|
|
|
|
help='Path to output version script.')
|
|
|
|
parser.add_argument('symbol_list',
|
|
|
|
type=resolved_path,
|
|
|
|
help='Path to output abigail symbol list.')
|
2020-06-23 00:10:31 +02:00
|
|
|
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
def main() -> None:
|
2020-06-23 00:10:31 +02:00
|
|
|
"""Program entry point."""
|
|
|
|
args = parse_args()
|
|
|
|
|
2020-07-30 23:32:55 +02:00
|
|
|
with args.api_map.open() as map_file:
|
2020-06-23 00:10:31 +02:00
|
|
|
api_map = json.load(map_file)
|
|
|
|
api = symbolfile.decode_api_level(args.api, api_map)
|
|
|
|
|
|
|
|
verbose_map = (logging.WARNING, logging.INFO, logging.DEBUG)
|
|
|
|
verbosity = args.verbose
|
|
|
|
if verbosity > 2:
|
|
|
|
verbosity = 2
|
|
|
|
logging.basicConfig(level=verbose_map[verbosity])
|
|
|
|
|
2022-07-16 16:30:09 +02:00
|
|
|
filt = symbolfile.Filter(args.arch, api, args.llndk, args.apex)
|
2020-07-30 23:32:55 +02:00
|
|
|
with args.symbol_file.open() as symbol_file:
|
2020-06-23 00:10:31 +02:00
|
|
|
try:
|
2022-07-16 16:30:09 +02:00
|
|
|
versions = symbolfile.SymbolFileParser(symbol_file, api_map, filt).parse()
|
2020-06-23 00:10:31 +02:00
|
|
|
except symbolfile.MultiplyDefinedSymbolError as ex:
|
2020-07-30 23:32:55 +02:00
|
|
|
sys.exit(f'{args.symbol_file}: error: {ex}')
|
|
|
|
|
|
|
|
with args.stub_src.open('w') as src_file:
|
|
|
|
with args.version_script.open('w') as version_script:
|
|
|
|
with args.symbol_list.open('w') as symbol_list:
|
|
|
|
generator = Generator(src_file, version_script, symbol_list,
|
2022-07-16 16:30:09 +02:00
|
|
|
filt)
|
2020-07-30 23:32:55 +02:00
|
|
|
generator.write(versions)
|
2020-06-23 00:10:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|