Skip to content

Commit

Permalink
rustPlatform.bindgenHook: init
Browse files Browse the repository at this point in the history
  • Loading branch information
symphorien committed Feb 22, 2022
1 parent d914868 commit bedabfb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
2 changes: 2 additions & 0 deletions doc/languages-frameworks/rust.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
10 changes: 10 additions & 0 deletions pkgs/build-support/rust/hooks/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{ buildPackages
, callPackage
, cargo
, clang
, diffutils
, lib
, makeSetupHook
Expand Down Expand Up @@ -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) {};
}
13 changes: 13 additions & 0 deletions pkgs/build-support/rust/hooks/rust-bindgen-hook.sh
Original file line number Diff line number Diff line change
@@ -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'
2 changes: 1 addition & 1 deletion pkgs/development/compilers/rust/make-rust-platform.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 3 additions & 1 deletion pkgs/development/tools/rust/bindgen/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down

0 comments on commit bedabfb

Please sign in to comment.