1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2023 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 subprocess
20
21sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir))
22from scripts.util import build_utils  # noqa: E402
23
24
25def parse_args(args):
26    args = build_utils.expand_file_args(args)
27
28    parser = optparse.OptionParser()
29    build_utils.add_depfile_option(parser)
30    parser.add_option('--output', help='generated ndk stub file')
31    parser.add_option('--headers',
32                      action='append',
33                      help='base directory of ndk common files')
34
35    options, _ = parser.parse_args(args)
36
37    return options
38
39
40def check_ndk_header(headers: list, output: str):
41    cmd_list = []
42    cmd_list.append('clang')
43    cmd_list.append('-I sdk-native/os-irrelevant/sysroot/')
44    cmd_list.append('-std=c99')
45    for file in headers:
46        command = cmd_list + [file]
47        result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
48        if result.returncode != 0:
49            with open(r'Error.txt', 'a', encoding='utf-8') as fs:
50                fs.write(f'Error: {result.stderr.decode()}')
51
52    build_utils.touch(output)
53
54
55def main(args):
56    options = parse_args(args)
57    build_utils.call_and_write_depfile_if_stale(lambda: check_ndk_header(options.headers, options.output),
58                                                options,
59                                                output_paths=([options.output]),
60                                                input_strings=args,
61                                                force=False,
62                                                add_pydeps=False)
63
64if __name__ == '__main__':
65    sys.exit(main(sys.argv[1:]))
66