about summary refs log tree commit diff
path: root/pkgs/development/tools/rust/bindgen
diff options
context:
space:
mode:
authorSymphorien Gibol <symphorien+git@xlumurb.eu>2018-08-19 14:26:20 +0200
committerSymphorien Gibol <symphorien+git@xlumurb.eu>2018-08-28 18:26:42 +0200
commit4c4fc2299c5e598f3a7f6f1e4a58739244acc975 (patch)
treea8829cf605cb8063c1a1c4235b601ebd268a7026 /pkgs/development/tools/rust/bindgen
parent6afd19e699c1505938d54312b522562fd75c8062 (diff)
downloadnixlib-4c4fc2299c5e598f3a7f6f1e4a58739244acc975.tar
nixlib-4c4fc2299c5e598f3a7f6f1e4a58739244acc975.tar.gz
nixlib-4c4fc2299c5e598f3a7f6f1e4a58739244acc975.tar.bz2
nixlib-4c4fc2299c5e598f3a7f6f1e4a58739244acc975.tar.lz
nixlib-4c4fc2299c5e598f3a7f6f1e4a58739244acc975.tar.xz
nixlib-4c4fc2299c5e598f3a7f6f1e4a58739244acc975.tar.zst
nixlib-4c4fc2299c5e598f3a7f6f1e4a58739244acc975.zip
rust-bindgen: wrap to add required library compilation flags
The easy part is to add NIX_CFLAGS_COMPILE for "regular" libraries.
A bit more tricky is to add the required flags for libclang to find
libstdcxx. For this we parse arguments to bindgen to look for
-x c++ or -xc++ and if found add NIX_CXXSTDLIB_COMPILE to the arguments.
This variable is populated by a complex dance of setupHooks. We trigger
this by adding clang to propagatedBuildInputs. A more subtle way may
exist.
Diffstat (limited to 'pkgs/development/tools/rust/bindgen')
-rw-r--r--pkgs/development/tools/rust/bindgen/default.nix49
-rwxr-xr-xpkgs/development/tools/rust/bindgen/wrapper.sh36
2 files changed, 75 insertions, 10 deletions
diff --git a/pkgs/development/tools/rust/bindgen/default.nix b/pkgs/development/tools/rust/bindgen/default.nix
index aeeae494e586..68df0af6ee46 100644
--- a/pkgs/development/tools/rust/bindgen/default.nix
+++ b/pkgs/development/tools/rust/bindgen/default.nix
@@ -1,6 +1,4 @@
-{ stdenv, fetchFromGitHub, rustPlatform, makeWrapper, llvmPackages }:
-
-# Future work: Automatically communicate NIX_CFLAGS_COMPILE to bindgen's tests and the bindgen executable itself.
+{ stdenv, fetchFromGitHub, fetchpatch, rustPlatform, clang, llvmPackages, rustfmt, writeScriptBin }:
 
 rustPlatform.buildRustPackage rec {
   name = "rust-bindgen-${version}";
@@ -13,23 +11,54 @@ rustPlatform.buildRustPackage rec {
     sha256 = "0cqjr7qspjrfgqcp4nqxljmhhbqyijb2jpw3lajgjj48y6wrnw93";
   };
 
-  nativeBuildInputs = [ makeWrapper ];
-  buildInputs = [ llvmPackages.clang-unwrapped.lib ];
+  cargoSha256 = "0b8v6c7q1abibzygrigldpd31lyd5ngmj4vq5d7zni96m20mm85w";
+
+  libclang = llvmPackages.libclang.lib; #for substituteAll
+
+  buildInputs = [ libclang ];
+
+  propagatedBuildInputs = [ clang ]; # to populate NIX_CXXSTDLIB_COMPILE
+
+  patches = [
+    # https://github.com/rust-lang-nursery/rust-bindgen/pull/1376
+    (fetchpatch {
+      url = https://github.com/rust-lang-nursery/rust-bindgen/commit/c8b5406f08af82a92bf8faf852c21ba941d9c176.patch;
+      sha256 = "16ibr2rplh0qz8rsq6gir45xlz5nasad4y8fprwhrb7ssv8wfkss";
+    })
+  ];
 
   configurePhase = ''
-    export LIBCLANG_PATH="${llvmPackages.clang-unwrapped.lib}/lib"
+    export LIBCLANG_PATH="${libclang}/lib"
   '';
 
   postInstall = ''
-    wrapProgram $out/bin/bindgen --set LIBCLANG_PATH "${llvmPackages.clang-unwrapped.lib}/lib"
+    mv $out/bin/{bindgen,.bindgen-wrapped};
+    substituteAll ${./wrapper.sh} $out/bin/bindgen
+    chmod +x $out/bin/bindgen
   '';
 
-  cargoSha256 = "0b8v6c7q1abibzygrigldpd31lyd5ngmj4vq5d7zni96m20mm85w";
-
-  doCheck = false; # A test fails because it can't find standard headers in NixOS
+  doCheck = false; # half the tests fail because our rustfmt is not nightly enough
+  checkInputs =
+    let fakeRustup = writeScriptBin "rustup" ''
+      #!${stdenv.shell}
+      shift
+      shift
+      exec "$@"
+    '';
+  in [
+    rustfmt
+    fakeRustup # the test suite insists in calling `rustup run nightly rustfmt`
+    clang
+  ];
 
   meta = with stdenv.lib; {
     description = "C and C++ binding generator";
+    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-nursery/rust-bindgen;
     license = with licenses; [ bsd3 ];
     maintainers = [ maintainers.ralith ];
diff --git a/pkgs/development/tools/rust/bindgen/wrapper.sh b/pkgs/development/tools/rust/bindgen/wrapper.sh
new file mode 100755
index 000000000000..95cd0901cec8
--- /dev/null
+++ b/pkgs/development/tools/rust/bindgen/wrapper.sh
@@ -0,0 +1,36 @@
+#!/usr/bin/env bash
+sep='--'   # whether to add -- before new options
+cxx=0      # whether cxx was explicitly requested
+lastWasx=0 # whether the last argument passed was -x
+for e in "$@"; do
+  if [[ "$e" == "--" ]]; then
+    sep=
+  fi;
+  if [[ "$sep" == "" ]]; then
+    # we look for -x c++ after -- only
+    if [[ "$e" == "-x" ]]; then
+      lastWasx=1
+    fi;
+    if [[ $lastWasx -eq 1 && "$e" == "c++" ]]; then
+      lastWasx=0
+      cxx=1
+    fi;
+    if [[ "$e" == "-xc++" || "$e" == -std=c++* ]]; then
+      cxx=1
+    fi;
+  fi;
+done;
+cxxflags=
+if [[ $cxx -eq 1 ]]; then
+  cxxflags=$NIX_CXXSTDLIB_COMPILE
+fi;
+if [[ -n "$NIX_DEBUG" ]]; then
+  set -x;
+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
+# note that we add the flags after $@ which is incorrect. This is only for the sake
+# of simplicity.
+