legacyCompile 30.8 KB
Newer Older
1 2 3 4 5 6 7 8 9
#!/bin/bash
#------------------------------------------------------------------------------
# Script
#     legacyCompile
#
# Description
#     Simple convenience script to install OpenFOAM on older systems.
#     Use ThirdParty compiler, but retain most default software versions.
#
10 11 12
#     [package]             - Top-level package information
#     [arch]                - Architecture
#     [software]            - Preferred software versions
13
#
14
#     Input Directory:
15
#     - ./packages          - Contains the tar files
16 17
#
#     Output Directories
18 19
#     - ./install           - The compiled installation
#     - ./log               - Log files from the build scripts
20
#
21 22
# License
#    GPL v3 or later
23
#    Copyright (C) 2016-2017 Roger Almenar, ESI GmbH.
24
#------------------------------------------------------------------------------
25 26 27 28 29
# Local directories
INSTALL_DIR=$PWD/install
PACKAGE_DIR=$PWD/packages
LOGFILE_DIR=$PWD/log

30 31 32 33 34 35
#------------------------------------------------------------------------------
usage() {
    exec 1>&2
    while [ "$#" -ge 1 ]; do echo "$1"; shift; done
    cat<<USAGE

36 37 38
usage: ${0##*/} [OPTIONS] OPERATION [...OPERATION] configFile
options:
  -purge                Purge old directories before unpacking (CAUTION)
39 40 41
  -int32 | -int64       Label width
  -SP | -DP             Precision
  -sigfpe | -no-sigfpe  Activate/deactivate FOAM_SIGFPE handling [defunct]
42 43
  -help

44 45 46 47 48
operations:
  -unpack           Unpack sources, configure
  -third            Compile ThirdParty
  -foam             Compile OpenFOAM
  -list             List configured versions only
49 50
  -all              Alias for -unpack -third -foam
  -rebuild          Alias for -third -foam
51 52

=========================
53
Make and install OpenFOAM/ThirdParty
54 55 56 57 58 59 60
      package = $PWD/packages
      logdir  = $PWD/log
      install = $PWD/install

USAGE
    exit 1
}
61

62 63 64 65 66 67 68 69 70 71 72 73
# Report error and exit
die()
{
    exec 1>&2
    echo
    echo "Error: see '${0##*/} -help' for usage"
    while [ "$#" -ge 1 ]; do echo "    $1"; shift; done
    echo
    exit 1
}
#------------------------------------------------------------------------------

74 75
# OpenFOAM options, various build options
unset optLabel optPrec optVerbose optFpe configFile
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
unset runLIST runUNPACK runTHIRD runFOAM purgeOLDFILES

# Parse options
while [ "$#" -gt 0 ]
do
    case "$1" in
    -h | -help | --help | help)
        usage
        ;;
    -unpack)            # Stage 1: unpack sources, configure
        runUNPACK=true
        ;;
    -purge)             # (Stage 1): purge old files before unpacking
        purgeOLDFILES=true
        ;;
    -third)             # Stage 2: compile ThirdParty
        runTHIRD=true
        ;;
    -foam)              # Stage 3: compile OpenFOAM
        runFOAM=true
        ;;

    -all)               # Stage 1-3 (all)
        runUNPACK=true
        runTHIRD=true
        runFOAM=true
        ;;
    -rebuild)           # Stage 2-3 (rebuild)
        runTHIRD=true
        runFOAM=true
        ;;
    -list)
        optList=true
        ;;
110 111 112
    -verbose)
        optVerbose=true
        ;;
113 114 115 116 117 118 119 120 121 122 123

    -int32 | -int64)
        optLabel="$1"
        ;;
    -SP | -DP)
        optPrec="$1"
        ;;
    -sigfpe | -no-sigfpe)
        optFpe="$1"
        ;;

124 125 126 127 128 129 130
    --)
        shift
        break
        ;;
    -*)
        die "unknown option: '$1'"
        ;;
131
    *)
132
        break
133 134 135 136 137
        ;;
    esac
    shift
done

138 139 140 141
configFile="$1"
[ "$#" -eq 1 ] || die "expecting a config filename"
[ -f "$configFile" ] || die "No such file: $configFile"

142 143 144
[ -n "$runUNPACK$runTHIRD$runFOAM$optList" ] || die "No operations specified"

#------------------------------------------------------------------------------
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
# [package]

# Get [config] section with these types of entries (as shell variables)
# FoamVersion=value
# OFdirName=value
# TPdirName=value
# ---------------

# Extract name=value - ignore blank and comment lines
eval $(
sed -n \
    -e '/^ *[;#]/d' \
    -e '/^\[package\]/,/^\s*\[/{ /^[^ =][^ =]*\=[^ =][^ =]/p }' \
    $configFile
)

# These must be defined
for envar in FoamVersion OFdirName TPdirName
do
    if [ "$optVerbose" ]
    then
        eval echo "$envar=\$$envar"
    fi
    eval test "\$$envar" || die "variable \$$envar not set"
done

# Alternative tar file names
: ${OFsource:=$OFdirName}
: ${TPsource:=$TPdirName}

#------------------------------------------------------------------------------
# [arch]

# Get [arch] section with these types of entries
# label=32
# precision=DP
# sigfpe=false
# ---------------

# Extract name=value - ignore blank and comment lines
values=$(
sed -n \
    -e '/^ *[;#]/d' \
    -e '/^\[arch\]/,/^\s*\[/{ /^[^ =][^ =]*\=[^ =][^ =]/p }' \
    $configFile
)

for line in $values
do
    case "$line" in
    (label=32 | label=64)
        optLabel="-int${line#*=}"
        ;;
    (scalar=32 | precision=SP | precision=sp)
        optPrec="-SP"
        ;;
    (scalar=64 | precision=DP | precision=dp)
        optPrec="-DP"
        ;;
    (sigfpe=true)
        optFpe="-sigfpe"
        ;;
    (sigfpe=false)
        optFpe="-no-sigfpe"
        ;;
    esac
done

# Set part of the architecture configuration
214 215
configArch="$optLabel $optPrec $optFpe"

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
echo "arch: $configArch"

#------------------------------------------------------------------------------
# [software]

# Get [software] section with these types of entries
# boost       boost_1_62_0
# cmake       cmake-system    cmake-3.5.2
# ---------------
# Ensure that these are always defined
for pkg in cmake qt gcc gmp mpfr mpc
do
    eval "${pkg}DEFAULT='${pkg}-system';${pkg}VERSION='${pkg}-system'"
done

#
# parse 'package default version' lines
# and transform to => packageDEFAULT, packageVERSION variables
#
_software()
{
    local line pkg def val
    while read -r line
    do
        # Packages must start with lowercase. Also removes rubbish
        set -- $(echo $line | sed -e 's/=/ /g')
        pkg="$1"; def="$2"; val="$3"

        ## echo "Using $# package=$pkg default='$def' version='$val'" 1>&2

        if [ "$pkg" = "${pkg#[a-z]}" ]
        then
            continue # Packages must start with lowercase
        elif [ $# -eq 2 ]
        then
            echo "${pkg}DEFAULT='${def}';${pkg}VERSION='${def}'"
        elif [ $# -eq 3 ]
        then
            echo "${pkg}DEFAULT='${def}';${pkg}VERSION='${val}'"
        else
            echo "Bad input while setting versions"  1>&2
            echo "   $line" 1>&2
            exit 1
        fi
    done
}

eval $(
sed -n \
    -e '/^ *[;#]/d' \
    -e '/^\[software\]/,/^\s*\[/{ s/^ *//g; s/ *$//g; s/  */=/g; p }' \
    $configFile | \
    _software
)

#------------------------------------------------------------------------------

# Name of qt source directory is a fixed value
qtSource="qt-everywhere-opensource-src-${qtVERSION##*-}"

# Change 'system' into 'gmp-system' etc
for pkg in gcc gmp mpfr mpc
do
    if eval test "\$${pkg}VERSION = system"
    then
        eval "${pkg}VERSION='${pkg}-system'"
        eval "${pkg}DEFAULT='${pkg}-system'"
    fi
done

# No gcc/gmp/mpfr/mpc for system compilers or non-Gcc compilers
case "$compilerTypeVERSION:$compilerVERSION" in
third:Gcc*)
    ;;
third:Clang* | system:Clang* | system:Gcc | system:Icc | system:*)
    # System, or known non-gcc
    for check in gcc gmp mpfr mpc
    do
        eval "unset ${check}VERSION ${check}DEFAULT"
    done
    ;;
*)
    echo "Error: unknown ${compilerTypeVERSION}-compiler '$compilerVERSION'"
    exit 1
    ;;
esac

# Separator lines
_line()
{
    echo  " ====================  ====================  ===================="
}

# Various formatting for -list output
# formatted print
_fmt2()
{
    local arg1="${1:-????}"
    local arg2="${2:-undefined}"
    printf "  %-20s  %-20s\n"  "$arg1"  "$arg2"
}

# formatted print
# name version default [-nocheck]
_fmt3()
{
    local what="${1:-????}"
    local arg1="${2:-undefined}"
    local arg2="${3:-undefined}"

    if [ "$#" -eq 2 ]
    then
        _fmt2 "$what"  "$arg1"
    elif [ "$#" -eq 3 -a "$arg1" = "$arg2" ]
    then
        _fmt2 "$what" "$arg1"
    else
        printf "  %-20s  %-20s  %-20s\n"  "$what"  "$arg1"  "$arg2"
    fi
}

# input:  'package'
# output: 'package' packageDEFAULT packageVERSION
_fmtDefVer()
{
    local pkg="$1"
    eval local "def=\$${pkg}DEFAULT"
    eval local "val=\$${pkg}VERSION"

    _fmt3 "$pkg"  "$def"  "$val"
}


# Location of the OpenFOAM bashrc
BASHRC_FILE=$INSTALL_DIR/$OFdirName/etc/bashrc

if [ "$ompiTypeVERSION" != "$ompiTypeDEFAULT" ]
353
then
354
    configArch="$configArch -openmpi-$ompiTypeVERSION"
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
fi

#------------------------------------------------------------------------------

_listSettings()
{
    echo
    echo "$OFdirName / $TPdirName"
    echo
    _fmt3 package  default  version -nocheck
    _line
    _fmtDefVer boost
    _fmtDefVer cgal
    _fmtDefVer fftw
    _fmtDefVer openmpi
    _fmtDefVer scotch
371
    _fmtDefVer kahip
372 373 374 375 376 377 378
    _fmtDefVer paraview
    _fmtDefVer cmake
    _fmtDefVer vtk
    _fmtDefVer mesa
    _fmtDefVer qt
    _fmtDefVer ompiType
    _fmtDefVer llvm
379 380 381 382
    _fmt3 gcc  "${gccDEFAULT:-system}"  "${gccVERSION:-system}"
    _fmt3 gmp  "${gmpDEFAULT:-system}"  "${gmpVERSION:-system}"
    _fmt3 mpfr "${mpfrDEFAULT:-system}" "${mpfrVERSION:-system}"
    _fmt3 mpc  "${mpcDEFAULT:-system}"  "${mpcVERSION:-system}"
383 384 385
    _line
    echo
    echo  "  configure:  $configArch"
386
    echo  "  compiler:   -$compilerTypeVERSION  $compilerVERSION"
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
    echo
}


# _mvdir src-dir dst-dir
# Renames src-dir to dst-dir if src-dir exists and dst-dir does not
_mvdir()
{
    local src="$1"
    local dst="$2"

    if [ -n "$src" -a -d "$src" ] && [ -n "$dst" -a ! -e "$dst" ]
    then
        # TODO? maybe need special treatment if dst is a link (eg, remove it)
        echo "Rename $src/ -> $dst/"
        mv "$src" "$dst"
    fi
}


# _unpack packageName,
# does not unpack anything ending with '-none' or '-system'
#
# Unpack given package from $PACKAGE_DIR if it can be resolved
# (as tar.gz, tar.bz2, ... file) and if "log.unpack..." sentinel
# log-file does not already exist
_unpack()
{
    [ $# -gt 0 ] || return 1
    local pkgName="$1"          # The package name
    local logFile="$LOGFILE_DIR/log.unpack_$pkgName.txt"
    local pkgFile

    pkgFile="${pkgName##*-}"
    if [ "$pkgFile" = none -o "$pkgFile" = system ]
    then
        return 2
    fi

    if [ -f "$logFile" ]
    then
        echo "Unpack $pkgName : log-file indicates it was already done"
        return 0
    fi

    # Check archive types (most compact first)
    local fileType              # tgz | tar.gz | tar.bz2 | tar.xz
    for ext in tar.xz tar.bz2 tar.gz tgz tar zip
    do
        pkgFile="$PACKAGE_DIR/$pkgName.$ext"
        if [ -f "$pkgFile" ]
        then
            fileType=$ext
            break
        fi
    done

    [ -n "$fileType" ] || {
        echo "Cannot unpack $pkgName : missing $PACKAGE_DIR/$pkgName.???"
        return 2
    }

    echo "Unpack $pkgName  $fileType ..."
    case "$fileType" in
    tgz | tar.gz)
        tar -xzvf $pkgFile > "$logFile.$$" 2>&1
        ;;
    tar.bz2)
        tar -xjvf $pkgFile > "$logFile.$$" 2>&1
        ;;
    tar.xz)
        tar -xJvf $pkgFile > "$logFile.$$" 2>&1
        ;;
    tar)
        tar -xvf  $pkgFile > "$logFile.$$" 2>&1
        ;;
    zip)
        unzip -v  $pkgFile > "$logFile.$$" 2>&1
        ;;
    esac
    \mv -f "$logFile.$$" "$logFile"
}


# _unpackIfDifferent packageName defaultVersion
#
# Wrapper for _unpack that checks for the existence of the directory
# and only tries to unpack if there is difference between the packageName
# and its default-version
#
_unpackIfDifferent()
{
    [ $# -gt 0 ] || return 2
    local pkgName="$1"          # The package name
    local pkgDeft="$2"

    if [ -z "$pkgName" ]
    then
        return 2
    elif [ "$pkgName" = "$pkgDeft" -a -d "$pkgName" ]
    then
        echo "  already exists $pkgName"
        return 0
    fi

    _unpack $pkgName
}

# service routine:
# Call with 3 arguments:
#     _ifDifferent -opt version default
#
# pass through '-opt version' if version differs from default
_ifDifferent()
{
    [ "$#" -eq 3 -a "$2" != "$3" ] && echo "$1 $2"
}

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

# Function to do replacement on file. Checks if any replacement has been done.
# _inlineSed <file> <regexp> <replacement> <msg>
_inlineSed()
{
    local file="$1"
    local regexp="$2"
    local replacement="$3"
    local msg="$4"
    local cmd='/^[^#]/s@'"$regexp"'@'"$replacement"'@'

    [ -f "$file" ] || {
        echo "Missing file: $file"
        exit 2 # Fatal
    }

    grep -q "$regexp" "$file" && sed -i -e "$cmd" "$file" || { \
        echo "Failed: ${msg:-replacement} in $file"
        return 1
    }

    [ -n "$msg" ] && echo "    $msg  ($file)"

    return 0
}


# Standard <key> <val> type of replacements.
# replace <file> <key1> <val1> .. <keyN> <valN>
# looks for KEYWORD=.*
replace()
{
    local file="$1"
    shift

    local key val

    while [ "$#" -ge 2 ]
    do
        key=$1
        val=$2
        shift 2

        _inlineSed \
            $file  \
            "$key=.*" \
            "$key=$val" \
            "Replaced $key setting by '$val'"
    done
}


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
#
# Unpack files and make any adjustments
#
# side-effect: cd to WM_THIRD_PARTY_DIR
#
unpackAllFiles()
{
    # Use the foamConfigurePaths tool to configure versions and paths
    local foamConfigure=bin/tools/foamConfigurePaths

    echo =================================================================
    if [ "${purgeOLDFILES:-false}" = true ]
    then
        echo "Purging old installation and log directories..."
        [ -d "$INSTALL_DIR" ] && rm -rf $INSTALL_DIR
        [ -d "$LOGFILE_DIR" ] && rm -rf $LOGFILE_DIR
    fi
    [ -d "$INSTALL_DIR" ] && echo "Using existing $INSTALL_DIR"
    [ -d "$LOGFILE_DIR" ] && echo "Using existing $LOGFILE_DIR"
    echo "Unpack OpenFOAM/ThirdParty sources"
    echo "    $FoamVersion"
    echo =================================================================

    mkdir -p $INSTALL_DIR
    mkdir -p $LOGFILE_DIR

    cd $INSTALL_DIR || {
        echo "Error: cannot change to $INSTALL_DIR"
        exit 1
    }

588 589 590
    # Unpack OpenFOAM and ThirdParty
    # Source package names may differ from the directory names
    # Also handle '-plus' extensions (common problem?)
591
    _unpack "$OFsource"
592 593 594
    _mvdir "$OFsource" "$OFdirName"
    _mvdir OpenFOAM-plus "$OFdirName"

595
    _unpack "$TPsource";
596 597 598
    _mvdir "$TPsource" "$TPdirName"
    _mvdir ThirdParty-plus "$TPdirName"

599
    echo
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614

    cd $INSTALL_DIR/$OFdirName || {
        echo "Error: cannot change to $INSTALL_DIR/$OFdirName"
        echo
        exit 1
    }

    [ -f "$foamConfigure" ] || {
        echo "Error: required $foamConfigure file is missing"
        echo
        exit 1
    }

    if true
    then
615 616 617
        # Pre-1812, no -gcc option for foamConfigurePaths
        hasOptGcc="$(exec /bin/sh $foamConfigure -help 2>&1 | sed -ne 's/^ *-gcc.*$/true/p')"

618
        # Compiler configuration (already sanity checked much further above)
619 620
        compilerOpt="-$compilerTypeVERSION $compilerVERSION"
        case "$compilerTypeVERSION:$compilerVERSION" in
621
        third:Clang*)
622
            compilerOpt="$compilerOpt -clang $llvmVERSION"
623 624
            ;;
        third:Gcc*)
625 626 627 628 629 630 631 632 633 634 635
            case "$hasOptGcc:$gccVERSION" in
            (true:gcc-[1-9]*)
                # Can use the -gcc option, and have gcc-version
                compilerOpt="$compilerOpt -gcc $gccVERSION"
                ;;
            (:gcc-[1-9]*)
                # Manual edit for gcc-version
                echo "Simulated '-gcc $gccVERSION' option manually"
                replace etc/config.sh/compiler gcc_version "$gccVERSION"
                ;;
            esac
636
            compilerOpt="$compilerOpt $gmpVERSION $mpfrVERSION $mpcVERSION"
637 638 639 640 641 642 643 644
            ;;
        esac

        set -- \
            -foamVersion $FoamVersion \
            $configArch $compilerOpt \
            $(
                # package versions
645 646 647 648 649
                _ifDifferent -cmake   $cmakeVERSION   $cmakeDEFAULT
                _ifDifferent -boost   $boostVERSION   $boostDEFAULT
                _ifDifferent -cgal    $cgalVERSION    $cgalDEFAULT
                _ifDifferent -fftw    $fftwVERSION    $fftwDEFAULT
                _ifDifferent -scotch  $scotchVERSION  $scotchDEFAULT
650
                _ifDifferent -kahip   $kahipVERSION   $kahipDEFAULT
651
                _ifDifferent -metis   $metisVERSION   $metisDEFAULT
652
                _ifDifferent -openmpi $openmpiVERSION $openmpiDEFAULT
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
            )

        echo "foam-configure with settings:"
        echo "    $@"
        echo
        exec /bin/sh $foamConfigure $@
    fi | tee "$LOGFILE_DIR/log.configure.txt"


    echo
    echo "Base configuration done."
    echo
    echo "Unpack required ThirdParty sources"

    cd $INSTALL_DIR/$TPdirName || {
        echo "Error: cannot change to $INSTALL_DIR/$TPdirName"
        exit 1
    }

672 673 674 675 676
    _unpackIfDifferent $cmakeVERSION    $cmakeDEFAULT
    _unpackIfDifferent $boostVERSION    $boostDEFAULT
    _unpackIfDifferent $cgalVERSION     $cgalDEFAULT
    _unpackIfDifferent $fftwVERSION     $fftwDEFAULT
    _unpackIfDifferent $scotchVERSION   $scotchDEFAULT
677
    _unpackIfDifferent $kahipVERSION    $kahipDEFAULT
678
    _unpackIfDifferent $metisVERSION    $metisDEFAULT
679
    _unpackIfDifferent $openmpiVERSION  $openmpiDEFAULT
680

681
    case "$compilerTypeVERSION:$compilerVERSION" in
682
    third:Gcc*)
683 684 685 686 687
        echo "# ThirdParty Compiler: $compilerVERSION"
        _unpack $gccVERSION
        _unpack $gmpVERSION
        _unpack $mpfrVERSION
        _unpack $mpcVERSION
688 689
        ;;
    third:Clang*)
690
        echo "# ThirdParty Compiler: $compilerVERSION"
691
        # Mostly unpacks to llvm-x.y.z.src, relocate to llvm-x.y.z
692 693
        _unpack $llvmVERSION
        _mvdir "$llvmVERSION.src" "$llvmVERSION"          # (*.src -> *)
694 695 696

        # Check if matching cfe (clang) version is required
        clangDir=tools/clang
697
        cfeVERSION="cfe${llvmVERSION#llvm}"
698

699
        if [ -d "$llvmVERSION" -a ! -d "$llvmVERSION/$clangDir" ]
700
        then
701 702 703
            _unpack $cfeVERSION
            _mvdir "$cfeVERSION.src" "$cfeVERSION"        # As above (*.src -> *)
            _mvdir "$cfeVERSION" "$llvmVERSION/$clangDir" # Relocate to tools/clang
704
        fi
705 706 707 708 709 710 711 712 713 714 715

        # Check if matching openmp version is required
        ompDir=tools/openmp
        ompVERSION="openmp${llvmVERSION#llvm}"

        if [ -d "$llvmVERSION" -a ! -d "$llvmVERSION/$ompDir" ]
        then
            _unpack $ompVERSION
            _mvdir "$ompVERSION.src" "$ompVERSION"        # As above (*.src -> *)
            _mvdir "$ompVERSION" "$llvmVERSION/$ompDir"   # Relocate to tools/openmp
        fi
716 717 718 719
        ;;
    esac

    echo "# Graphics"
720
    _unpackIfDifferent $paraviewVERSION $paraviewDEFAULT
721 722 723 724
    # Mostly unpacks to ParaView-vx.y.z | ParaView-vx.y.z-source,
    # relocate to ParaView-x.y.z

    # This is why bash
725 726 727
    _mvdir "${paraviewVERSION}-source"          "$paraviewVERSION"
    _mvdir "${paraviewVERSION/-/-v}"            "$paraviewVERSION"
    _mvdir "${paraviewVERSION/-/-v}-source"     "$paraviewVERSION"
728

729
    _unpack $mesaVERSION
730 731 732 733 734

    # May not really need QT (paraview only)
    _unpack $qtSource

    # VTK is a link to ParaView VTK
735
    if [ -n "$vtkVERSION" ]
736
    then
737
        if readlink "$vtkVERSION" > /dev/null
738
        then
739 740
            echo "  Remove old link to $vtkVERSION"
            rm "$vtkVERSION"
741
        fi
742
        if [ -d "${paraviewVERSION:-paraview#undef}/VTK" ]
743
        then
744 745
            echo "  Create new link  $paraviewVERSION/VTK $vtkVERSION"
            ln -sv $paraviewVERSION/VTK $vtkVERSION
746
        else
747 748
            echo "  did not make link for vtk $vtkVERSION"
            echo "  perhaps no paraview directory? ${paraviewVERSION:-???}/VTK"
749 750 751 752
        fi
    fi

    ## Check if these may be required (again, in the future)
753 754
    ## _unpack $automakeVERSION
    ## _unpack $autoconfVERSION
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775

    echo
    echo "Finished unpacking"
    echo =================================================================
}


#
# Compile Third-Party bits and pieces
#
# side-effect: cd to WM_THIRD_PARTY_DIR
#
compileThirdParty()
{
    echo
    echo =================================================================
    echo "Starting compilation of $TPdirName"
    echo "   Ignore any spurious warnings about missing compiler installation"
    echo "   ... we are probably just going to build it now"
    echo

776
    . $BASHRC_FILE ''
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794

    cd $WM_THIRD_PARTY_DIR || {
        echo "Error: cannot change to ThirdParty directory - $WM_THIRD_PARTY_DIR"
        echo "   cannot continue with building ThirdParty packages"
        exit 1
    }

    local logFile
    local makeCommand
    local makeOptions
    local makeTarget

    # Frequently used
    local platforms="$WM_THIRD_PARTY_DIR/platforms"

    # Cmake (build with system gcc to avoid circular dependency)
    echo
    echo "cmake:"
795
    if [ -d "$WM_THIRD_PARTY_DIR/${cmakeVERSION:-cmake#undef}" ]
796 797
    then
        makeCommand=makeCmake
798
        makeOptions="-gcc $cmakeVERSION"
799

800
        makeTarget="$platforms/$WM_ARCH$WM_COMPILER/$cmakeVERSION"
801 802 803 804 805 806 807 808 809 810 811 812 813 814
        logFile="$LOGFILE_DIR/log.$makeCommand-$WM_OPTIONS.txt"
        if [ -f "$logFile" ]
        then
            echo "  $makeCommand : log-file indicates it was already done"
        elif [ -d "$makeTarget" ]
        then
            echo "  $makeCommand : target appears to already exist"
            echo "  $makeTarget"
        else
            echo "  $makeCommand $makeOptions : can take quite awhile..." | tee $logFile
            ./$makeCommand $makeOptions >> $logFile 2>&1

            # Pickup changes (but should already be there)
            # - foamPV function should also work
815
            . $BASHRC_FILE '' 2>/dev/null
816 817 818 819 820 821
        fi
    else
        echo "  not specified or not found"
    fi

    ## Check if these may be required (again, in the future)
822 823
    ## ( cd $automakeVERSION && some more stuff )
    ## ( cd $autoconfVERSION && some more stuff )
824 825 826 827 828


    # Compiler ... this can take a while
    echo
    echo "compiler:"
829
    case "$compilerTypeVERSION:$compilerVERSION" in
830 831
    third:Gcc*)
        makeCommand=makeGcc
832
        makeOptions="-no-threadsafe $gccVERSION $gmpVERSION $mpfrVERSION $mpcVERSION"
833

834
        makeTarget="$platforms/$WM_ARCH/$gccVERSION"
835 836 837 838 839 840 841 842 843 844 845 846 847
        logFile="$LOGFILE_DIR/log.$makeCommand-$WM_OPTIONS.txt"
        if [ -f "$logFile" ]
        then
            echo "  $makeCommand : log-file indicates it was already done"
        elif [ -d "$makeTarget" ]
        then
            echo "  $makeCommand : target appears to already exist"
            echo "  $makeTarget"
        else
            echo "  $makeCommand $makeOptions : can take quite awhile..." | tee $logFile
            ./$makeCommand $makeOptions >> $logFile 2>&1

            # Pickup changes (but should already be there)
848
            . $BASHRC_FILE ''
849 850 851 852 853 854 855
        fi
        ;;
    third:Clang*)
        makeCommand=makeLLVM
        makeOptions=""
        # makeOptions="-gcc"

856
        makeTarget="$platforms/$WM_ARCH/$llvmVERSION"
857 858 859 860 861 862 863 864 865 866 867 868 869
        logFile="$LOGFILE_DIR/log.$makeCommand-$WM_OPTIONS.txt"
        if [ -f "$logFile" ]
        then
            echo "  $makeCommand : log-file indicates it was already done"
        elif [ -d "$makeTarget" ]
        then
            echo "  $makeCommand : target appears to already exist"
            echo "  $makeTarget"
        else
            echo "  $makeCommand $makeOptions : can take quite awhile..." | tee $logFile
            ./$makeCommand $makeOptions >> $logFile 2>&1

            # Pickup changes (but should already be there)
870
            . $BASHRC_FILE ''
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
        fi
        ;;
    esac

    # Compiler is now set, compile wmake support now
    ( cd $WM_PROJECT_DIR/wmake/src && make )


    # boost/CGAL:
    #   we only need to do this here if we have special options (eg, headers-only)
    #   otherwise the Allwmake can be used too
    if true
    then
        echo
        echo "boost/cgal:"
        makeCommand=makeCGAL
        makeOptions=""
        # makeOptions="-no-lib"

        logFile="$LOGFILE_DIR/log.$makeCommand-$WM_OPTIONS.txt"

        echo "  $makeCommand $makeOptions : can take quite awhile..." | tee $logFile
        ./$makeCommand $makeOptions >> $logFile 2>&1
    fi

    # fftw:
    #   only here for the extra log file
    #   otherwise the Allwmake can be used too
    if true
    then
        echo
        echo "fftw:"
        makeCommand=makeFFTW
        makeOptions=""
        # makeOptions="-gcc"

        logFile="$LOGFILE_DIR/log.$makeCommand-$WM_OPTIONS.txt"

        echo "  $makeCommand $makeOptions : can take quite awhile..." | tee $logFile
        ./$makeCommand $makeOptions >> $logFile 2>&1
    fi

    # Mesa
    # - the script itself handles Clang from a non-standard (ThirdParty) location
    # but need to check how it manages relocating the package
    echo
    echo "mesa:"
918 919
    mesaPREFIX="$platforms/$WM_ARCH$WM_COMPILER/$mesaVERSION"
    if [ -d "$WM_THIRD_PARTY_DIR/${mesaVERSION:-mesa#undef}" ]
920 921
    then
        makeCommand=makeMesa
922
        makeOptions="$mesaVERSION"
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

        makeTarget="$mesaPREFIX"
        logFile="$LOGFILE_DIR/log.$makeCommand-$WM_OPTIONS.txt"
        if [ -f "$logFile" ]
        then
            echo "  $makeCommand : log-file indicates it was already done"
        elif [ -d "$makeTarget" ]
        then
            echo "  $makeCommand : target appears to already exist"
            echo "  $mesaPREFIX"
        else
            echo "  $makeCommand $makeOptions : can take quite awhile..." | tee $logFile
            ./$makeCommand $makeOptions >> $logFile 2>&1
        fi

        ## TODO: is llvm-config (in mesa) relocatable?
        # platforms/linux64Clang/mesa-13.0.1/lib64/pkgconfig/glesv1_cm.pc
        # platforms/linux64Clang/mesa-13.0.1/lib64/pkgconfig/glesv2.pc
        # platforms/linux64Clang/mesa-13.0.1/lib64/pkgconfig/osmesa.pc
    else
        echo "  not specified or not found"
        unset mesaPREFIX
    fi

    echo
    echo "VTK (with mesa)"
949
    if [ -d "$mesaPREFIX" -a -d "$WM_THIRD_PARTY_DIR/${vtkVERSION:-vtk#undef}" ]
950 951
    then
        makeCommand=makeVTK
952
        makeOptions="$vtkVERSION -osmesa -mesa-prefix $mesaPREFIX"
953

954
        makeTarget="$platforms/$WM_ARCH$WM_COMPILER/$vtkVERSION"
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
        logFile="$LOGFILE_DIR/log.$makeCommand-$WM_OPTIONS.txt"
        if [ -f "$logFile" ]
        then
            echo "  $makeCommand : log-file indicates it was already done"
        elif [ -d "$makeTarget" ]
        then
            echo "  $makeCommand : target appears to already exist"
            echo "  $makeTarget"
        else
            echo "  $makeCommand $makeOptions : can take quite awhile..." | tee $logFile
            ./$makeCommand $makeOptions >> $logFile 2>&1
        fi
    else
        echo "  not specified or not found"
    fi

    echo
    echo "QT (needed for paraview)"
973
    qtPREFIX="$platforms/$WM_ARCH$WM_COMPILER/$qtVERSION"
974 975 976
    if [ -d "$WM_THIRD_PARTY_DIR/${qtSource:-qt#undef}" ]
    then
        makeCommand=makeQt
977
        makeOptions="$qtVERSION"
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
        case "$WM_COMPILER" in
        Clang*)
            # Some build issues with clang, use system gcc instead
            makeOptions="$makeOptions -gcc"
            ;;
        esac

        makeTarget="$qtPREFIX"
        logFile="$LOGFILE_DIR/log.$makeCommand-$WM_OPTIONS.txt"
        if [ -f "$logFile" ]
        then
            echo "  $makeCommand : log-file indicates it was already done"
        elif [ -d "$makeTarget" -a -x "$makeTarget/bin/qmake" ]
        then
            echo "  $makeCommand : target appears to already exist"
            echo "  $qtPREFIX"
        else
            echo "  $makeCommand $makeOptions : can take quite awhile..." | tee $logFile
            ./$makeCommand $makeOptions >> $logFile 2>&1
        fi
    else
        echo "  not specified or not found"
1000
        unset qtVERSION qtPREFIX # avoid using for paraview build
1001 1002 1003 1004
    fi

    echo
    echo "ParaView:"
1005
    if [ -d "$WM_THIRD_PARTY_DIR/${paraviewVERSION:-paraview#undef}" ]
1006 1007 1008 1009 1010 1011 1012
    then
        makeCommand=makeParaView
        makeOptions=""
        if [ -d "$mesaPREFIX" ]
        then
            makeOptions="$makeOptions -mesa-prefix $mesaPREFIX"
        fi
1013
        if [ -n "$qtVERSION" -a -x "$qtPREFIX/bin/qmake" ]
1014
        then
1015
            makeOptions="$makeOptions -$qtVERSION"
1016 1017
        fi

1018
        makeTarget="$platforms/$WM_ARCH$WM_COMPILER/$paraviewVERSION"
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
        logFile="$LOGFILE_DIR/log.$makeCommand-$WM_OPTIONS.txt"
        if [ -f "$logFile" ]
        then
            echo "  $makeCommand : log-file indicates it was already done"
        elif [ -d "$makeTarget" ]
        then
            echo "  $makeCommand : target appears to already exist"
            echo "  $makeTarget"
        else
            echo "  $makeCommand $makeOptions : can take quite awhile..." | tee $logFile
            ./$makeCommand $makeOptions >> $logFile 2>&1
        fi
    else
        echo "  not specified or not found"
    fi

    # Can do this in main OpenFOAM build, but nicer to separate and do it here.
    # No -s -j -log= options for ThirdParty Allwmake
    echo
    echo "ThirdParty ..."
    ./Allwmake > $LOGFILE_DIR/log.ThirdParty-$WM_OPTIONS.txt 2>&1

    echo
    echo "Finished compilation of ThirdParty code"
    echo =================================================================
}


#
# Compile OpenFOAM itself
#
# side-effect: cd to WM_PROJECT_DIR
#
compileOpenFOAM()
{
    echo
    echo =================================================================
    echo "Starting compilation of OpenFOAM"
    echo

1059
    . $BASHRC_FILE ''
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

    cd $WM_PROJECT_DIR || {
        echo "Error: cannot change to OpenFOAM directory - $WM_PROJECT_DIR"
        echo "   cannot continue with building OpenFOAM"
        exit 1
    }

    echo =================================================================
    echo "Using OpenFOAM $WM_PROJECT_DIR"
    echo
    echo =================================================================

    ## unneeded: ( cd $WM_DIR/rules && ln -s linux64Gcc linux64 )

    # pass1: build as much as possible, ignoring errors
    # pass2: catch any errors
    ./Allwmake -s -j -k -log=$LOGFILE_DIR/log.$WM_OPTIONS.pass1.txt
    ./Allwmake -s -j -log=$LOGFILE_DIR/log.$WM_OPTIONS.pass2.txt

    echo
    echo "Finished compilation of OpenFOAM $FoamVersion"
    echo "Please source from the shell to set the environment variables."
    echo "Eg,"
    echo "    .  $BASHRC_FILE"
    echo
    echo =================================================================
}

# -----------------------------------------------------------------------------
# Run the selected components

_listSettings
[ "$optList" = true ] && exit 0

1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
if [ "${purgeOLDFILES:-false}" = true ]
then
    commentUNPACK="(purge old files)"
else
    commentUNPACK="(keep old files)"
fi

commentTHIRD=""
commentFOAM=""

1104 1105 1106 1107
/bin/cat<<SUMMARY

Build stages selected
---------------------
1108 1109 1110
    -unpack   ${runUNPACK:-false}  $commentUNPACK
    -third    ${runTHIRD:-false}  $commentTHIRD
    -foam     ${runFOAM:-false}  $commentFOAM
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
---------------------
SUMMARY

[ "$runUNPACK" = true ] && unpackAllFiles
[ "$runTHIRD"  = true ] && compileThirdParty
[ "$runFOAM"   = true ] && compileOpenFOAM

/bin/cat<<- FOOTER

===============================================================================
Completed installation script for $FoamVersion

    Check all log files for any possible errors
    logfiles: $LOGFILE_DIR
===============================================================================
FOOTER
# -----------------------------------------------------------------------------