aboutsummaryrefslogtreecommitdiff
blob: e981df86d50bbfa67611c647fbacb96f6716d269 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
From 6df5a080f58ddb6a49a9d33e4a3619a34fffa78c Mon Sep 17 00:00:00 2001
From: Matt Jolly <Matt.Jolly@footclan.ninja>
Date: Fri, 5 Jul 2024 20:49:01 +1000
Subject: [PATCH] Make bindgen wrapper work with unbundled toolchain

The `run_bindgen.py` wrapper takes a --libclang-path option
and uses it to set the appropriate environment variable.

This is currently hardcoded to use libclang shipped alongside
bindgen (in our rust toolchain), but distributions may want to
override this and use a system path.

Additionally enable distros to feed in appropriate library paths.
---
 build/config/rust.gni       | 11 +++++++++++
 build/rust/rust_bindgen.gni | 28 ++++++++++++++++++----------
 2 files changed, 29 insertions(+), 10 deletions(-)

diff --git a/build/config/rust.gni b/build/config/rust.gni
index 97e788a227..78b9daa7e8 100644
--- a/build/config/rust.gni
+++ b/build/config/rust.gni
@@ -60,6 +60,17 @@ declare_args() {
   # the bindgen exectuable).
   rust_bindgen_root = "//third_party/rust-toolchain"
 
+  # Directory under which to find one of `libclang.{dll,so}` (a `lib[64]` or
+  # `bin` directory containing the libclang shared library).
+  # We don't need to worry about multlib, but specify the full path here
+  # in case a distribution does.
+  if (host_os == "win") {
+    bindgen_libclang_path = "//third_party/rust-toolchain/bin"
+  } else {
+    bindgen_libclang_path = "//third_party/rust-toolchain/lib"
+  }
+
+
   # If you're using a Rust toolchain as specified by rust_sysroot_absolute,
   # set this to the output of `rustc -V`. Changing this string will cause all
   # Rust targets to be rebuilt, which allows you to update your toolchain and
diff --git a/build/rust/rust_bindgen.gni b/build/rust/rust_bindgen.gni
index bf110ca93c..d7eb04eb00 100644
--- a/build/rust/rust_bindgen.gni
+++ b/build/rust/rust_bindgen.gni
@@ -16,13 +16,13 @@ if (host_os == "win") {
   _bindgen_path = "${_bindgen_path}.exe"
 }
 
-# On Windows, the libclang.dll is beside the bindgen.exe, otherwise it is in
-# ../lib.
-_libclang_path = rust_bindgen_root
-if (host_os == "win") {
-  _libclang_path += "/bin"
+if (clang_base_path != default_clang_base_path && custom_toolchain == "//build/toolchain/linux/unbundle:default") {
+  # Assume that the user has set this up properly, including handling multilib
+  _clang_libpath = clang_base_path + "/include"
+  _clang_ld_libpath  = bindgen_libclang_path
 } else {
-  _libclang_path += "/lib"
+  _clang_libpath = clang_base_path + "/lib/clang/" + clang_version
+  _clang_ld_libpath = clang_base_path + "/lib"
 }
 
 # Template to build Rust/C bindings with bindgen.
@@ -100,7 +100,7 @@ template("rust_bindgen") {
       "--output",
       rebase_path(out_gen_rs, root_build_dir),
       "--libclang-path",
-      rebase_path(_libclang_path, root_build_dir),
+      rebase_path(bindgen_libclang_path, root_build_dir),
     ]
 
     if (wrap_static_fns) {
@@ -117,7 +117,7 @@ template("rust_bindgen") {
       # point to.
       args += [
         "--ld-library-path",
-        rebase_path(clang_base_path + "/lib", root_build_dir),
+        rebase_path(_clang_ld_libpath, root_build_dir),
       ]
     }
 
@@ -145,8 +145,7 @@ template("rust_bindgen") {
     # make it behave consistently with our other command line flags and allows
     # system headers to be found.
     clang_resource_dir =
-        rebase_path(clang_base_path + "/lib/clang/" + clang_version,
-                    root_build_dir)
+        rebase_path(_clang_libpath, root_build_dir)
     args += [
       "-resource-dir",
       clang_resource_dir,
@@ -167,6 +166,15 @@ template("rust_bindgen") {
       }
     }
 
+    if (custom_toolchain == "//build/toolchain/linux/unbundle:default") {
+      # We need to pass the path to the libstdc++ headers to bindgen so that it
+      # can find them when parsing C++ headers.
+      args += [
+        "-I",
+        rebase_path(clang_base_path + "/include/", root_build_dir),
+      ]
+    }
+
     if (is_win) {
       # On Windows we fall back to using system headers from a sysroot from
       # depot_tools. This is negotiated by python scripts and the result is
-- 
2.45.2