about summary refs log tree commit diff homepage
path: root/scripts/build/p-llvm.inc
blob: 6ab1853165b6638788fb9f8369f47184a5ab6056 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
validate_build_config_llvm() {
    # Check variables that are needed for building 
    [[ -n "${LLVM_SRC_BASE}" ]] || { echo "LLVM_SRC_BASE not set"; exit 1; }
    [[ -n "${LLVM_VERSION_SHORT}" ]] || { echo "LLVM_VERSION_SHORT not set"; exit 1; }
}

setup_build_variables_llvm() {
    LLVM_SUFFIX=""

    local BuildMode=""
    local enable_optimized=$(to_bool "${ENABLE_OPTIMIZED}")
    local enable_debug=$(to_bool "${ENABLE_DEBUG}")
    local disable_assertions=$(to_bool "${DISABLE_ASSERTIONS}")
    local requires_rtti=$(to_bool "${REQUIRES_RTTI}")

    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_SRC_BASE="${BASE}/llvm-${LLVM_VERSION_SHORT}"
    LLVM_BUILD="${LLVM_SRC_BASE}-build${LLVM_SUFFIX}"
    LLVM_INSTALL="${LLVM_SRC_BASE}-install${LLVM_SUFFIX}"
    LLVM_BIN="${LLVM_INSTALL}/bin"
    LLVM_BUILD_BIN="${LLVM_BUILD}/bin"

    LLVM_CONFIG="${LLVM_BIN}/llvm-config"
    BITCODE_CC="${LLVM_BIN}/clang"
    BITCODE_CXX="${LLVM_BIN}/clang++"


    for sanitizer in "${SANITIZERS[@]}"; do
      [[ -z "${sanitizer}" ]] && continue
      # Undefined Behaviour Sanitizer
      if [ "${sanitizer}" == "memory" ]; then
        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[@]}")

        # Use the uninstrumented compiler
        BITCODE_CC="${SANITIZER_C_COMPILER}"
        BITCODE_CXX="${SANITIZER_CXX_COMPILER}"
        # But point to the instrumented llvm-config
        LLVM_CONFIG="${LLVM_BIN}/llvm-config"
        continue
      fi
      # Setup default sanitizer arguments
      SANITIZER_C_COMPILER="${LLVM_BIN}/clang"
      SANITIZER_CXX_COMPILER="${LLVM_BIN}/clang++"
    done
}

download_llvm() {
  # Skip step if we already checked out code
  [[ -f "${LLVM_SRC_BASE}/.src_checked_out" ]] && return 0

  source "${DIR}/common-functions"
  local LLVM_VERSION_MAJOR="${LLVM_VERSION/.*/}"

  branch_name="release/${LLVM_VERSION_MAJOR}.x"
  if [[ "${LLVM_VERSION_MAJOR}" -le 3 ]]; then
    branch_name="release/${LLVM_VERSION}.x"
  fi
  git_clone_or_update "https://github.com/llvm/llvm-project.git" "${LLVM_SRC_BASE}" "${branch_name}" || exit 1
  if [[ "${LLVM_VERSION_MAJOR}" -lt 4 ]]; then
    # Use symlinks for older versions whose build systems do not support the monorepo directory layout
    ln -s "${LLVM_SRC_BASE}/clang" "${LLVM_SRC_BASE}/llvm/tools/"
    ln -s "${LLVM_SRC_BASE}/compiler-rt" "${LLVM_SRC_BASE}/llvm/projects/"
    ln -s "${LLVM_SRC_BASE}/libcxx" "${LLVM_SRC_BASE}/llvm/projects/"
    ln -s "${LLVM_SRC_BASE}/libcxxabi" "${LLVM_SRC_BASE}/llvm/projects/"
  fi

  # Apply existing patches if needed
  if [ -f "${DIR}/patches/llvm${LLVM_VERSION_SHORT}.patch" ]; then
     cd "${LLVM_SRC_BASE}" || (echo "Directory does not exist"; exit 1)
     patch -p1 --follow-symlinks -i "${DIR}/patches/llvm${LLVM_VERSION_SHORT}.patch" || return 1
  fi

  touch "${LLVM_SRC_BASE}/.src_checked_out" 
}

build_llvm() {
    local enable_optimized=$(to_bool "${ENABLE_OPTIMIZED}")
    local enable_debug=$(to_bool "${ENABLE_DEBUG}")
    local disable_assertions=$(to_bool "${DISABLE_ASSERTIONS}")
    local requires_rtti=$(to_bool "${REQUIRES_RTTI}")

    # For memory sanitizer, we have a multi-stage build process
    if [[ "${SANITIZER_BUILD}" == "memory" ]]; then
       # Build uninstrumented compiler
       mkdir -p "${SANITIZER_LLVM_UNINSTRUMENTED}"
       cd "${SANITIZER_LLVM_UNINSTRUMENTED}"
       cmake -GNinja -DCMAKE_BUILD_TYPE=Release \
        "-DLLVM_ENABLE_PROJECTS=clang;compiler-rt;libcxx;libcxxabi" \
        "${LLVM_SRC_BASE}/llvm"
       ninja

       # Build instrumented libc/libc++
       mkdir -p "${SANITIZER_LLVM_LIBCXX}"
       cd "${SANITIZER_LLVM_LIBCXX}"
       cmake -GNinja -DCMAKE_BUILD_TYPE=RelWithDebInfo \
        "-DLLVM_ENABLE_PROJECTS=clang;compiler-rt;libcxx;libcxxabi" \
        -DLLVM_USE_SANITIZER=MemoryWithOrigins \
        "${SANITIZER_CMAKE_C_COMPILER[@]}" \
        "${SANITIZER_CMAKE_CXX_COMPILER[@]}" \
        "${LLVM_SRC_BASE}/llvm"
       ninja cxx cxxabi

       # Build instrumented clang
       mkdir -p "${LLVM_BUILD}"
       cd "${LLVM_BUILD}"
       C_F="${SANITIZER_C_FLAGS[*]}"
       CXX_F="${SANITIZER_CXX_FLAGS[*]}"
       LD_F="${SANITIZER_LD_FLAGS[*]}"
       cmake -GNinja \
          "${SANITIZER_CMAKE_C_COMPILER[@]}" \
          "${SANITIZER_CMAKE_CXX_COMPILER[@]}" \
          -DCMAKE_C_FLAGS="$C_F" \
          -DCMAKE_CXX_FLAGS="${CXX_F}" \
          -DCMAKE_BUILD_TYPE=RelWithDebInfo \
          "-DLLVM_ENABLE_PROJECTS=clang;compiler-rt;libcxx;libcxxabi" \
          -DLLVM_ENABLE_ASSERTIONS=On \
          -DLLVM_USE_SANITIZER=MemoryWithOrigins \
          -DLLVM_ENABLE_LIBCXX=ON \
          -DCMAKE_EXE_LINKER_FLAGS="${LD_F}" \
          -DCMAKE_INSTALL_PREFIX="${LLVM_INSTALL}" \
          "${LLVM_SRC_BASE}/llvm"
      # Build clang as a dependency and install all needed packages
      ninja clang
      return 0
    fi

  # Configure; build; and install
  mkdir -p "${LLVM_BUILD}"
  cd "${LLVM_BUILD}"

  # Skip building if already finished
  [[ -e "${LLVM_BUILD}/.build_finished" ]] && return 0


  # Configure LLVM
  CONFIG=(
     "-DCMAKE_INSTALL_PREFIX=${LLVM_INSTALL}"
     "-DLLVM_BUILD_LLVM_DYLIB=TRUE"
     "-DLLVM_ENABLE_PROJECTS=clang;compiler-rt;libcxx;libcxxabi"
  )
  # cmake build
  if [[ "${enable_optimized}" == "1" && "${enable_debug}" != "1" ]]; then
    CONFIG+=("-DCMAKE_BUILD_TYPE=Release")
  fi
  if [[ "${enable_optimized}" == "1" && "${enable_debug}" == "1" ]]; then
    CONFIG+=("-DCMAKE_BUILD_TYPE=RelWithDebInfo")
  fi
  if [[ "${enable_optimized}" != "1" && "${enable_debug}" == "1" ]]; then
    CONFIG+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ "${disable_assertions}" == "1" ]]; then
    CONFIG+=("-DLLVM_ENABLE_ASSERTIONS=Off")
  else
    CONFIG+=("-DLLVM_ENABLE_ASSERTIONS=On")
  fi

  if [[ -n "${LLVM_CFLAGS:-}" ]]; then
    CONFIG+=("-DCMAKE_C_FLAGS=\"$LLVM_CFLAGS\"")
  fi

  if [[ -n "${LLVM_CXXFLAGS:-}" ]]; then
    CONFIG+=("-DCMAKE_CXX_FLAGS=\"$LLVM_CXXFLAGS\"")
  fi

  if [[ "${requires_rtti}" -eq 1 ]]; then
    CONFIG+=("-DLLVM_ENABLE_RTTI=TRUE")
  fi

  # Remove unneeded targets
  CONFIG+=(
     "-DLLVM_INCLUDE_EXAMPLES=OFF"
#     "-DLLVM_INCLUDE_TESTS=OFF"
#     "-DCLANG_INCLUDE_TESTS=OFF"
     "-DLLVM_INCLUDE_BENCHMARKS=OFF"
     "-DBUILD_SHARED_LIBS=ON"
  )

  local variables=("")

  if [[ -n "${CC:-}" ]]; then
    variables+=("CC=${CC}")
  fi

  if [[ -n "${CXX:-}" ]]; then
    variables+=("CXX=${CXX}")
  fi

  if [[ -n "${LDFLAGS:-}" ]]; then
    variables+=("LDFLAGS=${LLVM_LDFLAGS}")
  fi

  if [[ -n "${variables[*]}" ]]; then
    "${variables[*]}" cmake "${CONFIG[@]}" "${LLVM_SRC_BASE}/llvm"
  else
    cmake "${CONFIG[@]}" "${LLVM_SRC_BASE}/llvm"
  fi

  # Linking LLVM can require a lot of memory.
  # First try multicore - if that doesn't work, try single core
  (make "-j$(nproc)") || (make) || return 1
  touch "${LLVM_BUILD}/.build_finished"
}

install_llvm() {
  if [[ "${SANITIZER_BUILD}" != "memory" ]]; then
    cd "${LLVM_BUILD}"
    make "-j$(nproc)" install
    cp "${LLVM_BUILD_BIN}/FileCheck" "${LLVM_INSTALL}/bin/"
    cp "${LLVM_BUILD_BIN}/not" "${LLVM_INSTALL}/bin/"
  
    # Remove debug information from binaries
    strip "${LLVM_INSTALL}/bin/"* || /bin/true
    strip "${LLVM_INSTALL}/lib/libclang"* || /bin/true
    strip "${LLVM_INSTALL}/lib/libLTO"* || /bin/true
  else
    # Handle memory sanitizer install
    LLVM_PACKAGES=( \
      install-clang install-llvm-config install-llvm-objdump
      install-llvm-link install-llvm-ar install-llvm-nm install-llvm-dis
      install-clang-headers install-llvm-as
      install-llvm-symbolizer install-LLVMSupport install-lli not FileCheck )
  
    if [[ ${LLVM_VERSION_SHORT} -eq 38 ]]; then
      LLVM_PACKAGES=("${LLVM_PACKAGES[@]}" installhdrs)
    else
      LLVM_PACKAGES=("${LLVM_PACKAGES[@]}" install-llvm-headers)
    fi
  
    ninja "${LLVM_PACKAGES[@]}"

    for i in $(ninja -t targets |grep install-LLVM | cut -d : -f 1); do ninja "$i"; done
    
    cp "${LLVM_BUILD}/bin/FileCheck" "${LLVM_INSTALL}/bin/"
    cp "${LLVM_BUILD}/bin/not" "${LLVM_INSTALL}/bin/"
  fi

  touch "${LLVM_INSTALL}/.install_finished"
}

# Check if the binary artifact is installed
is_installed_llvm() {
  (
    setup_build_variables_llvm
    # Check if the specific llvm-config exists
    [[ -f "${LLVM_BUILD_BIN}/llvm-config" ]]
    [[ -f "${LLVM_INSTALL}/.install_finished" ]]
  ) || return 1
}

# Returns a unique id for this configuration
get_docker_config_id_llvm() {
  (
    setup_build_variables_llvm
    echo "${LLVM_VERSION_SHORT}${LLVM_SUFFIX}"
  )
}

setup_artifact_variables_llvm() {
  setup_build_variables_llvm
}

get_build_artifacts_llvm() {
  (
    setup_build_variables_llvm
    echo "${LLVM_INSTALL}"
    echo "${LLVM_SRC_BASE}"
    [[ "${sanitizer}" == "memory" ]] && echo "${SANITIZER_LLVM_UNINSTRUMENTED}"
    [[ "${sanitizer}" == "memory" ]] && echo "${SANITIZER_LLVM_LIBCXX}"
  )
}