Stop Roxygen from making Rd file for non-exported function
[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 xcheck_CXXFLAGS=
59 xcheck_LDFLAGS=
60
61 if isdef __clang__; then
62     cxx_type=clang
63 elif isdef __llvm__; then
64     cxx_type=llvm
65 elif isdef __GNUC__; then
66     cxx_type=gcc
67 else
68     echo "Compiler unrecognized:"
69     $CXX --version
70     exit 1
71 fi
72
73 cxx_version=$($CXX -dumpversion | awk -F '.' '{print $1 "." $2}')
74
75 if [ "$cxx_type" = gcc ]; then
76     COMPILER_CXXFLAGS="-Wall -Wextra -Wno-unknown-pragmas -Wno-unused-parameter -Wvla"
77     if [ $(compareVersions $cxx_version 4.9) -gt -1 ]; then
78         COMPILER_CXXFLAGS="$COMPILER_CXXFLAGS -fdiagnostics-color=auto"
79         COMPILER_LDFLAGS="$COMPILER_LDFLAGS"
80         xcheck_CXXFLAGS="-fsanitize=undefined"
81         xcheck_LDFLAGS="-fsanitize=undefined"
82     fi
83     if [ "${OPENMP-yes}" = yes ]; then
84         OPENMP_CXXFLAGS="-fopenmp"
85         OPENMP_LDFLAGS="-fopenmp"
86     fi
87 fi
88
89 host_cpu=$(uname -p)
90 if [ "x$host_cpu" = xunknown ]; then
91   # tanglu 1 has the machine architecture here
92   host_cpu=$(uname -m)
93 fi
94 case "$host_cpu" in
95     *86_64) omx_arch=x86_64 ;;
96     *86) omx_arch=x86 ;;
97     *)
98         echo "Host arch is not recognized: ${host_cpu}"
99         exit 1
100         ;;
101 esac
102
103 npsol_dir='inst/npsol'
104
105 if [ -r inst/npsol/checksum ]; then
106     os=$(uname -s)
107     case "$os" in
108         *Linux)
109             omx_os=linux
110             pick="linux/${omx_arch}/gcc${cxx_version}"
111             npsol_url="http://openmx.psyc.virginia.edu/packages/npsol/$pick/libnpsol.a"
112             if [ ! -f src/libnpsol.a ]; then
113                 curl -s -o src/libnpsol.a "$npsol_url"
114             fi
115             want=$(grep "$pick" inst/npsol/checksum | cut -f1 -d' ')
116             got=$(sha1sum src/libnpsol.a | cut -f1 -d' ')
117             if [ "x$got" != "x$want" ]; then
118                 rm -f src/libnpsol.a
119                 echo "WARNING: Failed to download libnpsol.a from $npsol_url"
120             else
121                 npsol_library="src"
122             fi
123             ;;
124         *Darwin)
125             omx_os=osx
126             arch_LDFLAGS="-mmacosx-version-min=10.4"
127             arch_CXXFLAGS="-mmacosx-version-min=10.4"
128             npsol_library="${npsol_dir}/osx"
129             ;;
130         *)
131             echo "Host os is not recognized: $os"
132             exit 1
133             ;;
134     esac
135 fi
136
137 echo "### Generated $(date)" > src/Makevars
138 echo >> src/Makevars
139
140 if [ ! -e inst/no-npsol -a -n "${npsol_library+1}" ]; then
141     npsol_file="${npsol_library}/libnpsol.a"
142     if [ ! -f "$npsol_file" ]; then
143         echo "$npsol_file not found"
144         exit 1
145     else
146         echo "NPSOL_LDFLAGS=-L../${npsol_library} -lnpsol" >> src/Makevars
147     fi
148 fi
149
150 if false && pkg-config --exists nlopt; then
151   echo "NLOPT_CXXFLAGS=-DHAS_NLOPT $(pkg-config --cflags nlopt)" >> src/Makevars
152   echo "NLOPT_LDFLAGS=$(pkg-config --libs nlopt)" >> src/Makevars
153 else
154   echo "NLOPT_CXXFLAGS=" >> src/Makevars
155   echo "NLOPT_LDFLAGS=" >> src/Makevars
156 fi
157
158 echo "ARCH_SPECIFIC_LINKER_FLAGS=$arch_LDFLAGS" >> src/Makevars
159 echo "ARCH_SPECIFIC_COMPILER_FLAGS=$arch_CXXFLAGS" >> src/Makevars
160 echo "COMPILER_CXXFLAGS=$COMPILER_CXXFLAGS" >> src/Makevars
161 echo "COMPILER_LDFLAGS=$COMPILER_LDFLAGS" >> src/Makevars
162 echo "OPENMP_CXXFLAGS=$OPENMP_CXXFLAGS" >> src/Makevars
163 echo "OPENMP_LDFLAGS=$OPENMP_LDFLAGS" >> src/Makevars
164 echo "xcheck_CXXFLAGS=$xcheck_CXXFLAGS" >> src/Makevars
165 echo "xcheck_LDFLAGS=$xcheck_LDFLAGS" >> src/Makevars
166 echo >> src/Makevars
167 echo "# ---- appending Makevars.in ----" >> src/Makevars
168 echo >> src/Makevars
169 cat src/Makevars.in >> src/Makevars