From 84b3b8335f7efbb8ed5ab9c9a260ea4f5d77192b Mon Sep 17 00:00:00 2001 From: Matt Jolly Date: Mon, 11 Nov 2024 20:52:48 +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. --- 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 --- 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. --- 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,7 @@ 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, - root_build_dir) + clang_resource_dir = rebase_path(clang_base_path + "/include", root_build_dir) args += [ "-resource-dir", clang_resource_dir, @@ -238,6 +236,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.47.0