From 04fe2f9010c2cc58eced407e6eb2124c67a60109 Mon Sep 17 00:00:00 2001 From: Matt Jolly Date: Thu, 6 Feb 2025 11:40:08 +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 | 12 ++++++------ build/rust/rust_bindgen_generator.gni | 22 ++++++++++++++++++---- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/build/config/rust.gni b/build/config/rust.gni index 5b9e3e1e65..1e93be41ff 100644 --- a/build/config/rust.gni +++ b/build/config/rust.gni @@ -64,6 +64,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 5c809c6932..a493daa909 100644 --- a/build/rust/rust_bindgen.gni +++ b/build/rust/rust_bindgen.gni @@ -17,13 +17,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. diff --git a/build/rust/rust_bindgen_generator.gni b/build/rust/rust_bindgen_generator.gni index c91916be93..6afbef2f31 100644 --- a/build/rust/rust_bindgen_generator.gni +++ b/build/rust/rust_bindgen_generator.gni @@ -151,7 +151,7 @@ template("rust_bindgen_generator") { "--output", rebase_path(output_file, root_build_dir), "--libclang-path", - rebase_path(_libclang_path, root_build_dir), + rebase_path(bindgen_libclang_path, root_build_dir), ] if (_wrap_static_fns) { @@ -172,7 +172,7 @@ template("rust_bindgen_generator") { # point to. args += [ "--ld-library-path", - rebase_path(clang_base_path + "/lib", root_build_dir), + rebase_path(bindgen_libclang_path, root_build_dir), ] } @@ -215,9 +215,14 @@ template("rust_bindgen_generator") { # says the wrong thing. We point it to our clang's resource dir which will # 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, + if (clang_base_path != default_clang_base_path && custom_toolchain == "//build/toolchain/linux/unbundle:default") { + clang_resource_dir = + rebase_path(clang_base_path + "/include", root_build_dir) + } else { + clang_resource_dir = + rebase_path(clang_base_path + "/lib/clang/" + clang_version, root_build_dir) + } args += [ "-resource-dir", clang_resource_dir, @@ -238,6 +243,15 @@ template("rust_bindgen_generator") { } } + 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.48.0