1#!/bin/bash
2# Copyright (c) 2018 Google LLC.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16# Linux Build Script.
17
18# Fail on any error.
19set -e
20# Display commands being run.
21set -x
22
23# This is required to run any git command in the docker since owner will
24# have changed between the clone environment, and the docker container.
25# Marking the root of the repo as safe for ownership changes.
26git config --global --add safe.directory $ROOT_DIR
27
28. /bin/using.sh # Declare the bash `using` function for configuring toolchains.
29
30if [ $COMPILER = "clang" ]; then
31  using clang-10.0.0
32elif [ $COMPILER = "gcc" ]; then
33  using gcc-9
34fi
35
36cd $ROOT_DIR
37
38function clean_dir() {
39  dir=$1
40  if [[ -d "$dir" ]]; then
41    rm -fr "$dir"
42  fi
43  mkdir "$dir"
44}
45
46if [ $TOOL != "cmake-smoketest" ]; then
47  # Get source for dependencies, as specified in the DEPS file
48  /usr/bin/python3 utils/git-sync-deps --treeless
49fi
50
51if [ $TOOL = "cmake" ]; then
52  using cmake-3.17.2
53  using ninja-1.10.0
54
55  # Possible configurations are:
56  # ASAN, UBSAN, COVERAGE, RELEASE, DEBUG, DEBUG_EXCEPTION, RELEASE_MINGW
57  BUILD_TYPE="Debug"
58  if [ $CONFIG = "RELEASE" ] || [ $CONFIG = "RELEASE_MINGW" ]; then
59    BUILD_TYPE="RelWithDebInfo"
60  fi
61
62  SKIP_TESTS="False"
63  ADDITIONAL_CMAKE_FLAGS=""
64  if [ $CONFIG = "ASAN" ]; then
65    ADDITIONAL_CMAKE_FLAGS="-DSPIRV_USE_SANITIZER=address,bounds,null"
66    [ $COMPILER = "clang" ] || { echo "$CONFIG requires clang"; exit 1; }
67  elif [ $CONFIG = "UBSAN" ]; then
68    # UBSan requires RTTI, and by default UBSan does not exit when errors are
69    # encountered - additional compiler options are required to force this.
70    # The -DSPIRV_USE_SANITIZER=undefined option instructs SPIR-V Tools to be
71    # built with UBSan enabled.
72    ADDITIONAL_CMAKE_FLAGS="-DSPIRV_USE_SANITIZER=undefined -DENABLE_RTTI=ON -DCMAKE_C_FLAGS=-fno-sanitize-recover=all -DCMAKE_CXX_FLAGS=-fno-sanitize-recover=all"
73    [ $COMPILER = "clang" ] || { echo "$CONFIG requires clang"; exit 1; }
74  elif [ $CONFIG = "COVERAGE" ]; then
75    ADDITIONAL_CMAKE_FLAGS="-DENABLE_CODE_COVERAGE=ON"
76    SKIP_TESTS="True"
77  elif [ $CONFIG = "DEBUG_EXCEPTION" ]; then
78    ADDITIONAL_CMAKE_FLAGS="-DDISABLE_EXCEPTIONS=ON -DDISABLE_RTTI=ON"
79  elif [ $CONFIG = "RELEASE_MINGW" ]; then
80    ADDITIONAL_CMAKE_FLAGS="-Dgtest_disable_pthreads=ON -DCMAKE_TOOLCHAIN_FILE=$SRC/cmake/linux-mingw-toolchain.cmake"
81    SKIP_TESTS="True"
82  fi
83
84  if [ $COMPILER = "clang" ]; then
85    ADDITIONAL_CMAKE_FLAGS="$ADDITIONAL_CMAKE_FLAGS -DSPIRV_BUILD_LIBFUZZER_TARGETS=ON"
86  fi
87
88  clean_dir "$ROOT_DIR/build"
89  cd "$ROOT_DIR/build"
90
91  # Invoke the build.
92  BUILD_SHA=${KOKORO_GITHUB_COMMIT:-$KOKORO_GITHUB_PULL_REQUEST_COMMIT}
93  echo $(date): Starting build...
94  cmake -DPYTHON_EXECUTABLE:FILEPATH=/usr/bin/python3 -GNinja -DCMAKE_INSTALL_PREFIX=$KOKORO_ARTIFACTS_DIR/install -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DRE2_BUILD_TESTING=OFF -DSPIRV_BUILD_FUZZER=ON $ADDITIONAL_CMAKE_FLAGS ..
95
96  echo $(date): Build everything...
97  ninja
98  echo $(date): Build completed.
99
100  if [ $CONFIG = "COVERAGE" ]; then
101    echo $(date): Check coverage...
102    ninja report-coverage
103    echo $(date): Check coverage completed.
104  fi
105
106  echo $(date): Starting ctest...
107  if [ $SKIP_TESTS = "False" ]; then
108    ctest -j4 --output-on-failure --timeout 300
109  fi
110  echo $(date): ctest completed.
111
112  # Package the build.
113  ninja install
114  cd $KOKORO_ARTIFACTS_DIR
115  tar czf install.tgz install
116elif [ $TOOL = "cmake-smoketest" ]; then
117  using cmake-3.17.2
118  using ninja-1.10.0
119
120  # Get shaderc.
121  SHADERC_DIR=/tmp/shaderc
122  clean_dir "$SHADERC_DIR"
123  cd $SHADERC_DIR
124  git clone https://github.com/google/shaderc.git .
125  cd $SHADERC_DIR/third_party
126
127  # Get shaderc dependencies. Link the appropriate SPIRV-Tools.
128  git clone https://github.com/google/googletest.git
129  git clone https://github.com/KhronosGroup/glslang.git
130  ln -s $ROOT_DIR spirv-tools
131  git clone https://github.com/KhronosGroup/SPIRV-Headers.git spirv-headers
132  git clone https://github.com/google/re2
133  git clone https://github.com/google/effcee
134  git clone https://github.com/abseil/abseil-cpp abseil_cpp
135
136  cd $SHADERC_DIR
137  mkdir build
138  cd $SHADERC_DIR/build
139
140  # Invoke the build.
141  echo $(date): Starting build...
142  cmake -GNinja -DRE2_BUILD_TESTING=OFF -DCMAKE_BUILD_TYPE="Release" ..
143
144  echo $(date): Build glslang...
145  ninja glslang-standalone
146
147  echo $(date): Build everything...
148  ninja
149  echo $(date): Build completed.
150
151  echo $(date): Check Shaderc for copyright notices...
152  ninja check-copyright
153
154  echo $(date): Starting ctest...
155  ctest --output-on-failure -j4
156  echo $(date): ctest completed.
157elif [ $TOOL = "cmake-android-ndk" ]; then
158  using cmake-3.17.2
159  using ndk-r25c
160  using ninja-1.10.0
161
162  clean_dir "$ROOT_DIR/build"
163  cd "$ROOT_DIR/build"
164
165  echo $(date): Starting build...
166  cmake -DCMAKE_BUILD_TYPE=Release \
167        -DANDROID_NATIVE_API_LEVEL=android-24 \
168        -DANDROID_ABI="armeabi-v7a with NEON" \
169        -DSPIRV_SKIP_TESTS=ON \
170        -DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake" \
171        -GNinja \
172        -DANDROID_NDK=$ANDROID_NDK \
173        ..
174
175  echo $(date): Build everything...
176  ninja
177  echo $(date): Build completed.
178elif [ $TOOL = "android-ndk-build" ]; then
179  using ndk-r25c
180
181  clean_dir "$ROOT_DIR/build"
182  cd "$ROOT_DIR/build"
183
184  echo $(date): Starting ndk-build ...
185  $ANDROID_NDK_HOME/ndk-build \
186    -C $ROOT_DIR/android_test \
187    NDK_PROJECT_PATH=. \
188    NDK_LIBS_OUT=./libs \
189    NDK_APP_OUT=./app \
190    -j4
191
192  echo $(date): ndk-build completed.
193elif [ $TOOL = "bazel" ]; then
194  using bazel-5.0.0
195
196  echo $(date): Build everything...
197  bazel build --cxxopt=-std=c++17 :all
198  echo $(date): Build completed.
199
200  echo $(date): Starting bazel test...
201  bazel test --cxxopt=-std=c++17 :all
202  echo $(date): Bazel test completed.
203fi
204