Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame^] | 1 | #!/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.""" |
| 18 | import argparse |
| 19 | import os |
| 20 | import re |
| 21 | |
| 22 | |
| 23 | ALL_ARCHITECTURES = ( |
| 24 | 'arm', |
| 25 | 'arm64', |
| 26 | 'mips', |
| 27 | 'mips64', |
| 28 | 'x86', |
| 29 | 'x86_64', |
| 30 | ) |
| 31 | |
| 32 | |
| 33 | class Scope(object): |
| 34 | """Enum for version script scope. |
| 35 | |
| 36 | Top: Top level of the file. |
| 37 | Global: In a version and visibility section where symbols should be visible |
| 38 | to the NDK. |
| 39 | Local: In a visibility section of a public version where symbols should be |
| 40 | hidden to the NDK. |
| 41 | Private: In a version where symbols should not be visible to the NDK. |
| 42 | """ |
| 43 | Top = 1 |
| 44 | Global = 2 |
| 45 | Local = 3 |
| 46 | Private = 4 |
| 47 | |
| 48 | |
| 49 | class Stack(object): |
| 50 | """Basic stack implementation.""" |
| 51 | def __init__(self): |
| 52 | self.stack = [] |
| 53 | |
| 54 | def push(self, obj): |
| 55 | """Push an item on to the stack.""" |
| 56 | self.stack.append(obj) |
| 57 | |
| 58 | def pop(self): |
| 59 | """Remove and return the item on the top of the stack.""" |
| 60 | return self.stack.pop() |
| 61 | |
| 62 | @property |
| 63 | def top(self): |
| 64 | """Return the top of the stack.""" |
| 65 | return self.stack[-1] |
| 66 | |
| 67 | |
| 68 | def version_is_private(version): |
| 69 | """Returns True if the version name should be treated as private.""" |
| 70 | return version.endswith('_PRIVATE') or version.endswith('_PLATFORM') |
| 71 | |
| 72 | |
| 73 | def enter_version(scope, line, version_file): |
| 74 | """Enters a new version block scope.""" |
| 75 | if scope.top != Scope.Top: |
| 76 | raise RuntimeError('Encountered nested version block.') |
| 77 | |
| 78 | # Entering a new version block. By convention symbols with versions ending |
| 79 | # with "_PRIVATE" or "_PLATFORM" are not included in the NDK. |
| 80 | version_name = line.split('{')[0].strip() |
| 81 | if version_is_private(version_name): |
| 82 | scope.push(Scope.Private) |
| 83 | else: |
| 84 | scope.push(Scope.Global) # By default symbols are visible. |
| 85 | version_file.write(line) |
| 86 | |
| 87 | |
| 88 | def leave_version(scope, line, version_file): |
| 89 | """Leave a version block scope.""" |
| 90 | # There is no close to a visibility section, just the end of the version or |
| 91 | # a new visiblity section. |
| 92 | assert scope.top in (Scope.Global, Scope.Local, Scope.Private) |
| 93 | if scope.top != Scope.Private: |
| 94 | version_file.write(line) |
| 95 | scope.pop() |
| 96 | assert scope.top == Scope.Top |
| 97 | |
| 98 | |
| 99 | def enter_visibility(scope, line, version_file): |
| 100 | """Enters a new visibility block scope.""" |
| 101 | leave_visibility(scope) |
| 102 | version_file.write(line) |
| 103 | visibility = line.split(':')[0].strip() |
| 104 | if visibility == 'local': |
| 105 | scope.push(Scope.Local) |
| 106 | elif visibility == 'global': |
| 107 | scope.push(Scope.Global) |
| 108 | else: |
| 109 | raise RuntimeError('Unknown visiblity label: ' + visibility) |
| 110 | |
| 111 | |
| 112 | def leave_visibility(scope): |
| 113 | """Leaves a visibility block scope.""" |
| 114 | assert scope.top in (Scope.Global, Scope.Local) |
| 115 | scope.pop() |
| 116 | assert scope.top == Scope.Top |
| 117 | |
| 118 | |
| 119 | def handle_top_scope(scope, line, version_file): |
| 120 | """Processes a line in the top level scope.""" |
| 121 | if '{' in line: |
| 122 | enter_version(scope, line, version_file) |
| 123 | else: |
| 124 | raise RuntimeError('Unexpected contents at top level: ' + line) |
| 125 | |
| 126 | |
| 127 | def handle_private_scope(scope, line, version_file): |
| 128 | """Eats all input.""" |
| 129 | if '}' in line: |
| 130 | leave_version(scope, line, version_file) |
| 131 | |
| 132 | |
| 133 | def handle_local_scope(scope, line, version_file): |
| 134 | """Passes through input.""" |
| 135 | if ':' in line: |
| 136 | enter_visibility(scope, line, version_file) |
| 137 | elif '}' in line: |
| 138 | leave_version(scope, line, version_file) |
| 139 | else: |
| 140 | version_file.write(line) |
| 141 | |
| 142 | |
| 143 | def symbol_in_arch(tags, arch): |
| 144 | """Returns true if the symbol is present for the given architecture.""" |
| 145 | has_arch_tags = False |
| 146 | for tag in tags: |
| 147 | if tag == arch: |
| 148 | return True |
| 149 | if tag in ALL_ARCHITECTURES: |
| 150 | has_arch_tags = True |
| 151 | |
| 152 | # If there were no arch tags, the symbol is available for all |
| 153 | # architectures. If there were any arch tags, the symbol is only available |
| 154 | # for the tagged architectures. |
| 155 | return not has_arch_tags |
| 156 | |
| 157 | |
| 158 | def symbol_in_version(tags, arch, version): |
| 159 | """Returns true if the symbol is present for the given version.""" |
| 160 | introduced_tag = None |
| 161 | arch_specific = False |
| 162 | for tag in tags: |
| 163 | # If there is an arch-specific tag, it should override the common one. |
| 164 | if tag.startswith('introduced=') and not arch_specific: |
| 165 | introduced_tag = tag |
| 166 | elif tag.startswith('introduced-' + arch + '='): |
| 167 | introduced_tag = tag |
| 168 | arch_specific = True |
| 169 | |
| 170 | if introduced_tag is None: |
| 171 | # We found no "introduced" tags, so the symbol has always been |
| 172 | # available. |
| 173 | return True |
| 174 | |
| 175 | # The tag is a key=value pair, and we only care about the value now. |
| 176 | _, _, version_str = introduced_tag.partition('=') |
| 177 | return version >= int(version_str) |
| 178 | |
| 179 | |
| 180 | def handle_global_scope(scope, line, src_file, version_file, arch, api): |
| 181 | """Emits present symbols to the version file and stub source file.""" |
| 182 | if ':' in line: |
| 183 | enter_visibility(scope, line, version_file) |
| 184 | return |
| 185 | if '}' in line: |
| 186 | leave_version(scope, line, version_file) |
| 187 | return |
| 188 | |
| 189 | if ';' not in line: |
| 190 | raise RuntimeError('Expected ; to terminate symbol: ' + line) |
| 191 | if '*' in line: |
| 192 | raise RuntimeError('Wildcard global symbols are not permitted.') |
| 193 | |
| 194 | # Line is now in the format "<symbol-name>; # tags" |
| 195 | # Tags are whitespace separated. |
| 196 | symbol_name, _, rest = line.strip().partition(';') |
| 197 | _, _, all_tags = rest.partition('#') |
| 198 | tags = re.split(r'\s+', all_tags) |
| 199 | |
| 200 | if not symbol_in_arch(tags, arch): |
| 201 | return |
| 202 | if not symbol_in_version(tags, arch, api): |
| 203 | return |
| 204 | |
| 205 | if 'var' in tags: |
| 206 | src_file.write('int {} = 0;\n'.format(symbol_name)) |
| 207 | else: |
| 208 | src_file.write('void {}() {{}}\n'.format(symbol_name)) |
| 209 | version_file.write(line) |
| 210 | |
| 211 | |
| 212 | def generate(symbol_file, src_file, version_file, arch, api): |
| 213 | """Generates the stub source file and version script.""" |
| 214 | scope = Stack() |
| 215 | scope.push(Scope.Top) |
| 216 | for line in symbol_file: |
| 217 | if line.strip() == '' or line.strip().startswith('#'): |
| 218 | version_file.write(line) |
| 219 | elif scope.top == Scope.Top: |
| 220 | handle_top_scope(scope, line, version_file) |
| 221 | elif scope.top == Scope.Private: |
| 222 | handle_private_scope(scope, line, version_file) |
| 223 | elif scope.top == Scope.Local: |
| 224 | handle_local_scope(scope, line, version_file) |
| 225 | elif scope.top == Scope.Global: |
| 226 | handle_global_scope(scope, line, src_file, version_file, arch, api) |
| 227 | |
| 228 | |
| 229 | def parse_args(): |
| 230 | """Parses and returns command line arguments.""" |
| 231 | parser = argparse.ArgumentParser() |
| 232 | |
| 233 | parser.add_argument('--api', type=int, help='API level being targeted.') |
| 234 | parser.add_argument( |
| 235 | '--arch', choices=ALL_ARCHITECTURES, |
| 236 | help='Architecture being targeted.') |
| 237 | |
| 238 | parser.add_argument( |
| 239 | 'symbol_file', type=os.path.realpath, help='Path to symbol file.') |
| 240 | parser.add_argument( |
| 241 | 'stub_src', type=os.path.realpath, |
| 242 | help='Path to output stub source file.') |
| 243 | parser.add_argument( |
| 244 | 'version_script', type=os.path.realpath, |
| 245 | help='Path to output version script.') |
| 246 | |
| 247 | return parser.parse_args() |
| 248 | |
| 249 | |
| 250 | def main(): |
| 251 | """Program entry point.""" |
| 252 | args = parse_args() |
| 253 | |
| 254 | with open(args.symbol_file) as symbol_file: |
| 255 | with open(args.stub_src, 'w') as src_file: |
| 256 | with open(args.version_script, 'w') as version_file: |
| 257 | generate(symbol_file, src_file, version_file, args.arch, |
| 258 | args.api) |
| 259 | |
| 260 | |
| 261 | if __name__ == '__main__': |
| 262 | main() |