about summary refs log tree commit diff
path: root/nixpkgs/pkgs/development/tools/rust/bindgen
diff options
context:
space:
mode:
Diffstat (limited to 'nixpkgs/pkgs/development/tools/rust/bindgen')
-rw-r--r--nixpkgs/pkgs/development/tools/rust/bindgen/default.nix75
-rwxr-xr-xnixpkgs/pkgs/development/tools/rust/bindgen/wrapper.sh36
2 files changed, 111 insertions, 0 deletions
diff --git a/nixpkgs/pkgs/development/tools/rust/bindgen/default.nix b/nixpkgs/pkgs/development/tools/rust/bindgen/default.nix
new file mode 100644
index 000000000000..cc3907ba7f29
--- /dev/null
+++ b/nixpkgs/pkgs/development/tools/rust/bindgen/default.nix
@@ -0,0 +1,75 @@
+{ lib, fetchFromGitHub, rustPlatform, clang, llvmPackages_latest, rustfmt, writeTextFile
+, runtimeShell
+, bash
+}:
+
+rustPlatform.buildRustPackage rec {
+  pname = "rust-bindgen";
+  version = "0.57.0";
+
+  RUSTFLAGS = "--cap-lints warn"; # probably OK to remove after update
+
+  src = fetchFromGitHub {
+    owner = "rust-lang";
+    repo = pname;
+    rev = "v${version}";
+    sha256 = "sha256-0d8+Rkb4h1DoFUQ7u2/kPR/fUUz0YvI+hNT4iXL3mxY=";
+  };
+
+  cargoSha256 = "0r60smhlx1992a1s1k5sxjpdqllb2xsqcimgx3ldp5fdkfphk3cw";
+
+  #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};
+    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 ];
+  };
+}
diff --git a/nixpkgs/pkgs/development/tools/rust/bindgen/wrapper.sh b/nixpkgs/pkgs/development/tools/rust/bindgen/wrapper.sh
new file mode 100755
index 000000000000..0b3e3cd4c1e0
--- /dev/null
+++ b/nixpkgs/pkgs/development/tools/rust/bindgen/wrapper.sh
@@ -0,0 +1,36 @@
+#!@bash@/bin/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.
+