From dd39a58f900d046696c73efde0cdbd7e0d11191b Mon Sep 17 00:00:00 2001
From: Zhaofeng Li <hello@zhaofeng.li>
Date: Sat, 5 Jun 2021 13:35:25 -0700
Subject: [PATCH] openblas: Enable riscv64-linux

---
 .../science/math/openblas/default.nix         | 23 ++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/pkgs/development/libraries/science/math/openblas/default.nix b/pkgs/development/libraries/science/math/openblas/default.nix
index 8c52a3d4fd1e..5d3c3f40f1d4 100644
--- a/pkgs/development/libraries/science/math/openblas/default.nix
+++ b/pkgs/development/libraries/science/math/openblas/default.nix
@@ -1,4 +1,4 @@
-{ lib, stdenv, fetchFromGitHub, perl, which
+{ lib, stdenv, fetchFromGitHub, fetchpatch, perl, which
 # Most packages depending on openblas expect integer width to match
 # pointer width, but some expect to use 32-bit integers always
 # (for compatibility with reference BLAS).
@@ -94,6 +94,13 @@ let
       DYNAMIC_ARCH = setDynamicArch true;
       USE_OPENMP = !stdenv.hostPlatform.isMusl;
     };
+
+    riscv64-linux = {
+      BINARY = 64;
+      TARGET = setTarget "RISCV64_GENERIC";
+      DYNAMIC_ARCH = setDynamicArch false;
+      USE_OPENMP = true;
+    };
   };
 in
 
@@ -133,6 +140,20 @@ stdenv.mkDerivation rec {
     sha256 = "1qjr02cqncv20abdp1yzr55n7smhx6h9chqvb0xbp18byynvj87w";
   };
 
+  # remove both patches when updating to 0.3.16
+  patches = [
+    (fetchpatch {
+      name = "riscv64-imin-fix-wrong-comparison.patch";
+      url = "https://github.com/xianyi/OpenBLAS/commit/1e0192a5ccac28fc0c749f49d36ec7eda9757428.patch";
+      sha256 = "0kjkmrj8023vcjxhgin5dqs5w3gf93hzhwdhg0vsjhdra2ghkwzj";
+    })
+    (fetchpatch {
+      name = "riscv64-generic-use-generic-kernel-for-dsdot.patch";
+      url = "https://github.com/xianyi/OpenBLAS/commit/3521cd48cbfb3d50f6ae9a10377382d37075c696.patch";
+      sha256 = "0ljwbldff4db377s8rzmqxrszilqdivy656yqvfq46x5338v3gi0";
+    })
+  ];
+
   inherit blas64;
 
   # Some hardening features are disabled due to sporadic failures in