ThirdPartyFunctions 18.9 KB
Newer Older
Henry's avatar
Henry committed
1
2
3
4
#---------------------------------*- sh -*-------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
5
#   \\  /    A nd           | www.openfoam.com
OpenFOAM bot's avatar
OpenFOAM bot committed
6
7
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
8
9
#     Copyright (C) 2011-2016 OpenFOAM Foundation
#     Copyright (C) 2016-2019 OpenCFD Ltd.
Henry's avatar
Henry committed
10
11
#------------------------------------------------------------------------------
# License
Mark Olesen's avatar
Mark Olesen committed
12
13
#     This file is part of OpenFOAM, licensed under GNU General Public License
#     <http://www.gnu.org/licenses/>.
Henry's avatar
Henry committed
14
15
16
17
18
#
# File
#     etc/tools/ThirdPartyFunctions
#
# Description
19
#     Various functions used in building ThirdParty packages
Henry's avatar
Henry committed
20
21
22
#
#     Define the standard buildBASE and installBASE for the platform
#     Define WM_NCOMPPROCS always.
23
24
25
26
#
#     Compiler and flags are managed via the 'wmake -show-c, -show-cflags, ..'
#     but also with WM_CC, WM_CFLAGS,... env variables
#
Henry's avatar
Henry committed
27
28
#------------------------------------------------------------------------------

29
# The normal locations for source, build and installation (prefix-dir)
30
31
32
33
34
35
sourceBASE="$WM_THIRD_PARTY_DIR"
buildBASE="$WM_THIRD_PARTY_DIR/build/$WM_ARCH$WM_COMPILER"
installBASE="$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER"

# Synthetic value combining precision and label size (Eg, DPInt32)
WM_SIZE_OPTIONS="${WM_PRECISION_OPTION}Int${WM_LABEL_SIZE}"
Henry's avatar
Henry committed
36

37
38
39
# Dynamic library ending (default is .so)
EXT_SO="$(wmake -show-ext-so 2>/dev/null)"
if [ -z "$EXT_SO" ]
40
then
41
    [ "$(uname -s)" = Darwin ] && EXT_SO=.dylib || EXT_SO=.so
42
43
44
45
46
47
48
49
50
51
52
53
54
55
fi


# Fallback values, needed for our scotch Makefile which uses
# WM_CFLAGS and WM_LDFLAGS for arch information
if [ -z "$WM_CFLAGS" ]
then
    export WM_CFLAGS="$(wmake -show-cflags 2>/dev/null)"
fi

if [ -z "$WM_LDFLAGS" ]
then
    export WM_LDFLAGS="$(wmake -show-cflags-arch 2>/dev/null)"
fi
56

57
unset BUILD_SUFFIX
58
#------------------------------------------------------------------------------
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Service routine to strip out OpenFOAM-specific portions from the compiler
# flags (ie, everything after "-DOPENFOAM=...") while retaining '-fPIC'
#
stripCompilerFlags()
{
    local input="$@"
    local flags="${input%-DOPENFOAM=*}"  # Strip out OpenFOAM-specific
    flags="${flags## }"                  # Trim leading space
    flags="${flags%% }"                  # Trim trailing space

    # Retain -fPIC though
    case "$flags" in
    (*-fPIC*)
        # -fPIC already included
        ;;

    (*)
        case "$input" in
        (*-fPIC*)
            # Add -fPIC back in (was after the -DOPENFOAM=... content)
            flags="$flags${flags+ }-fPIC"
            ;;
        esac
        ;;
    esac

    echo "$flags"
}


89
90
91
92
93
# Export compiler settings (and flags) for CMake/configure
# based on the values from wmake -show-compile-*
#
# Since "wmake -show-XX" is only available after 1904, continue to support
# the previous env variables method (WM_CC, WM_CFLAGS, WM_CXX, WM_CXXFLAGS)
94
95
96
97
98
99
100
#
# $1 = "basic|minimal|strip" (optional)
#
# If the option 'basic' is provided, the OpenFOAM-specific portions of
# the flags are stripped out. Ie, everything after "-DOPENFOAM=..."
# but retaining '-fPIC'
#
101
102
exportCompiler()
{
103
    local option="$1"
104
105
106
107
108
109
110
111
112
113
    local comp flag

    # C compiler and flags
    comp="$(wmake -show-c 2>/dev/null)" && \
    flag="$(wmake -show-cflags 2>/dev/null)" || {
        comp="$WM_CC"
        flag="$WM_CFLAGS"
    }

    if test -n "$comp" && command -v "$comp" >/dev/null
114
    then
115
116
        export CC="$comp"
        if [ -n "$flag" ]
117
        then
118
119
120
121
122
            case "$option" in
                (basic | minimal | strip)
                    flag="$(stripCompilerFlags $flag)"
                    ;;
            esac
123
            export CFLAGS="$flag"
124
125
126
        fi
    fi

127
128
129
130
131
132
133
134
    # C++ compiler and flags
    comp="$(wmake -show-cxx 2>/dev/null)" && \
    flag="$(wmake -show-cxxflags 2>/dev/null)" || {
        comp="$WM_CXX"
        flag="$WM_CXXFLAGS"
    }

    if test -n "$comp" && command -v "$comp" >/dev/null
135
    then
136
137
        export CXX="$comp"
        if [ -n "$flag" ]
138
        then
139
140
141
142
143
            case "$option" in
                (basic | minimal | strip)
                    flag="$(stripCompilerFlags $flag)"
                    ;;
            esac
144
            export CXXFLAGS="$flag"
145
146
147
148
        fi
    fi
}

149

150
# Export linker settings for CMake/configure
151
152
exportLinker()
{
153
154
155
156
157
158
    local flag

    # Linker flags, actually just the machine arch
    flag="$(wmake -show-cflags-arch 2>/dev/null)" || flag="$WM_LDFLAGS"

    if test -n "$flag"
159
    then
160
        export LDFLAGS="$flag"
161
    fi
162
163
}

164
165

# Force use of gcc/g++, but the values of CFLAGS, CXXFLAGS may be incorrect
166
167
useGcc()
{
168
    export CC=gcc
169
170
171
    export CXX=g++
}

172
173
174
175
176
177
178
179
180
181
182
183
184
# Scan arguments for a '-gcc' option, forcing gcc/g++ when found
useGccFlag()
{
    for i
    do
        if [ "$i" = "-gcc" ]
        then
            useGcc
            break
        fi
    done
}

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
214
215
216
217
218
# Return current value of $CC if set or obtain from 'wmake -show-c',
# using $WM_CC (older env variable) as the first level fallback.
# Final fallback is <gcc>
whichCC()
{
    local comp="$CC"

    if [ -z "$comp" ]
    then
        comp="$(wmake -show-c 2>/dev/null)" || comp="$WM_CC"
        test -n "$comp" && command -v "$comp" >/dev/null || unset comp
    fi

    echo "${comp:-gcc}"
}


# Return current value of $CXX if set or obtain from 'wmake -show-cxx',
# using $WM_CXX (older env variable) as the first level fallback.
# Final fallback is <g++>
whichCXX()
{
    local comp="$CXX"

    if [ -z "$comp" ]
    then
        comp="$(wmake -show-cxx 2>/dev/null)" || comp="$WM_CXX"
        test -n "$comp" && command -v "$comp" >/dev/null || unset comp
    fi

    echo "${comp:-g++}"
}


219
220
# Return <mpicc> by default or <mpiicc> if possible for INTELMPI.
# Cray doesn't have <mpicc>, but its <cc> manages mpi paths directly.
221
# NOTE: could further refine based on "wmake -show-c", but not yet needed
222
223
whichMpicc()
{
224
    local comp="$(command -v mpicc)"
225
226
    case "$WM_MPLIB" in
    (INTELMPI)
227
        comp="$(command -v mpiicc)"     # Intel <mpiicc> available?
228
229
        ;;
    (CRAY-MPI*)
230
        : "${comp:=cc}"                 # Cray <cc> if there is no <mpicc>
231
232
        ;;
    esac
233
    echo "${comp:-mpicc}"
234
235
}

236

237
238
239
# Return <mpicxx> by default or <mpiicpc> if possible for INTELMPI.
# Cray doesn't have <mpicxx>, but its <CC> manages mpi paths directly.
# NOTE: could further refine based on "wmake -show-cxx", but not yet needed
240
241
whichMpicxx()
{
242
    local comp="$(command -v mpicxx)"
243
244
    case "$WM_MPLIB" in
    (INTELMPI)
245
        comp="$(command -v mpiicpc)"    # Intel <mpiicpc> available?
246
247
        ;;
    (CRAY-MPI*)
248
        : "${comp:=CC}"                 # Cray <CC> if there is no <mpicc>
249
250
        ;;
    esac
251
    echo "${comp:-mpicxx}"
252
253
}

254
# Require wmkdepend etc when building with wmake
255
256
requireWMakeToolchain()
{
257
258
    local wmDir="${WM_DIR:-$WM_PROJECT_DIR/wmake}"
    local archDir="$wmDir/platforms/$WM_ARCH$WM_COMPILER"
259

260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
    if [ -x "$archDir/wmkdepend" ] || [ -x "$archDir/wmkdep" ]
    then
        echo "Appear to have {wmkdepend,wmkdep} binary" 1>&2
    else
        echo "Warning: appear to be missing {wmkdepend,wmkdep} binary ... building" 1>&2
        ( cd "$wmDir/src" && make -s )

        [ -x "$archDir/wmkdepend" ] || [ -x "$archDir/wmkdep" ] || {
            exec 1>&2
            echo
            echo "Error: cannot use wmake build for '${0##*/}"
            echo "    Missing {wmkdepend,wmkdep} binary"
            echo "    Please try run the top-level OpenFOAM Allwmake first"
            echo
            exit 1
        }
    fi
277
278
}

279

280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# grep for package http or ftp entries in BUILD.md
# Should be of the form "[link xx]:  http://..."
showDownloadHint()
{
    local package="$1"

    if [ -n "$package" ]
    then
        echo "Possible download locations for $package :"

        if [ -f "$WM_THIRD_PARTY_DIR/BUILD.md" ]
        then
            grep -i "$package" "$WM_THIRD_PARTY_DIR/BUILD.md" | \
            grep -E '(http|ftp)' | sed -ne 's/^ *\[.*\]: */    /p'
        fi
    fi
}


299
300
301
#------------------------------------------------------------------------------
# Some functions as per OpenFOAM etc/config.sh/functions

302
unset -f _foamAddLib _foamAddLibAuto _foamAddMan _foamAddPath # Get settings only
303
unset -f _foamClean _foamConfig
304
305
306
307
308
309
310
311
312
313
314
315
316

if [ -x "$WM_PROJECT_DIR/bin/foamCleanPath" ]
then
    # Cleaning environment variables
    _foamClean()
    {
         local var=$1
         shift
         eval $($WM_PROJECT_DIR/bin/foamCleanPath -sh-env=$var "$@")
    }
else
    _foamClean() { echo "No foamCleanPath" 1>&2; }
fi
317

318
319
# Source an etc/config.sh file
_foamConfig() { eval "$($WM_PROJECT_DIR/bin/foamEtcFile -sh -config $@)"; }
320

321
322
323
# Source an etc file (as per OpenFOAM functions). Eg, for mpi setup.
_foamEtc() { eval "$($WM_PROJECT_DIR/bin/foamEtcFile -sh $@)"; }

324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#------------------------------------------------------------------------------

#
# Set a suffix for the build
# - eg, for -mpi, or -mesa etc
#
setBuildSuffix()
{
    BUILD_SUFFIX="${1##-}"
    if [ -n "$BUILD_SUFFIX" ]
    then
        BUILD_SUFFIX="-${BUILD_SUFFIX}"
    else
        unset BUILD_SUFFIX
    fi
}


Henry's avatar
Henry committed
342
#
343
# Mostly building without wmake
Henry's avatar
Henry committed
344
# - disable wmakeScheduler variables
345
# - use max number of cores for building
Henry's avatar
Henry committed
346
347
#
unset WM_HOSTS WM_SCHEDULER
348

349
WM_NCOMPPROCS=$(getconf _NPROCESSORS_ONLN 2>/dev/null) || WM_NCOMPPROCS=1
350
: ${WM_NCOMPPROCS:=1}
351
export WM_NCOMPPROCS
Henry's avatar
Henry committed
352
353
354


#
355
356
# If WM_CONTINUE_ON_ERROR not set activate the shell option "stop on error"
#
357
if [ -z "$WM_CONTINUE_ON_ERROR" ]
358
359
360
361
362
then
    set -e
fi


363
364
365
366
367
368
369
370
371
372
373
# 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
}

374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# Warn
warnBuildIssues()
{
    echo
    echo "    ---------------------------------------------------"
    echo "    Optional component ($1) had build issues"
    echo "    OpenFOAM will nonetheless remain largely functional"
    echo "    ---------------------------------------------------"
    echo
}

# Warn
warnNotFound()
{
    echo "Optional component ($1) was not found"
}


392
393
394
395
396
397
398
399
400
401
402
403
404
# Test if it matches "*-none"
_foamIsNone()
{
    test "${1##*-}" = none
}

# Test if it matches "*-system"
_foamIsSystem()
{
    test "${1##*-}" = system
}


405
#
406
407
408
409
# Try to locate cmake according to the CMAKE_PATH.
#
# On success: return the resolved value as output.
# On failure: just report what is found in the path.
410
#
411
unset CMAKE_PATH # clear when first loaded
412
413
findCMake()
{
414
    local candidate foundExe
415
416
417

    if [ -n "$CMAKE_PATH" ]
    then
418
        # Check as directory
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
        if [ -d "$CMAKE_PATH" ]
        then
            for candidate in \
                $CMAKE_PATH/cmake \
                $CMAKE_PATH/bin/cmake \
            ;
            do
                if [ -f "$candidate" -a -x "$candidate" ]
                then
                    foundExe=$candidate
                    break
                fi
            done
        fi

434
        # Check as file, include ThirdParty installation in the search
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
        if [ -z "$foundExe" ]
        then
            for candidate in \
                $CMAKE_PATH \
                $installBASE/$CMAKE_PATH/bin/cmake \
                $installBASE/cmake-$CMAKE_PATH/bin/cmake \
            ;
            do
                if [ -f "$candidate" -a -x "$candidate" ]
                then
                    foundExe=$candidate
                    break
                fi
            done
        fi

        if [ -n "$foundExe" ]
        then
            # Use absolute path
            if [ "${foundExe#/}" = "$foundExe" ]
            then
                foundExe="$(cd ${foundExe%/cmake} 2>/dev/null && pwd)/cmake"
            fi
458
            echo "Using cmake=$foundExe" 1>&2
459
            echo $foundExe
460
461
462
463
464
465
466
467
468
469
            return 0
        else
            cat << NOT_FOUND 1>&2
'cmake' not found under specified CMAKE_PATH
    CMAKE_PATH=$CMAKE_PATH
reverting to using command from path
NOT_FOUND
        fi
    fi

470
471
472
473
474
475
476
    # Default to use the path, try resolving (so we know what we are using).
    for candidate in cmake
    do
        foundExe=$(command -v $candidate 2>/dev/null) && break
    done
    : ${foundExe:=false}

477
478
    echo "Using cmake=$foundExe" 1>&2
    echo $foundExe
479
480
481
482
}


#
483
# Try to locate qmake or qmake-qt5 according to the QMAKE_PATH
484
485
486
#
# On success: return the resolved value as output.
# On failure: just report what is found in the path.
487
#
488
unset QMAKE_PATH # clear when first loaded
489
findQMake()
490
{
491
    local candidate foundExe
492
493
494

    if [ -n "$QMAKE_PATH" ]
    then
495
        # Check as directory
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
        if [ -d "$QMAKE_PATH" ]
        then
            for candidate in \
                $QMAKE_PATH/qmake \
                $QMAKE_PATH/bin/qmake \
            ;
            do
                if [ -f "$candidate" -a -x "$candidate" ]
                then
                    foundExe=$candidate
                    break
                fi
            done
        fi

511
        # Check as file, include ThirdParty installation in the search
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
        if [ -z "$foundExe" ]
        then
            for candidate in \
                $QMAKE_PATH \
                $installBASE/$QMAKE_PATH/bin/qmake \
                $installBASE/qt-$QMAKE_PATH/bin/qmake \
            ;
            do
                if [ -f "$candidate" -a -x "$candidate" ]
                then
                    foundExe=$candidate
                    break
                fi
            done
        fi

        if [ -n "$foundExe" ]
        then
            # Use absolute path
            if [ "${foundExe#/}" = "$foundExe" ]
            then
                foundExe="$(cd ${foundExe%/qmake} 2>/dev/null && pwd)/qmake"
            fi
535
            echo "Using qmake=$foundExe" 1>&2
536
            echo $foundExe
537
538
539
540
541
542
543
544
545
546
            return 0
        else
            cat << NOT_FOUND 1>&2
'qmake' not found under specified QMAKE_PATH
    QMAKE_PATH=$QMAKE_PATH
reverting to using command from path
NOT_FOUND
        fi
    fi

547
548
549
550
551
552
553
554
    # Default to use the path, try resolving (so we know what we are using).
    # Some systems have qmake-qt5 as well as qmake
    for candidate in qmake-qt5 qmake
    do
        foundExe=$(command -v $candidate 2>/dev/null) && break
    done
    : ${foundExe:=false}

555
556
    echo "Using qmake=$foundExe" 1>&2
    echo $foundExe
557
558
559
}


560
#
561
# Set a new prefix=... in pkgconfig files
562
#
563
pkgconfigNewPrefix()
564
{
565
    local dir="${1%%/}"
566

567
    if [ -n "$dir" -a -d "$dir" ]
568
    then
569
570
571
572
573
574
575
576
577
578
579
580
581
582
        # Require absolute path, but use logical (not physical) location
        [ "${dir}" != "${dir#/}" ] || dir=$(cd $dir 2>/dev/null && /bin/pwd -L)
        # Strip sub-level
        case "$dir" in (*/pkgconfig) dir="${dir%/*}";; esac
        # Strip a level
        case "$dir" in (*/lib | */lib64 | */bin) dir="${dir%/*}";; esac
    fi

    # Verify that the prefix path is valid
    # Warning (not an error) - thus no special return code
    [ -n "$dir" -a -d "$dir" ] || {
        echo "Warning: invalid prefix directory: $dir" 1>&2
        return 0
    }
583

584
    echo "Set pkgconfig prefix : $dir"
585

586
587
588
589
590
591
    local nfiles
    for libdir in lib/pkgconfig lib64/pkgconfig
    do
        unset nfiles
        [ -d "$dir/$libdir" ] || continue
        for i in $dir/$libdir/*.pc
592
        do
593
            if [ -f "$i" -a ! -L "$i" ]
594
595
            then
                nfiles="x$nfiles"
596
                sed -i~ -e 's@^\(prefix=\).*$@\1'"$dir@" $i
597
598
            fi
        done
599
600
        echo "    $libdir/*.pc  (edited ${#nfiles})"
    done
601
602
603
604
}


#
605
606
607
608
609
# Adjust pkgconfig information to use '${prefix} where possible instead
# of directory paths.
#
# Adjusts includedir=, libdir=, -I/... and -L/... and
# any *_location= entries (QT)
610
611
612
#
pkgconfigAdjust()
{
613
    local dir="${1%%/}"
614

615
    if [ -n "$dir" -a -d "$dir" ]
616
    then
617
618
619
620
621
622
623
        # Require absolute path, but use logical (not physical) location
        [ "${dir}" != "${dir#/}" ] || dir=$(cd $dir 2>/dev/null && /bin/pwd -L)
        # Strip sub-level
        case "$dir" in (*/pkgconfig) dir="${dir%/*}";; esac
        # Strip a level
        case "$dir" in (*/lib | */lib64 | */bin) dir="${dir%/*}";; esac
    fi
624

625
626
627
628
629
630
    # Verify that the prefix path is valid
    # Warning (not an error) - thus no special return code
    [ -n "$dir" -a -d "$dir" ] || {
        echo "Warning: invalid prefix directory: $dir" 1>&2
        return 0
    }
631

632
633
634
635
636
637
638
639
    echo "Adjust pkgconfig locations : $dir"

    local nfiles
    for libdir in lib/pkgconfig lib64/pkgconfig
    do
        unset nfiles
        [ -d "$dir/$libdir" ] || continue
        for i in $dir/$libdir/*.pc
640
        do
641
            if [ -f "$i" -a ! -L "$i" ]
642
643
            then
                nfiles="x$nfiles"
644
                sed -i~ \
645
646
647
648
                    -e 's@^\(includedir=\)'"$dir/"'@\1${prefix}/@' \
                    -e 's@^\(libdir=\)'"$dir/"'@\1${prefix}/@'     \
                    -e 's@\(_location=\)'"$dir/"'@\1${prefix}/@'   \
                    -e 's@\(-[IL]\)'"$dir/"'@\1${prefix}/@g'       \
649
650
651
                    $i
            fi
        done
652
653
        echo "    $libdir/*.pc  (edited ${#nfiles})"
    done
654
655
656
}


657
658
#
# Download file $1 from url $2 into download/ directory
Henry's avatar
Henry committed
659
660
661
662
663
664
665
666
#
downloadFile()
{
    [ "$#" -eq 2 ] || {
        echo "downloadFile called with incorrect number of arguments $@"
        return 1
    }

667
668
    local file="$1"
    local url="$2"
Henry's avatar
Henry committed
669
670
671
672
673
674
675
676
677
678
679

    if [ ! -e download/$file ]
    then
        mkdir -p download
        echo "downloading $tarFile from $url"
        ( cd download && wget --no-check-certificate $url -O $file )
    fi
}


#
680
# Copy Make/{files,options} from etc/makeFiles/PACKAGE
Henry's avatar
Henry committed
681
682
#
# $1 = PACKAGE
683
# $2 = TARGET DIRECTORY (optional)
Henry's avatar
Henry committed
684
685
686
687
688
689
690
cpMakeFiles()
{
    [ "$#" -eq 1 -o "$#" -eq 2 ] || {
        echo "cpMakeFiles called with incorrect number of arguments $@"
        return 1
    }

691
692
    local pkg=$1
    local dst="${2:-.}"
Henry's avatar
Henry committed
693
694
    echo "cpMakeFiles" $pkg $dst

695
    wmakeFiles=$WM_THIRD_PARTY_DIR/etc/makeFiles/$pkg
Henry's avatar
Henry committed
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719

    for i in $(cd $wmakeFiles && find . -type f)
    do
        d=${i%/*}   # dirname
        b=${i##*/}   # basename

        mkdir -p $dst/$d/Make 2>/dev/null

        # NOTE the behaviour of '-nt' can cause problems
        #
        # - bash, ksh, /usr/bin/test
        #   True, if file1 exists and file2 does not
        #
        # - dash, zsh (and maybe others)
        #   False, if file1 or file2 does not exist
        #
        if [ ! -e $dst/$d/Make/$b -o $wmakeFiles/$i -nt $dst/$d/Make/$b ]
        then
            cp $wmakeFiles/$i $dst/$d/Make/$b
        fi
    done
}


720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
#
# Apply source-code patch if possible.
# Patches are taken from etc/patches/PACKAGE
#
# $1 = PACKAGE
# $2 = TARGET DIRECTORY (optional)
applyPatch()
{
    [ "$#" -eq 1 -o "$#" -eq 2 ] || {
        echo "applyPatch called with incorrect number of arguments ($#): $@"
        return 1
    }

    local pkg="$1"
    local dst="${2:-.}"

    local patch="$WM_THIRD_PARTY_DIR/etc/patches/$pkg"
    local sentinel="PATCHED_DURING_OPENFOAM_BUILD"

    if [ -r "$patch" ]
    then
    (
742
        cd "$dst" || exit
743
744
745
746
747
748
        if [ -f "$sentinel" ]
        then
            echo "patch for $pkg was already applied"
        else
            echo "apply patch for $pkg"
            touch "$sentinel"
749
            patch -b -l -p1 < $patch 2>&1 | tee $sentinel
750
751
752
753
754
755
756
        fi
    )
    else
        echo "no patch found for $pkg"
    fi
}

757

758
#------------------------------------------------------------------------------