• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
9import argparse
10import os
11import sys
12import zipfile
13
14from util import build_utils
15import action_helpers  # build_utils adds //build to sys.path.
16import zip_helpers
17
18
19_NATIVE_LIBRARIES_TEMPLATE = """\
20// This file is autogenerated by
21//     build/android/gyp/write_native_libraries_java.py
22// Please do not change its content.
23
24package org.chromium.build;
25
26public class NativeLibraries {{
27    public static final int CPU_FAMILY_UNKNOWN = 0;
28    public static final int CPU_FAMILY_ARM = 1;
29    public static final int CPU_FAMILY_MIPS = 2;
30    public static final int CPU_FAMILY_X86 = 3;
31    public static final int CPU_FAMILY_RISCV = 4;
32
33    // Set to true to enable the use of the Chromium Linker.
34    public static {MAYBE_FINAL}boolean sUseLinker{USE_LINKER};
35
36    // This is the list of native libraries to be loaded (in the correct order)
37    // by LibraryLoader.java.
38    public static {MAYBE_FINAL}String[] LIBRARIES = {{{LIBRARIES}}};
39
40    public static {MAYBE_FINAL}int sCpuFamily = {CPU_FAMILY};
41}}
42"""
43
44
45def _FormatLibraryName(library_name):
46  filename = os.path.split(library_name)[1]
47  assert filename.startswith('lib')
48  assert filename.endswith('.so')
49  # Remove lib prefix and .so suffix.
50  return '"%s"' % filename[3:-3]
51
52
53def main():
54  parser = argparse.ArgumentParser()
55
56  action_helpers.add_depfile_arg(parser)
57  parser.add_argument('--final', action='store_true', help='Use final fields.')
58  parser.add_argument(
59      '--enable-chromium-linker',
60      action='store_true',
61      help='Enable Chromium linker.')
62  parser.add_argument(
63      '--native-libraries-list', help='File with list of native libraries.')
64  parser.add_argument('--cpu-family',
65                      choices={
66                          'CPU_FAMILY_ARM', 'CPU_FAMILY_X86', 'CPU_FAMILY_MIPS',
67                          'CPU_FAMILY_RISCV', 'CPU_FAMILY_UNKNOWN'
68                      },
69                      required=True,
70                      default='CPU_FAMILY_UNKNOWN',
71                      help='CPU family.')
72  parser.add_argument(
73      '--main-component-library',
74      help='If used, the list of native libraries will only contain this '
75      'library. Dependencies are found in the library\'s "NEEDED" section.')
76
77  parser.add_argument(
78      '--output', required=True, help='Path to the generated srcjar file.')
79
80  options = parser.parse_args(build_utils.ExpandFileArgs(sys.argv[1:]))
81
82  native_libraries = []
83  if options.main_component_library:
84    native_libraries.append(options.main_component_library)
85  elif options.native_libraries_list:
86    with open(options.native_libraries_list) as f:
87      native_libraries.extend(l.strip() for l in f)
88
89  if options.enable_chromium_linker and len(native_libraries) > 1:
90    sys.stderr.write(
91        'Multiple libraries not supported when using chromium linker. Found:\n')
92    sys.stderr.write('\n'.join(native_libraries))
93    sys.stderr.write('\n')
94    sys.exit(1)
95
96  def bool_str(value):
97    if value:
98      return ' = true'
99    if options.final:
100      return ' = false'
101    return ''
102
103  format_dict = {
104      'MAYBE_FINAL': 'final ' if options.final else '',
105      'USE_LINKER': bool_str(options.enable_chromium_linker),
106      'LIBRARIES': ','.join(_FormatLibraryName(n) for n in native_libraries),
107      'CPU_FAMILY': options.cpu_family,
108  }
109  with action_helpers.atomic_output(options.output) as f:
110    with zipfile.ZipFile(f.name, 'w') as srcjar_file:
111      zip_helpers.add_to_zip_hermetic(
112          zip_file=srcjar_file,
113          zip_path='org/chromium/build/NativeLibraries.java',
114          data=_NATIVE_LIBRARIES_TEMPLATE.format(**format_dict))
115
116  if options.depfile:
117    assert options.native_libraries_list
118    action_helpers.write_depfile(options.depfile,
119                                 options.output,
120                                 inputs=[options.native_libraries_list])
121
122
123if __name__ == '__main__':
124  sys.exit(main())
125