• 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
32    // Set to true to enable the use of the Chromium Linker.
33    public static {MAYBE_FINAL}boolean sUseLinker{USE_LINKER};
34
35    // This is the list of native libraries to be loaded (in the correct order)
36    // by LibraryLoader.java.
37    public static {MAYBE_FINAL}String[] LIBRARIES = {{{LIBRARIES}}};
38
39    public static {MAYBE_FINAL}int sCpuFamily = {CPU_FAMILY};
40}}
41"""
42
43
44def _FormatLibraryName(library_name):
45  filename = os.path.split(library_name)[1]
46  assert filename.startswith('lib')
47  assert filename.endswith('.so')
48  # Remove lib prefix and .so suffix.
49  return '"%s"' % filename[3:-3]
50
51
52def main():
53  parser = argparse.ArgumentParser()
54
55  action_helpers.add_depfile_arg(parser)
56  parser.add_argument('--final', action='store_true', help='Use final fields.')
57  parser.add_argument(
58      '--enable-chromium-linker',
59      action='store_true',
60      help='Enable Chromium linker.')
61  parser.add_argument(
62      '--native-libraries-list', help='File with list of native libraries.')
63  parser.add_argument(
64      '--cpu-family',
65      choices={
66          'CPU_FAMILY_ARM', 'CPU_FAMILY_X86', 'CPU_FAMILY_MIPS',
67          '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