makeLLVM 5.75 KB
Newer Older
Henry's avatar
Henry committed
1 2 3 4 5
#!/bin/sh
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
6
#   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
7
#    \\/     M anipulation  | Copyright (C) 2016-2018 OpenCFD Ltd.
Henry's avatar
Henry committed
8 9
#------------------------------------------------------------------------------
# License
Mark Olesen's avatar
Mark Olesen committed
10 11
#     This file is part of OpenFOAM, licensed under GNU General Public License
#     <http://www.gnu.org/licenses/>.
Henry's avatar
Henry committed
12 13 14 15 16
#
# Script
#     makeLLVM
#
# Description
17
#     Build script for llvm, clang etc.
Henry's avatar
Henry committed
18
#
19 20
# Note
#    - Ensure that you always use matching versions between llvm and clang.
21
#    - LLVM components such as clang reside in the LLVM tools/ subdirectory
Henry's avatar
Henry committed
22
#
23
#    For example, when building from tar files (version 4.0.0)
24
#
25
#    1) Unpack LLVM:
Mark Olesen's avatar
Mark Olesen committed
26 27
#        tar -xJf llvm-4.0.1.src.tar.xz
#        mv llvm-4.0.1.src  llvm-4.0.1
28
#
29
#    2) Unpack Clang (also know as cfe):
Mark Olesen's avatar
Mark Olesen committed
30 31 32 33 34 35
#        tar -xJf cfe-4.0.1.src.tar.xz
#        mv cfe-4.0.1.src  llvm-4.0.1/tools/clang
#
#    3) Unpack openmp (optional):
#        tar -xJf openmp-4.0.1.src.tar.xz
#        mv openmp-4.0.1.src  llvm-4.0.1/tools/openmp
Henry's avatar
Henry committed
36
#
37 38 39 40
# ----------------------------------------------
# NO USER-CONFIGURABLE SETTINGS WITHIN THIS FILE
#------------------------------------------------------------------------------
# Run from third-party directory only
41
cd ${0%/*} && wmakeCheckPwd "$WM_THIRD_PARTY_DIR" 2>/dev/null || {
42 43 44 45 46
    echo "Error (${0##*/}) : not located in \$WM_THIRD_PARTY_DIR"
    echo "    Check your OpenFOAM environment and installation"
    exit 1
}
. etc/tools/ThirdPartyFunctions
Henry's avatar
Henry committed
47
#------------------------------------------------------------------------------
48

49 50 51 52
[ "${WM_COMPILER#Clang}" = "$WM_COMPILER" ] && WM_COMPILER=Clang  # Force clang
WM_COMPILER_TYPE=ThirdParty                # Ensure we get the correct settings

# LLVM/Clang version from OpenFOAM etc/config.sh file:
53
_foamConfig compiler
54

55
llvmPACKAGE=$clang_version
56

Henry's avatar
Henry committed
57 58 59 60 61 62
#------------------------------------------------------------------------------
usage() {
    exec 1>&2
    while [ "$#" -ge 1 ]; do echo "$1"; shift; done
    cat<<USAGE

63
usage: ${0##*/} [OPTION] [llvm-VERSION]
64
options:
65 66
  -cmake PATH       with cmake from the path given
  -gcc              force gcc/g++ instead of the values from \$WM_CC, \$WM_CXX
67
  -help
Henry's avatar
Henry committed
68 69

* build llvm/clang
70
      ${llvmPACKAGE:-'unspecified LLVM version'}
Henry's avatar
Henry committed
71 72 73 74

USAGE
    exit 1
}
75 76
#------------------------------------------------------------------------------
exportCompiler          # Compiler info for CMake/configure
77

78
# Parse options
Henry's avatar
Henry committed
79 80 81
while [ "$#" -gt 0 ]
do
    case "$1" in
82 83 84 85
    '') ;;      # Ignore empty
    -h | -help) usage   ;;
    -gcc)       useGcc  ;;

86 87 88 89 90
    -cmake)
        [ "$#" -ge 2 ] || die "'$1' option requires an argument"
        CMAKE_PATH="${2%%/}"
        shift
        ;;
Henry's avatar
Henry committed
91 92 93
    llvm-[0-9]* | llvm-svn*)
        llvmPACKAGE="${1%%/}"
        ;;
94 95 96
    [1-9]*)
        llvmPACKAGE="llvm-${1%%/}"
        ;;
Henry's avatar
Henry committed
97
    *)
98
        die "unknown option/argument: '$1'"
Henry's avatar
Henry committed
99 100
        ;;
    esac
101
    shift
Henry's avatar
Henry committed
102 103
done

104
[ -n "$llvmPACKAGE" ] || die "The llvm-VERSION was not specified"
105

Henry's avatar
Henry committed
106
#------------------------------------------------------------------------------
107
# Build/install locations without a compiler name
Henry's avatar
Henry committed
108 109 110
buildBASE=$WM_THIRD_PARTY_DIR/build/$WM_ARCH$WM_COMPILER_ARCH
installBASE=$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER_ARCH

111 112
# Build LLVM (clang)
#   LLVM_SOURCE_DIR : location of the original sources
113
#   LLVM_BUILD_DIR  : location of the build
114 115 116
#   LLVM_ARCH_PATH  : location of the installed program
# - Strip any trailing '.src' from the proper names

117
LLVM_SOURCE_DIR=$sourceBASE/$llvmPACKAGE
118
LLVM_BUILD_DIR=$buildBASE/${llvmPACKAGE%%.src}
119
LLVM_ARCH_PATH=$installBASE/${llvmPACKAGE%%.src}
Henry's avatar
Henry committed
120 121 122 123 124 125 126 127

#
# Build LLVM
#
echo "---------------"
if [ -d $LLVM_ARCH_PATH ]
then
    echo "Already built: $llvmPACKAGE"
128 129 130 131 132 133 134 135 136
elif [ -z "$CMAKE_PATH" ] && $LLVM_SOURCE_DIR/configure -help >/dev/null 2>&1
then
    # configure can be used prior to 3.9.0
    # but use cmake if someone explicitly mentioned -cmake on the command-line

    echo "Starting build: $llvmPACKAGE (using configure)"
    echo
    (
        cd $LLVM_SOURCE_DIR || exit 1
137
        export GIT_DIR=$PWD/.git # Mask seeing our own git-repo
138 139
        make distclean 2>/dev/null

140 141 142
        rm -rf $LLVM_BUILD_DIR
        mkdir -p $LLVM_BUILD_DIR
        cd $LLVM_BUILD_DIR
143 144 145 146 147 148 149

        set -x
        $LLVM_SOURCE_DIR/configure \
            --prefix=$LLVM_ARCH_PATH \
            --with-gcc-toolchain=$(which gcc | sed s%/bin/gcc%%) \
            --enable-optimized \
            --enable-shared \
150
        && set +x \
151 152 153 154 155 156 157 158
        && make -j $WM_NCOMPPROCS \
        && make install \
        && echo "Built: $llvmPACKAGE"
    ) || {
        echo "Error building: $llvmPACKAGE"
        exit 1
    }

Henry's avatar
Henry committed
159
else
160 161 162
    # CMake used with 3.9.0 and later

    echo "Starting build: $llvmPACKAGE (using cmake)"
Henry's avatar
Henry committed
163
    echo
164
    (
165 166 167
        # Configuration options:
        unset configOpt

168
        cd $LLVM_SOURCE_DIR || exit 1
169
        export GIT_DIR=$PWD/.git # Mask seeing our own git-repo
170

171 172 173 174 175
        if [ -f tools/openmp/CMakeLists.txt ]
        then
            configOpt="$configOpt -DLLVM_TOOL_OPENMP_BUILD=ON"
        fi

176 177 178
        rm -rf $LLVM_BUILD_DIR
        mkdir -p $LLVM_BUILD_DIR
        cd $LLVM_BUILD_DIR
179 180 181 182 183 184 185

        cmake=$(findCMake)

        set -x
        $cmake \
            -DCMAKE_INSTALL_PREFIX=$LLVM_ARCH_PATH \
            -DCMAKE_BUILD_TYPE=Release \
186
            -DBUILD_SHARED_LIBS=ON \
187
            $configOpt \
188
            $LLVM_SOURCE_DIR \
189
        && set +x \
190 191 192 193 194 195 196 197
        && make -j $WM_NCOMPPROCS \
        && make install \
        && echo "Built: $llvmPACKAGE"
    ) || {
        echo "Error building: $llvmPACKAGE"
        exit 1
    }

Henry's avatar
Henry committed
198 199
fi

200
#------------------------------------------------------------------------------