1 { lib, stdenv, fetchFromGitHub, fetchpatch, perl, which
2 # Most packages depending on openblas expect integer width to match
3 # pointer width, but some expect to use 32-bit integers always
4 # (for compatibility with reference BLAS).
6 # Multi-threaded applications must not call a threaded OpenBLAS
7 # (the only exception is when an application uses OpenMP as its
8 # *only* form of multi-threading). See
9 # https://github.com/xianyi/OpenBLAS/wiki/Faq/4bded95e8dc8aadc70ce65267d1093ca7bdefc4c#multi-threaded
10 # https://github.com/xianyi/OpenBLAS/issues/2543
11 # This flag builds a single-threaded OpenBLAS using the flags
13 , singleThreaded ? false
15 # Select a specific optimization target (other than the default)
16 # See https://github.com/xianyi/OpenBLAS/blob/develop/TargetList.txt
18 , enableStatic ? stdenv.hostPlatform.isStatic
19 , enableShared ? !stdenv.hostPlatform.isStatic
24 let blas64_ = blas64; in
27 setTarget = x: if target == null then x else target;
29 # To add support for a new platform, add an element to this set.
33 TARGET = setTarget "ARMV6";
40 TARGET = setTarget "ARMV7";
47 TARGET = setTarget "VORTEX";
50 MACOSX_DEPLOYMENT_TARGET = "11.0";
55 TARGET = setTarget "ARMV8";
62 TARGET = setTarget "P2";
69 TARGET = setTarget "ATHLON";
72 MACOSX_DEPLOYMENT_TARGET = "10.7";
77 TARGET = setTarget "ATHLON";
79 USE_OPENMP = !stdenv.hostPlatform.isMusl;
84 TARGET = setTarget "POWER5";
86 USE_OPENMP = !stdenv.hostPlatform.isMusl;
93 configs.${stdenv.hostPlatform.system}
94 or (throw "unsupported system: ${stdenv.hostPlatform.system}");
101 else hasPrefix "x86_64" stdenv.hostPlatform.system;
102 # Convert flag values to format OpenBLAS's build expects.
103 # `toString` is almost what we need other than bools,
104 # which we need to map {true -> 1, false -> 0}
105 # (`toString` produces empty string `""` for false instead of `0`)
106 mkMakeFlagValue = val:
107 if !builtins.isBool val then toString val
108 else if val then "1" else "0";
109 mkMakeFlagsFromConfig = mapAttrsToList (var: val: "${var}=${mkMakeFlagValue val}");
111 shlibExt = stdenv.hostPlatform.extensions.sharedLibrary;
114 stdenv.mkDerivation rec {
118 outputs = [ "out" "dev" ];
120 src = fetchFromGitHub {
124 sha256 = "14jxh0v3jfbw4mfjx4mcz4dd51lyq7pqvh9k8dg94539ypzjr2lj";
127 # apply https://github.com/xianyi/OpenBLAS/pull/3060 to fix a crash on arm
128 # remove this when updating to 0.3.14 or newer
131 name = "label-get_cpu_ftr-as-volatile.patch";
132 url = "https://github.com/xianyi/OpenBLAS/commit/6fe0f1fab9d6a7f46d71d37ebb210fbf56924fbc.diff";
133 sha256 = "06gwh73k4sas1ap2fi3jvpifbjkys2vhmnbj4mzrsvj279ljsfdk";
139 # Some hardening features are disabled due to sporadic failures in
140 # OpenBLAS-based programs. The problem may not be with OpenBLAS itself, but
141 # with how these flags interact with hardening measures used downstream.
142 # In either case, OpenBLAS must only be used by trusted code--it is
143 # inherently unsuitable for security-conscious applications--so there should
144 # be no objection to disabling these hardening measures.
146 # don't modify or move the stack
147 "stackprotector" "pic"
148 # don't alter index arithmetic
150 # don't interfere with dynamic target detection
154 nativeBuildInputs = [
160 buildPackages.gfortran
161 buildPackages.stdenv.cc
164 makeFlags = mkMakeFlagsFromConfig (config // {
165 FC = "${stdenv.cc.targetPrefix}gfortran";
166 CC = "${stdenv.cc.targetPrefix}${if stdenv.cc.isClang then "clang" else "cc"}";
167 PREFIX = placeholder "out";
169 INTERFACE64 = blas64;
170 NO_STATIC = !enableStatic;
171 NO_SHARED = !enableShared;
172 CROSS = stdenv.hostPlatform != stdenv.buildPlatform;
174 # Makefile.system only checks defined status
175 # This seems to be a bug in the openblas Makefile:
176 # on x86_64 it expects NO_BINARY_MODE=
177 # but on aarch64 it expects NO_BINARY_MODE=0
178 NO_BINARY_MODE = if stdenv.isx86_64
179 then toString (stdenv.hostPlatform != stdenv.buildPlatform)
180 else stdenv.hostPlatform != stdenv.buildPlatform;
181 } // (lib.optionalAttrs singleThreaded {
182 # As described on https://github.com/xianyi/OpenBLAS/wiki/Faq/4bded95e8dc8aadc70ce65267d1093ca7bdefc4c#multi-threaded
184 USE_LOCKING = true; # available with openblas >= 0.3.7
185 USE_OPENMP = false; # openblas will refuse building with both USE_OPENMP=1 and USE_THREAD=0
189 checkTarget = "tests";
192 # Write pkgconfig aliases. Upstream report:
193 # https://github.com/xianyi/OpenBLAS/issues/1740
194 for alias in blas cblas lapack; do
195 cat <<EOF > $out/lib/pkgconfig/$alias.pc
198 Description: $alias provided by the OpenBLAS package.
199 Cflags: -I$out/include
200 Libs: -L$out/lib -lopenblas
204 # Setup symlinks for blas / lapack
205 ln -s $out/lib/libopenblas${shlibExt} $out/lib/libblas${shlibExt}
206 ln -s $out/lib/libopenblas${shlibExt} $out/lib/libcblas${shlibExt}
207 ln -s $out/lib/libopenblas${shlibExt} $out/lib/liblapack${shlibExt}
208 ln -s $out/lib/libopenblas${shlibExt} $out/lib/liblapacke${shlibExt}
209 '' + lib.optionalString stdenv.hostPlatform.isLinux ''
210 ln -s $out/lib/libopenblas${shlibExt} $out/lib/libblas${shlibExt}.3
211 ln -s $out/lib/libopenblas${shlibExt} $out/lib/libcblas${shlibExt}.3
212 ln -s $out/lib/libopenblas${shlibExt} $out/lib/liblapack${shlibExt}.3
213 ln -s $out/lib/libopenblas${shlibExt} $out/lib/liblapacke${shlibExt}.3
217 description = "Basic Linear Algebra Subprograms";
218 license = licenses.bsd3;
219 homepage = "https://github.com/xianyi/OpenBLAS";
220 platforms = platforms.unix;
221 maintainers = with maintainers; [ ttuegel ];