blob: f893d41e2a35906c2f6d3e8d4a230b647b5c6e8b [file] [log] [blame]
Dan Albert06f58af2020-06-22 15:10:31 -07001#!/usr/bin/env python
2#
3# Copyright (C) 2016 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"""Generates source for stub shared libraries for the NDK."""
18import argparse
19import json
20import logging
Dan Albertf1d14c72020-07-30 14:32:55 -070021from pathlib import Path
Dan Albert06f58af2020-06-22 15:10:31 -070022import sys
Dan Albertaf7b36d2020-06-23 11:21:21 -070023from typing import Iterable, TextIO
Dan Albert06f58af2020-06-22 15:10:31 -070024
25import symbolfile
Dan Albertaf7b36d2020-06-23 11:21:21 -070026from symbolfile import Arch, Version
Dan Albert06f58af2020-06-22 15:10:31 -070027
28
29class Generator:
30 """Output generator that writes stub source files and version scripts."""
Dan Albertf1d14c72020-07-30 14:32:55 -070031 def __init__(self, src_file: TextIO, version_script: TextIO,
Jiyong Park3f9c41d2022-07-16 23:30:09 +090032 symbol_list: TextIO, filt: symbolfile.Filter) -> None:
Dan Albert06f58af2020-06-22 15:10:31 -070033 self.src_file = src_file
34 self.version_script = version_script
Dan Albertf1d14c72020-07-30 14:32:55 -070035 self.symbol_list = symbol_list
Jiyong Park3f9c41d2022-07-16 23:30:09 +090036 self.filter = filt
37 self.api = filt.api
Dan Albert06f58af2020-06-22 15:10:31 -070038
Dan Albertaf7b36d2020-06-23 11:21:21 -070039 def write(self, versions: Iterable[Version]) -> None:
Dan Albert06f58af2020-06-22 15:10:31 -070040 """Writes all symbol data to the output files."""
Dan Albertf1d14c72020-07-30 14:32:55 -070041 self.symbol_list.write('[abi_symbol_list]\n')
Dan Albert06f58af2020-06-22 15:10:31 -070042 for version in versions:
43 self.write_version(version)
44
Dan Albertaf7b36d2020-06-23 11:21:21 -070045 def write_version(self, version: Version) -> None:
Dan Albert06f58af2020-06-22 15:10:31 -070046 """Writes a single version block's data to the output files."""
Jiyong Park3f9c41d2022-07-16 23:30:09 +090047 if self.filter.should_omit_version(version):
Dan Albert06f58af2020-06-22 15:10:31 -070048 return
49
50 section_versioned = symbolfile.symbol_versioned_in_api(
51 version.tags, self.api)
52 version_empty = True
53 pruned_symbols = []
54 for symbol in version.symbols:
Jiyong Park3f9c41d2022-07-16 23:30:09 +090055 if self.filter.should_omit_symbol(symbol):
Dan Albert06f58af2020-06-22 15:10:31 -070056 continue
57
58 if symbolfile.symbol_versioned_in_api(symbol.tags, self.api):
59 version_empty = False
60 pruned_symbols.append(symbol)
61
62 if len(pruned_symbols) > 0:
63 if not version_empty and section_versioned:
64 self.version_script.write(version.name + ' {\n')
65 self.version_script.write(' global:\n')
66 for symbol in pruned_symbols:
67 emit_version = symbolfile.symbol_versioned_in_api(
68 symbol.tags, self.api)
69 if section_versioned and emit_version:
70 self.version_script.write(' ' + symbol.name + ';\n')
71
72 weak = ''
73 if 'weak' in symbol.tags:
74 weak = '__attribute__((weak)) '
75
76 if 'var' in symbol.tags:
Dan Albertf1d14c72020-07-30 14:32:55 -070077 self.src_file.write(f'{weak}int {symbol.name} = 0;\n')
Dan Albert06f58af2020-06-22 15:10:31 -070078 else:
Dan Albertf1d14c72020-07-30 14:32:55 -070079 self.src_file.write(f'{weak}void {symbol.name}() {{}}\n')
80
81 self.symbol_list.write(f'{symbol.name}\n')
Dan Albert06f58af2020-06-22 15:10:31 -070082
83 if not version_empty and section_versioned:
84 base = '' if version.base is None else ' ' + version.base
85 self.version_script.write('}' + base + ';\n')
86
87
Dan Albertaf7b36d2020-06-23 11:21:21 -070088def parse_args() -> argparse.Namespace:
Dan Albert06f58af2020-06-22 15:10:31 -070089 """Parses and returns command line arguments."""
90 parser = argparse.ArgumentParser()
91
Dan Albertf1d14c72020-07-30 14:32:55 -070092 def resolved_path(raw: str) -> Path:
93 """Returns a resolved Path for the given string."""
94 return Path(raw).resolve()
95
Dan Albert06f58af2020-06-22 15:10:31 -070096 parser.add_argument('-v', '--verbose', action='count', default=0)
97
98 parser.add_argument(
99 '--api', required=True, help='API level being targeted.')
100 parser.add_argument(
101 '--arch', choices=symbolfile.ALL_ARCHITECTURES, required=True,
102 help='Architecture being targeted.')
103 parser.add_argument(
104 '--llndk', action='store_true', help='Use the LLNDK variant.')
105 parser.add_argument(
Dan Albert56f52de2021-06-04 14:31:58 -0700106 '--apex',
107 action='store_true',
Jiyong Park85cc35a2022-07-17 11:30:47 +0900108 help='Use the APEX variant.')
Dan Albert56f52de2021-06-04 14:31:58 -0700109 parser.add_argument(
Jiyong Park85cc35a2022-07-17 11:30:47 +0900110 '--systemapi',
Dan Albert56f52de2021-06-04 14:31:58 -0700111 action='store_true',
Jiyong Park85cc35a2022-07-17 11:30:47 +0900112 dest='systemapi',
113 help='Use the SystemAPI variant.')
Dan Albert06f58af2020-06-22 15:10:31 -0700114
Dan Albertf1d14c72020-07-30 14:32:55 -0700115 parser.add_argument('--api-map',
116 type=resolved_path,
117 required=True,
118 help='Path to the API level map JSON file.')
Dan Albert06f58af2020-06-22 15:10:31 -0700119
Dan Albertf1d14c72020-07-30 14:32:55 -0700120 parser.add_argument('symbol_file',
121 type=resolved_path,
122 help='Path to symbol file.')
123 parser.add_argument('stub_src',
124 type=resolved_path,
125 help='Path to output stub source file.')
126 parser.add_argument('version_script',
127 type=resolved_path,
128 help='Path to output version script.')
129 parser.add_argument('symbol_list',
130 type=resolved_path,
131 help='Path to output abigail symbol list.')
Dan Albert06f58af2020-06-22 15:10:31 -0700132
133 return parser.parse_args()
134
135
Dan Albertaf7b36d2020-06-23 11:21:21 -0700136def main() -> None:
Dan Albert06f58af2020-06-22 15:10:31 -0700137 """Program entry point."""
138 args = parse_args()
139
Dan Albertf1d14c72020-07-30 14:32:55 -0700140 with args.api_map.open() as map_file:
Dan Albert06f58af2020-06-22 15:10:31 -0700141 api_map = json.load(map_file)
142 api = symbolfile.decode_api_level(args.api, api_map)
143
144 verbose_map = (logging.WARNING, logging.INFO, logging.DEBUG)
145 verbosity = args.verbose
146 if verbosity > 2:
147 verbosity = 2
148 logging.basicConfig(level=verbose_map[verbosity])
149
Jiyong Park85cc35a2022-07-17 11:30:47 +0900150 filt = symbolfile.Filter(args.arch, api, args.llndk, args.apex, args.systemapi)
Dan Albertf1d14c72020-07-30 14:32:55 -0700151 with args.symbol_file.open() as symbol_file:
Dan Albert06f58af2020-06-22 15:10:31 -0700152 try:
Jiyong Park3f9c41d2022-07-16 23:30:09 +0900153 versions = symbolfile.SymbolFileParser(symbol_file, api_map, filt).parse()
Dan Albert06f58af2020-06-22 15:10:31 -0700154 except symbolfile.MultiplyDefinedSymbolError as ex:
Dan Albertf1d14c72020-07-30 14:32:55 -0700155 sys.exit(f'{args.symbol_file}: error: {ex}')
Dan Albert06f58af2020-06-22 15:10:31 -0700156
Dan Albertf1d14c72020-07-30 14:32:55 -0700157 with args.stub_src.open('w') as src_file:
158 with args.version_script.open('w') as version_script:
159 with args.symbol_list.open('w') as symbol_list:
160 generator = Generator(src_file, version_script, symbol_list,
Jiyong Park3f9c41d2022-07-16 23:30:09 +0900161 filt)
Dan Albertf1d14c72020-07-30 14:32:55 -0700162 generator.write(versions)
Dan Albert06f58af2020-06-22 15:10:31 -0700163
164
165if __name__ == '__main__':
166 main()