summaryrefslogtreecommitdiff
path: root/tcwg-llvm-build.sh
blob: 8efef9d27577da943e6efc81e552245085893069 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
#!/usr/bin/env bash

# This script is an experiment on how to build LLVM in Jenkins.
# The steps here are based on the scripts currently in:
# https://git.linaro.org/toolchain/llvm/linaro-scripts.git

set -e
set -o pipefail

# Syntax
SYN_BUILD="--build={ninja|make} (def. make)"
SYN_BUILDTYPE="--buildtype={Release|Debug} (def. Release)"
SYN_ASSERTS="--asserts={True|False} (def. True)"
SYN_SRCDIR="--srcdir=path/to/source"
SYN_BUILDDIR="--builddir=path/to/build"
SYN_INSTDIR="--instdir=path/to/install"
SYN_BUILDJOBS="--buildjobs=N (def. CPUS)"
SYN_LINKJOBS="--linkjobs=N (def. RAM+1)"
SYNTAX="$0 $SYN_BUILD $SYN_ASSERTS $SYN_RELEASETYPE $SYN_SRCDIR $SYN_BUILDDIR $SYN_INSTDIR $SYN_LINKJOBS $SYN_BUILDJOBS"

# Environment Variables and default values
GEN="Unix Makefiles"
BUILD="make"
BUILDTYPE="Release"
ASSERTS="True"
CPUS=$(nproc --all)
SRCDIR=""
BUILDDIR=""
INSTDIR=""
BUILDJOBS=$CPUS
LINKJOBS=$(free -g | awk '/Mem/ {print $2}')
# We don't want more link jobs than CPUs, even if there's plenty of RAM
if [ "$LINKJOBS" -ge "$CPUS" ]; then
  LINKJOBS=$CPUS
# We may use between 500MB and 1GB per link job, though
else
  LINKJOBS=$((LINKJOBS+1))
fi

# Command line parsing
while [ "$#" -gt 0 ]; do
    ARG=$(echo "$1" | cut -d "=" -f 1)
    VAL=$(echo "$1" | cut -d "=" -f 2)
    case "$ARG" in
      --build)
        if [ "$VAL" = "ninja" ]; then
          GEN="Ninja"
          BUILD="ninja" 
        elif [ "$VAL" != "make" ]; then
          echo "ERROR: $1"
          echo "Syntax: $SYN_BUILD"
          exit 1
        fi
        shift ;;
      --buildtype)
        if [ "$VAL" = "Debug" ]; then
          BUILDTYPE="$VAL"
        elif [ "$VAL" != "Release" ]; then
          echo "ERROR: $1"
          echo "Syntax: $SYN_BUILDTYPE"
          exit 1
        fi
        shift ;;
      --asserts)
        if [ "$VAL" = "False" ]; then
          ASSERTS="$VAL"
        elif [ "$VAL" != "True" ]; then
          echo "ERROR: $1"
          echo "Syntax: $SYN_ASSERTS"
          exit 1
        fi
        shift ;;
      --srcdir)
        if [ -d "$VAL" ]; then
          SRCDIR="$VAL"
        else
          echo "ERROR: Source dir '$VAL' doesn't exist"
          echo "Syntax: $SYN_SRCDIR"
          exit 1
        fi
        shift ;;
      --builddir)
        if [ ! -d "$VAL" ] && ! mkdir -p "$VAL"; then
          echo "ERROR: Build dir '$VAL' doesn't exist and can't be created"
          echo "Syntax: $SYN_BUILDDIR"
          exit 1
        fi
        BUILDDIR="$VAL"
        shift ;;
      --instdir)
        if [ ! -d "$VAL" ] && ! mkdir -p "$VAL"; then
          echo "ERROR: Install dir '$VAL' doesn't exist and can't be created"
          echo "Syntax: $SYN_INSTDIR"
          exit 1
        fi
        INSTDIR="$VAL"
        shift ;;
      --buildjobs)
        if [ "$VAL" -gt 0 ]; then
          BUILDJOBS="$VAL"
        else
          echo "ERROR: Build jobs '$VAL' not valid"
          echo "Syntax: $SYN_BUILDJOBS"
          exit 1
        fi
        shift ;;
      --linkjobs)
        if [ "$VAL" -gt 0 ]; then
          LINKJOBS="$VAL"
        else
          echo "ERROR: Link jobs '$VAL' not valid"
          echo "Syntax: $SYN_LINKJOBS"
          exit 1
        fi
        shift ;;
      *)
        echo "ERROR: Invalid argument '$1'"
        echo "Syntax: $SYNTAX"
        exit 1
        ;;
    esac
done

# Validate options
if [ "$GEN" = "" ] || [ "$BUILD" = "" ]; then
  echo "ERROR: Missing Generator / Builder"
  echo "$SYNTAX"
  exit 1
fi
if [ "$BUILDTYPE" = "" ] || [ "$ASSERTS" = "" ]; then
  echo "ERROR: Missing build type / asserts option"
  echo "$SYNTAX"
  exit 1
fi
if [ "$BUILDDIR" = "" ] && [ "$SRCDIR" != "" ]; then
  BUILDDIR="$SRCDIR/../build"
  if [ ! -d "BUILDDIR" ] && ! mkdir -p "$BUILDDIR"; then
    echo "ERROR: Forced build dir '$BUILDDIR' can't be created"
    echo "Syntax: $SYN_BUILDDIR"
    exit 1
  fi
fi
if [ "$INSTDIR" = "" ] && [ "$BUILDDIR" != "" ]; then
  INSTDIR="$BUILDDIR/../install"
  if [ ! -d "INSTDIR" ] && ! mkdir -p "$INSTDIR"; then
    echo "ERROR: Forced install dir '$INSTDIR' can't be created"
    echo "Syntax: $SYN_INSTDIR"
    exit 1
  fi
fi
if [ "$SRCDIR" = "" ] || [ "$BUILDDIR" = "" ] || [ "$INSTDIR" = "" ]; then
  echo "ERROR: Missing source / build / install directories"
  echo "$SYNTAX"
  exit 1
fi
if [ "$LINKJOBS" = "" ] || [ "$BUILDJOBS" = "" ]; then
  echo "ERROR: Missing number of build / link jobs"
  echo "$SYNTAX"
  exit 1
fi

# Dump
echo "GEN = $GEN"
echo "BUILD = $BUILD"
echo "BUILDTYPE = $BUILDTYPE"
echo "ASSERTS = $ASSERTS"
echo "SRCDIR = $SRCDIR"
echo "BUILDDIR = $BUILDDIR"
echo "INSTDIR = $INSTDIR"
echo "BUILDJOBS = $BUILDJOBS"
echo "LINKJOBS = $LINKJOBS"

# Logs
LOGBASE="$SRCDIR/../tcwg-llvm"
LOGEXT="txt"

# Checkout
git clone http://llvm.org/git/llvm.git "$SRCDIR" |& tee "$LOGBASE-clone.$LOGEXT"
git clone http://llvm.org/git/clang.git "$SRCDIR/tools/clang" |& tee -a "$LOGBASE-clone.$LOGEXT"

# CMake
OPTIONS="-DLLVM_BUILD_TESTS=True "
OPTIONS+="-DCMAKE_BUILD_TYPE='$BUILDTYPE' "
OPTIONS+="-DLLVM_ENABLE_ASSERTIONS='$ASSERTS' "
OPTIONS+="-DLLVM_PARALLEL_COMPILE_JOBS='$BUILDJOBS' "
OPTIONS+="-DLLVM_PARALLEL_LINK_JOBS='$LINKJOBS' "
cd "$BUILDDIR" && cmake -G "$GEN" "$SRCDIR" "$OPTIONS" -DLLVM_LIT_ARGS="-sv -j$CPUS" |& tee "$LOGBASE-cmake.$LOGEXT"

# Build + check
cd "$BUILDDIR" && $BUILD "-j$CPUS" |& tee "$LOGBASE-build.$LOGEXT"
cd "$BUILDDIR" && $BUILD "-j$CPUS" check-all |& tee "$LOGBASE-check.$LOGEXT"
cd "$BUILDDIR" && $BUILD "-j$CPUS" DESTDIR="$INSTDIR" install |& tee "$LOGBASE-install.$LOGEXT"