rust_bindgen.gni 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Copyright 2022 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. import("//build/config/clang/clang.gni")
  5. import("//build/config/rust.gni")
  6. import("//build/config/sysroot.gni")
  7. import("//build/rust/rust_static_library.gni")
  8. # Template to build Rust/C bindings with bindgen.
  9. #
  10. # This template expands to a static_library containing the Rust side of the
  11. # bindings. Simply treat it as a public dependency.
  12. #
  13. # Parameters:
  14. #
  15. # header:
  16. # The .h file to generate bindings for.
  17. #
  18. # deps: (optional)
  19. # C targets on which the headers depend in order to build successfully.
  20. #
  21. # configs: (optional)
  22. # C compilation targets determine the correct list of -D and -I flags based
  23. # on their dependencies and any configs applied. The same applies here. Set
  24. # any configs here as if this were a C target.
  25. #
  26. # Rust targets depending on the output must include! the generated file.
  27. #
  28. template("rust_bindgen") {
  29. assert(defined(invoker.header),
  30. "Must specify the C header file to make bindings for.")
  31. _target_name = target_name
  32. _testonly = false
  33. if (defined(invoker.testonly)) {
  34. _testonly = invoker.testonly
  35. }
  36. if (defined(invoker.visibility)) {
  37. _visibility = invoker.visibility
  38. }
  39. _deps = []
  40. if (defined(invoker.deps)) {
  41. _deps += invoker.deps
  42. }
  43. action(_target_name) {
  44. testonly = _testonly
  45. if (defined(_visibility)) {
  46. visibility = _visibility
  47. }
  48. sources = [ invoker.header ]
  49. # Several important compiler flags come from default_compiler_configs
  50. configs = default_compiler_configs
  51. if (defined(invoker.configs)) {
  52. configs += invoker.configs
  53. }
  54. bindgen_target = "//third_party/rust/bindgen/v0_59:bindgen($host_toolchain)"
  55. bindgen_obj_dir = get_label_info(bindgen_target, "root_out_dir")
  56. bindgen_executable = "${bindgen_obj_dir}/bindgen"
  57. if (is_win) {
  58. bindgen_executable = "${bindgen_executable}.exe"
  59. }
  60. output_dir = "$target_gen_dir"
  61. out_gen_rs = "$output_dir/${target_name}.rs"
  62. script = rebase_path("//build/rust/run_bindgen.py")
  63. inputs = [ bindgen_executable ]
  64. depfile = "$target_out_dir/${target_name}.d"
  65. outputs = [ out_gen_rs ]
  66. deps = [ bindgen_target ]
  67. # bindgen relies on knowing the {{defines}} and {{include_dirs}} required
  68. # to build the C++ headers which it's parsing. These are passed to the
  69. # script's args and are populated using deps and configs.
  70. deps += _deps
  71. args = [
  72. "--exe",
  73. rebase_path(bindgen_executable),
  74. "--header",
  75. rebase_path(invoker.header, root_build_dir),
  76. "--depfile",
  77. rebase_path(depfile, root_build_dir),
  78. "--output",
  79. rebase_path(out_gen_rs, root_build_dir),
  80. "--ld-library-path",
  81. rebase_path(clang_base_path + "/lib", root_build_dir),
  82. ]
  83. args += [
  84. "--",
  85. "{{defines}}",
  86. "{{include_dirs}}",
  87. "{{cflags}}",
  88. "{{cflags_c}}",
  89. # This path contains important C headers (e.g. stddef.h) and {{cflags}}
  90. # does not include it. Normally this path is implicitly added by clang but
  91. # it does not happen for libclang.
  92. #
  93. # Add it last so includes from deps and configs take precedence.
  94. "-isystem" + rebase_path(
  95. clang_base_path + "/lib/clang/" + clang_version + "/include",
  96. root_build_dir),
  97. # Passes C comments through as rustdoc attributes.
  98. "-fparse-all-comments",
  99. # Default configs include "-fvisibility=hidden", and for some reason this
  100. # causes bindgen not to emit function bindings. Override it.
  101. "-fvisibility=default",
  102. ]
  103. }
  104. }