aboutsummaryrefslogtreecommitdiffstats
blob: 66aacc4752bdb597682c765b592b012008eccc11 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
#!/bin/bash

# This configure script is hand-generated, not auto-generated.
# It creates the file kaldi.mk, which is %included by the Makefiles
# in the subdirectories.
# The file kaldi.mk is editable by hand -- for example, you may want to
# remove the options -g -O0 -DKALDI_PARANOID, or edit the
# DOUBLE_PRECISION variable (to be 1 not 0).


#  Example command lines:
# ./configure --shared  ## shared libraries.
# ./configure
# ./configure --mkl-root=/opt/intel/mkl
# ./configure --mkl-root=/opt/intel/mkl --threaded-math=yes
# ./configure --mkl-root=/opt/intel/mkl --threaded-math=yes --mkl-threading=tbb
#        # This is for MKL 11.3, which does not seem  to provide Intel OMP libs
# ./configure --openblas-root=../tools/OpenBLAS/install
#        # Before doing this, cd to ../tools and type "make openblas".
#        # Note: this is not working correctly on all platforms, do "make test"
#        # and look out for segmentation faults.
# ./configure --atlas-root=../tools/ATLAS/build
# ./configure --use-cuda=no   # disable CUDA detection (will build cpu-only
#                             # version of kaldi even on CUDA-enabled machine
# ./configure --static --fst-root=/opt/cross/armv8hf \
# --atlas-root=/opt/cross/armv8hf --host=armv8-rpi3-linux-gnueabihf
#        # Cross compile for armv8hf, this assumes that you have openfst built
#        # with the armv8-rpi3-linux-gnueabihf toolchain and installed to
#        # /opt/cross/armv8hf. It also assumes that you have an ATLAS library
#        # built for the target install to /opt/cross/armv8hf and that the
#        # armv8-rpi3-linux-gnueabihf toolchain is available in your path
# ./configure --static --openblas-root=/opt/cross/arm-linux-androideabi \
# --fst-root=/opt/cross/arm-linux-androideabi --fst-version=1.4.1 \
# --android-incdir=/opt/cross/arm-linux-androideabi/sysroot/usr/include \
# --host=arm-linux-androideabi
#        # Cross compile for Android on arm. The only difference here is the
#        # addition of the the --android-includes flag because the toolchains
#        # produced by the Android NDK don't always include the C++ stdlib
#        # headers in the normal cross compile include path.

# This should be incremented after any significant change to the configure
# script, i.e. any change affecting kaldi.mk or the build system as a whole.
CONFIGURE_VERSION=6

if ! [ -x "$PWD/configure" ]; then
  echo 'You must run "configure" from the src/ directory.'
  exit 1
fi

function usage {
  cat <<EOF
'configure' configures Kaldi installation.

Usage: [VAR=VALUE]... $0 [OPTION]...

The default configuration is to build and link against static Kaldi libraries.
OpenFst and Math libraries are linked dynamically.

Configuration options:
  --help                Display this help message and exit
  --version             Display the version of 'configure' and exit
  --static              Build and link against static libraries [default=no]
  --shared              Build and link against shared libraries [default=no]
  --use-cuda            Build with CUDA [default=yes]
  --cudatk-dir=DIR      CUDA toolkit directory
  --double-precision    Build with BaseFloat set to double if yes [default=no],
                        mostly useful for testing purposes.
  --static-fst          Build with static OpenFst libraries [default=no]
  --fst-root=DIR        OpenFst root directory [default=../tools/openfst/]
  --fst-version=STR     OpenFst version string
  --mathlib=LIB         Math library [default=ATLAS]
                        Supported libraries: ATLAS, MKL, CLAPACK, OPENBLAS.
  --static-math         Build with static math libraries [default=no]
  --threaded-math       Build with multi-threaded math libraries [default=no]
  --threaded-atlas      Build with multi-threaded ATLAS libraries [default=no]
  --atlas-root=DIR      ATLAS root directory [default=../tools/ATLAS/]
  --openblas-root=DIR   OpenBLAS root directory
  --clapack-root=DIR    CLAPACK root directory
  --mkl-root=DIR        MKL root directory
  --mkl-libdir=DIR      MKL library directory
  --mkl-threading=LIB   MKL threading layer [default=sequential]
                        Supported layers: sequential, iomp, tbb, gomp.
  --omp-libdir=DIR      OpenMP directory
  --speex-root=DIR      SPEEX root directory
  --speex-libdir=DIR    SPEEX library directory
  --speex-incdir=DIR    SPEEX include directory
  --host=HOST           Host triple in the format 'cpu-vendor-os'
                        If provided, it is prepended to all toolchain programs.
  --android-incdir=DIR  Andraid include directory

Following environment variables can be used to override the default toolchain.
  CXX         C++ compiler [default=g++]
  AR          Archive maintenance utility [default=ar]
  AS          Assembler [default=as]
  RANLIB      Archive indexing utility [default=ranlib]

If a host triple is provided, it is prepended to CXX, AR, AS and RANLIB.

Following environment variables can be used to provide additional flags to the
compiler/linker.
  CXXFLAGS    Additional C++ compiler flags, e.g. -I<include-dir>
  LDFLAGS     Additional linker flags, e.g. -L<lib-dir>
  LDLIBS      Additional libraries to pass to the linker, e.g. -l<lib>

EOF
}

function rel2abs {
  if [ ! -z "$1" ]; then
    local retval=`cd $1 2>/dev/null && pwd || exit 1`
    echo $retval
  fi
}

function read_dirname {
  local dir_name=`expr "X$1" : '[^=]*=\(.*\)'`;
  local retval=`rel2abs $dir_name`
  [ -z $retval ] && echo "Bad option '$1': no such directory" && exit 1;
  echo $retval
}

function is_set {
  local myvar=${1:-notset}
  if [ "$myvar" == "notset" ]; then
    return 1
  else
    return 0
  fi
}

function failure {
  echo "***configure failed: $* ***" >&2
  if [ -f kaldi.mk ]; then rm kaldi.mk; fi
  exit 1;
}

function check_exists {
  if [ ! -f $1 ]; then failure "$1 not found."; fi
}

function check_library {
  local libpath=$1
  local libname=$2
  local libext=$3
  local full_libname="$libpath/$libname.$libext"
  ##echo "Testing $full_libname" >&2
  test -f "$full_libname" && return ;
  return 1
}

function check_compiler {
  COMPILER=$1
  if ! which $COMPILER >&/dev/null; then
    failure "$COMPILER is not installed.
             You need g++ >= 4.7, Apple clang >= 5.0 or LLVM clang >= 3.3."
  else
    COMPILER_VER_INFO=$($COMPILER --version 2>/dev/null)
    if [[ $COMPILER_VER_INFO == *"g++"* ]]; then
      GCC_VER=$($COMPILER -dumpversion)
      GCC_VER_NUM=$(echo $GCC_VER | sed 's/\./ /g' | xargs printf "%d%02d%02d")
      if [ $GCC_VER_NUM -lt 40700 ]; then
        failure "$COMPILER (g++-$GCC_VER) is not supported.
                 You need g++ >= 4.7, Apple clang >= 5.0 or LLVM clang >= 3.3."
      elif [ $GCC_VER_NUM  == 40801 ] || [ $GCC_VER_NUM == 40802 ]; then
        failure "$COMPILER (g++-$GCC_VER) is not supported.
                 GCC 4.8.1 and 4.8.2 have a bug in the implementation of
                 the nth_element algorithm provided by the standard library.
                 This will cause Kaldi to crash (make test would fail).
                 Please use another C++ compiler with C++11 support.
                 You need g++ >= 4.7, Apple clang >= 5.0 or LLVM clang >= 3.3."
      fi
    elif [[ $COMPILER_VER_INFO == *"Apple"* ]]; then
      CLANG_VER=$(echo $COMPILER_VER_INFO | grep version | sed "s/.*version \([0-9\.]*\).*/\1/")
      CLANG_VER_NUM=$(echo $COMPILER_VER_INFO | grep version | sed "s/.*clang-\([0-9]*\).*/\1/")
      if [ $CLANG_VER_NUM -lt 500 ]; then
        failure "$COMPILER (Apple clang-$CLANG_VER) is not supported.
                 You need g++ >= 4.7, Apple clang >= 5.0 or LLVM clang >= 3.3."
      fi
    elif [[ $COMPILER_VER_INFO == *"LLVM"* ]]; then
      CLANG_VER=$(echo $COMPILER_VER_INFO | grep version | sed "s/.*version \([0-9\.]*\).*/\1/")
      CLANG_VER_NUM=$(echo $CLANG_VER | sed 's/\./ /g' | xargs printf "%d%02d")
      if [ $CLANG_VER_NUM -lt 303 ]; then
        failure "$COMPILER (LLVM clang-$CLANG_VER) is not supported.
                You need g++ >= 4.7, Apple clang >= 5.0 or LLVM clang >= 3.3."
      fi
    fi
  fi
}

function check_for_slow_expf {
  # We cannot run this test if we are cross compiling.
  if [[ "$TARGET_ARCH" == "`uname -m`" ]] ; then
    cd probe
    rm -f exp-test
    make -f Makefile.slow_expf 1>/dev/null
    ./exp-test
    if [ $? -eq 1 ]; then
        echo "*** WARNING: expf() seems to be slower than exp() on your machine. This is a known bug in old versions of glibc. Please consider updating glibc. ***"
        echo "*** Kaldi will be configured to use exp() instead of expf() in base/kaldi-math.h Exp() routine for single-precision floats. ***"
        echo "CXXFLAGS += -DKALDI_NO_EXPF" >> ../kaldi.mk
    fi
    cd ..
  fi
}

# MKL functions
function linux_configure_mkllibdir {
  local mklroot=$1

  if [ -d $mklroot/lib/em64t ]; then
    echo $mklroot/lib/em64t
  elif [ -d $mklroot/lib/intel64 ]; then
    echo $mklroot/lib/intel64
  else
    return 1;
  fi
}

function linux_configure_mkl_includes {
  test -d $1/include && echo "$1/include" && return;
  test -d $2/../../include && echo "$2/../../include" && return;
  failure "Could not find the MKL include directory"
}

function linux_configure_mkl_libraries {
  local mkllibdir=$1
  local static=$2
  local threaded=$3
  local mplib=$4

  declare -A mkl_libs
  mkl_libs=(
    [sequential]="mkl_intel_lp64 mkl_core mkl_sequential"
    [gomp]="mkl_intel_lp64 mkl_core mkl_gnu_thread"
    [iomp]="mkl_intel_lp64 mkl_core mkl_intel_thread "
    [tbb]="mkl_intel_lp64 mkl_core mkl_tbb_thread "
  )

  if [ -z "${mkl_libs[$threaded]}" ]; then
    echo >&2 "Unknown threading mode: $threaded"
    return 1;
  fi

  local linkline=""
  if  ! $static ; then
    linkline="-L$mkllibdir -Wl,-rpath=$mkllibdir"
    for file in ${mkl_libs[$threaded]}; do
      local libfile=$mkllibdir/lib$file.so
      check_exists $libfile
      linkline+=" -l$file "
    done
  else
    if [ $threaded == "sequential" ] ; then
      test -f "$mkllibdir/libmkl_solver_lp64.a" && \
        linkline="$linkline $mkllibdir/libmkl_solver_lp64.a"
    else
      test -f "$mkllibdir/libmkl_solver_lp64_sequential.a" && \
        linkline="$linkline $mkllibdir/libmkl_solver_lp64_sequential.a"
    fi
    linkline="$linkline -Wl,--start-group"
    for file in ${mkl_libs[$threaded]}; do
      local libfile=$mkllibdir/lib${file}.a
      check_exists $libfile
      linkline="$linkline $libfile"
    done
    linkline="$linkline -Wl,--end-group "
  fi
  echo "$linkline"
}

function linux_configure_mkl_extra {
  local static=$1
  local threaded=$2

  declare -A extra_libs
  extra_libs=(
    [sequential]="-ldl -lpthread -lm"
    [gomp]="-lgomp -ldl -lpthread -lm"
    [iomp]="-ldl -lpthread -lm"
    [tbb]=" -ldl -lpthread -lm "
  )
  echo "$linkline ${extra_libs[$threaded]}"
}

function linux_configure_threadinglibdir {
  local library=$1
  local mklroot=$2
  local mkllibdir=$3
  local libexts=$4

  ##First we try to use the library in the same directory
  ##where the mkl libraries reside
  ##Afterwards, just try some possibilities for different MKL layouts
  for libext in $libexts; do
    check_library $mkllibdir "lib$library" $libext \
    && echo `readlink -f $mkllibdir` && return 0

    local testdir=`(cd $mklroot; cd ..; cd lib/intel64;pwd)`
    test -d $testdir && check_library $testdir "lib$library" $libext && echo `readlink -f $testdir` && return 0;
    local testdir=`(cd $mklroot; cd ..; cd lib/em64t;pwd)`
    test -d $testdir && check_library $testdir "lib$library" $libext && echo `readlink -f $testdir` && return 0;

    local testdir=`(cd $mkllibdir; cd ../../..; cd lib/intel64;pwd)`
    test -d $testdir && check_library $testdir "lib$library" $libext && echo `readlink -f $testdir` && return 0;
    local testdir=`(cd $mklroot; cd ../../..; cd lib/em64t;pwd)`
    test -d $testdir && check_library $testdir "lib$library" $libext && echo `readlink -f $testdir` && return 0;
  done

  #failure "Could not find the library iomp5, use the configure switch --omp-libdir"
  return 1
}

function linux_configure_mkl_threading {
  local mklroot=$1
  local mkllibdir=$2
  local static=$3
  local threading=$4

  declare -A libs
  libs=(
    [sequential]=""
    [gomp]=""
    [iomp]="iomp5"
    [tbb]="tbb"
  )

  echo >&2 "Configuring MKL threading as $threading"
  library=${libs[$threading]}
  if [ -z "$library" ]; then
    return 0
  fi

  if ! is_set $OMPLIBDIR ; then
    if  $static ; then
      OMPLIBDIR=`linux_configure_threadinglibdir $library "$MKLROOT" "$MKLLIBDIR" "a"`
    else
      OMPLIBDIR=`linux_configure_threadinglibdir $library "$MKLROOT" "$MKLLIBDIR" "so"`
    fi
  fi

  check_library $OMPLIBDIR "lib$library" "a" || \
  check_library $OMPLIBDIR "lib$library" "so" || \
  failure "Could not find the $library library, have your tried the --omp-libdir switch?"

  OMP_LINK_LINE=''
  # TODO(arnab): in the following conditional, the $static_math test is
  # needed since the OpenMP library is assumed to be dynamic.
  if [ "$OMPLIBDIR" != "$MKLLIBDIR" ] ; then
    OMP_LINK_LINE="-L${OMPLIBDIR}"
  fi
  #if the libiomp5 library is dynamic, we add the rpath attribute
  if ! $static_math ; then
    OMP_LINK_LINE="$OMP_LINK_LINE -Wl,-rpath=$OMPLIBDIR -l$library"
  else
    OMP_LINK_LINE="$OMP_LINK_LINE -Wl,-Bstatic -l$library -Wl,-Bdynamic"
  fi
  echo "$OMP_LINK_LINE"
}


# CUDA is used only in selected directories including src/cudamatrix, src/nnet*
# and src/chain*. It is used to accelerate the neural network training.
# The rest of Kaldi runs on CPUs.

function configure_cuda {
  # Check for CUDA toolkit in the system
  if [ ! -d  "$CUDATKDIR" ]; then
    for base in /Developer/NVIDIA/CUDA-6.0 /usr/local/share/cuda /usr/local/cuda /pkgs_local/cuda-3.2/ /opt/nvidia_cuda/cuda-6.0/ /usr/; do
      if [ -f $base/bin/nvcc ]; then
        CUDATKDIR=$base
      fi
    done
  fi

  if [ -d "$CUDATKDIR" ]; then
    if [ ! -f $CUDATKDIR/bin/nvcc ]; then
      failure "Cannnot find nvcc in CUDATKDIR=$CUDATKDIR"
    fi

    if [[ "$TARGET_ARCH" != "`uname -m`" ]] ; then
      failure "Cannot cross compile with CUDA support"
    fi

    # Determine 'CUDA_ARCH',
    CUDA_VERSION=$($CUDATKDIR/bin/nvcc -V | tr '.,' '_ ' | awk '/release/{sub(/.*release/,""); print $1;}') # MAJOR_MINOR,
    if [ -z "$CUDA_VERSION" ] ; then
      echo "Cannot figure out CUDA_VERSION from the nvcc output. Either your CUDA is too new or too old."
      exit 1
    fi

    COMPILER_VER_INFO=$($CXX --version 2>/dev/null)
    if [[ $COMPILER_VER_INFO == *"g++"* ]]; then
      GCC_VER=$($COMPILER -dumpversion)
      GCC_VER_NUM=$(echo $GCC_VER | sed 's/\./ /g' | xargs printf "%d%02d%02d")
      case $CUDA_VERSION in
        5_5)
          MIN_UNSUPPORTED_GCC_VER="5.0"
          MIN_UNSUPPORTED_GCC_VER_NUM=50000;
        ;;
        6_*)
          MIN_UNSUPPORTED_GCC_VER="5.0"
          MIN_UNSUPPORTED_GCC_VER_NUM=50000;
        ;;
        7_*)
          MIN_UNSUPPORTED_GCC_VER="5.0"
          MIN_UNSUPPORTED_GCC_VER_NUM=50000;
        ;;
        8_*)
          MIN_UNSUPPORTED_GCC_VER="6.0"
          MIN_UNSUPPORTED_GCC_VER_NUM=60000;
        ;;
        9_*)
          MIN_UNSUPPORTED_GCC_VER="7.0"
          MIN_UNSUPPORTED_GCC_VER_NUM=70000;
        ;;
        *)
          echo "Unsupported CUDA_VERSION (CUDA_VERSION=$CUDA_VERSION), please report it to Kaldi mailing list, together with 'nvcc -h' or 'ptxas -h' which lists allowed -gencode values..."; exit 1;
        ;;
      esac
      if [ $GCC_VER_NUM -ge $MIN_UNSUPPORTED_GCC_VER_NUM ]; then
        failure "CUDA $CUDA_VERSION does not support $CXX (g++-$GCC_VER).
                 You need g++ < $MIN_UNSUPPORTED_GCC_VER."
      fi
    fi

    case $CUDA_VERSION in
      5_5) CUDA_ARCH="-gencode arch=compute_20,code=sm_20 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35" ;;
      6_*) CUDA_ARCH="-gencode arch=compute_20,code=sm_20 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_50,code=sm_50" ;;
      7_*) CUDA_ARCH="-gencode arch=compute_20,code=sm_20 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_53,code=sm_53" ;;
      8_*) CUDA_ARCH="-gencode arch=compute_20,code=sm_20 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_53,code=sm_53 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_62,code=sm_62" ;;
      9_*) CUDA_ARCH="-gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_53,code=sm_53 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_62,code=sm_62 -gencode arch=compute_70,code=sm_70" ;;
      *) echo "Unsupported CUDA_VERSION (CUDA_VERSION=$CUDA_VERSION), please report it to Kaldi mailing list, together with 'nvcc -h' or 'ptxas -h' which lists allowed -gencode values..."; exit 1 ;;
    esac

    echo "Using CUDA toolkit $CUDATKDIR (nvcc compiler and runtime libraries)"
    echo >> kaldi.mk
    echo "# CUDA configuration" >> kaldi.mk
    echo >> kaldi.mk
    echo CUDA = true >> kaldi.mk
    echo CUDATKDIR = $CUDATKDIR >> kaldi.mk
    echo "CUDA_ARCH = $CUDA_ARCH" >> kaldi.mk
    echo >> kaldi.mk

    # 64bit/32bit? We do not support cross compilation with CUDA so, use direct calls to uname -m here
    if [ "`uname -m`" == "x86_64" ]; then
      if [ "`uname`" == "Darwin" ]; then
        sed 's/lib64/lib/g' < makefiles/cuda_64bit.mk >> kaldi.mk
      else
        cat makefiles/cuda_64bit.mk >> kaldi.mk
      fi
    elif [ "`uname -m`" == "ppc64le" ]; then
      cat makefiles/cuda_64bit.mk >> kaldi.mk
    else
      cat makefiles/cuda_32bit.mk >> kaldi.mk
    fi

  else
    echo "CUDA will not be used! If you have already installed cuda drivers "
    echo "and cuda toolkit, try using --cudatk-dir=... option.  Note: this is"
    echo "only relevant for neural net experiments"
  fi
}

function linux_configure_speex {
  # Check whether the user has called tools/extras/install_speex.sh or not
  [ ! -z "$SPEEXROOT" ] || SPEEXROOT=`pwd`/../tools/speex
  [ ! -z "$SPEEXLIBDIR" ] || SPEEXLIBDIR="$SPEEXROOT"/lib
  [ ! -z "$SPEEXINCDIR" ] || SPEEXINCDIR="$SPEEXROOT"/include
  static_speex=$1
  if [ "foo"$static_speex == "foo" ]; then
    static_speex=false
  fi

  if $static_speex; then
    spx_type=a
  else
    spx_type=so
  fi
  if [ ! -f "$SPEEXLIBDIR/libspeex.${spx_type}" ];then
    echo "Info: configuring Kaldi not to link with Speex (don't worry, it's only needed if you"
    echo "intend to use 'compress-uncompress-speex', which is very unlikely)"
    return
  fi

  if [ -f $SPEEXINCDIR/speex/speex.h ]; then
    echo >> kaldi.mk
    echo CXXFLAGS += -DHAVE_SPEEX -I${SPEEXINCDIR} >> kaldi.mk

    if $static_speex; then
      echo LDLIBS += $SPEEXLIBDIR/libspeex.a
    else
      echo LDLIBS += -L${SPEEXLIBDIR} -lspeex >> kaldi.mk
      echo LDFLAGS += -Wl,-rpath=${SPEEXLIBDIR} >> kaldi.mk
    fi

    echo "Successfully configured with Speex at $SPEEXROOT, (static=[$static_speex])"
  else
    echo "Speex will not be used. If you want to use it, run tools/extras/install_speex.sh first."
  fi
}

function linux_atlas_failure {
  echo ATLASINC = $ATLASROOT/include >> kaldi.mk
  echo ATLASLIBS = [somewhere]/liblapack.a [somewhere]/libcblas.a [somewhere]/libatlas.a [somewhere]/libf77blas.a $ATLASLIBDIR >> kaldi.mk
  echo >> kaldi.mk
  if [[ "$TARGET_ARCH" == arm* ]]; then
    cat makefiles/linux_atlas_arm.mk >> kaldi.mk
  elif [[ "$TARGET_ARCH" == ppc64le ]]; then
    cat makefiles/linux_atlas_ppc64le.mk >> kaldi.mk
  else
    cat makefiles/linux_atlas.mk >> kaldi.mk
  fi
  echo "** $* ***"
  echo "**  ERROR   **"
  echo "** Configure cannot proceed automatically."
  echo "**  If you know that you have ATLAS installed somewhere on your machine, you"
  echo "** may be able to proceed by replacing [somewhere] in kaldi.mk with a directory."
  echo "**  If you have sudo (root) access you could install the ATLAS package on your"
  echo "** machine, e.g. 'sudo apt-get install libatlas-dev libatlas-base-dev' or"
  echo "** 'sudo yum install atlas.x86_64' or 'sudo zypper install libatlas3-devel',"
  echo "** or on cygwin, install atlas from the installer GUI; and then run ./configure"
  echo "** again."
  echo "**"
  echo "**  Otherwise (or if you prefer OpenBLAS for speed), you could go the OpenBLAS"
  echo "** route: cd to ../tools, type 'extras/install_openblas.sh', cd back to here,"
  echo "** and type './configure  --openblas-root=../tools/OpenBLAS/install'"
  exit 1;
}

function linux_check_static {
  # will exit with success if $dir seems to contain ATLAS libraries with
  # right architecture (compatible with default "nm")
  echo "int main(void) { return 0; }" > test_linking.cc;
  if [ -f $dir/libatlas.a ]; then # candidate...
    # Note: on the next line, the variable assignment
    # LANG=en_US should apply just to the program called on that line.
    if LANG=en_US $CXX -o test_linking test_linking.cc -u ATL_flushcache $dir/libatlas.a 2>&1 | grep -i "incompatible" >/dev/null; then
      echo "Directory $dir may contain ATLAS libraries but seems to be wrong architecture";
      rm test_linking test_linking.cc 2>/dev/null
      return 1;
    fi
    rm test_linking test_linking.cc 2>/dev/null
    return 0;
  else
    rm test_linking.cc
    return 1;
  fi
}

function linux_configure_debian_ubuntu {
  m=$1
  ATLASLIBS="/usr/lib$m/atlas-base/libatlas.so.3gf  /usr/lib$m/atlas-base/libf77blas.so.3gf /usr/lib$m/atlas-base/libcblas.so.3gf  /usr/lib$m/atlas-base/liblapack_atlas.so.3gf"
  for f in $ATLASLIBS; do
    [ ! -f $f ] && return 1;
  done
  lapacklib=$(echo $ATLASLIBS | awk '{print $NF}')
  if ! nm --dynamic $lapacklib | grep ATL_cgetrf >/dev/null; then
    exit 1;
  fi
  echo ATLASINC = $ATLASROOT/include >> kaldi.mk
  echo ATLASLIBS = $ATLASLIBS >> kaldi.mk
  echo >> kaldi.mk
  if [[ "$TARGET_ARCH" == arm* ]]; then
    cat makefiles/linux_atlas_arm.mk >> kaldi.mk
   elif [[ "$TARGET_ARCH" == ppc64le ]]; then
    cat makefiles/linux_atlas_ppc64le.mk >> kaldi.mk
  else
    cat makefiles/linux_atlas.mk >> kaldi.mk
  fi
  echo "Successfully configured for Debian/Ubuntu Linux [dynamic libraries] with ATLASLIBS =$ATLASLIBS"
  $use_cuda && configure_cuda
  linux_configure_speex
}

function linux_configure_debian_ubuntu3 {
  ATLASLIBS="/usr/lib/libatlas.so.3  /usr/lib/libf77blas.so.3 /usr/lib/libcblas.so.3  /usr/lib/liblapack_atlas.so.3"
  for f in $ATLASLIBS; do
    [ ! -f $f ] && return 1;
  done
  lapacklib=$(echo $ATLASLIBS | awk '{print $NF}')
  if ! nm --dynamic $lapacklib | grep ATL_cgetrf >/dev/null; then
    exit 1;
  fi
  echo ATLASINC = $ATLASROOT/include >> kaldi.mk
  echo ATLASLIBS = $ATLASLIBS >> kaldi.mk
  echo >> kaldi.mk
  if [[ "$TARGET_ARCH" == arm* ]]; then
    cat makefiles/linux_atlas_arm.mk >> kaldi.mk
  elif [[ "$TARGET_ARCH" == ppc64le ]]; then
    cat makefiles/linux_atlas_ppc64le.mk >> kaldi.mk
  else
    cat makefiles/linux_atlas.mk >> kaldi.mk
  fi
  echo "Successfully configured for Debian/Ubuntu Linux [dynamic libraries] with ATLASLIBS =$ATLASLIBS"
  $use_cuda && configure_cuda
  linux_configure_speex
}

function linux_configure_debian7 {
  ATLASLIBS="/usr/lib/atlas-base/libatlas.so.3.0 /usr/lib/atlas-base/libf77blas.so.3.0 /usr/lib/atlas-base/libcblas.so.3 /usr/lib/atlas-base/liblapack_atlas.so.3"
  for f in $ATLASLIBS; do
    [ ! -f $f ] && return 1;
  done
  lapacklib=$(echo $ATLASLIBS | awk '{print $NF}')
  if ! nm --dynamic $lapacklib | grep ATL_cgetrf >/dev/null; then
    exit 1;
  fi
  libdir=$(dirname $(echo $ATLASLIBS | awk '{print $1}'))
  [ -z "$libdir" ] && echo "Error getting libdir in linux_configure_debian7" && exit 1;
  echo ATLASINC = $ATLASROOT/include >> kaldi.mk
  echo ATLASLIBS = $ATLASLIBS -Wl,-rpath=$libdir >> kaldi.mk
  echo >> kaldi.mk
  if [[ "$TARGET_ARCH" == arm* ]]; then
    cat makefiles/linux_atlas_arm.mk >> kaldi.mk
  elif [[ "$TARGET_ARCH" == ppc64le ]]; then
    cat makefiles/linux_atlas_ppc64le.mk >> kaldi.mk
  else
    cat makefiles/linux_atlas.mk >> kaldi.mk
  fi
  echo "Successfully configured for Debian 7 [dynamic libraries] with ATLASLIBS =$ATLASLIBS"
  $use_cuda && configure_cuda
  linux_configure_speex
}

function linux_configure_redhat {
  m=$1  # 64 or empty.
  ATLASLIBS="/usr/lib$m/atlas/libatlas.so.3 /usr/lib$m/atlas/libf77blas.so.3 /usr/lib$m/atlas/libcblas.so.3 /usr/lib$m/atlas/libclapack.so.3"
  for f in $ATLASLIBS; do
    [ ! -f $f ] && return 1;
  done
  libdir=$(dirname $(echo $ATLASLIBS | awk '{print $1}'))
  [ -z "$libdir" ] && echo "Error getting libdir in linux_configure_redhat" && exit 1;
  echo ATLASINC = $ATLASROOT/include >> kaldi.mk
  echo ATLASLIBS = $ATLASLIBS -Wl,-rpath=$libdir >> kaldi.mk
  echo >> kaldi.mk
  if [[ "$TARGET_ARCH" == arm* ]]; then
    cat makefiles/linux_atlas_arm.mk >> kaldi.mk
  elif [[ "$TARGET_ARCH" == ppc64le ]]; then
    cat makefiles/linux_atlas_ppc64le.mk >> kaldi.mk
  else
    cat makefiles/linux_atlas.mk >> kaldi.mk
  fi
  echo "Successfully configured for red hat [dynamic libraries] with ATLASLIBS =$ATLASLIBS"
  $use_cuda && configure_cuda
}

function linux_configure_redhat_fat {
  # This is for when only two so-called 'fat' ATLAS libs are provided:
  # libsatlas.so.3 and libtatlas.so.3.
  # See http://stackoverflow.com/questions/13439296/build-shared-libraries-in-atlas.
  m=$1  # 64 or empty.
  ATLASLIBS="/usr/lib$m/atlas/libsatlas.so.3 /usr/lib$m/atlas/libtatlas.so.3"
  for f in $ATLASLIBS; do
    [ ! -f $f ] && return 1;
  done
  libdir=$(dirname $(echo $ATLASLIBS | awk '{print $1}'))
  [ -z "$libdir" ] && echo "Error getting libdir in linux_configure_redhat_fat" && exit 1;
  echo ATLASINC = $ATLASROOT/include >> kaldi.mk
  echo ATLASLIBS = $ATLASLIBS -Wl,-rpath=$libdir >> kaldi.mk
  echo >> kaldi.mk
  if [[ "$TARGET_ARCH" == arm* ]]; then
    cat makefiles/linux_atlas_arm.mk >> kaldi.mk
  elif [[ "$TARGET_ARCH" == ppc64le ]]; then
    cat makefiles/linux_atlas_ppc64le.mk >> kaldi.mk
  else
    cat makefiles/linux_atlas.mk >> kaldi.mk
  fi
  echo "Successfully configured for red hat [dynamic libraries, fat] with ATLASLIBS =$ATLASLIBS"
  $use_cuda && configure_cuda
}

function linux_configure_static {
  if $threaded_atlas; then pt=pt; else pt=""; fi

  if [ -z $ATLASLIBDIR ]; then # Note: it'll pick up the last one below.
    for dir in /usr{,/local}/lib{64,}{,/atlas,/atlas-sse2,/atlas-sse3} \
       /usr/local/atlas/lib{,64} `pwd`/../tools/ATLAS/build/install/lib/ $ATLASROOT/lib; do
     linux_check_static &&  ATLASLIBDIR=$dir
    done
    if [ -z $ATLASLIBDIR ]; then # Note: it'll pick up the last one below.
      echo "Could not find libatlas.a in any of the generic-Linux places, but we'll try other stuff..."
      return 1;
    fi
  elif [ ! -f $ATLASLIBDIR/libatlas.a ]; then
    echo "Could not find libatlas.a in '$ATLASLIBDIR'"
    return 1;
  fi
  echo "Validating presence of ATLAS libs in $ATLASLIBDIR"
  ATLASLIBS=
  # The Lapack part of ATLAS seems to appear under various different names.. but it
  # should always have symbols like ATL_cgetrf defined, so we test for this,
  # for all the names we have encountered.
  for libname in liblapack liblapack_atlas  libclapack; do
    if [ -f $ATLASLIBDIR/${libname}.a -a "$ATLASLIBS" == "" ]; then
      if nm  $ATLASLIBDIR/${libname}.a  | grep ATL_cgetrf >/dev/null; then
         ATLASLIBS=$ATLASLIBDIR/${libname}.a
         echo "Using library $ATLASLIBS as ATLAS's CLAPACK library."
      fi
    fi
  done
  if [ "$ATLASLIBS" == "" ]; then
    echo Could not find any libraries $ATLASLIBDIR/{liblapack,liblapack_atlas,libclapack} that seem to be an ATLAS CLAPACK library.
    return 1;
  fi

  for x in lib${pt}cblas.a libatlas.a lib${pt}f77blas.a; do
    if [ ! -f $ATLASLIBDIR/$x ]; then
      echo "Configuring static ATLAS libraries failed: Could not find library $x in directory $ATLASLIBDIR"
      return 1;
    fi
    ATLASLIBS="$ATLASLIBS $ATLASLIBDIR/$x"
  done
  if $threaded_atlas; then ATLASLIBS="$ATLASLIBS"; fi

  echo ATLASINC = $ATLASROOT/include >> kaldi.mk
  echo ATLASLIBS = $ATLASLIBS >> kaldi.mk
  echo >> kaldi.mk
  if [[ "$TARGET_ARCH" == arm* ]]; then
    cat makefiles/linux_atlas_arm.mk >> kaldi.mk
  elif [[ "$TARGET_ARCH" == ppc64le ]]; then
    cat makefiles/linux_atlas_ppc64le.mk >> kaldi.mk
  else
    cat makefiles/linux_atlas.mk >> kaldi.mk
  fi
  $use_cuda && configure_cuda
  linux_configure_speex
  echo "Successfully configured for Linux [static libraries] with ATLASLIBS =$ATLASLIBS"
}

function linux_check_dynamic {
  # will exit with success if $dir seems to contain ATLAS libraries with
  # right architecture (compatible with default "nm")
  if $threaded_atlas; then pt=t; else pt=s; fi
  for atlas_libname in libatlas.so lib${pt}atlas.so; do
    if [ -f $dir/$atlas_libname ]; then # candidate...
      if nm --dynamic $dir/$atlas_libname 2>&1 | grep "File format not recognized" >/dev/null; then
        echo "Directory $dir may contain dynamic ATLAS libraries but seems to be wrong architecture";
        return 1;
      fi
        echo "Atlas found in $dir";
        return 0;
      fi
  done
  # echo "... no {libatlas,lib${pt}atlas}.so in $dir";
  return 1;
}

function linux_configure_dynamic {
  if $threaded_atlas; then pt=t; else pt=s; fi # relevant to "fat" libraries, will change later for separate ones
  if [ -z $ATLASLIBDIR ]; then # Note: it'll pick up the last one below.
    for dir in /usr{,/local}/lib{,64}{,/atlas,/atlas-sse2,/atlas-sse3} \
      `pwd`/../tools/ATLAS/build/install/lib/ $ATLASROOT/lib; do
      linux_check_dynamic && ATLASLIBDIR=$dir && ATLASLIBNAME=$atlas_libname
    done
    if [ -z $ATLASLIBDIR -o -z $ATLASLIBNAME ]; then
      echo "Could not find {libatlas,lib${pt}atlas}.so in any of the obvious places, will most likely try static:"
      return 1;
    fi
  fi

  # If using "fat" libraries we only need one file to link against
  if [ $ATLASLIBNAME != libatlas.so ]; then
    if [ -f $ATLASLIBDIR/$ATLASLIBNAME ]; then
      ATLASLIBS="$ATLASLIBDIR/$ATLASLIBNAME"
    else
      echo "Configuring dynamic ATLAS library failed: library $ATLASLIBNAME not found in $ATLASLIBDIR"
      return 1;
    fi
  else  # with "thin" libraries, we have several object to link against, and different single/multi-thread names
    if $threaded_atlas; then pt=pt; else pt=""; fi
    echo "Validating presence of ATLAS libs in $ATLASLIBDIR"
    ATLASLIBS=
    # The Lapack part of ATLAS seems to appear under various different names.. but it
    # should always have symbols like ATL_cgetrf defined, so we test for this,
    # for all the names we have encountered.
    for libname in lapack lapack_atlas  clapack; do
      if [ -f $ATLASLIBDIR/lib${libname}.so -a "$ATLASLIBS" == "" ]; then
        if nm  --dynamic $ATLASLIBDIR/lib${libname}.so  | grep ATL_cgetrf >/dev/null; then
           ATLASLIBS="$ATLASLIBDIR/lib${libname}.so"
           echo "Using library $ATLASLIBS as ATLAS's CLAPACK library."
        fi
      fi
    done
    if [ "$ATLASLIBS" == "" ]; then
      echo Could not find any libraries $ATLASLIBDIR/{liblapack,liblapack_atlas,libclapack} that seem to be an ATLAS CLAPACK library.
      return 1;
    fi

    for x in ${pt}cblas atlas ${pt}f77blas; do
      if [ ! -f $ATLASLIBDIR/lib$x.so ]; then
        echo "Configuring dynamic ATLAS libraries failed: Could not find library $x in directory $ATLASLIBDIR"
        return 1;
      fi
      ATLASLIBS="$ATLASLIBS $ATLASLIBDIR/lib${x}.so"
    done
    if $threaded_atlas; then ATLASLIBS="$ATLASLIBS"; fi
  fi

  echo ATLASINC = $ATLASROOT/include >> kaldi.mk
  echo ATLASLIBS = $ATLASLIBS >> kaldi.mk
  echo >> kaldi.mk
  if [[ "$TARGET_ARCH" == arm* ]]; then
    cat makefiles/linux_atlas_arm.mk >> kaldi.mk
  elif [[ "$TARGET_ARCH" == ppc64le ]]; then
    cat makefiles/linux_atlas_ppc64le.mk >> kaldi.mk
  else
    cat makefiles/linux_atlas.mk >> kaldi.mk
  fi
  $use_cuda && configure_cuda
  linux_configure_speex
  echo "Successfully configured for Linux [dynamic libraries] with ATLASLIBS =$ATLASLIBS"
}

#############################    CONFIGURATION    #############################

# If configuration sets any of these variables, we will switch the external
# math library. Here we unset them so that we can check later.
unset MKLROOT
unset CLAPACKROOT
unset OPENBLASROOT
unset MKLLIBDIR

# This variable identifies the type of system where built programs and
# libraries will run. It is set by the configure script when cross compiling.
unset HOST

# These environment variables can be used to override the default toolchain.
CXX=${CXX:-g++}
AR=${AR:-ar}
AS=${AS:-as}
RANLIB=${RANLIB:-ranlib}

# These environment variables can be used to provide additional flags to the
# compiler/linker. We want these flags to override the flags determined by the
# configure script, so we append them to the appropriate variables (CXXFLAGS,
# LDFLAGS and LDLIBS) after those variables are set by the configure script.
ENV_CXXFLAGS=$CXXFLAGS
ENV_LDFLAGS=$LDFLAGS
ENV_LDLIBS=$LDLIBS

# Default configuration
double_precision=false
dynamic_kaldi=false
use_cuda=true
static_fst=false
static_math=false
threaded_atlas=false
mkl_threading=sequential
android=false

MATHLIB='ATLAS'
ATLASROOT=`rel2abs ../tools/ATLAS/`
FSTROOT=`rel2abs ../tools/openfst`

# Save the command line to include in kaldi.mk
cmd_line="$0 $@"

while [ $# -gt 0 ];
do
  case "$1" in
  --help)
    usage; exit 0 ;;
  --version)
    echo $CONFIGURE_VERSION; exit 0 ;;
  --static)
    dynamic_kaldi=false;
    static_math=true;
    static_fst=true;
    shift ;;
  --shared)
    dynamic_kaldi=true;
    static_math=false;
    static_fst=false;
    shift ;;
  --double-precision)
    double_precision=true;
    shift ;;
  --double-precision=yes)
    double_precision=true;
    shift ;;
  --double-precision=no)
    double_precision=false;
    shift ;;
  --atlas-root=*)
    ATLASROOT=`read_dirname $1`;
    shift ;;
  --threaded-atlas)
    threaded_atlas=true;
    shift ;;
  --threaded-atlas=yes)
    threaded_atlas=true;
    shift ;;
  --threaded-atlas=no)
    threaded_atlas=false;
    shift ;;
  --threaded-math)
    threaded_atlas=true;
    mkl_threading=iomp
    shift ;;
  --threaded-math=yes)
    threaded_atlas=true;
    mkl_threading=iomp
    shift ;;
  --threaded-math=no)
    threaded_atlas=false;
    mkl_threading=sequential
    shift ;;
  --use-cuda)
    use_cuda=true;
    shift ;;
  --use-cuda=yes)
    use_cuda=true;
    shift ;;
  --use-cuda=no)
    use_cuda=false;
    shift ;;
  --static-math)
    static_math=true;
    shift ;;
  --static-math=yes)
    static_math=true;
    shift ;;
  --static-math=no)
    static_math=false;
    shift ;;
  --static-fst)
    static_fst=true;
    shift ;;
  --static-fst=yes)
    static_fst=true;
    shift ;;
  --static-fst=no)
    static_fst=false;
    shift ;;
  --mkl-threading=sequential)
    threaded_atlas=false;
    mkl_threading=sequential;
    shift ;;
  --mkl-threading=*)
    mkl_threading=`expr "X$1" : '[^=]*=\(.*\)'`;
    threaded_atlas=true;
    shift ;;
  --fst-root=*)
    FSTROOT=`read_dirname $1`;
    shift ;;
  --clapack-root=*)
    CLAPACKROOT=`read_dirname $1`;
    shift ;;
  --openblas-root=*)
    OPENBLASROOT=`read_dirname $1`;
    shift ;;
  --mkl-root=*)
    MKLROOT=`read_dirname $1`;
    shift ;;
  --mkl-libdir=*)
    MKLLIBDIR=`read_dirname $1`;
    shift ;;
  --speex-root=*)
    SPEEXROOT=`read_dirname $1`;
    shift ;;
  --speex-libdir=*)
    SPEEXLIBDIR=`read_dirname $1`;
    shift ;;
  --speex-incdir=*)
    SPEEXINCDIR=`read_dirname $1`;
    shift ;;
  --omp-libdir=*)
    OMPLIBDIR=`read_dirname $1`;
    shift ;;
  --mathlib=*)
    MATHLIB=`expr "X$1" : '[^=]*=\(.*\)'`;
    shift ;;
  --cudatk-dir=*)
    CUDATKDIR=`read_dirname $1`;
    shift ;; #CUDA is used in src/cudamatrix and src/nnet{,bin} only
  --fst-version=*)
    OPENFST_VER=`expr "X$1" : '[^=]*=\(.*\)'`;
    shift;;
  --host=*)
    # The type of system where built programs and libraries will run.
    # It should be in the format cpu-vendor-os. If specified, this script
    # will infer the target architecture from the specified host triple.
    HOST=`expr "X$1" : '[^=]*=\(.*\)'`;
    shift ;;
  --android-incdir=*)
    android=true;
    threaded_math=false;
    static_math=true;
    static_fst=true;
    dynamic_kaldi=false;
    MATHLIB='OPENBLAS';
    ANDROIDINC=`read_dirname $1`;
    shift;;
  *)  echo "Unknown argument: $1, exiting"; usage; exit 1 ;;
  esac
done

# The idea here is that if you change the configuration options from using
# CUDA to not using it, or vice versa, we want to recompile all parts of the
# code that may use a GPU. Touching this file is a way to force this.
touch cudamatrix/cu-common.h 2>/dev/null

if $android && [[ "$CXX" != *clang++*  ]] ; then
  failure "Android build requires clang++. Make sure you have clang++ installed
  on your system and then override the default compiler by setting CXX, e.g.
  CXX=clang++ ./configure"
fi

# If HOST is set
# 1. We prepend it to CXX, AR, AS and RANLIB.
# 2. We parse the target architecture from the HOST triple.
# Otherwise we set the target architecture to the output of `uname -m`.
if is_set $HOST; then
  CXX="$HOST-$CXX"
  AR="$HOST-$AR"
  AS="$HOST-$AS"
  RANLIB="$HOST-$RANLIB"

  # The host triple will be something like "armv8-rpi3-linux-gnueabihf". We
  # need the first field which is the target architecture for this build. The
  # following command will take the host triple "armv8-rpi3-linux-gnueabihf"
  # and return ["armv8", "rpi3", "linux", "gnueabihf"] in PARTS.
  IFS='-' read -ra PARTS <<< "$HOST"
  # The first field in the PARTS list is the target architecture.
  TARGET_ARCH="$PARTS"
  if [[ "$TARGET_ARCH" != arm* && "$TARGET_ARCH" != ppc64le && "$TARGET_ARCH" != x86* ]] ; then
    # We currently only support building for x86[_64], arm*, and ppc64le.
    # If TARGET_ARCH was read from the HOST variable, it must be one of these.
    failure "$TARGET_ARCH is not a supported architecture.
             Supported architectures: x86[_64], arm*, ppc64le."
  fi
else
  TARGET_ARCH="`uname -m`"
fi

# If one of these variables is set, we switch the external math library.
is_set $MKLLIBDIR && echo "Configuring KALDI to use MKL" && export MATHLIB="MKL"
is_set $MKLROOT && echo "Configuring KALDI to use MKL"&& export MATHLIB="MKL"
is_set $CLAPACKROOT && echo "Configuring KALDI to use CLAPACK"&& export MATHLIB="CLAPACK"
is_set $OPENBLASROOT && echo "Configuring KALDI to use OPENBLAS"&& export MATHLIB="OPENBLAS"

echo "Configuring ..."

# Back up the old kaldi.mk in case we modified it
if [ -f kaldi.mk ]; then
  echo "Backing up kaldi.mk to kaldi.mk.bak ..."
  cp kaldi.mk kaldi.mk.bak
fi

# Generate the new kaldi.mk file
echo "# This file was generated using the following command:" > kaldi.mk
echo "# $cmd_line" >> kaldi.mk
echo >> kaldi.mk
echo "CONFIGURE_VERSION := $CONFIGURE_VERSION" >> kaldi.mk
echo >> kaldi.mk

echo "# Toolchain configuration" >> kaldi.mk
echo >> kaldi.mk
echo "CXX = $CXX" >> kaldi.mk
echo "AR = $AR" >> kaldi.mk
echo "AS = $AS" >> kaldi.mk
echo "RANLIB = $RANLIB" >> kaldi.mk
echo >> kaldi.mk

echo "Checking compiler $CXX ..."
check_compiler $CXX

echo "# Base configuration" >> kaldi.mk
echo >> kaldi.mk
if $dynamic_kaldi ; then
  KALDILIBDIR=`pwd`/lib
  echo "KALDI_FLAVOR := dynamic" >> kaldi.mk
  echo "KALDILIBDIR := $KALDILIBDIR" >> kaldi.mk
fi
if $double_precision; then
  echo "DOUBLE_PRECISION = 1" >> kaldi.mk
else
  echo "DOUBLE_PRECISION = 0" >> kaldi.mk
fi
echo "Checking OpenFst library in $FSTROOT ..."
if [ ! -f $FSTROOT/include/fst/fst.h  ]; then
  failure "Could not find file $FSTROOT/include/fst/fst.h:
  you may not have installed OpenFst. See ../tools/INSTALL"
fi
OPENFST_VER=${OPENFST_VER:-$(grep 'PACKAGE_VERSION' $FSTROOT/Makefile | sed -e 's:.*= ::')}
OPENFST_VER_NUM=$(echo $OPENFST_VER | sed 's/\./ /g' | xargs printf "%d%02d%02d")
if [ $OPENFST_VER_NUM -lt 10600 ]; then
  failure "OpenFst-$OPENFST_VER is not supported. You need OpenFst >= 1.6.0.)"
fi
echo "OPENFSTINC = $FSTROOT/include" >> kaldi.mk
if $static_fst ; then
  OPENFSTLIBS="$FSTROOT/lib/libfst.a"
else
  if [ "`uname`" == "Darwin"  ]; then
    OPENFSTLIBS="$FSTROOT/lib/libfst.dylib"
    OPENFSTLDFLAGS="-Wl,-rpath -Wl,${FSTROOT}/lib"
  elif [ "`uname`" == "Linux" ]; then
    OPENFSTLIBS="$FSTROOT/lib/libfst.so"
    OPENFSTLDFLAGS="-Wl,-rpath=${FSTROOT}/lib"
  else
    failure "Dynamic libraries are not supported on this platform.
             Run configure with --static --static-fst=no flag."
  fi
fi
if [ ! -f "$OPENFSTLIBS" ]; then
  failure "Static=[$static_fst] OpenFST library not found:  See ../tools/INSTALL"
fi
echo "OPENFSTLIBS = $OPENFSTLIBS" >> kaldi.mk
echo "OPENFSTLDFLAGS = $OPENFSTLDFLAGS" >> kaldi.mk
echo >> kaldi.mk

# OS-specific steps given below append to kaldi.mk
echo "Doing OS specific configurations ..."

if $android ; then
  if [ -z $ANDROIDINC ] ;  then
    failure "--android-incdir must be specified for android builds."
  fi

  if ! is_set $HOST; then
    failure "HOST must be specified for android builds."
  fi

  OPENBLASROOT=`rel2abs "$OPENBLASROOT"`
  if [ -z "$OPENBLASROOT" ]; then
    failure "The location of OPENBLAS must be specified for android builds
             using --openblas-root (and it must exist)"
  fi
  if [ ! -f $OPENBLASROOT/lib/libopenblas.a ]; then
    failure "Expected to find the file $OPENBLASROOT/lib/libopenblas.a"
  fi
  echo "Using OpenBLAS as the linear algebra library."

  OPENBLASLIBS="$OPENBLASROOT/lib/libopenblas.a $OPENBLASROOT/lib/libclapack.a $OPENBLASROOT/lib/liblapack.a $OPENBLASROOT/lib/libblas.a $OPENBLASROOT/lib/libf2c.a"
  echo "OPENBLASINC = $OPENBLASROOT/include" >> kaldi.mk
  echo "OPENBLASLIBS = $OPENBLASLIBS" >> kaldi.mk
  echo "ANDROIDINC = $ANDROIDINC" >> kaldi.mk

  cat makefiles/android_openblas.mk >> kaldi.mk

  echo "Successfully configured for Android with OpenBLAS from $OPENBLASROOT."

elif [ "`uname`" == "Darwin" ]; then
  # Check for Darwin first, because we later call uname -o (for Cygwin)
  # which crashes on Darwin.

  echo "On Darwin: Checking for Accelerate framework ..."
  if [ ! -e /System/Library/Frameworks/Accelerate.framework ]; then
    failure "Need the Accelerate framework to compile on Darwin."
  fi
  OSX_VER=$(sw_vers | grep ProductVersion | awk '{print $2}' | awk '{split($0,a,"."); print a[1] "." a[2]; }')
  OSX_VER_NUM=$(echo $OSX_VER | sed 's/\./ /g' | xargs printf "%d%02d")
  echo "Configuring for OS X version $OSX_VER ..."
  if [ $OSX_VER_NUM -ge 1005 ]; then
    cat makefiles/darwin.mk >> kaldi.mk
  else
    failure "Mac OS X version '$OSX_VER' is not supported."
  fi

  if [ $OSX_VER_NUM == 1011 ]; then
    echo "**BAD WARNING**: You are using OS X El Capitan.  Some versions of this OS"
    echo "**BAD WARNING**: have a bug in the BLAS implementation that affects Kaldi."
    echo "**BAD WARNING**: After compiling, cd to matrix/ and type 'make test'.  The"
    echo "**BAD WARNING**: test will fail if the problem exists in your version. "
    echo "**BAD WARNING**: Eventually this issue will be fixed by system updates from"
    echo "**BAD WARNING**: Apple.  Unexplained crashes with reports of NaNs will"
    echo "**BAD WARNING**: be caused by this bug, but some recipes will (sometimes) work."
    sleep 1; echo -n .; sleep 1; echo -n .; sleep 1; echo .
  fi
  echo "Successfully configured for Darwin with Accelerate framework."
  $use_cuda && configure_cuda

elif [ "`uname -o`" == "Cygwin"  ]; then
  echo "On Cygwin: Checking for linear algebra libraries ..."
  if [ ! -f ../tools/CLAPACK/clapack.h ]; then
      failure "could not find file ../tools/CLAPACK/clapack.h"
  fi
  if [ ! -f /usr/lib/lapack/cygblas-0.dll ]; then
     failure "please first install package liblapack0"
  fi
  cat makefiles/cygwin.mk >> kaldi.mk
  echo "Successfully configured for Cygwin with CLAPACK."

elif [ "`uname`" == "Linux" ]; then
  echo "On Linux: Checking for linear algebra header files ..."
  if [ "$MATHLIB" == "ATLAS" ]; then
    if [ ! -f $ATLASROOT/include/cblas.h ] || [ ! -f $ATLASROOT/include/clapack.h ] ; then
      failure "Could not find required header files cblas.h or clapack.h in ATLAS dir '$ATLASROOT/include'"
    fi
    echo "Using ATLAS as the linear algebra library."

    # Finding out where the libraries are located:
    # First we look for the static libraries and then look for dynamic ones.
    # We're looking for four libraries, all in the same directory, named
    # libcblas.a, libatlas.a, libf77blas.a, and a library that's variously
    # named liblapack.a, libclapack.a, or liblapack_atlas.a, but which exports
    # the symbol ATL_cgetrf.
    # Note: there is a different type of ATLAS installation that is not
    # covered.  We saw a case where there was a directory called /usr/lib/atlas
    # containing {liblapack.a,libblas.a}, and linking against just these two
    # libraries worked.

    if $static_math; then
      # Prefer static to dynamic math.
      linux_configure_static || \
        linux_configure_debian_ubuntu3 || \
        linux_configure_dynamic || \
        linux_configure_debian_ubuntu 64 || \
        linux_configure_debian_ubuntu || \
        linux_configure_debian7 || \
        linux_configure_redhat 64 || \
        linux_configure_redhat || \
        linux_configure_redhat_fat 64 || \
        linux_configure_redhat_fat || \
        linux_atlas_failure "Failed to configure ATLAS libraries";
    else
      # Prefer dynamic to static math.
      linux_configure_debian_ubuntu3 || \
        linux_configure_dynamic || \
        linux_configure_static || \
        linux_configure_debian_ubuntu 64 || \
        linux_configure_debian_ubuntu || \
        linux_configure_debian7 || \
        linux_configure_redhat 64 || \
        linux_configure_redhat || \
        linux_configure_redhat_fat 64 || \
        linux_configure_redhat_fat || \
        linux_atlas_failure "Failed to configure ATLAS libraries";
    fi

  elif [ "$MATHLIB" == "MKL" ]; then
    if [ "$TARGET_ARCH" != "x86_64" ]; then
      failure "MKL on Linux only supported for Intel(R) 64 architecture (x86_64).
      See makefiles/linux_64_mkl.mk to manually configure for other platforms."
    fi

    if  ( is_set "$MKLROOT" && ! is_set "$MKLLIBDIR" ); then
      echo -n "Configuring MKL library directory: "
      MKLLIBDIR=`linux_configure_mkllibdir $MKLROOT`
      if [ $? -ne 0 ]; then
        failure "MKL libraries could not be found. Please use the switch --mkl-libdir "
      else
        echo "Found: $MKLLIBDIR"
      fi
    fi

    MKL_LINK_LINE=`linux_configure_mkl_libraries "$MKLLIBDIR" $static_math $mkl_threading` || exit 1
    echo "MKL configured with threading: $mkl_threading, libs: $MKL_LINK_LINE"

    MKL_COMPILE_LINE=`linux_configure_mkl_includes "$MKLROOT" "$MKLLIBDIR"` || exit 1
    echo "MKL include directory configured as: $MKL_COMPILE_LINE"
    MKL_COMPILE_LINE=" -I${MKL_COMPILE_LINE} "

    THREADING_LINE=`linux_configure_mkl_threading $MKLROOT $MKLLIBDIR $static_math $mkl_threading` || exit 1
    EXTRA_LIBS=`linux_configure_mkl_extra $static_math $mkl_threading` || exit 1
    if [ ! -z "$THREADING_LINE" ] || [ ! -z "$EXTRA_LIBS" ]; then
      echo "MKL threading libraries configured as $THREADING_LINE $EXTRA_LIBS"
    fi

    echo "Using Intel MKL as the linear algebra library."
    (
      cd probe; rm -f mkl-test;
      g++ mkl-test.cc -o mkl-test $MKL_COMPILE_LINE $MKL_LINK_LINE $THREADING_LINE $EXTRA_LIBS || exit 1
      test -f ./mkl-test || exit 1
      ./mkl-test || exit 1
      cd ..
    ) || failure "Cannot validate the MKL switches"

    echo MKLROOT = $MKLROOT >> kaldi.mk
    if [ ! -z $MKLLIBDIR ]; then
      echo MKLLIB = $MKLLIBDIR >> kaldi.mk
    fi
    echo >> kaldi.mk
    check_exists makefiles/linux_x86_64_mkl.mk
    cat makefiles/linux_x86_64_mkl.mk >> kaldi.mk
    echo "MKLFLAGS = ${MKL_LINK_LINE} ${THREADING_LINE} $EXTRA_LIBS " >> kaldi.mk
    echo "Successfully configured for Linux with MKL libs from $MKLROOT"
    $use_cuda && configure_cuda
    linux_configure_speex

  elif [ "$MATHLIB" == "CLAPACK" ]; then
    if [ -z "$CLAPACKROOT" ]; then
      failure "Must specify the location of CLAPACK with --clapack-root option (and it must exist)"
    fi
    if [ ! -f ../tools/CLAPACK/clapack.h ]; then
      failure "could not find file ../tools/CLAPACK/clapack.h"
    fi
    if [ ! -d "$CLAPACKROOT" ]; then
      failure "The directory $CLAPACKROOT does not exist"
    fi
    # Also check for cblas.h and f2c.h
    echo "Using CLAPACK libs from $CLAPACKROOT as the linear algebra library."
    if [ ! -f makefiles/linux_clapack.mk ]; then
      failure "makefiles/linux_clapack.mk not found."
    fi
    if [[ "$TARGET_ARCH" == arm* ]]; then
      cat makefiles/linux_clapack_arm.mk >> kaldi.mk
    else
      cat makefiles/linux_clapack.mk >> kaldi.mk
    fi
    echo "Warning (CLAPACK): this part of the configure process is not properly tested and may not work."
    echo "Successfully configured for Linux with CLAPACK libs from $CLAPACKROOT"
    $use_cuda && configure_cuda
    linux_configure_speex

  elif [ "$MATHLIB" == "OPENBLAS" ]; then
    OPENBLASROOT=`rel2abs "$OPENBLASROOT"`
    if [ -z "$OPENBLASROOT" ]; then
      failure "Must specify the location of OPENBLAS with --openblas-root option (and it must exist)"
    fi
    if [ ! -f $OPENBLASROOT/lib/libopenblas.so ]; then
      failure "Expected to find the file $OPENBLASROOT/lib/libopenblas.so"
    fi
    echo "Your math library seems to be OpenBLAS from $OPENBLASROOT.  Configuring appropriately."
    if $static_math; then
      echo "Configuring static OpenBlas since --static-math=yes"
      OPENBLASLIBS="$OPENBLASROOT/lib/libopenblas.a -lgfortran"
    else
      echo "Configuring dynamically loaded OpenBlas since --static-math=no (the default)"
      OPENBLASLIBS="-L$OPENBLASROOT/lib -lopenblas -lgfortran -Wl,-rpath=$OPENBLASROOT/lib"
    fi
    echo "OPENBLASINC = $OPENBLASROOT/include" >> kaldi.mk
    echo "OPENBLASLIBS = $OPENBLASLIBS" >> kaldi.mk
    echo >> kaldi.mk
    if [[ "$TARGET_ARCH" == arm* ]]; then
      cat makefiles/linux_openblas_arm.mk >> kaldi.mk
    elif [[ "$TARGET_ARCH" == ppc64le ]]; then
      cat makefiles/linux_openblas_ppc64le.mk >> kaldi.mk
    else
      cat makefiles/linux_openblas.mk >> kaldi.mk
    fi
    echo "Successfully configured for Linux with OpenBLAS from $OPENBLASROOT"
    $use_cuda && configure_cuda
    linux_configure_speex

  else
    failure "Unsupported linear algebra library '$MATHLIB'"
  fi
else
  failure "Could not detect the platform or we have not yet worked out the
  appropriate configuration for this platform. Please contact the developers."
fi

# Append the flags set by environment variables last so they can be used
# to override the automatically generated configuration.
echo >> kaldi.mk
echo "# Environment configuration" >> kaldi.mk
echo >> kaldi.mk
if [ -n "$ENV_CXXFLAGS" ]; then echo "CXXFLAGS += $ENV_CXXFLAGS" >> kaldi.mk; fi
if [ -n "$ENV_LDFLAGS" ]; then echo "LDFLAGS += $ENV_LDFLAGS" >> kaldi.mk; fi
if [ -n "$ENV_LDLIBS" ]; then echo "LDLIBS += $ENV_LDLIBS" >> kaldi.mk; fi

# We check for slow exp implementation just before we exit. This check uses
# and possibly modifies the kaldi.mk file that we just generated.
check_for_slow_expf;
echo "SUCCESS"
echo "To compile: make clean -j; make depend -j; make -j"
echo " ... or e.g. -j 10, instead of -j, to use a specified number of CPUs"
exit 0;