1#!/bin/bash -eu
2# Copyright 2019 Google Inc.
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################################################################################
17
18# Mostly copied from
19# https://github.com/google/oss-fuzz/blob/7f8013db108e62727fba1c3cbcccac07d543682b/projects/grpc/build.sh
20
21# Copy $CFLAGS and $CXXFLAGS into Bazel command-line flags, for both
22# compilation and linking.
23#
24# Some flags, such as `-stdlib=libc++`, generate warnings if used on a C source
25# file. Since the build runs with `-Werror` this will cause it to break, so we
26# use `--conlyopt` and `--cxxopt` instead of `--copt`.
27readonly EXTRA_BAZEL_FLAGS="$(
28for f in ${CFLAGS}; do
29  echo "--conlyopt=${f}" "--linkopt=${f}"
30done
31for f in ${CXXFLAGS}; do
32  echo "--cxxopt=${f}" "--linkopt=${f}"
33done
34if [ "${SANITIZER}" = "undefined" ]
35then
36  # Bazel uses clang to link binary, which does not link clang_rt ubsan library for C++ automatically.
37  # See issue: https://github.com/bazelbuild/bazel/issues/8777
38  echo "--linkopt=$(find $(llvm-config --libdir) -name libclang_rt.ubsan_standalone_cxx-x86_64.a | head -1)"
39fi
40)"
41
42# Temporary hack, see https://github.com/google/oss-fuzz/issues/383
43readonly NO_VPTR='--copt=-fno-sanitize=vptr --linkopt=-fno-sanitize=vptr'
44
45readonly FUZZER_TARGETS=()
46readonly ENABLED=false
47
48if [ "$ENABLED" = true ]; then
49  bazel build \
50    --client_env=CC=${CC} \
51    --client_env=CXX=${CXX} \
52    --dynamic_mode=off \
53    --spawn_strategy=standalone \
54    --genrule_strategy=standalone \
55    ${NO_VPTR} \
56    --strip=never \
57    --linkopt=-lc++ \
58    --linkopt=-pthread \
59    --cxxopt=-std=c++11 \
60    --copt=${LIB_FUZZING_ENGINE} \
61    --linkopt=${LIB_FUZZING_ENGINE} \
62    --remote_cache=https://storage.googleapis.com/oak-bazel-cache \
63    --remote_upload_local_results=false \
64    ${EXTRA_BAZEL_FLAGS} \
65    ${FUZZER_TARGETS[@]}
66
67  for target in ${FUZZER_TARGETS}; do
68    # Replace : with /.
69    fuzzer_name="${target/:/\/}"
70    cp "./bazel-bin/${fuzzer_name}" "${OUT}/"
71  done
72
73  # Cleanup bazel- symlinks to avoid oss-fuzz trying to copy out of the build
74  # cache.
75  rm -f ./bazel-*
76fi
77