aboutsummaryrefslogtreecommitdiff
path: root/helpers/llvm-build
blob: 773c16a079ddea6305aa79f3393293218c615324 (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
#!/usr/bin/env bash

# This script helps you build LLVM.

. llvm-common

progdir=$(dirname $0)
llvmtool=$progdir/../scripts/llvm.py

safe_run verify_env
source_dir=$LLVM_SRC

## CMD line options and defaults
CPUS=$(grep -c proc /proc/cpuinfo)
PARALLEL=-j$CPUS
build_dir=$LLVM_BLD
install_dir=$LLVM_INSTALL
build_type=Release
shared=
targets=
prog=`basename $0`
syntax="Syntax: $prog [-jN] [targets]"
minimal_targets="--cmake-def LLVM_TARGETS_TO_BUILD='ARM;X86;AArch64'"
link_jobs=

if [ "$1" = "-h" ]; then
  echo $syntax
  exit 0
fi

## Choose between make and ninja
generator="Unix Makefiles"
if which ninja > /dev/null 2>&1; then
  generator="Ninja"
fi

## Debug mode, make it lighter
if [ "$LLVM_DEBUG" = true ]; then
  build_type=Debug
  shared="--cmake-def BUILD_SHARED_LIBS=True"
  targets=$minimal_targets
fi

# Building on ARM?
if grep -q "ARM.* Processor" /proc/cpuinfo; then
  targets=$minimal_targets
  if [ "$generator" = "Ninja" ]; then
    link=$(free -g | awk '/Mem/ {print $2}')
    if [ "$link" -gt "$CPUS" ]; then
      link=$CPUS
    else
      link=$((link+1))
    fi
    link_jobs="--cmake-def LLVM_PARALLEL_LINK_JOBS=$link"
  fi
fi

## Make sure sure build dir is there
if [ ! -d $build_dir ]; then
  safe_run mkdir -p $build_dir
fi
cd $build_dir

## Allow the user to override the number of CPUs used with -jN
if [[ $1 =~ -j[0-9]+ ]]; then
  PARALLEL=$1
  shift
fi

## Re-run CMake if files are damaged / not there
if [ ! -f build.ninja ] && [ ! -f Makefile ]; then
  echo " + Configuring Build"
  safe_run python3 $llvmtool configure \
            --source-dir $source_dir --build-dir $build_dir \
            --cmake-generator "$generator" \
            --cmake-def CMAKE_BUILD_TYPE=$build_type \
            --cmake-def LLVM_BUILD_TESTS=True \
            --cmake-def LLVM_ENABLE_ASSERTIONS=True \
            --cmake-def PYTHON_EXECUTABLE=/usr/bin/python2 \
            --cmake-def CMAKE_INSTALL_PREFIX=$install_dir \
            --cmake-def LLVM_LIT_ARGS="-sv $PARALLEL" \
            $LLVM_CMAKE_FLAGS $targets $shared $link_jobs
fi

## Build
if [ "$1" == "" ]; then
  echo " + Building LLVM"
  safe_run python3 $llvmtool build --build-dir $build_dir --build-flag=$PARALLEL
else
  for target in "$@"; do
    echo " + Running $target"
    safe_run python3 $llvmtool build --build-dir $build_dir \
                                     --build-flag=$PARALLEL --build-flag $target
  done
fi