configure 11 KB
Newer Older
ulrich_y's avatar
ulrich_y committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#! /bin/bash
# configure script
# note: has nothing to do with GNU autoconf

exec 2> ${LOGFILE:-/dev/null} 3>&1
eval ${LOGFILE:+set -x}


shopt -s nullglob
export LC_ALL=C

CONF_DIR="${0%/*}"
CONF_OS="`uname -s`"
CONF_MACH="`uname -m`"
ulrich_y's avatar
ulrich_y committed
15 16 17
HAVE_GINAC=false
HAVE_MCC=false
CONF_BITS=
ulrich_y's avatar
ulrich_y committed
18

ulrich_y's avatar
ulrich_y committed
19 20 21 22 23 24
if [ "$CONF_OS" -eq "Darwin" ]; then
    CONF_PREFIX=/usr/local/
else
    CONF_PREFIX=/usr/
fi

ulrich_y's avatar
ulrich_y committed
25 26 27 28 29 30 31 32 33 34
DEBUG=false
for arg in "$@" ; do
    case "$arg" in
        --debug)
            DEBUG=true ;;
        --prefix=*)
            CONF_PREFIX="${arg#--prefix=}" ;;
        --host=*)
            CONF_TARGET="${arg#--host=}" ;;
        --with-ginac)
ulrich_y's avatar
ulrich_y committed
35
            HAVE_GINAC=true ;;
ulrich_y's avatar
ulrich_y committed
36
        --with-mcc)
ulrich_y's avatar
ulrich_y committed
37
            HAVE_MCC=true ;;
ulrich_y's avatar
ulrich_y committed
38 39
        --mmprefix=*)
            CONF_MMPREFIX="${arg#--mmprefix=}" ;;
ulrich_y's avatar
ulrich_y committed
40 41 42 43
        --32)
            CONF_BITS="-m32" ;;
        --64)
            CONF_BITS="-m64" ;;
ulrich_y's avatar
ulrich_y committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
        -*)
            echo "Warning: $arg is not a valid option." 1>&3 ;;
        *=*)
            eval "$arg" ;;
        *)
            echo "Warning: $arg is not a valid argument." 1>&3 ;;
    esac
done


test=test$$
trap "rm -fr $test* =." 0 1 2 3 15


## look for some programs

findprog() {
    echo -n "looking for $1... " 1>&3
    var="$2"
    set -- ${!var:+"${!var}"} "${@:3}"
    for prog in "$@" ; do
        full="`type -P "$prog"`" && {
            echo "$full" 1>&3
ulrich_y's avatar
ulrich_y committed
67
            printf -v "CONF_$var" "%q" "$full"
ulrich_y's avatar
ulrich_y committed
68 69 70 71 72 73
            return 0
        }
    done
    echo "no $@ in your path" 1>&3
    return 1
}
ulrich_y's avatar
ulrich_y committed
74 75 76 77 78 79 80 81

findlib() {
    echo -n "looking for lib$1... " 1>&3
    for flag in $CONF_LFLAGS ; do
        case "$flag" in
            -L* )
                libfolder=${flag/-L/}
                if [ -f "$libfolder/lib$1.so" ] ; then
ulrich_y's avatar
ulrich_y committed
82 83 84
                    path=`realpath "$libfolder/lib$1.so"`
                    echo $path 1>&3
                    printf -v "CONF_$1" "%q" "$path"
ulrich_y's avatar
ulrich_y committed
85 86 87
                    return 0
                fi
                if [ -f "$libfolder/lib$1.a" ] ; then
ulrich_y's avatar
ulrich_y committed
88 89 90
                    path=`realpath "$libfolder/lib$1.a"`
                    echo $path 1>&3
                    printf -v "CONF_$1" "%q" "$path"
ulrich_y's avatar
ulrich_y committed
91 92 93 94 95 96 97 98
                    return 0
                fi
            ;;
        esac
    done
    echo "not found" 1>&3
    return 1
}
ulrich_y's avatar
ulrich_y committed
99
addflag() {
ulrich_y's avatar
ulrich_y committed
100
    eval "CONF_$1=\"\${CONF_$1} ${@:2}\""
ulrich_y's avatar
ulrich_y committed
101 102 103 104 105
}

gnuflags() {
    eval addflag FFLAGS -fdefault-real-8             # default double is 8 byte
    eval addflag FFLAGS -cpp                         # C pre-processor
ulrich_y's avatar
ulrich_y committed
106
    eval addflag FFLAGS -pedantic-errors -std=f2008  # folow f08 strictly
ulrich_y's avatar
ulrich_y committed
107 108
    eval addflag FFLAGS -J build                     # where to put mods
    eval addflag FFLAGS -Werror -Wall                # make every warning an error
ulrich_y's avatar
ulrich_y committed
109
    eval addflag FFLAGS -Wno-uninitialized
ulrich_y's avatar
ulrich_y committed
110 111 112 113 114 115
}

findprog fortran FC  ifort gfortran  || exit 1



ulrich_y's avatar
ulrich_y committed
116
set -- `eval $CONF_FC --version -c | sed '/^$/d;s/([^)]*)//;q' 2>&1`
ulrich_y's avatar
ulrich_y committed
117 118 119 120 121
case "$1,$2,$3" in
    GNU,Fortran,[123].*)
        echo "Only version >= 4 are supported"
        exit
    ;;
ulrich_y's avatar
ulrich_y committed
122
    GNU,Fortran,4.*)
ulrich_y's avatar
ulrich_y committed
123 124
    echo "Code optimisation is *not* suported on gfortran 4"
        gnuflags
ulrich_y's avatar
ulrich_y committed
125
        eval addflag $CONF_BITS
ulrich_y's avatar
ulrich_y committed
126 127
        if $DEBUG ; then
            eval addflag FFLAGS -g -O0
ulrich_y's avatar
ulrich_y committed
128
            eval addflag FFLAGS -ffpe-trap=invalid,overflow
ulrich_y's avatar
ulrich_y committed
129 130 131 132 133 134 135 136 137 138 139
            eval addflag FFLAGS -fdump-core -fbacktrace
            eval addflag FFLAGS -frange-check
            eval addflag FFLAGS -DDEBUG
        else
            eval addflag FFLAGS -DRELEASE
            eval addflag FFLAGS -march=native -mtune=native
        fi
    ;;
    GNU,Fortran,*)
        gnuflags
        eval addflag FFLAGS -Wno-maybe-uninitialized
ulrich_y's avatar
ulrich_y committed
140
        eval addflag $CONF_BITS
ulrich_y's avatar
ulrich_y committed
141 142
        if $DEBUG ; then
            eval addflag FFLAGS -g -O0
ulrich_y's avatar
ulrich_y committed
143
            eval addflag FFLAGS -ffpe-trap=invalid,overflow
ulrich_y's avatar
ulrich_y committed
144 145 146 147 148 149 150 151 152 153 154 155 156
            eval addflag FFLAGS -fdump-core -fbacktrace
            eval addflag FFLAGS -fcheck=all -frange-check
            eval addflag FFLAGS -DDEBUG
        else
            eval addflag FFLAGS -DRELEASE -O3
            eval addflag FFLAGS -march=native -mtune=native
        fi
    ;;
    ifort*)
        eval addflag FFLAGS -autodouble
        eval addflag FFLAGS -module build
        eval addflag FFLAGS -fpp
        eval addflag FFLAGS -stand f03
ulrich_y's avatar
ulrich_y committed
157
        eval addflag $CONF_BITS
ulrich_y's avatar
ulrich_y committed
158 159 160 161 162 163 164
        if $DEBUG ; then
            eval addflag FFLAGS -g -O0
            eval addflag FFLAGS -DDEBUG
        else
            eval addflag FFLAGS -DRELEASE -O3
            eval addflag FFLAGS -xHost -fast
        fi
ulrich_y's avatar
ulrich_y committed
165
        if HAVE_GINAC ; then
ulrich_y's avatar
ulrich_y committed
166
            echo "Option --with-ginac is incompatible with ifort, disabling"
ulrich_y's avatar
ulrich_y committed
167
            HAVE_GINAC=false
ulrich_y's avatar
ulrich_y committed
168
        fi
ulrich_y's avatar
ulrich_y committed
169
        if HAVE_MCC ; then
ulrich_y's avatar
ulrich_y committed
170
            echo "Option --with-mcc is incompatible with ifort, disabling"
ulrich_y's avatar
ulrich_y committed
171
            HAVE_MCC=false
ulrich_y's avatar
ulrich_y committed
172 173 174 175 176 177 178 179
        fi
    ;;
    *)
        echo "Unknown compiler"
        exit
    ;;
esac

ulrich_y's avatar
ulrich_y committed
180 181 182 183 184 185 186 187
echo -n "extracting the Fortran libraries... " 1>&3

rm -fr $test*

tee $test.f90 << _EOF_ 1>&2
    program test
    integer i
    common /uscore/ i
ulrich_y's avatar
ulrich_y committed
188
    call exit(i)
ulrich_y's avatar
ulrich_y committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    end
_EOF_

while read line ; do
  set -- ${line//[:,()]/ }
  [[ "$1" =~ (/collect2|/ld|^ld)$ ]] && while test $# -gt 1 ; do
    shift
    case "$1" in
    *.o | -lc*)
      ;;
    -l* | -L* | *.a)
      FLDFLAGS+=" $1" ;;
    -Bstatic | -Bdynamic | *.ld)
      FLDFLAGS+=" -Wl,$1" ;;
    /*)
      FLDFLAGS+=" -L$1" ;;
    -rpath*)
      FLDFLAGS+=" -Wl,$1,$2"
      shift ;;
    -dynamic-linker)
      shift ;;
    esac
  done
ulrich_y's avatar
ulrich_y committed
212
done < <(eval $CONF_FC  -v -o $test $test.f90 2>&1)
ulrich_y's avatar
ulrich_y committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
echo "ok" 1>&3

eval addflag LFLAGS "$LDFLAGS $CONF_LDFLAGS $FLDFLAGS"



if ( $HAVE_GINAC || $HAVE_MCC ); then
    findprog gcc CC clang gcc || exit 1
    findprog g++ CXX clang++ g++ || exit 1
    eval addflag CFLAGS "-std=c99"

    ## does Fortran append underscores to symbols?

    echo -n "does $CONF_FC append underscores... " 1>&3

    tee $test-c.c << _EOF_ 1>&2
int uscore_ = 95;
int uscore = 59;
_EOF_

    for CONF_BITS in ${CONF_BITS:--m64 -m32} ; do
      eval $CONF_CC $CONF_CFLAGS $CONF_BITS -c $test-c.c 1>&2 || continue
ulrich_y's avatar
ulrich_y committed
235
      eval $CONF_FC  -o $test $test.f90 $test-c.o 1>&2 && break
ulrich_y's avatar
ulrich_y committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    done

    ./$test
    case $? in
    95)
      echo "yes" 1>&3
      CONF_NOUNDERSCORE=0 ;;
    59)
      echo "no" 1>&3
      CONF_NOUNDERSCORE=1 ;;
    *)
      echo "error linking Fortran and C" 1>&3
      exit 1 ;;
    esac

else
    $CONF_CC=
    $CONF_CXX=
    $CONF_CFLAGS=
    $CONF_NOUNDERSCORE=
ulrich_y's avatar
ulrich_y committed
256 257
fi

ulrich_y's avatar
ulrich_y committed
258 259 260 261 262 263 264 265 266 267 268 269

if $HAVE_MCC ; then
    PATH=$PATH:/Applications/Mathematica.app/Contents/MacOS/ findprog mathematica MATH math MathKernel math10 math11


    # Checking Mathematica system id

    echo -n "what is $CONF_MATH SystemID... " 1>&3

    tee $test.m << _EOF_ 1>&2
        Print["SYSID "<>ToString[\$SystemID]];
        Print["INSTDIR "<>ToString[\$InstallationDirectory]];
ulrich_y's avatar
ulrich_y committed
270
        Print["USERDIR "<>ToString[\$UserBaseDirectory]];
ulrich_y's avatar
ulrich_y committed
271 272 273 274
_EOF_
    $CONF_MATH < $test.m > $test.log
    CONF_MATH_SYS=`cat $test.log | grep "SYSID"   | sed 's/.*SYSID //'`
    CONF_MATH_DIR=`cat $test.log | grep "INSTDIR" | sed 's/.*INSTDIR //'`
ulrich_y's avatar
ulrich_y committed
275 276
    CONF_MATH_USR=`cat $test.log | grep "USERDIR" | sed 's/.*USERDIR //'`
    CONF_MMPREFIX=${CONF_MMPREFIX:=$CONF_MATH_USR/Kernel}
ulrich_y's avatar
ulrich_y committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    echo  $CONF_MATH_SYS 1>&3


    # Finding mcc
    CONF_COMPAD=$CONF_MATH_DIR/SystemFiles/Links/MathLink/DeveloperKit/$CONF_MATH_SYS/CompilerAdditions/
    PATH=$CONF_COMPAD:$PATH findprog mcc MCC mcc

    echo -n "looking for MLDK directory... " 1>&3
    $CONF_MCC --internals > $test.int
    CONF_MLDK=`cat $test.int | grep "MLDK Directory" | cut -f2 -d":"`
    echo $CONF_MLDK 1>&3

    arch=`cat $test.int | grep "Library Bit Type:" | cut -f2 -d":"` ; \
    CONF_MCC_CFLAGS=`cat $test.int | grep "Compile Flags$arch:" | cut -f2 -d":"`
    CONF_MCC_LFLAGS=`cat $test.int | grep "Linker Libraries" | cut -f2 -d":"`
    cat $test.int | grep "Linker Libraries" | cut -f2 -d":"
    if [ ! -z $CONF_BITS ]; then
        if [ "$CONF_BITS" -ne "$MCC_BITS" ]; then
            echo "mcc wants $MCC_BITS ($arch), $CONF_BITS selected." 1>&3
            exit 1
        fi
    fi

ulrich_y's avatar
ulrich_y committed
300 301 302 303 304 305 306 307 308 309 310
    if [[ $CONF_MCC_LFAGS == *"uuid"* ]]; then
        if ! findlib uuid UUID ; then
            echo -n "improvising uuid... " 1>&3
            CONF_MLIBDIR=$CONF_MATH_DIR/SystemFiles/Libraries/$CONF_MATH_SYS/
            if [ -f $CONF_MLIBDIR/libuuid.a ]; then
                CONF_MCC_LFLAGS="-L$CONF_MLIBDIR $CONF_MCC_LFLAGS"
                echo "okay" 1>&3
            else
                echo "failed" 1>&3
                exit 1
            fi
ulrich_y's avatar
ulrich_y committed
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
        fi
    fi

    # Finding MPREP
    PATH=$CONF_COMPAD:$PATH findprog mprep MPREP mprep

    ## can we do MathLink compilations
    echo -n "do we have working MathLink... " 1>&3
    tee $test.tm << _EOF_ 1>&2
:Begin:
:Function: test
:Pattern: Test[i_Integer]
:Arguments: {i}
:ArgumentTypes: {Integer}
:ReturnType: Integer
:End:

#include "mathlink.h"

static int test(const int i) {
  return i + 1;
}

int main(int argc, char **argv) {
  return MLMain(argc, argv);
}
_EOF_
    $CONF_MPREP $test.tm > $test.tm.c
    $CONF_CC $CONF_MCC_CFLAGS $CONF_CFLAGS -c $test.tm.c
    $CONF_CC -o $test.math $test.tm.o $CONF_MCC_LFLAGS
    test -x $test.math && {
        echo "yes" 1>&3
        CONF_ML=1
    } || {
        echo "no" 1>&3
        CONF_ML=0
ulrich_y's avatar
ulrich_y committed
347
        exit 1;
ulrich_y's avatar
ulrich_y committed
348 349
    }
fi
ulrich_y's avatar
ulrich_y committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371


echo "creating makefile" 1>&3

CONF_LD=$CONF_FC

cat  > makefile <<EOF
# Generated through $0 $@ on `date` by `whoami`

AR=ar rcs
FC=$CONF_FC
CC=$CONF_CC
CXX=$CONF_CXX
LD=$CONF_LD
MPREP=$CONF_MPREP

FFLAGS=$CONF_FFLAGS
CFLAGS=$CONF_CFLAGS
LFLAGS=$CONF_LFLAGS
MCFLAGS=$CONF_MCC_CFLAGS
MLFLAGS=$CONF_MCC_LFLAGS

ulrich_y's avatar
ulrich_y committed
372 373 374
PREFIX=$CONF_PREFIX
MMPREFIX=$CONF_MMPREFIX

ulrich_y's avatar
ulrich_y committed
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

files=globals.o ieps.o utils.o shuffle.o maths_functions.o mpl_module.o gpl_module.o GPL.o
objects = \$(addprefix build/,\$(files))


all: libgpl.a ${CONF_MPREP:+gpl} eval test

libgpl.a:\$(objects)
		@echo "AR \$@"
		@\$(AR) \$@ $^

# rules to make object files into /build
build/%.o: src/%.f90
		@echo "F90 \$@"
		@\$(FC) \$(FFLAGS) -c $< -o \$@

build/%.o: src/%.cpp
		@echo "C++ \$@"
		@\$(CC) -c $<  -o \$@

EOF

$HAVE_MCC && cat >> makefile <<EOF
build/%.tm.c: src/%.tm
		@echo "MPREP \$@"
		@\$(MPREP) \$< -o \$@

ulrich_y's avatar
ulrich_y committed
402
build/mathlink.o: build/mathlink.tm.c
ulrich_y's avatar
ulrich_y committed
403 404 405
		@echo "CC \$<"
		@\$(CC) \$(MCFLAGS) \$(CFLAGS) -o \$@ -c \$<

ulrich_y's avatar
ulrich_y committed
406
gpl: build/mathlink.o libgpl.a
ulrich_y's avatar
ulrich_y committed
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
		@echo "LD \$@"
		@\$(LD)  $< libgpl.a -o \$@ \$(LFLAGS) \$(MLFLAGS)
EOF

cat  >> makefile <<EOF


eval: libgpl.a build/eval.o
		@echo "LD \$@"
		@\$(LD) -o \$@ build/eval.o -L. -lgpl \$(LFLAGS)


test: \$(objects) ${CONF_GINAC:+build/ginac.o} build/test.o
		@echo "LD \$@"
		@\$(LD) -o \$@ $^ \$(LFLAGS)

ulrich_y's avatar
ulrich_y committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
install: libgpl.a build/gpl.mod ${CONF_MPREP:+gpl}
		mkdir -p \$(PREFIX)
		mkdir -p \$(PREFIX)/lib
		cp libgpl.a \$(PREFIX)/lib/
		mkdir -p \$(PREFIX)/include
		cp build/gpl.mod \$(PREFIX)/include/
EOF
$HAVE_MCC && cat >> makefile <<EOF
		mkdir -p \$(MMPREFIX)
		cp gpl \$(MMPREFIX)
EOF

echo "uninstall:" >> makefile
echo -ne "\t\trm -i " >> makefile
echo -n "\$(PREFIX)/lib/libgpl.a " >> makefile
echo -n "\$(PREFIX)/include/gpl.mod " >> makefile
$HAVE_MCC && echo -n "\$(MMPREFIX)/gpl" >> makefile
cat  >> makefile <<EOF


ulrich_y's avatar
ulrich_y committed
443 444 445 446 447 448 449 450 451 452 453 454
check: test
		./$<

clean:
		@rm -f build/*.o build/*.mod build/*.c build/mcc.internals*
		@rm -f test eval libgpl.a gpl

distclean:
		@rm -f build/*.o build/*.mod build/*.c build/mcc.internals*
		@rm -f test eval libgpl.a gpl
		@rm -f makefile
EOF