tor-browser

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

write_native_libraries_java.py (4675B)


      1 #!/usr/bin/env python3
      2 #
      3 # Copyright 2019 The Chromium Authors
      4 # Use of this source code is governed by a BSD-style license that can be
      5 # found in the LICENSE file.
      6 
      7 """Writes list of native libraries to srcjar file."""
      8 
      9 import argparse
     10 import os
     11 import re
     12 import sys
     13 import zipfile
     14 
     15 from util import build_utils
     16 import action_helpers  # build_utils adds //build to sys.path.
     17 import zip_helpers
     18 
     19 
     20 _NATIVE_LIBRARIES_TEMPLATE = """\
     21 // This file is autogenerated by
     22 //     build/android/gyp/write_native_libraries_java.py
     23 // Please do not change its content.
     24 
     25 package org.chromium.build;
     26 
     27 public class NativeLibraries {{
     28    public static final int CPU_FAMILY_UNKNOWN = 0;
     29    public static final int CPU_FAMILY_ARM = 1;
     30    public static final int CPU_FAMILY_MIPS = 2;
     31    public static final int CPU_FAMILY_X86 = 3;
     32    public static final int CPU_FAMILY_RISCV = 4;
     33 
     34    // Set to true to enable the use of the Chromium Linker.
     35    public static {MAYBE_FINAL}boolean sUseLinker{USE_LINKER};
     36 
     37    // This is the list of native libraries to be loaded (in the correct order)
     38    // by LibraryLoader.java.
     39    public static {MAYBE_FINAL}String[] LIBRARIES = {{{LIBRARIES}}};
     40 
     41    public static {MAYBE_FINAL}int sCpuFamily = {CPU_FAMILY};
     42 
     43    public static {MAYBE_FINAL}boolean sSupport32Bit{SUPPORT_32_BIT};
     44 
     45    public static {MAYBE_FINAL}boolean sSupport64Bit{SUPPORT_64_BIT};
     46 }}
     47 """
     48 
     49 
     50 def _FormatLibraryName(library_name):
     51  filename = os.path.split(library_name)[1]
     52  assert filename.startswith('lib') and filename.endswith('.so'), filename
     53  # Remove lib prefix and .so suffix.
     54  return '"%s"' % filename[3:-3]
     55 
     56 
     57 def main():
     58  parser = argparse.ArgumentParser()
     59 
     60  action_helpers.add_depfile_arg(parser)
     61  parser.add_argument('--final', action='store_true', help='Use final fields.')
     62  parser.add_argument(
     63      '--enable-chromium-linker',
     64      action='store_true',
     65      help='Enable Chromium linker.')
     66  parser.add_argument(
     67      '--native-libraries-list', help='File with list of native libraries.')
     68  parser.add_argument('--cpu-family',
     69                      choices={
     70                          'CPU_FAMILY_ARM', 'CPU_FAMILY_X86', 'CPU_FAMILY_MIPS',
     71                          'CPU_FAMILY_RISCV', 'CPU_FAMILY_UNKNOWN'
     72                      },
     73                      required=True,
     74                      default='CPU_FAMILY_UNKNOWN',
     75                      help='CPU family.')
     76 
     77  parser.add_argument(
     78      '--output', required=True, help='Path to the generated srcjar file.')
     79  parser.add_argument('--native-lib-32-bit',
     80                      action='store_true',
     81                      help='32-bit binaries.')
     82  parser.add_argument('--native-lib-64-bit',
     83                      action='store_true',
     84                      help='64-bit binaries.')
     85 
     86  options = parser.parse_args(build_utils.ExpandFileArgs(sys.argv[1:]))
     87 
     88  native_libraries = []
     89  if options.native_libraries_list:
     90    with open(options.native_libraries_list) as f:
     91      native_libraries.extend(l.strip() for l in f)
     92 
     93  if options.enable_chromium_linker and len(native_libraries) > 1:
     94    sys.stderr.write(
     95        'Multiple libraries not supported when using chromium linker. Found:\n')
     96    sys.stderr.write('\n'.join(native_libraries))
     97    sys.stderr.write('\n')
     98    sys.exit(1)
     99 
    100  # When building for robolectric in component buildS, OS=linux causes
    101  # "libmirprotobuf.so.9" to be a dep. This script, as well as
    102  # System.loadLibrary("name") require libraries to end with ".so", so just
    103  # filter it out.
    104  native_libraries = [
    105      f for f in native_libraries if not re.search(r'\.so\.\d+$', f)
    106  ]
    107 
    108  def bool_str(value):
    109    if value:
    110      return ' = true'
    111    if options.final:
    112      return ' = false'
    113    return ''
    114 
    115  format_dict = {
    116      'MAYBE_FINAL': 'final ' if options.final else '',
    117      'USE_LINKER': bool_str(options.enable_chromium_linker),
    118      'LIBRARIES': ','.join(_FormatLibraryName(n) for n in native_libraries),
    119      'CPU_FAMILY': options.cpu_family,
    120      'SUPPORT_32_BIT': bool_str(options.native_lib_32_bit),
    121      'SUPPORT_64_BIT': bool_str(options.native_lib_64_bit),
    122  }
    123  with action_helpers.atomic_output(options.output) as f:
    124    with zipfile.ZipFile(f.name, 'w') as srcjar_file:
    125      zip_helpers.add_to_zip_hermetic(
    126          zip_file=srcjar_file,
    127          zip_path='org/chromium/build/NativeLibraries.java',
    128          data=_NATIVE_LIBRARIES_TEMPLATE.format(**format_dict))
    129 
    130  if options.depfile:
    131    assert options.native_libraries_list
    132    action_helpers.write_depfile(options.depfile,
    133                                 options.output,
    134                                 inputs=[options.native_libraries_list])
    135 
    136 
    137 if __name__ == '__main__':
    138  sys.exit(main())