• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash -eu
2# Copyright 2020 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# This is a wrapper around calling cargo
17# This just expands RUSTFLAGS in case of a coverage build
18# We need this until https://github.com/rust-lang/cargo/issues/5450 is merged
19# because cargo uses relative paths for the current crate
20# and absolute paths for its dependencies
21#
22################################################################################
23
24if [ "$SANITIZER" = "coverage" ] && [ $1 = "build" ]
25then
26    crate_src_abspath=`cargo metadata --no-deps --format-version 1 | jq -r '.workspace_root'`
27    export RUSTFLAGS="$RUSTFLAGS --remap-path-prefix src=$crate_src_abspath/src"
28fi
29
30if [ "$SANITIZER" = "coverage" ] && [ $1 = "fuzz" ] && [ $2 = "build" ]
31then
32    # hack to turn cargo fuzz build into cargo build so as to get coverage
33    # cargo fuzz adds "--target" "x86_64-unknown-linux-gnu"
34    (
35    # go into fuzz directory if not already the case
36    cd fuzz || true
37    fuzz_src_abspath=`pwd`
38    # Default directory is fuzz_targets, but some projects like image-rs use fuzzers.
39    while read i; do
40        export RUSTFLAGS="$RUSTFLAGS --remap-path-prefix $i=$fuzz_src_abspath/$i"
41        # Bash while syntax so that we modify RUSTFLAGS in main shell instead of a subshell.
42    done <<< "$(ls */*.rs | cut -d/ -f1 | uniq)"
43    # we do not want to trigger debug assertions and stops
44    export RUSTFLAGS="$RUSTFLAGS -C debug-assertions=no"
45    # do not optimize with --release, leading to Malformed instrumentation profile data
46    cargo build --bins
47    # copies the build output in the expected target directory
48    cd `cargo metadata --format-version 1 --no-deps | jq -r '.target_directory'`
49    mkdir -p x86_64-unknown-linux-gnu/release
50    cp -r debug/* x86_64-unknown-linux-gnu/release/
51    )
52    exit 0
53fi
54
55/rust/bin/cargo "$@"
56