tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

generate_linker_version_script.py (3254B)


      1 #!/usr/bin/env python3
      2 # Copyright 2018 The Chromium Authors
      3 # Use of this source code is governed by a BSD-style license that can be
      4 # found in the LICENSE file.
      5 """Generate linker version scripts for Chrome on Android shared libraries."""
      6 
      7 import argparse
      8 import os
      9 
     10 from util import build_utils
     11 import action_helpers  # build_utils adds //build to sys.path.
     12 
     13 _SCRIPT_HEADER = """\
     14 # AUTO-GENERATED FILE.  DO NOT MODIFY.
     15 #
     16 # See: %s
     17 
     18 {
     19  global:
     20 """ % os.path.relpath(__file__, build_utils.DIR_SOURCE_ROOT)
     21 
     22 _SCRIPT_FOOTER = """\
     23  local:
     24    *;
     25 };
     26 """
     27 
     28 
     29 def main():
     30  parser = argparse.ArgumentParser()
     31  parser.add_argument(
     32      '--output',
     33      required=True,
     34      help='Path to output linker version script file.')
     35  parser.add_argument(
     36      '--jni-multiplexing',
     37      action='store_true',
     38      help='Export only the JNI methods generated by multiplexing')
     39  parser.add_argument('--export-fortesting-java-symbols',
     40                      action='store_true',
     41                      help='Export Java_*_ForTesting JNI methods')
     42  parser.add_argument(
     43      '--export-symbol-allowlist-file',
     44      action='append',
     45      default=[],
     46      dest='allowlists',
     47      help='Path to an input file containing an allowlist of extra symbols to '
     48      'export, one symbol per line. Multiple files may be specified.')
     49  parser.add_argument(
     50      '--export-feature-registrations',
     51      action='store_true',
     52      help='Export JNI_OnLoad_* methods')
     53  options = parser.parse_args()
     54 
     55  # JNI_OnLoad is always exported.
     56  # CrashpadHandlerMain() is the entry point to the Crashpad handler, required
     57  # for libcrashpad_handler_trampoline.so.
     58  symbol_list = ['CrashpadHandlerMain', 'JNI_OnLoad']
     59 
     60  if options.export_fortesting_java_symbols or options.jni_multiplexing:
     61    symbol_list.append('Java_*')
     62  else:
     63    # The linker uses unix shell globbing patterns, not regex. So, we have to
     64    # include everything that doesn't end in "ForTest(ing)" with this set of
     65    # globs.
     66    symbol_list.append('Java_*[!F]orTesting')
     67    symbol_list.append('Java_*[!o]rTesting')
     68    symbol_list.append('Java_*[!r]Testing')
     69    symbol_list.append('Java_*[!T]esting')
     70    symbol_list.append('Java_*[!e]sting')
     71    symbol_list.append('Java_*[!s]ting')
     72    symbol_list.append('Java_*[!t]ing')
     73    symbol_list.append('Java_*[!i]ng')
     74    symbol_list.append('Java_*[!n]g')
     75    symbol_list.append('Java_*[!F]orTest')
     76    symbol_list.append('Java_*[!o]rTest')
     77    symbol_list.append('Java_*[!r]Test')
     78    symbol_list.append('Java_*[!T]est')
     79    symbol_list.append('Java_*[!e]st')
     80    symbol_list.append('Java_*[!s]t')
     81    symbol_list.append('Java_*[!gt]')
     82 
     83  if options.export_feature_registrations:
     84    symbol_list.append('JNI_OnLoad_*')
     85 
     86  for allowlist in options.allowlists:
     87    with open(allowlist, 'rt') as f:
     88      for line in f:
     89        line = line.strip()
     90        if not line or line[0] == '#':
     91          continue
     92        symbol_list.append(line)
     93 
     94  script_content = [_SCRIPT_HEADER]
     95  for symbol in symbol_list:
     96    script_content.append('    %s;\n' % symbol)
     97  script_content.append(_SCRIPT_FOOTER)
     98 
     99  script = ''.join(script_content)
    100 
    101  with action_helpers.atomic_output(options.output, mode='w') as f:
    102    f.write(script)
    103 
    104 
    105 if __name__ == '__main__':
    106  main()