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
19cd "${ROOT_DIR}"
20
21function show_cmds { set -x; }
22function hide_cmds { { set +x; } 2>/dev/null; }
23function status {
24    echo ""
25    echo "*****************************************************************"
26    echo "* $@"
27    echo "*****************************************************************"
28    echo ""
29}
30
31status "Fetching submodules"
32git submodule update --init
33
34status "Setting up environment"
35
36if [ "$BUILD_SYSTEM" == "cmake" ]; then
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_SANITIZER" == "asan" ]; then
53        COMMON_CMAKE_FLAGS+=" -DMARL_ASAN=1"
54    elif [ "$BUILD_SANITIZER" == "msan" ]; then
55        COMMON_CMAKE_FLAGS+=" -DMARL_MSAN=1"
56    elif [ "$BUILD_SANITIZER" == "tsan" ]; then
57        COMMON_CMAKE_FLAGS+=" -DMARL_TSAN=1"
58    fi
59
60    # clean
61    # Ensures BUILD_DIR is empty.
62    function clean {
63        if [ -d ${BUILD_DIR} ]; then
64            rm -fr ${BUILD_DIR}
65        fi
66        mkdir ${BUILD_DIR}
67    }
68
69    # build <description> <flags>
70    # Cleans build directory and performs a build using the provided CMake flags.
71    function build {
72        DESCRIPTION=$1
73        CMAKE_FLAGS=$2
74
75        status "Building ${DESCRIPTION}"
76        clean
77        cd ${BUILD_DIR}
78        show_cmds
79            cmake ${SRC_DIR} ${CMAKE_FLAGS} ${COMMON_CMAKE_FLAGS}
80            make --jobs=$(sysctl -n hw.logicalcpu)
81        hide_cmds
82    }
83
84    # test <description>
85    # Runs the pre-built unit tests (if not an NDK build).
86    function test {
87        DESCRIPTION=$1
88
89        status "Testing ${DESCRIPTION}"
90        cd ${BUILD_DIR}
91        show_cmds
92            if [ "$BUILD_TOOLCHAIN" != "ndk" ]; then
93                ./marl-unittests
94                ./fractal
95                ./hello_task
96                ./primes > /dev/null
97                ./tasks_in_tasks
98            fi
99        hide_cmds
100    }
101
102    # install <description>
103    # Installs the pre-built library to ${INSTALL_DIR}.
104    function install {
105        DESCRIPTION=$1
106
107        status "Installing ${DESCRIPTION}"
108        cd ${BUILD_DIR}
109        show_cmds
110            make install
111        hide_cmds
112    }
113
114    # build <description> <flags>
115    # Cleans build directory and performs a build using the provided CMake
116    # flags, then runs tests.
117    function buildAndTest {
118        DESCRIPTION=$1
119        CMAKE_FLAGS=$2
120        build "$DESCRIPTION" "$CMAKE_FLAGS"
121        test  "$DESCRIPTION"
122    }
123
124    # build <description> <flags>
125    # Cleans build directory and performs a build using the provided CMake
126    # flags, then installs the library to ${INSTALL_DIR}.
127    function buildAndInstall {
128        DESCRIPTION=$1
129        CMAKE_FLAGS=$2
130        build   "$DESCRIPTION" "$CMAKE_FLAGS"
131        install "$DESCRIPTION"
132    }
133
134    if [ -n "$RUN_TESTS" ]; then
135        buildAndTest "marl for test" ""
136    fi
137
138    buildAndInstall "marl for install" "-DMARL_INSTALL=1"
139
140    if [ -n "$BUILD_ARTIFACTS" ]; then
141        status "Copying build artifacts"
142        show_cmds
143            tar -czvf "$BUILD_ARTIFACTS/build.tar.gz" -C "$INSTALL_DIR" .
144        hide_cmds
145    fi
146
147elif [ "$BUILD_SYSTEM" == "bazel" ]; then
148    # Get bazel
149    BAZEL_DIR="${ROOT_DIR}/bazel"
150    curl -L -k -O -s https://github.com/bazelbuild/bazel/releases/download/0.29.1/bazel-0.29.1-installer-darwin-x86_64.sh
151    mkdir "${BAZEL_DIR}"
152    sh bazel-0.29.1-installer-darwin-x86_64.sh --prefix="${BAZEL_DIR}"
153    rm bazel-0.29.1-installer-darwin-x86_64.sh
154    BAZEL="${BAZEL_DIR}/bin/bazel"
155
156    show_cmds
157        "${BAZEL}" test //:tests --test_output=all
158        "${BAZEL}" run //examples:fractal
159        "${BAZEL}" run //examples:hello_task
160        "${BAZEL}" run //examples:primes > /dev/null
161        "${BAZEL}" run //examples:tasks_in_tasks
162    hide_cmds
163else
164    status "Unknown build system: $BUILD_SYSTEM"
165    exit 1
166fi
167
168status "Done"
169