legacyCompile 29.9 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 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
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
)

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

# Special treatment for gcc sub-components (needs rethinking)
case "$compilerVERSION" in
Gcc | Gcc48)
    gccVERSION=gcc-4.8.5   # match value in config.sh/compiler
    ;;
Gcc49)
    gccVERSION=gcc-4.9.3   # match value in config.sh/compiler
    ;;
Gcc62)
    gccVERSION=gcc-6.2.0   # match value in config.sh/compiler
    ;;
Gcc63)
    gccVERSION=gcc-6.3.0   # match value in config.sh/compiler
    ;;
Gcc71)
    gccVERSION=gcc-7.1.0   # match value in config.sh/compiler
    ;;
Gcc*)
    echo "Error: unconfigured gcc version '$compilerVERSION'"
    exit 1 # Really exit, since this must be an input error
    ;;
esac
# -----------------------------------------------------------------------------
# NO FURTHER EDITING BELOW THIS LINE
#------------------------------------------------------------------------------

# 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" ]
379
then
380
    configArch="$configArch -openmpi-$ompiTypeVERSION"
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
fi

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

_listSettings()
{
    echo
    echo "$OFdirName / $TPdirName"
    echo
    _fmt3 package  default  version -nocheck
    _line
    _fmtDefVer boost
    _fmtDefVer cgal
    _fmtDefVer fftw
    _fmtDefVer openmpi
    _fmtDefVer scotch
397
    _fmtDefVer kahip
398 399 400 401 402 403 404
    _fmtDefVer paraview
    _fmtDefVer cmake
    _fmtDefVer vtk
    _fmtDefVer mesa
    _fmtDefVer qt
    _fmtDefVer ompiType
    _fmtDefVer llvm
405 406 407 408
    _fmt3 gcc  "${gccDEFAULT:-system}"  "${gccVERSION:-system}"
    _fmt3 gmp  "${gmpDEFAULT:-system}"  "${gmpVERSION:-system}"
    _fmt3 mpfr "${mpfrDEFAULT:-system}" "${mpfrVERSION:-system}"
    _fmt3 mpc  "${mpcDEFAULT:-system}"  "${mpcVERSION:-system}"
409 410 411
    _line
    echo
    echo  "  configure:  $configArch"
412
    echo  "  compiler:   -$compilerTypeVERSION  $compilerVERSION"
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
    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"
}

#
# 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
    }

562 563 564
    # Unpack OpenFOAM and ThirdParty
    # Source package names may differ from the directory names
    # Also handle '-plus' extensions (common problem?)
565
    _unpack "$OFsource"
566 567 568
    _mvdir "$OFsource" "$OFdirName"
    _mvdir OpenFOAM-plus "$OFdirName"

569
    _unpack "$TPsource";
570 571 572
    _mvdir "$TPsource" "$TPdirName"
    _mvdir ThirdParty-plus "$TPdirName"

573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588

    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
        # Compiler configuration (already sanity checked much further above)
589 590
        compilerOpt="-$compilerTypeVERSION $compilerVERSION"
        case "$compilerTypeVERSION:$compilerVERSION" in
591
        third:Clang*)
592
            compilerOpt="$compilerOpt -clang $llvmVERSION"
593 594
            ;;
        third:Gcc*)
595
            compilerOpt="$compilerOpt $gmpVERSION $mpfrVERSION $mpcVERSION"
596 597 598 599 600 601 602 603
            ;;
        esac

        set -- \
            -foamVersion $FoamVersion \
            $configArch $compilerOpt \
            $(
                # package versions
604 605 606 607 608
                _ifDifferent -cmake   $cmakeVERSION   $cmakeDEFAULT
                _ifDifferent -boost   $boostVERSION   $boostDEFAULT
                _ifDifferent -cgal    $cgalVERSION    $cgalDEFAULT
                _ifDifferent -fftw    $fftwVERSION    $fftwDEFAULT
                _ifDifferent -scotch  $scotchVERSION  $scotchDEFAULT
609
                _ifDifferent -kahip   $kahipVERSION   $kahipDEFAULT
610
                _ifDifferent -openmpi $openmpiVERSION $openmpiDEFAULT
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
            )

        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
    }

630 631 632 633 634
    _unpackIfDifferent $cmakeVERSION    $cmakeDEFAULT
    _unpackIfDifferent $boostVERSION    $boostDEFAULT
    _unpackIfDifferent $cgalVERSION     $cgalDEFAULT
    _unpackIfDifferent $fftwVERSION     $fftwDEFAULT
    _unpackIfDifferent $scotchVERSION   $scotchDEFAULT
635
    _unpackIfDifferent $kahipVERSION    $kahipDEFAULT
636
    _unpackIfDifferent $openmpiVERSION  $openmpiDEFAULT
637

638
    case "$compilerTypeVERSION:$compilerVERSION" in
639
    third:Gcc*)
640 641 642 643 644
        echo "# ThirdParty Compiler: $compilerVERSION"
        _unpack $gccVERSION
        _unpack $gmpVERSION
        _unpack $mpfrVERSION
        _unpack $mpcVERSION
645 646
        ;;
    third:Clang*)
647
        echo "# ThirdParty Compiler: $compilerVERSION"
648
        # Mostly unpacks to llvm-x.y.z.src, relocate to llvm-x.y.z
649 650
        _unpack $llvmVERSION
        _mvdir "$llvmVERSION.src" "$llvmVERSION"          # (*.src -> *)
651 652 653

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

656
        if [ -d "$llvmVERSION" -a ! -d "$llvmVERSION/$clangDir" ]
657
        then
658 659 660
            _unpack $cfeVERSION
            _mvdir "$cfeVERSION.src" "$cfeVERSION"        # As above (*.src -> *)
            _mvdir "$cfeVERSION" "$llvmVERSION/$clangDir" # Relocate to tools/clang
661
        fi
662 663 664 665 666 667 668 669 670 671 672

        # 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
673 674 675 676
        ;;
    esac

    echo "# Graphics"
677
    _unpackIfDifferent $paraviewVERSION $paraviewDEFAULT
678 679 680 681
    # Mostly unpacks to ParaView-vx.y.z | ParaView-vx.y.z-source,
    # relocate to ParaView-x.y.z

    # This is why bash
682 683 684
    _mvdir "${paraviewVERSION}-source"          "$paraviewVERSION"
    _mvdir "${paraviewVERSION/-/-v}"            "$paraviewVERSION"
    _mvdir "${paraviewVERSION/-/-v}-source"     "$paraviewVERSION"
685

686
    _unpack $mesaVERSION
687 688 689 690 691

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

    # VTK is a link to ParaView VTK
692
    if [ -n "$vtkVERSION" ]
693
    then
694
        if readlink "$vtkVERSION" > /dev/null
695
        then
696 697
            echo "  Remove old link to $vtkVERSION"
            rm "$vtkVERSION"
698
        fi
699
        if [ -d "${paraviewVERSION:-paraview#undef}/VTK" ]
700
        then
701 702
            echo "  Create new link  $paraviewVERSION/VTK $vtkVERSION"
            ln -sv $paraviewVERSION/VTK $vtkVERSION
703
        else
704 705
            echo "  did not make link for vtk $vtkVERSION"
            echo "  perhaps no paraview directory? ${paraviewVERSION:-???}/VTK"
706 707 708 709
        fi
    fi

    ## Check if these may be required (again, in the future)
710 711
    ## _unpack $automakeVERSION
    ## _unpack $autoconfVERSION
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732

    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

733
    . $BASHRC_FILE ''
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751

    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:"
752
    if [ -d "$WM_THIRD_PARTY_DIR/${cmakeVERSION:-cmake#undef}" ]
753 754
    then
        makeCommand=makeCmake
755
        makeOptions="-gcc $cmakeVERSION"
756

757
        makeTarget="$platforms/$WM_ARCH$WM_COMPILER/$cmakeVERSION"
758 759 760 761 762 763 764 765 766 767 768 769 770 771
        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
772
            . $BASHRC_FILE '' 2>/dev/null
773 774 775 776 777 778
        fi
    else
        echo "  not specified or not found"
    fi

    ## Check if these may be required (again, in the future)
779 780
    ## ( cd $automakeVERSION && some more stuff )
    ## ( cd $autoconfVERSION && some more stuff )
781 782 783 784 785


    # Compiler ... this can take a while
    echo
    echo "compiler:"
786
    case "$compilerTypeVERSION:$compilerVERSION" in
787 788
    third:Gcc*)
        makeCommand=makeGcc
789
        makeOptions="-no-threadsafe $gccVERSION $gmpVERSION $mpfrVERSION $mpcVERSION"
790

791
        makeTarget="$platforms/$WM_ARCH/$gccVERSION"
792 793 794 795 796 797 798 799 800 801 802 803 804
        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)
805
            . $BASHRC_FILE ''
806 807 808 809 810 811 812
        fi
        ;;
    third:Clang*)
        makeCommand=makeLLVM
        makeOptions=""
        # makeOptions="-gcc"

813
        makeTarget="$platforms/$WM_ARCH/$llvmVERSION"
814 815 816 817 818 819 820 821 822 823 824 825 826
        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)
827
            . $BASHRC_FILE ''
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
        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:"
875 876
    mesaPREFIX="$platforms/$WM_ARCH$WM_COMPILER/$mesaVERSION"
    if [ -d "$WM_THIRD_PARTY_DIR/${mesaVERSION:-mesa#undef}" ]
877 878
    then
        makeCommand=makeMesa
879
        makeOptions="$mesaVERSION"
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

        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)"
906
    if [ -d "$mesaPREFIX" -a -d "$WM_THIRD_PARTY_DIR/${vtkVERSION:-vtk#undef}" ]
907 908
    then
        makeCommand=makeVTK
909
        makeOptions="$vtkVERSION -osmesa -mesa-prefix $mesaPREFIX"
910

911
        makeTarget="$platforms/$WM_ARCH$WM_COMPILER/$vtkVERSION"
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
        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)"
930
    qtPREFIX="$platforms/$WM_ARCH$WM_COMPILER/$qtVERSION"
931 932 933
    if [ -d "$WM_THIRD_PARTY_DIR/${qtSource:-qt#undef}" ]
    then
        makeCommand=makeQt
934
        makeOptions="$qtVERSION"
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
        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"
957
        unset qtVERSION qtPREFIX # avoid using for paraview build
958 959 960 961
    fi

    echo
    echo "ParaView:"
962
    if [ -d "$WM_THIRD_PARTY_DIR/${paraviewVERSION:-paraview#undef}" ]
963 964 965 966 967 968 969
    then
        makeCommand=makeParaView
        makeOptions=""
        if [ -d "$mesaPREFIX" ]
        then
            makeOptions="$makeOptions -mesa-prefix $mesaPREFIX"
        fi
970
        if [ -n "$qtVERSION" -a -x "$qtPREFIX/bin/qmake" ]
971
        then
972
            makeOptions="$makeOptions -$qtVERSION"
973 974
        fi

975
        makeTarget="$platforms/$WM_ARCH$WM_COMPILER/$paraviewVERSION"
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
        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

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

    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

1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
if [ "${purgeOLDFILES:-false}" = true ]
then
    commentUNPACK="(purge old files)"
else
    commentUNPACK="(keep old files)"
fi

commentTHIRD=""
commentFOAM=""

1061 1062 1063 1064
/bin/cat<<SUMMARY

Build stages selected
---------------------
1065 1066 1067
    -unpack   ${runUNPACK:-false}  $commentUNPACK
    -third    ${runTHIRD:-false}  $commentTHIRD
    -foam     ${runFOAM:-false}  $commentFOAM
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
---------------------
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
# -----------------------------------------------------------------------------