Collin Baker | ca981904 | 2022-04-07 21:42:23 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
Avi Drissman | 73a09d1 | 2022-09-08 20:33:38 | [diff] [blame^] | 3 | # Copyright 2022 The Chromium Authors |
Collin Baker | ca981904 | 2022-04-07 21:42:23 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | import argparse |
| 8 | import os |
| 9 | import subprocess |
| 10 | import sys |
| 11 | |
| 12 | # Set up path to be able to import build_utils. |
| 13 | sys.path.append( |
| 14 | os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir, |
| 15 | os.pardir, 'build', 'android', 'gyp')) |
| 16 | from util import build_utils |
| 17 | |
| 18 | |
| 19 | def atomic_copy(in_path, out_path): |
| 20 | with open(in_path, 'rb') as input: |
| 21 | with build_utils.AtomicOutput(out_path, only_if_changed=True) as output: |
| 22 | content = input.read() |
| 23 | output.write(content) |
| 24 | |
| 25 | |
| 26 | def copy_to_prefixed_filename(path, filename, prefix): |
| 27 | atomic_copy(os.path.join(path, filename), |
| 28 | os.path.join(path, prefix + "_" + filename)) |
| 29 | |
| 30 | |
| 31 | def filter_clang_args(clangargs): |
| 32 | def do_filter(args): |
| 33 | i = 0 |
| 34 | while i < len(args): |
| 35 | # Intercept plugin arguments |
| 36 | if args[i] == '-Xclang': |
| 37 | i += 1 |
| 38 | if args[i] == '-add-plugin': |
| 39 | pass |
| 40 | elif args[i].startswith('-plugin-arg'): |
| 41 | i += 2 |
| 42 | else: |
| 43 | yield args[i] |
| 44 | i += 1 |
| 45 | |
| 46 | return list(do_filter(clangargs)) |
| 47 | |
| 48 | |
| 49 | def main(): |
| 50 | parser = argparse.ArgumentParser("run_bindgen.py") |
| 51 | parser.add_argument("--exe", help="Path to bindgen", required=True), |
| 52 | parser.add_argument("--header", |
| 53 | help="C header file to generate bindings for", |
| 54 | required=True) |
| 55 | parser.add_argument("--depfile", |
| 56 | help="depfile to output with header dependencies") |
| 57 | parser.add_argument("--output", help="output .rs bindings", required=True) |
| 58 | parser.add_argument("--ld-library-path", help="LD_LIBRARY_PATH to set") |
| 59 | parser.add_argument("-I", "--include", help="include path", action="append") |
| 60 | parser.add_argument( |
| 61 | "clangargs", |
| 62 | metavar="CLANGARGS", |
| 63 | help="arguments to pass to libclang (see " |
| 64 | "https://docs.rs/bindgen/latest/bindgen/struct.Builder.html#method.clang_args)", |
| 65 | nargs="*") |
| 66 | args = parser.parse_args() |
| 67 | genargs = [] |
| 68 | |
| 69 | # Bindgen settings we use for Chromium |
| 70 | genargs.append('--no-layout-tests') |
| 71 | genargs.append('--size_t-is-usize') |
| 72 | genargs += ['--rust-target', 'nightly'] |
| 73 | |
| 74 | if args.depfile: |
| 75 | genargs.append('--depfile') |
| 76 | genargs.append(args.depfile) |
| 77 | genargs.append('--output') |
| 78 | genargs.append(args.output) |
| 79 | genargs.append(args.header) |
| 80 | genargs.append('--') |
| 81 | genargs.extend(filter_clang_args(args.clangargs)) |
| 82 | env = os.environ |
| 83 | if args.ld_library_path: |
| 84 | env["LD_LIBRARY_PATH"] = args.ld_library_path |
Collin Baker | d27f0bd | 2022-04-21 14:06:47 | [diff] [blame] | 85 | returncode = subprocess.run([args.exe, *genargs], env=env).returncode |
| 86 | if returncode != 0: |
Collin Baker | ca981904 | 2022-04-07 21:42:23 | [diff] [blame] | 87 | # Make sure we don't emit anything if bindgen failed. |
| 88 | try: |
| 89 | os.remove(args.output) |
| 90 | except FileNotFoundError: |
| 91 | pass |
| 92 | try: |
| 93 | os.remove(args.depfile) |
| 94 | except FileNotFoundError: |
| 95 | pass |
Collin Baker | d27f0bd | 2022-04-21 14:06:47 | [diff] [blame] | 96 | return returncode |
Collin Baker | ca981904 | 2022-04-07 21:42:23 | [diff] [blame] | 97 | |
| 98 | |
| 99 | if __name__ == '__main__': |
| 100 | sys.exit(main()) |