From f37be82593a90a414e879253e8624588b51153e7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= <joerg@thalheim.io>
Date: Mon, 11 Dec 2023 21:05:46 +0100
Subject: [PATCH] include-what-you-use: 0.19 -> 0.21

also add a compiler-wrapper similar to clang-tools so it can find dependencies in nix-shell environments.
---
 .../analysis/include-what-you-use/default.nix | 14 ++++++++--
 .../analysis/include-what-you-use/wrapper     | 26 +++++++++++++++++++
 pkgs/top-level/all-packages.nix               |  2 +-
 3 files changed, 39 insertions(+), 3 deletions(-)
 create mode 100755 pkgs/development/tools/analysis/include-what-you-use/wrapper

diff --git a/pkgs/development/tools/analysis/include-what-you-use/default.nix b/pkgs/development/tools/analysis/include-what-you-use/default.nix
index 98aade37c2b5..7266b76a4ad6 100644
--- a/pkgs/development/tools/analysis/include-what-you-use/default.nix
+++ b/pkgs/development/tools/analysis/include-what-you-use/default.nix
@@ -3,11 +3,11 @@
 stdenv.mkDerivation rec {
   pname = "include-what-you-use";
   # Also bump llvmPackages in all-packages.nix to the supported version!
-  version = "0.19";
+  version = "0.21";
 
   src = fetchurl {
     url = "${meta.homepage}/downloads/${pname}-${version}.src.tar.gz";
-    hash = "sha256-KxAVe2DqCK3AjjiWtJIcc/yt1exOtlKymjQSnVAeXuA=";
+    hash = "sha256-ajUZGf+JvafJXIlUcmAYaNs9qrlqlYs44DYokNWHYLY=";
   };
 
   postPatch = ''
@@ -17,11 +17,21 @@ stdenv.mkDerivation rec {
   nativeBuildInputs = with llvmPackages; [ cmake llvm.dev llvm python3 ];
   buildInputs = with llvmPackages; [ libclang clang-unwrapped python3 ];
 
+  clang = llvmPackages.clang;
+
   cmakeFlags = [ "-DCMAKE_PREFIX_PATH=${llvmPackages.llvm.dev}" ];
 
   postInstall = ''
     substituteInPlace $out/bin/iwyu_tool.py \
       --replace "'include-what-you-use'" "'$out/bin/include-what-you-use'"
+
+
+    mv $out/bin/include-what-you-use $out/bin/.include-what-you-use-unwrapped
+    mv $out/bin/iwyu_tool.py $out/bin/.iwyu_tool.py-unwrapped
+    substituteAll ${./wrapper} $out/bin/include-what-you-use
+    ln -s $out/bin/include-what-you-use $out/bin/iwyu_tool.py
+    chmod +x $out/bin/include-what-you-use
+    patchShebangs $out/bin/include-what-you-use
   '';
 
   meta = with lib; {
diff --git a/pkgs/development/tools/analysis/include-what-you-use/wrapper b/pkgs/development/tools/analysis/include-what-you-use/wrapper
new file mode 100755
index 000000000000..3fd37902cf2d
--- /dev/null
+++ b/pkgs/development/tools/analysis/include-what-you-use/wrapper
@@ -0,0 +1,26 @@
+#!/bin/sh
+
+buildcpath() {
+  local path after
+  while (( $# )); do
+    case $1 in
+        -isystem)
+            shift
+            path=$path${path:+':'}$1
+            ;;
+        -idirafter)
+            shift
+            after=$after${after:+':'}$1
+            ;;
+    esac
+    shift
+  done
+  echo $path${after:+':'}$after
+}
+
+export CPATH=${CPATH}${CPATH:+':'}$(buildcpath ${NIX_CFLAGS_COMPILE} \
+                                               $(<@clang@/nix-support/libc-cflags)):@clang@/resource-root/include
+export CPLUS_INCLUDE_PATH=${CPLUS_INCLUDE_PATH}${CPLUS_INCLUDE_PATH:+':'}$(buildcpath ${NIX_CFLAGS_COMPILE} \
+                                                                                      $(<@clang@/nix-support/libcxx-cxxflags) \
+                                                                                      $(<@clang@/nix-support/libc-cflags)):@clang@/resource-root/include
+exec -a "$0" @out@/bin/.$(basename $0)-unwrapped "$@"
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index e3311d2b5f47..8dd214b13296 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -19390,7 +19390,7 @@ with pkgs;
   img = callPackage ../development/tools/img { };
 
   include-what-you-use = callPackage ../development/tools/analysis/include-what-you-use {
-    llvmPackages = llvmPackages_15;
+    llvmPackages = llvmPackages_17;
   };
 
   indent = callPackage ../development/tools/misc/indent { };