xref: /aosp_15_r20/external/swiftshader/third_party/marl/kokoro/ubuntu/docker.sh (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1#!/bin/bash
2
3# Copyright 2020 The Marl Authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     https://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17set -e # Fail on any error.
18
19function show_cmds { set -x; }
20function hide_cmds { { set +x; } 2>/dev/null; }
21function status {
22    echo ""
23    echo "*****************************************************************"
24    echo "* $@"
25    echo "*****************************************************************"
26    echo ""
27}
28
29. /bin/using.sh # Declare the bash `using` function for configuring toolchains.
30
31status "Setting up environment"
32using gcc-9 # Always update gcc so we get a newer standard library.
33
34if [ "$BUILD_SYSTEM" == "cmake" ]; then
35    using cmake-3.17.2
36
37    SRC_DIR=$(pwd)
38    BUILD_DIR=/tmp/marl-build
39    INSTALL_DIR=${BUILD_DIR}/install
40
41    COMMON_CMAKE_FLAGS=""
42    COMMON_CMAKE_FLAGS+=" -DCMAKE_BUILD_TYPE=${BUILD_TYPE}"
43    COMMON_CMAKE_FLAGS+=" -DMARL_BUILD_EXAMPLES=1"
44    COMMON_CMAKE_FLAGS+=" -DMARL_BUILD_TESTS=1"
45    COMMON_CMAKE_FLAGS+=" -DMARL_BUILD_BENCHMARKS=1"
46    COMMON_CMAKE_FLAGS+=" -DMARL_WARNINGS_AS_ERRORS=1"
47    COMMON_CMAKE_FLAGS+=" -DMARL_DEBUG_ENABLED=1"
48    COMMON_CMAKE_FLAGS+=" -DMARL_BUILD_SHARED=${BUILD_SHARED:-0}"
49    COMMON_CMAKE_FLAGS+=" -DBENCHMARK_ENABLE_INSTALL=0"
50    COMMON_CMAKE_FLAGS+=" -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR}"
51
52    if [ "$BUILD_TOOLCHAIN" == "ndk" ]; then
53        using ndk-r21d
54        COMMON_CMAKE_FLAGS+=" -DANDROID_ABI=$BUILD_TARGET_ARCH"
55        COMMON_CMAKE_FLAGS+=" -DANDROID_NATIVE_API_LEVEL=18"
56        COMMON_CMAKE_FLAGS+=" -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake"
57    else # !ndk
58        if [ "$BUILD_TOOLCHAIN" == "clang" ]; then
59            using clang-10.0.0
60        fi
61        if [ "$BUILD_TARGET_ARCH" == "x86" ]; then
62            COMMON_CMAKE_FLAGS+=" -DCMAKE_CXX_FLAGS=-m32"
63            COMMON_CMAKE_FLAGS+=" -DCMAKE_C_FLAGS=-m32"
64            COMMON_CMAKE_FLAGS+=" -DCMAKE_ASM_FLAGS=-m32"
65        fi
66    fi
67
68    if [ "$BUILD_SANITIZER" == "asan" ]; then
69        COMMON_CMAKE_FLAGS+=" -DMARL_ASAN=1"
70    elif [ "$BUILD_SANITIZER" == "msan" ]; then
71        COMMON_CMAKE_FLAGS+=" -DMARL_MSAN=1"
72    elif [ "$BUILD_SANITIZER" == "tsan" ]; then
73        COMMON_CMAKE_FLAGS+=" -DMARL_TSAN=1"
74    fi
75
76
77    # clean
78    # Ensures BUILD_DIR is empty.
79    function clean {
80        if [ -d ${BUILD_DIR} ]; then
81            rm -fr ${BUILD_DIR}
82        fi
83        mkdir ${BUILD_DIR}
84    }
85
86    # build <description> <flags>
87    # Cleans build directory and performs a build using the provided CMake flags.
88    function build {
89        DESCRIPTION=$1
90        CMAKE_FLAGS=$2
91
92        status "Building ${DESCRIPTION}"
93        clean
94        cd ${BUILD_DIR}
95        show_cmds
96            cmake ${SRC_DIR} ${CMAKE_FLAGS} ${COMMON_CMAKE_FLAGS}
97            make --jobs=$(nproc)
98        hide_cmds
99    }
100
101    # test <description>
102    # Runs the pre-built unit tests (if not an NDK build).
103    function test {
104        DESCRIPTION=$1
105
106        status "Testing ${DESCRIPTION}"
107        cd ${BUILD_DIR}
108        show_cmds
109            if [ "$BUILD_TOOLCHAIN" != "ndk" ]; then
110                ./marl-unittests
111                ./fractal
112                ./hello_task
113                ./primes > /dev/null
114                ./tasks_in_tasks
115            fi
116        hide_cmds
117    }
118
119    # install <description>
120    # Installs the pre-built library to ${INSTALL_DIR}.
121    function install {
122        DESCRIPTION=$1
123
124        status "Installing ${DESCRIPTION}"
125        cd ${BUILD_DIR}
126        show_cmds
127            make install
128        hide_cmds
129    }
130
131    # build <description> <flags>
132    # Cleans build directory and performs a build using the provided CMake
133    # flags, then runs tests.
134    function buildAndTest {
135        DESCRIPTION=$1
136        CMAKE_FLAGS=$2
137        build "$DESCRIPTION" "$CMAKE_FLAGS"
138        test  "$DESCRIPTION"
139    }
140
141    # build <description> <flags>
142    # Cleans build directory and performs a build using the provided CMake
143    # flags, then installs the library to ${INSTALL_DIR}.
144    function buildAndInstall {
145        DESCRIPTION=$1
146        CMAKE_FLAGS=$2
147        build   "$DESCRIPTION" "$CMAKE_FLAGS"
148        install "$DESCRIPTION"
149    }
150
151    if [ -n "$RUN_TESTS" ]; then
152        buildAndTest "marl with ucontext fibers" "-DMARL_FIBERS_USE_UCONTEXT=1"
153        buildAndTest "marl with assembly fibers" "-DMARL_FIBERS_USE_UCONTEXT=0"
154    fi
155
156    buildAndInstall "marl for install" "-DMARL_INSTALL=1"
157
158    if [ -n "$BUILD_ARTIFACTS" ]; then
159        status "Copying build artifacts"
160        show_cmds
161            tar -czvf "$BUILD_ARTIFACTS/build.tar.gz" -C "$INSTALL_DIR" .
162        hide_cmds
163    fi
164
165elif [ "$BUILD_SYSTEM" == "bazel" ]; then
166    using bazel-3.1.0
167
168    show_cmds
169        bazel test //:tests --test_output=all
170        bazel run //examples:fractal
171        bazel run //examples:hello_task
172        bazel run //examples:primes > /dev/null
173        bazel run //examples:tasks_in_tasks
174    hide_cmds
175else
176    status "Unknown build system: $BUILD_SYSTEM"
177    exit 1
178fi
179
180status "Done"
181