Merge pull request #159899 from symphorien/rust-bindgen-hook

rustPlatform.bindgenHook: init
This commit is contained in:
zowoq 2022-02-23 08:37:56 +10:00 committed by GitHub
commit 64f1f314bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 36 additions and 51 deletions

View file

@ -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}

View file

@ -40,6 +40,7 @@ stdenv.mkDerivation rec {
rustPlatform.cargoSetupHook
rustPlatform.rust.cargo
rustPlatform.rust.rustc
rustPlatform.bindgenHook
];
buildInputs = [
@ -49,8 +50,6 @@ stdenv.mkDerivation rec {
pipewire
];
LIBCLANG_PATH = "${libclang.lib}/lib";
meta = with lib; {
description = "A GTK patchbay for pipewire";
homepage = "https://gitlab.freedesktop.org/pipewire/helvum";

View file

@ -11,7 +11,7 @@
, hunspell, libevent, libstartup_notification
, libvpx
, icu70, libpng, glib, pciutils
, autoconf213, which, gnused, rustPackages
, autoconf213, which, gnused, rustPackages, rustPlatform
, rust-cbindgen, nodejs, nasm, fetchpatch
, gnum4
, gtk3, wrapGAppsHook
@ -196,6 +196,7 @@ buildStdenv.mkDerivation ({
which
unzip
wrapGAppsHook
rustPlatform.bindgenHook
]
++ lib.optionals buildStdenv.isDarwin [ xcbuild rsync ]
++ extraNativeBuildInputs;
@ -210,28 +211,8 @@ buildStdenv.mkDerivation ({
rm -f .mozconfig*
# this will run autoconf213
configureScript="$(realpath ./mach) configure"
export MOZCONFIG=$(pwd)/mozconfig
export MOZBUILD_STATE_PATH=$(pwd)/mozbuild
# Set C flags for Rust's bindgen program. Unlike ordinary C
# compilation, bindgen does not invoke $CC directly. Instead it
# uses LLVM's libclang. To make sure all necessary flags are
# included we need to look in a few places.
# TODO: generalize this process for other use-cases.
BINDGEN_CFLAGS="$(< ${buildStdenv.cc}/nix-support/libc-crt1-cflags) \
$(< ${buildStdenv.cc}/nix-support/libc-cflags) \
$(< ${buildStdenv.cc}/nix-support/cc-cflags) \
$(< ${buildStdenv.cc}/nix-support/libcxx-cxxflags) \
${lib.optionalString buildStdenv.cc.isClang "-idirafter ${buildStdenv.cc.cc.lib}/lib/clang/${lib.getVersion buildStdenv.cc.cc}/include"} \
${lib.optionalString buildStdenv.cc.isGNU "-isystem ${lib.getDev buildStdenv.cc.cc}/include/c++/${lib.getVersion buildStdenv.cc.cc} -isystem ${buildStdenv.cc.cc}/include/c++/${lib.getVersion buildStdenv.cc.cc}/${buildStdenv.hostPlatform.config}"} \
$NIX_CFLAGS_COMPILE"
${
# Bindgen doesn't like the flag added by `separateDebugInfo`.
lib.optionalString enableDebugSymbols ''
BINDGEN_CFLAGS="''${BINDGEN_CFLAGS/ -Wa,--compress-debug-sections/}"
''}
echo "ac_add_options BINDGEN_CFLAGS='$BINDGEN_CFLAGS'" >> $MOZCONFIG
'' + (lib.optionalString googleAPISupport ''
# Google API key used by Chromium and Firefox.
# Note: These are for NixOS/nixpkgs use ONLY. For your own distribution,

View file

@ -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) {};
}

View file

@ -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'

View file

@ -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;
}

View file

@ -48,6 +48,7 @@ stdenv.mkDerivation rec {
rustPlatform.cargoSetupHook
rustPlatform.rust.rustc
wrapGAppsHook4
rustPlatform.bindgenHook
desktop-file-utils
glib # for glib-compile-schemas
];
@ -63,11 +64,6 @@ stdenv.mkDerivation rec {
libshumate
];
# libspa-sys requires this for bindgen
LIBCLANG_PATH = "${llvmPackages.libclang.lib}/lib";
# <spa-0.2/spa/utils/defs.h> included by libspa-sys requires <stdbool.h>
BINDGEN_EXTRA_CLANG_ARGS = "-I${llvmPackages.libclang.lib}/lib/clang/${lib.getVersion llvmPackages.clang}/include -I${glibc.dev}/include";
passthru = {
updateScript = nix-update-script {
attrPath = pname;

View file

@ -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)"

View file

@ -14,21 +14,14 @@ rustPlatform.buildRustPackage rec {
cargoSha256 = "sha256-fpjzc2HiWP6nV8YZOwxsIOhy4ht/tQqcvCkcLMIFUaQ=";
nativeBuildInputs = with pkgs; [
clang
llvmPackages.libclang
rustPlatform.bindgenHook
];
buildInputs = with pkgs; [
pkg-config
clangStdenv
llvmPackages.libclang.lib
rocksdb
];
preBuild = with pkgs; ''
export LIBCLANG_PATH="${llvmPackages.libclang.lib}/lib";
'';
cargoBuildFlags = "--bin conduit";
meta = with lib; {

View file

@ -24,25 +24,14 @@ rustPlatform.buildRustPackage rec {
nativeBuildInputs = [
makeWrapper
pkg-config
rustPlatform.bindgenHook
];
buildInputs = [
udev
v4l-utils.lib
v4l-utils
];
LIBCLANG_PATH="${llvmPackages.libclang.lib}/lib";
# Works around the issue with rust-bindgen and the Nix gcc wrapper:
# https://hoverbear.org/blog/rust-bindgen-in-nix/
preBuild = ''
export BINDGEN_EXTRA_CLANG_ARGS="$(< ${stdenv.cc}/nix-support/libc-cflags) \
$(< ${stdenv.cc}/nix-support/cc-cflags) \
-isystem ${llvmPackages.libclang.lib}/lib/clang/${lib.getVersion llvmPackages.clang}/include \
-idirafter ${stdenv.cc.cc}/lib/gcc/${stdenv.hostPlatform.config}/${lib.getVersion stdenv.cc.cc}/include \
-idirafter ${v4l-utils.dev}/include"
'';
postInstall = ''
wrapProgram $out/bin/wluma \
--prefix LD_LIBRARY_PATH : "${lib.makeLibraryPath [ vulkan-loader ]}"