Merge pull request #158041 from symphorien/bindgen

rust-bindgen: fix finding c++ stdlib
This commit is contained in:
Guillaume Girol 2022-02-06 12:41:57 +00:00 committed by GitHub
commit 942b0817e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 108 additions and 74 deletions

View File

@ -1,75 +1,45 @@
{ lib, fetchFromGitHub, rustPlatform, clang, llvmPackages_latest, rustfmt, writeTextFile
, runtimeShell
, bash
}:
rustPlatform.buildRustPackage rec {
pname = "rust-bindgen";
version = "0.59.2";
RUSTFLAGS = "--cap-lints warn"; # probably OK to remove after update
src = fetchFromGitHub {
owner = "rust-lang";
repo = pname;
rev = "v${version}";
sha256 = "sha256-bJYdyf5uZgWe7fQ80/3QsRV0qyExYn6P9UET3tzwPFs=";
};
cargoSha256 = "sha256-zhENlrqj611RkKDvpDtDFWc58wfQVamkJnpe2nvRieE=";
#for substituteAll
libclang = llvmPackages_latest.libclang.lib;
inherit bash;
buildInputs = [ libclang ];
propagatedBuildInputs = [ clang ]; # to populate NIX_CXXSTDLIB_COMPILE
configurePhase = ''
export LIBCLANG_PATH="${libclang.lib}/lib"
'';
postInstall = ''
mv $out/bin/{bindgen,.bindgen-wrapped};
{ rust-bindgen-unwrapped, zlib, bash, runCommand, runCommandCC }:
let
clang = rust-bindgen-unwrapped.clang;
self = runCommand "rust-bindgen-${rust-bindgen-unwrapped.version}"
{
#for substituteAll
inherit bash;
unwrapped = rust-bindgen-unwrapped;
libclang = clang.cc.lib;
meta = rust-bindgen-unwrapped.meta // {
longDescription = rust-bindgen-unwrapped.meta.longDescription + ''
This version of bindgen is wrapped with the required compiler flags
required to find the c and c++ standard libary, as well as the libraries
specified in the buildInputs of your derivation.
'';
};
passthru.tests = {
simple-c = runCommandCC "simple-c-bindgen-tests" { } ''
echo '#include <stdlib.h>' > a.c
${self}/bin/bindgen a.c --whitelist-function atoi | tee output
grep atoi output
touch $out
'';
simple-cpp = runCommandCC "simple-cpp-bindgen-tests" { } ''
echo '#include <cmath>' > a.cpp
${self}/bin/bindgen a.cpp --whitelist-function erf -- -xc++ | tee output
grep erf output
touch $out
'';
with-lib = runCommandCC "zlib-bindgen-tests" { buildInputs = [ zlib ]; } ''
echo '#include <zlib.h>' > a.c
${self}/bin/bindgen a.c --whitelist-function compress | tee output
grep compress output
touch $out
'';
};
} ''
mkdir -p $out/bin
export cincludes="$(< ${clang}/nix-support/cc-cflags) $(< ${clang}/nix-support/libc-cflags)"
export cxxincludes="$(< ${clang}/nix-support/libcxx-cxxflags)"
substituteAll ${./wrapper.sh} $out/bin/bindgen
chmod +x $out/bin/bindgen
'';
doCheck = true;
checkInputs =
let fakeRustup = writeTextFile {
name = "fake-rustup";
executable = true;
destination = "/bin/rustup";
text = ''
#!${runtimeShell}
shift
shift
exec "$@"
'';
};
in [
rustfmt
fakeRustup # the test suite insists in calling `rustup run nightly rustfmt`
clang
];
preCheck = ''
# for the ci folder, notably
patchShebangs .
'';
meta = with lib; {
description = "Automatically generates Rust FFI bindings to C (and some C++) libraries";
longDescription = ''
Bindgen takes a c or c++ header file and turns them into
rust ffi declarations.
As with most compiler related software, this will only work
inside a nix-shell with the required libraries as buildInputs.
'';
homepage = "https://github.com/rust-lang/rust-bindgen";
license = with licenses; [ bsd3 ];
platforms = platforms.unix;
maintainers = with maintainers; [ johntitor ralith ];
};
}
in
self

View File

@ -0,0 +1,63 @@
{ lib, fetchFromGitHub, rustPlatform, clang, rustfmt, writeTextFile
, runtimeShell
, bash
}:
rustPlatform.buildRustPackage rec {
pname = "rust-bindgen-unwrapped";
version = "0.59.2";
RUSTFLAGS = "--cap-lints warn"; # probably OK to remove after update
src = fetchFromGitHub {
owner = "rust-lang";
repo = "rust-bindgen";
rev = "v${version}";
sha256 = "sha256-bJYdyf5uZgWe7fQ80/3QsRV0qyExYn6P9UET3tzwPFs=";
};
cargoSha256 = "sha256-RKZY5vf6CSFaKweuuNkeFF0ZXlSUibAkcL/YhkE0MoQ=";
buildInputs = [ clang.cc.lib ];
preConfigure = ''
export LIBCLANG_PATH="${clang.cc.lib}/lib"
'';
doCheck = true;
checkInputs =
let fakeRustup = writeTextFile {
name = "fake-rustup";
executable = true;
destination = "/bin/rustup";
text = ''
#!${runtimeShell}
shift
shift
exec "$@"
'';
};
in [
rustfmt
fakeRustup # the test suite insists in calling `rustup run nightly rustfmt`
clang
];
preCheck = ''
# for the ci folder, notably
patchShebangs .
'';
passthru = { inherit clang; };
meta = with lib; {
description = "Automatically generates Rust FFI bindings to C (and some C++) libraries";
longDescription = ''
Bindgen takes a c or c++ header file and turns them into
rust ffi declarations.
'';
homepage = "https://github.com/rust-lang/rust-bindgen";
license = with licenses; [ bsd3 ];
platforms = platforms.unix;
maintainers = with maintainers; [ johntitor ralith ];
};
}

View File

@ -22,7 +22,7 @@ for e in "$@"; do
done;
cxxflags=
if [[ $cxx -eq 1 ]]; then
cxxflags=$NIX_CXXSTDLIB_COMPILE
cxxflags="@cxxincludes@"
fi;
if [[ -n "$NIX_DEBUG" ]]; then
set -x;
@ -30,7 +30,7 @@ fi;
export LIBCLANG_PATH="@libclang@/lib"
# shellcheck disable=SC2086
# cxxflags and NIX_CFLAGS_COMPILE should be word-split
exec -a "$0" @out@/bin/.bindgen-wrapped "$@" $sep $cxxflags $NIX_CFLAGS_COMPILE
exec -a "$0" @unwrapped@/bin/bindgen "$@" $sep $cxxflags @cincludes@ $NIX_CFLAGS_COMPILE
# note that we add the flags after $@ which is incorrect. This is only for the sake
# of simplicity.

View File

@ -13291,6 +13291,7 @@ with pkgs;
inherit (darwin.apple_sdk.frameworks) CoreServices;
};
rust-analyzer = callPackage ../development/tools/rust/rust-analyzer/wrapper.nix { };
rust-bindgen-unwrapped = callPackage ../development/tools/rust/bindgen/unwrapped.nix { };
rust-bindgen = callPackage ../development/tools/rust/bindgen { };
rust-cbindgen = callPackage ../development/tools/rust/cbindgen {
inherit (darwin.apple_sdk.frameworks) Security;