module_desc_java.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2019 The Chromium Authors. All rights reserved.
  4. # Use of this source code is governed by a BSD-style license that can be
  5. # found in the LICENSE file.
  6. """Writes Java module descriptor to srcjar file."""
  7. import argparse
  8. import os
  9. import sys
  10. import zipfile
  11. sys.path.append(
  12. os.path.join(
  13. os.path.dirname(__file__), "..", "..", "..", "build", "android", "gyp"))
  14. from util import build_utils
  15. _TEMPLATE = """\
  16. // This file is autogenerated by
  17. // components/module_installer/android/module_desc_java.py
  18. // Please do not change its content.
  19. package org.chromium.components.module_installer.builder;
  20. import org.chromium.base.annotations.UsedByReflection;
  21. @UsedByReflection("Module.java")
  22. public class ModuleDescriptor_{MODULE} implements ModuleDescriptor {{
  23. private static final String[] LIBRARIES = {{{LIBRARIES}}};
  24. private static final String[] PAKS = {{{PAKS}}};
  25. @Override
  26. public String[] getLibraries() {{
  27. return LIBRARIES;
  28. }}
  29. @Override
  30. public String[] getPaks() {{
  31. return PAKS;
  32. }}
  33. @Override
  34. public boolean getLoadNativeOnGetImpl() {{
  35. return {LOAD_NATIVE_ON_GET_IMPL};
  36. }}
  37. }}
  38. """
  39. def main():
  40. parser = argparse.ArgumentParser()
  41. build_utils.AddDepfileOption(parser)
  42. parser.add_argument('--module', required=True, help='The module name.')
  43. parser.add_argument('--libraries-file',
  44. required=True,
  45. help='Path to file with GN list of library paths')
  46. parser.add_argument('--paks', help='GN list of PAK file paths')
  47. parser.add_argument(
  48. '--output', required=True, help='Path to the generated srcjar file.')
  49. parser.add_argument('--load-native-on-get-impl', action='store_true',
  50. default=False,
  51. help='Load module automatically on calling Module.getImpl().')
  52. options = parser.parse_args()
  53. options.paks = build_utils.ParseGnList(options.paks)
  54. with open(options.libraries_file) as f:
  55. libraries_list = build_utils.ParseGnList(f.read())
  56. libraries = []
  57. for path in libraries_list:
  58. path = path.strip()
  59. filename = os.path.split(path)[1]
  60. assert filename.startswith('lib')
  61. assert filename.endswith('.so')
  62. # Remove lib prefix and .so suffix.
  63. libraries += [filename[3:-3]]
  64. paks = options.paks if options.paks else []
  65. format_dict = {
  66. 'MODULE': options.module,
  67. 'LIBRARIES': ','.join(['"%s"' % l for l in libraries]),
  68. 'PAKS': ','.join(['"%s"' % os.path.basename(p) for p in paks]),
  69. 'LOAD_NATIVE_ON_GET_IMPL': (
  70. 'true' if options.load_native_on_get_impl else 'false'),
  71. }
  72. with build_utils.AtomicOutput(options.output) as f:
  73. with zipfile.ZipFile(f.name, 'w') as srcjar_file:
  74. build_utils.AddToZipHermetic(
  75. srcjar_file,
  76. 'org/chromium/components/module_installer/builder/'
  77. 'ModuleDescriptor_%s.java' % options.module,
  78. data=_TEMPLATE.format(**format_dict))
  79. if options.depfile:
  80. build_utils.WriteDepfile(options.depfile,
  81. options.output,
  82. inputs=[options.libraries_file])
  83. if __name__ == '__main__':
  84. sys.exit(main())