File: module_desc_java.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (105 lines) | stat: -rwxr-xr-x 3,421 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
#
# Copyright 2019 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Writes Java module descriptor to srcjar file."""

import argparse
import os
import sys
import zipfile

sys.path.append(
    os.path.join(
        os.path.dirname(__file__), "..", "..", "..", "build"))
import action_helpers
import zip_helpers

_TEMPLATE = """\
// This file is autogenerated by
//     components/module_installer/android/module_desc_java.py
// Please do not change its content.

package org.chromium.components.module_installer.builder;

import org.chromium.build.annotations.UsedByReflection;

@UsedByReflection("Module.java")
public class ModuleDescriptor_{MODULE} implements ModuleDescriptor {{
    private static final String[] LIBRARIES = {{{LIBRARIES}}};
    private static final String[] PAKS = {{{PAKS}}};

    @UsedByReflection("Module.java")
    public ModuleDescriptor_{MODULE}() {{}}

    @Override
    public String[] getLibraries() {{
        return LIBRARIES;
    }}

    @Override
    public String[] getPaks() {{
        return PAKS;
    }}

    @Override
    public boolean getLoadNativeOnGetImpl() {{
        return {LOAD_NATIVE_ON_GET_IMPL};
    }}
}}
"""


def main():
    parser = argparse.ArgumentParser()
    action_helpers.add_depfile_arg(parser)
    parser.add_argument('--module', required=True, help='The module name.')
    parser.add_argument('--libraries-file',
                        required=True,
                        help='Path to file with GN list of library paths')
    parser.add_argument('--paks', help='GN list of PAK file paths')
    parser.add_argument(
        '--output', required=True, help='Path to the generated srcjar file.')
    parser.add_argument('--load-native-on-get-impl', action='store_true',
        default=False,
        help='Load module automatically on calling Module.getImpl().')
    options = parser.parse_args()
    options.paks = action_helpers.parse_gn_list(options.paks)

    with open(options.libraries_file) as f:
        libraries_list = action_helpers.parse_gn_list(f.read())

    libraries = []
    for path in libraries_list:
        path = path.strip()
        filename = os.path.split(path)[1]
        assert filename.startswith('lib')
        assert filename.endswith('.so')
        # Remove lib prefix and .so suffix.
        libraries += [filename[3:-3]]
    paks = options.paks if options.paks else []

    format_dict = {
        'MODULE': options.module,
        'LIBRARIES': ','.join(['"%s"' % l for l in libraries]),
        'PAKS': ','.join(['"%s"' % os.path.basename(p) for p in paks]),
        'LOAD_NATIVE_ON_GET_IMPL': (
            'true' if options.load_native_on_get_impl else 'false'),
    }
    with action_helpers.atomic_output(options.output) as f:
        with zipfile.ZipFile(f.name, 'w') as srcjar_file:
            zip_path = (f'org/chromium/components/module_installer/builder/'
                        f'ModuleDescriptor_{options.module}.java')
            zip_helpers.add_to_zip_hermetic(
                srcjar_file, zip_path,
                data=_TEMPLATE.format(**format_dict))

    if options.depfile:
        action_helpers.write_depfile(options.depfile,
                                     options.output,
                                     inputs=[options.libraries_file])


if __name__ == '__main__':
    sys.exit(main())