about summary refs log tree commit diff homepage
path: root/scripts/build/common-functions
blob: 45c94fefb8c411b66387b4b935925a1be4f45d53 (plain) (blame)
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
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
# Defines common function and variables used by the scripts

function git_clone_or_update() {
  local url="$1"
  local destination="$2"
  local branch="$3"

  if [[ ! -e "${destination}/.git" ]]; then
    git clone $([ "${branch}x" != "x" ] && echo "--depth 1 -b ${branch}" || echo "") "$url" "$destination"
  else
    cd "$destination"
    git pull
    if [[ ! -z "$branch" ]]; then
      git checkout "${branch}"
    fi
  fi
}

function to_upper() {
  echo "$1" | tr '[:lower:]' '[:upper:]'
}

function to_lower() {
  echo "$1" | tr '[:upper:]' '[:lower:]'
}

# This file is meant to be included by shell scripts
# that need to do a sanitized build.

# Users of this script can use this variable
# to detect if a Sanitized build was enabled.
IS_SANITIZED_BUILD=0

# Remove ":" separator
sanitizers=(${SANITIZER_BUILD//:/ })
# Sort sanitizers
IFS=$'\n' sorted_sanitizers=($(sort <<<"${sanitizers[*]}"))
unset IFS

# Set variables used by clients.
## Build variables
SANITIZER_CXX_FLAGS=""
SANITIZER_C_FLAGS=""
SANITIZER_LD_FLAGS=""
# Docker_suffix
SANITIZER_SUFFIX=""

#
for sanitizer in ${sorted_sanitizers[@]}; do
  # AddressSanitizer
  if [ "X${sanitizer}" == "Xaddress" ]; then
    echo "Using ASan"
    flags=" -fsanitize=address -fno-omit-frame-pointer -g"
    SANITIZER_CXX_FLAGS+=" ${flags}"
    SANITIZER_C_FLAGS+=" ${flags}"
    SANITIZER_LD_FLAGS+=" ${flags}"
    SANITIZER_SUFFIX+="_asan"
    IS_SANITIZED_BUILD=1
    continue
  fi

  # Undefined Behaviour Sanitizer
  if [ "X${sanitizer}" == "Xundefined" ]; then
    echo "Using UBSan"
    flags="-fsanitize=undefined -fno-omit-frame-pointer -g"
    SANITIZER_CXX_FLAGS+=" ${flags}"
    SANITIZER_C_FLAGS+=" ${flags}"
    SANITIZER_LD_FLAGS+=" ${flags} -lubsan -fuse-ld=gold"
    SANITIZER_SUFFIX+="_ubsan"
    IS_SANITIZED_BUILD=1
    continue
  fi

  # Undefined Behaviour Sanitizer
  if [ "X${sanitizer}" == "Xmemory" ]; then
    echo "Using Memory Sanitizer"
    if [[ "X${PACKAGED}" == "X1" ]]; then
      echo "simultanously memory sanitizer and packaged is not supported "
      exit 1
    fi

    SANITIZER_SUFFIX+="_memsan"
    SANITIZER_LLVM_UNINSTRUMENTED="${LLVM_BUILD}_uninstrumented"
    SANITIZER_LLVM_LIBCXX="${LLVM_BUILD}_libcxx"

    SANITIZER_C_COMPILER="${SANITIZER_LLVM_UNINSTRUMENTED}/bin/clang"
    SANITIZER_CXX_COMPILER="${SANITIZER_LLVM_UNINSTRUMENTED}/bin/clang++"
    SANITIZER_CMAKE_C_COMPILER="-DCMAKE_C_COMPILER=${SANITIZER_C_COMPILER}"
    SANITIZER_CMAKE_CXX_COMPILER="-DCMAKE_CXX_COMPILER=${SANITIZER_CXX_COMPILER}"

    MSAN_LINK_FLAGS="-lc++abi -Wl,--rpath=${SANITIZER_LLVM_LIBCXX}/lib -L${SANITIZER_LLVM_LIBCXX}/lib";
    MSAN_FLAGS="$MSAN_LINK_FLAGS -nostdinc++ \
       -isystem ${SANITIZER_LLVM_LIBCXX}/include \
       -isystem ${SANITIZER_LLVM_LIBCXX}/include/c++/v1  \
       -fsanitize=memory \
       -fsanitize-memory-track-origins \
       -w -fno-omit-frame-pointer -g";

    SANITIZER_CXX_FLAGS+="${MSAN_FLAGS} -stdlib=libc++"
    SANITIZER_C_FLAGS+="${MSAN_FLAGS}"
    SANITIZER_LD_FLAGS+="${MSAN_LINK_FLAGS}"

    IS_SANITIZED_BUILD=1
    continue
  fi

  echo "Unknown sanitizer: $sanitizer"
  exit 1
done

# This file is meant to be included by shell scripts
# that need to retrieve llvm-specifi variables

LLVM_VERSION_SHORT="${LLVM_VERSION/./}"
LLVM_SUFFIX=""

BuildMode=''

# Build from source
if [[ "${PACKAGED}x" == "1x" ]]; then
  LLVM_SUFFIX+="_packaged"
  set +e
  llvm_config_path=$(which llvm-config-${LLVM_VERSION})
  if [[ -e "${llvm_config_path}" ]]; then
    LLVM_BIN="$(llvm-config-${LLVM_VERSION} --bindir)"
  fi
  set -e
else
  if [[ "${ENABLE_OPTIMIZED}" == "1" ]]; then
    LLVM_SUFFIX+="_O"
    BuildMode="Release"
  else
    LLVM_SUFFIX+="_NO"
  fi

  if [[ ("${ENABLE_DEBUG}" == "1") || ("${ENABLE_OPTIMIZED}" != "1") ]]; then
    LLVM_SUFFIX+="_D"
    [ -z "$BuildMode" ] && BuildMode="Debug" || BuildMode="$BuildMode+Debug"
  else
    LLVM_SUFFIX+="_ND"
  fi

  if [[ "${DISABLE_ASSERTIONS}" == "1" || -z "${DISABLE_ASSERTIONS}" ]]; then
    LLVM_SUFFIX+="_NA"
  else
    LLVM_SUFFIX+="_A"
    [ -z "$BuildMode" ] && BuildMode="Asserts" || BuildMode="$BuildMode+Asserts"
  fi

  if [[ "${REQUIRES_RTTI}" == "1" ]]; then
    LLVM_SUFFIX+="_RTTI"
  fi

  if [[ "${SANITIZER_SUFFIX}x" != "x" ]]; then
    LLVM_SUFFIX+="${SANITIZER_SUFFIX}"
  fi

  LLVM_BASE="${BASE}/llvm-${LLVM_VERSION_SHORT}"
  LLVM_BUILD="${LLVM_BASE}-build${LLVM_SUFFIX}"
  LLVM_INSTALL="${LLVM_BASE}-install${LLVM_SUFFIX}"
  LLVM_BIN="${LLVM_INSTALL}/bin"
  if [[ ${LLVM_VERSION_SHORT} -le 37 ]]; then
    LLVM_BUILDMODE="$BuildMode"
    LLVM_BUILD_BIN="${LLVM_BUILD}/${LLVM_BUILDMODE}/bin"
  else
    LLVM_BUILD_BIN="${LLVM_BUILD}/bin"
  fi
fi

# Generate the suffix used for the solver combination
# Every solver needs to have a version explicitly specified
SOLVER_SUFFIX=""

# Remove ":" separator
solvers=(${SOLVERS//:/ })

# Sort solvers
IFS=$'\n' sorted_solvers=($(sort <<<"${solvers[*]}"))
unset IFS

for solver in ${sorted_solvers[@]}; do
  # Make sure version a solver specific version variable is used
  solver_var="$(to_upper "$solver")_VERSION"
  SOLVER_VERSION="${!solver_var}"
  if [[ -z "${SOLVER_VERSION}" ]] ; then
  	echo "Solver version for $solver not defined"
  	exit 1
  fi
  # Add the solver to the suffix and lower case it
  SOLVER_SUFFIX+="_$(to_lower "$solver")$(to_lower "$SOLVER_VERSION")"
  echo $SOLVER_SUFFIX
  if [[ "$(to_lower "$solver")" == "metasmt" ]]; then
    if [[ "${REQUIRES_RTTI}x" != "1x" ]]; then
      echo "METASMT requires an RTTI build"
      exit 1
    fi
    if [[ -z "${METASMT_DEFAULT}" ]]; then
      echo "METASMT_DEFAULT not defined"
      exit 1
    fi
    if [[ -z "${METASMT_BOOST_VERSION}" ]] ; then
      echo "METASMT_BOOST_VERSION not defined - use default"
      SOLVER_SUFFIX+="_$(to_lower "$METASMT_DEFAULT")_boost-dev"
    else
      SOLVER_SUFFIX+="_$(to_lower "$METASMT_DEFAULT")_$(to_lower "$METASMT_BOOST_VERSION")"
    fi
  fi
done

# This file is meant to be included by shell scripts that handle dependencies by
# KLEE beside LLVM and solvers
#
# Generate the suffix used for direct dependencies of KLEE like uclibc
DEPS_SUFFIX=""
if [[ "${KLEE_UCLIBC}" != "0" ]]; then
  DEPS_SUFFIX+="_${KLEE_UCLIBC}"
fi