NLOPT and Simulated annealing added
[openmx:openmx.git] / configure
1 #!/bin/bash
2
3 set -o errexit
4 set -o nounset
5
6 isdef() {
7     tmp1=/tmp/openmx.$$.cpp
8     tmp2=/tmp/openmx.$$.out
9     trap 'rm -f $tmp1 $tmp2' EXIT
10     cat >$tmp1 <<EOF
11 int main()
12 {
13 #ifndef $1
14 crash me
15 #endif
16         ; return 0;
17 }
18 EOF
19     $CXX -c $tmp1 -o $tmp2 > /dev/null 2>&1
20 }
21
22 compareVersions() {
23   typeset    IFS='.'
24   typeset -a v1=( $1 )
25   typeset -a v2=( $2 )
26   typeset    n diff
27
28   for (( n=0; n<4; n+=1 )); do
29     diff=$((v1[n]-v2[n]))
30     if [ $diff -ne 0 ] ; then
31       [ $diff -le 0 ] && echo '-1' || echo '1'
32       return
33     fi
34   done
35   echo  '0'
36 }
37
38 : ${R_HOME:=`R RHOME`}
39 if test -z "${R_HOME}"; then 
40    echo "could not determine R_HOME" 
41    exit 1 
42 fi 
43
44 REXEC=${R_HOME}/bin/R
45
46 eval $(grep R_HOME_DIR $REXEC | head -1)
47 Makeconf=$(find $R_HOME_DIR/etc -print | egrep 'Makeconf$' )
48 echo "Change default C/C++ compiler and default compile flags by editing $Makeconf"
49
50 CXX=$($REXEC CMD config CXX)
51
52 COMPILER_CXXFLAGS=
53 COMPILER_LDFLAGS=
54 arch_CXXFLAGS=
55 arch_LDFLAGS=
56 OPENMP_CXXFLAGS=
57 OPENMP_LDFLAGS=
58
59 if isdef __clang__; then
60     cxx_type=clang
61 elif isdef __llvm__; then
62     cxx_type=llvm
63 elif isdef __GNUC__; then
64     cxx_type=gcc
65 else
66     echo "Compiler unrecognized:"
67     $CXX --version
68     exit 1
69 fi
70
71 cxx_version=$($CXX -dumpversion | awk -F '.' '{print $1 "." $2}')
72
73 if [ "$cxx_type" = gcc ]; then
74     COMPILER_CXXFLAGS="-Wall -Wextra -Wno-unknown-pragmas -Wno-unused-parameter -Wvla"
75     if [ $(compareVersions $cxx_version 4.9) -gt -1 ]; then
76         COMPILER_CXXFLAGS="$COMPILER_CXXFLAGS -fdiagnostics-color=auto"
77     fi
78     if [ "${OPENMP-yes}" = yes ]; then
79         OPENMP_CXXFLAGS="-fopenmp"
80         OPENMP_LDFLAGS="-fopenmp"
81     fi
82 fi
83
84 host_cpu=$(uname -p)
85 if [ "x$host_cpu" = xunknown ]; then
86   # tanglu 1 has the machine architecture here
87   host_cpu=$(uname -m)
88 fi
89 case "$host_cpu" in
90     *86_64) omx_arch=x86_64 ;;
91     *86) omx_arch=x86 ;;
92     *)
93         echo "Host arch is not recognized: ${host_cpu}"
94         exit 1
95         ;;
96 esac
97
98 npsol_dir='inst/npsol'
99
100 if [ -r inst/npsol/checksum ]; then
101     os=$(uname -s)
102     case "$os" in
103         *Linux)
104             omx_os=linux
105             pick="linux/${omx_arch}/gcc${cxx_version}"
106             npsol_url="http://openmx.psyc.virginia.edu/packages/npsol/$pick/libnpsol.a"
107             if [ ! -f src/libnpsol.a ]; then
108                 curl -s -o src/libnpsol.a "$npsol_url"
109             fi
110             want=$(grep "$pick" inst/npsol/checksum | cut -f1 -d' ')
111             got=$(sha1sum src/libnpsol.a | cut -f1 -d' ')
112             if [ "x$got" != "x$want" ]; then
113                 rm -f src/libnpsol.a
114                 echo "WARNING: Failed to download libnpsol.a from $npsol_url"
115             else
116                 npsol_library="src"
117             fi
118             ;;
119         *Darwin)
120             omx_os=osx
121             arch_LDFLAGS="-mmacosx-version-min=10.4"
122             arch_CXXFLAGS="-mmacosx-version-min=10.4"
123             npsol_library="${npsol_dir}/osx"
124             ;;
125         *)
126             echo "Host os is not recognized: $os"
127             exit 1
128             ;;
129     esac
130 fi
131
132 echo "### Generated $(date)" > src/Makevars
133 echo >> src/Makevars
134
135 if [ ! -e inst/no-npsol ]; then
136     npsol_file="${npsol_library}/libnpsol.a"
137     if [ ! -f "$npsol_file" ]; then
138         echo "$npsol_file not found"
139         exit 1
140     else
141         echo "NPSOL_LDFLAGS=-L../${npsol_library} -lnpsol" >> src/Makevars
142     fi
143 fi
144
145 echo "NLOPT_LDFLAGS=-L/usr/local/lib -lnlopt" >> src/Makevars
146 echo "ARCH_SPECIFIC_LINKER_FLAGS=$arch_LDFLAGS" >> src/Makevars
147 echo "ARCH_SPECIFIC_COMPILER_FLAGS=$arch_CXXFLAGS" >> src/Makevars
148 echo "COMPILER_CXXFLAGS=$COMPILER_CXXFLAGS" >> src/Makevars
149 echo "COMPILER_LDFLAGS=$COMPILER_LDFLAGS" >> src/Makevars
150 echo "OPENMP_CXXFLAGS=$OPENMP_CXXFLAGS" >> src/Makevars
151 echo "OPENMP_LDFLAGS=$OPENMP_LDFLAGS" >> src/Makevars
152 echo >> src/Makevars
153 echo "# ---- appending Makevars.in ----" >> src/Makevars
154 echo >> src/Makevars
155 cat src/Makevars.in >> src/Makevars