• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
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 argparse
17import os
18import subprocess
19import sys
20
21
22def remove_args_of_clang(ohos_clangargs):
23    def filter_args(args):
24        for i, j in enumerate(args):
25            if args[i] == '-Xclang':
26                i += 1
27                if args[i].startswith('-plugin-arg'):
28                    i += 2
29                elif args[i] == '-add-plugin':
30                    pass
31            else:
32                yield args[i]
33    return list(filter_args(ohos_clangargs))
34
35
36def main():
37    parser = argparse.ArgumentParser("rust_bindgen.py")
38    parser.add_argument("--exe", help="Path to bindgen", required=True)
39    parser.add_argument("--llvm-config-path", help="Path to bindgen", required=True)
40    parser.add_argument("--clang-path", help="Path to bindgen", required=True)
41    parser.add_argument("--output", help="output .rs bindings", required=True)
42    parser.add_argument("--ld-library-path", help="LD_LIBRARY_PATH to set")
43    parser.add_argument("--header",
44                        help="C header file to generate bindings for",
45                        required=True)
46    parser.add_argument("--depfile",
47                        help="depfile to output with header dependencies")
48    parser.add_argument("-I", "--include", help="include path", action="append")
49    parser.add_argument(
50        "ohos_clangargs",
51        metavar="CLANGARGS",
52        help="arguments to pass to libclang (see "
53        "https://docs.rs/bindgen/latest/bindgen/struct.Builder.html#method.clang_args)",
54        nargs="*")
55    args = parser.parse_args()
56    ohos_genargs = []
57    ohos_genargs.append('--no-layout-tests')
58    ohos_genargs.append('--size_t-is-usize')
59    ohos_genargs += ['--rust-target', 'nightly']
60    if args.depfile:
61        ohos_genargs.append('--depfile')
62        ohos_genargs.append(args.depfile)
63    ohos_genargs.append('--output')
64    ohos_genargs.append(args.output)
65    ohos_genargs.append(args.header)
66    ohos_genargs.append('--')
67    ohos_genargs.extend(remove_args_of_clang(args.ohos_clangargs))
68    env = os.environ
69    if args.ld_library_path:
70        env["LD_LIBRARY_PATH"] = args.ld_library_path
71    env["LLVM_CONFIG_PATH"] = args.llvm_config_path
72    env["CLANG_PATH"] = args.clang_path
73    rescode = subprocess.run([args.exe, *ohos_genargs], env=env).returncode
74    if rescode != 0:
75        if os.path.exists(args.depfile):
76            os.remove(args.depfile)
77        if os.path.exists(args.output):
78            os.remove(args.output)
79    return rescode
80
81
82if __name__ == '__main__':
83    sys.exit(main())