#---------------------------------*- sh -*------------------------------------- # ========= | # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox # \\ / O peration | # \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation # \\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd. #------------------------------------------------------------------------------ # License # This file is part of OpenFOAM, licensed under GNU General Public License # . # # File # etc/tools/ThirdPartyFunctions # # Description # Various functions used in building ThirdParty packages # # Define the standard buildBASE and installBASE for the platform # Define WM_NCOMPPROCS always. #------------------------------------------------------------------------------ # The normal locations for source, build and installation (prefix-dir) 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 unset BUILD_SUFFIX #------------------------------------------------------------------------------ # Export WM compiler settings (and flags) for CMake/configure # $1 = true: export compiler flags too exportCompiler() { if test -n "$WM_CC" && command -v "$WM_CC" >/dev/null then export CC="$WM_CC" if [ "$1" = true -a -n "$WM_CFLAGS" ] then export CFLAGS="$WM_CFLAGS" fi fi if test -n "$WM_CXX" && command -v "$WM_CXX" >/dev/null then export CXX="$WM_CXX" if [ "$1" = true -a -n "$WM_CXXFLAGS" ] then export CXXFLAGS="$WM_CXXFLAGS" fi fi } # Export WM linker settings for CMake/configure exportLinker() { [ -n "$WM_LDFLAGS" ] && export LDFLAGS="$WM_LDFLAGS" } # Force use of gcc/g++ useGcc() { export CC=gcc # Use gcc/g++ export CXX=g++ } # Scan arguments for a '-gcc' option, forcing gcc/g++ when found useGccFlag() { for i do if [ "$i" = "-gcc" ] then useGcc break fi done } # Return by default or if possible for INTELMPI. # Cray doesn't have , but its manages mpi paths directly. # NOTE: could further refine based on $CC or $WM_CC, but not yet needed whichMpicc() { local mpicc=$(command -v mpicc) case "$WM_MPLIB" in (INTELMPI) mpicc=$(command -v mpiicc) # Intel available? ;; (CRAY-MPI*) : ${mpicc:=cc} # Cray if there is no ;; esac echo "${mpicc:-mpicc}" } #------------------------------------------------------------------------------ # Some functions as per OpenFOAM etc/config.sh/functions unset -f _foamAddLib _foamAddMan _foamAddPath # Get settings only unset -f _foamEtc # Source an etc file _foamEtc() { local file if [ $# -gt 0 ] && file=$($WM_PROJECT_DIR/bin/foamEtcFile "$@") then . $file fi } #------------------------------------------------------------------------------ # # 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 } # # Mostly building without wmake # - disable wmakeScheduler variables # - use max number of cores for building # unset WM_HOSTS WM_SCHEDULER if [ -r /proc/cpuinfo ] then WM_NCOMPPROCS=$(egrep "^processor" /proc/cpuinfo | wc -l) else WM_NCOMPPROCS=1 fi export WM_NCOMPPROCS # # If WM_CONTINUE_ON_ERROR not set activate the shell option "stop on error" # if [ -z "${WM_CONTINUE_ON_ERROR}" ] then set -e fi # 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 } # Test if it matches "*-none" _foamIsNone() { test "${1##*-}" = none } # Test if it matches "*-system" _foamIsSystem() { test "${1##*-}" = system } # # 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. # unset CMAKE_PATH # clear when first loaded findCMake() { local candidate foundExe if [ -n "$CMAKE_PATH" ] then # Check as directory 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 # Check as file, include ThirdParty installation in the search 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 echo "Using cmake=$foundExe" 1>&2 echo $foundExe 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 # Default to use the path. Resolve so we know what we are using. foundExe=$(command -v cmake 2> /dev/null) || foundExe=false echo "Using cmake=$foundExe" 1>&2 echo $foundExe } # # try to locate qmake according to the QMAKE_PATH # # On success: return the resolved value as output. # On failure: just report what is found in the path. # unset QMAKE_PATH # clear when first loaded findQtMake() { local candidate foundExe if [ -n "$QMAKE_PATH" ] then # Check as directory 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 # Check as file, include ThirdParty installation in the search 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 echo "Using qmake=$foundExe" 1>&2 echo $foundExe 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 # Default to use the path, try resolving (so we know what we are using) foundExe=$(command -v qmake 2> /dev/null) || foundExe=false echo "Using qmake=$foundExe" 1>&2 echo $foundExe } # # Set a new prefix=... in pkgconfig files # pkgconfigNewPrefix() { local dir="${1%%/}" if [ -n "$dir" -a -d "$dir" ] then # 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 } echo "Set pkgconfig prefix : $dir" local nfiles for libdir in lib/pkgconfig lib64/pkgconfig do unset nfiles [ -d "$dir/$libdir" ] || continue for i in $dir/$libdir/*.pc do if [ -f "$i" -a ! -L "$i" ] then nfiles="x$nfiles" sed -i -e 's@^\(prefix=\).*$@\1'"$dir@" $i fi done echo " $libdir/*.pc (edited ${#nfiles})" done } # # Adjust pkgconfig information to use '${prefix} where possible instead # of directory paths. # # Adjusts includedir=, libdir=, -I/... and -L/... and # any *_location= entries (QT) # pkgconfigAdjust() { local dir="${1%%/}" if [ -n "$dir" -a -d "$dir" ] then # 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 } 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 do if [ -f "$i" -a ! -L "$i" ] then nfiles="x$nfiles" sed -i \ -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' \ $i fi done echo " $libdir/*.pc (edited ${#nfiles})" done } # # Download file $1 from url $2 into download/ directory # downloadFile() { [ "$#" -eq 2 ] || { echo "downloadFile called with incorrect number of arguments $@" return 1 } local file="$1" local url="$2" if [ ! -e download/$file ] then mkdir -p download echo "downloading $tarFile from $url" ( cd download && wget --no-check-certificate $url -O $file ) fi } # # Copy Make/{files,options} from etc/makeFiles/PACKAGE # # $1 = PACKAGE # $2 = TARGET DIRECTORY (optional) cpMakeFiles() { [ "$#" -eq 1 -o "$#" -eq 2 ] || { echo "cpMakeFiles called with incorrect number of arguments $@" return 1 } local pkg=$1 local dst="${2:-.}" echo "cpMakeFiles" $pkg $dst wmakeFiles=$WM_THIRD_PARTY_DIR/etc/makeFiles/$pkg 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 } # # 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 ( cd $dst || exit 1 if [ -f "$sentinel" ] then echo "patch for $pkg was already applied" else echo "apply patch for $pkg" touch "$sentinel" patch -b -l -p1 < $patch 2>&1 | tee $sentinel fi ) else echo "no patch found for $pkg" fi } #------------------------------------------------------------------------------