1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2021 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import optparse
17import os
18import sys
19import json
20
21sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir))
22from scripts.util import build_utils  # noqa: E402
23
24STUB_FUNCTION_TEMPLATE = '''
25void {}() {{  }}
26'''
27
28STUB_VARIABLE_TEMPLATE = '''
29int {} = 0;
30'''
31
32
33def parse_args(args):
34    args = build_utils.expand_file_args(args)
35
36    parser = optparse.OptionParser()
37    build_utils.add_depfile_option(parser)
38    parser.add_option('--output', help='generated ndk stub file')
39    parser.add_option('--ndk-description-file', help='ndk description file')
40
41    options, _ = parser.parse_args(args)
42    return options
43
44
45def generate_stub_file(options):
46    contents = []
47    with open(options.ndk_description_file, 'r') as f:
48        interfaces = json.load(f)
49        for inf in interfaces:
50            name = inf.get('name')
51            if inf.get('type') == 'variable':
52                contents.append(STUB_VARIABLE_TEMPLATE.format(name))
53            else:
54                contents.append(STUB_FUNCTION_TEMPLATE.format(name))
55    with open(options.output, 'w') as f:
56        f.write('\n'.join(contents))
57
58
59def main(args):
60    options = parse_args(args)
61
62    depfile_deps = ([options.ndk_description_file])
63
64    build_utils.call_and_write_depfile_if_stale(
65        lambda: generate_stub_file(options),
66        options,
67        depfile_deps=depfile_deps,
68        input_paths=depfile_deps,
69        output_paths=([options.output]),
70        force=False,
71        add_pydeps=False)
72
73
74if __name__ == '__main__':
75    sys.exit(main(sys.argv[1:]))
76