From bedabfbcef9285804729aa78f933d8ceb4897ecf Mon Sep 17 00:00:00 2001 From: Guillaume Girol Date: Sun, 13 Feb 2022 12:00:00 +0000 Subject: [PATCH] rustPlatform.bindgenHook: init --- doc/languages-frameworks/rust.section.md | 2 ++ pkgs/build-support/rust/hooks/default.nix | 10 ++++++++++ pkgs/build-support/rust/hooks/rust-bindgen-hook.sh | 13 +++++++++++++ .../compilers/rust/make-rust-platform.nix | 2 +- pkgs/development/tools/rust/bindgen/default.nix | 4 +++- 5 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 pkgs/build-support/rust/hooks/rust-bindgen-hook.sh diff --git a/doc/languages-frameworks/rust.section.md b/doc/languages-frameworks/rust.section.md index 56faf636df1..e19783e29e6 100644 --- a/doc/languages-frameworks/rust.section.md +++ b/doc/languages-frameworks/rust.section.md @@ -464,6 +464,8 @@ you of the correct hash. be disabled by setting `dontUseCargoParallelTests`. * `cargoInstallHook`: install binaries and static/shared libraries that were built using `cargoBuildHook`. +* `bindgenHook`: for crates which use `bindgen` as a build dependency, lets + `bindgen` find `libclang` and `libclang` find the libraries in `buildInputs`. ### Examples {#examples} diff --git a/pkgs/build-support/rust/hooks/default.nix b/pkgs/build-support/rust/hooks/default.nix index d86c9ebaed8..d3369245707 100644 --- a/pkgs/build-support/rust/hooks/default.nix +++ b/pkgs/build-support/rust/hooks/default.nix @@ -1,6 +1,7 @@ { buildPackages , callPackage , cargo +, clang , diffutils , lib , makeSetupHook @@ -92,4 +93,13 @@ in { rustBuildPlatform rustTargetPlatform rustTargetPlatformSpec; }; } ./maturin-build-hook.sh) {}; + + bindgenHook = callPackage ({}: makeSetupHook { + name = "rust-bindgen-hook"; + substitutions = { + libclang = clang.cc.lib; + inherit clang; + }; + } + ./rust-bindgen-hook.sh) {}; } diff --git a/pkgs/build-support/rust/hooks/rust-bindgen-hook.sh b/pkgs/build-support/rust/hooks/rust-bindgen-hook.sh new file mode 100644 index 00000000000..53624b124f2 --- /dev/null +++ b/pkgs/build-support/rust/hooks/rust-bindgen-hook.sh @@ -0,0 +1,13 @@ +# populates LIBCLANG_PATH and BINDGEN_EXTRA_CLANG_ARGS for rust projects that +# depend on the bindgen crate + +# if you modify this, you probably also need to modify the wrapper for the cli +# of bindgen in pkgs/development/tools/rust/bindgen/wrapper.sh + +populateBindgenEnv () { + export LIBCLANG_PATH=@libclang@/lib + BINDGEN_EXTRA_CLANG_ARGS="$(< @clang@/nix-support/cc-cflags) $(< @clang@/nix-support/libc-cflags) $(< @clang@/nix-support/libcxx-cxxflags) $NIX_CFLAGS_COMPILE" + export BINDGEN_EXTRA_CLANG_ARGS +} + +postHook="${postHook:-}"$'\n'"populateBindgenEnv"$'\n' diff --git a/pkgs/development/compilers/rust/make-rust-platform.nix b/pkgs/development/compilers/rust/make-rust-platform.nix index f479b052019..2071c48cc6d 100644 --- a/pkgs/development/compilers/rust/make-rust-platform.nix +++ b/pkgs/development/compilers/rust/make-rust-platform.nix @@ -31,5 +31,5 @@ rec { # Hooks inherit (callPackage ../../../build-support/rust/hooks { inherit stdenv cargo rustc; - }) cargoBuildHook cargoCheckHook cargoInstallHook cargoSetupHook maturinBuildHook; + }) cargoBuildHook cargoCheckHook cargoInstallHook cargoSetupHook maturinBuildHook bindgenHook; } diff --git a/pkgs/development/tools/rust/bindgen/default.nix b/pkgs/development/tools/rust/bindgen/default.nix index 782996d9218..444db117322 100644 --- a/pkgs/development/tools/rust/bindgen/default.nix +++ b/pkgs/development/tools/rust/bindgen/default.nix @@ -34,7 +34,9 @@ let touch $out ''; }; - } '' + } + # if you modify the logic to find the right clang flags, also modify rustPlatform.bindgenHook + '' mkdir -p $out/bin export cincludes="$(< ${clang}/nix-support/cc-cflags) $(< ${clang}/nix-support/libc-cflags)" export cxxincludes="$(< ${clang}/nix-support/libcxx-cxxflags)"