aboutsummaryrefslogtreecommitdiff
path: root/infra/base-images/base-builder/cargo
blob: c60c7611b54cdfdc7406e2c179689f9f6f3410cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash -eu
# Copyright 2020 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This is a wrapper around calling cargo
# This just expands RUSTFLAGS in case of a coverage build
# We need this until https://github.com/rust-lang/cargo/issues/5450 is merged
# because cargo uses relative paths for the current crate
# and absolute paths for its dependencies
#
################################################################################

if [ "$SANITIZER" = "coverage" ] && [ $1 = "build" ]
then
    crate_src_abspath=`cargo metadata --no-deps --format-version 1 | jq -r '.workspace_root'`
    export RUSTFLAGS="$RUSTFLAGS --remap-path-prefix src=$crate_src_abspath/src"
fi

if [ "$SANITIZER" = "coverage" ] && [ $1 = "fuzz" ] && [ $2 = "build" ]
then
    # hack to turn cargo fuzz build into cargo build so as to get coverage
    # cargo fuzz adds "--target" "x86_64-unknown-linux-gnu"
    (
    # go into fuzz directory if not already the case
    cd fuzz || true
    fuzz_src_abspath=`pwd`
    # Default directory is fuzz_targets, but some projects like image-rs use fuzzers.
    while read i; do
        export RUSTFLAGS="$RUSTFLAGS --remap-path-prefix $i=$fuzz_src_abspath/$i"
        # Bash while syntax so that we modify RUSTFLAGS in main shell instead of a subshell.
    done <<< "$(ls */*.rs | cut -d/ -f1 | uniq)"
    # we do not want to trigger debug assertions and stops
    export RUSTFLAGS="$RUSTFLAGS -C debug-assertions=no"
    # do not optimize with --release, leading to Malformed instrumentation profile data
    cargo build --bins
    # copies the build output in the expected target directory
    cd `cargo metadata --format-version 1 --no-deps | jq -r '.target_directory'`
    mkdir -p x86_64-unknown-linux-gnu/release
    cp -r debug/* x86_64-unknown-linux-gnu/release/
    )
    exit 0
fi

/rust/bin/cargo "$@"