1{
2 lib,
3 stdenv,
4 blas,
5 lapack,
6 openfst,
7 icu,
8 pkg-config,
9 fetchFromGitHub,
10 python,
11 openblas,
12 zlib,
13 gfortran,
14}:
15
16let
17 old-openfst = openfst.overrideAttrs (prev: {
18 version = "kag-unstable-2022-05-06";
19
20 src = fetchFromGitHub {
21 owner = "kkm000";
22 repo = "openfst";
23 # required by https://github.com/daanzu/kaldi-fork-active-grammar/blob/e9c7d0ffca401cf312779d25f2c05a34b41ff696/cmake/third_party/openfst.cmake#L7
24 rev = "0bca6e76d24647427356dc242b0adbf3b5f1a8d9";
25 sha256 = "1802rr14a03zl1wa5a0x1fa412kcvbgprgkadfj5s6s3agnn11rx";
26 };
27 buildInputs = [ zlib ];
28 });
29in
30
31assert blas.implementation == "openblas" && lapack.implementation == "openblas";
32
33stdenv.mkDerivation rec {
34 pname = "kaldi";
35 version = "kag-v2.1.0";
36
37 src = fetchFromGitHub {
38 owner = "daanzu";
39 repo = "kaldi-fork-active-grammar";
40 rev = version;
41 sha256 = "+kT2xJRwDj/ECv/v/J1FpsINWOK8XkP9ZvZ9moFRl70=";
42 };
43
44 patches = [
45 ./0004-fork-cmake.patch
46 ./0006-fork-configure.patch
47 ];
48
49 enableParallelBuilding = true;
50
51 buildInputs = [
52 openblas
53 old-openfst
54 icu
55 ];
56
57 nativeBuildInputs = [
58 pkg-config
59 python
60 gfortran
61 ];
62
63 buildFlags = [
64 "dragonfly"
65 "dragonflybin"
66 "bin"
67 "fstbin"
68 "lmbin"
69 ];
70
71 postPatch = ''
72 # Replace the shebangs for the various build scripts
73 patchShebangs src
74 # Compatability with OpenBLAS 0.3.21
75 substituteInPlace src/matrix/cblas-wrappers.h \
76 --replace stptri_ LAPACK_stptri \
77 --replace dtptri_ LAPACK_dtptri \
78 --replace sgetrf_ LAPACK_sgetrf \
79 --replace dgetrf_ LAPACK_dgetrf \
80 --replace sgetri_ LAPACK_sgetri \
81 --replace dgetri_ LAPACK_dgetri \
82 --replace sgesvd_ LAPACK_sgesvd \
83 --replace dgesvd_ LAPACK_dgesvd \
84 --replace ssptri_ LAPACK_ssptri \
85 --replace dsptri_ LAPACK_dsptri \
86 --replace ssptrf_ LAPACK_ssptrf \
87 --replace dsptrf_ LAPACK_dsptrf
88 '';
89
90 configurePhase = ''
91 cd src
92 ./configure --shared --fst-root="${old-openfst}" --use-cuda=no --openblas-root="${openblas}" --mathlib=OPENBLAS
93 '';
94
95 installPhase = ''
96 # Fixes "patchelf: wrong ELF type"
97 find . -type f -name "*.o" -print0 | xargs -0 rm -f
98 mkdir -p $out/{bin,lib}
99 cp lib/* $out/lib/
100 patchelf \
101 --set-rpath "${lib.makeLibraryPath buildInputs}:$out/lib" \
102 $out/lib/*
103 '';
104
105 meta = with lib; {
106 description = "Speech Recognition Toolkit";
107 homepage = "https://kaldi-asr.org";
108 license = licenses.mit;
109 maintainers = [ ];
110 platforms = platforms.linux;
111 };
112}