cargo 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env bash
  2. # Copyright Materialize, Inc. and contributors. All rights reserved.
  3. #
  4. # Use of this software is governed by the Business Source License
  5. # included in the LICENSE file at the root of this repository.
  6. #
  7. # As of the Change Date specified in that file, in accordance with
  8. # the Business Source License, use of this software will be governed
  9. # by the Apache License, Version 2.0.
  10. #
  11. # sanshim/cargo — shims cargo(1) to enable sanitizers.
  12. # See: https://github.com/japaric/rust-san
  13. #
  14. # > Be sure to always pass `--target [HOST-TRIPLE]`` to Cargo or else you'll end
  15. # > up sanitizing the build scripts that Cargo runs or run into compilation
  16. # > errors if your crate depends on a dylib.
  17. #
  18. # We have to parse Cargo's arguments a bit so that we can stick the `--target`
  19. # argument in the right spot: after `build`, `test`, or `run`.
  20. set -euo pipefail
  21. # shellcheck source=misc/shlib/shlib.bash
  22. . "$(dirname "$0")/../shlib/shlib.bash"
  23. option_args=()
  24. command=()
  25. target_args=()
  26. for arg in "$@"; do
  27. case "$arg" in
  28. --target*) die "sanshim/cargo: specifying --target explicitly is prohibited" ;;
  29. +*) die "sanshim/cargo: overriding toolchain with +TOOLCHAIN is prohibited" ;;
  30. esac
  31. done
  32. for arg in "$@"; do
  33. shift
  34. if [[ "$arg" = -* ]]; then
  35. option_args+=("$arg")
  36. else
  37. command=("$arg")
  38. break
  39. fi
  40. done
  41. case "${command[0]:-}" in
  42. build|run|test) target_args+=(--target "$(rustc -vV | awk '/host/ { print $2 }')") ;;
  43. esac
  44. # TODO(benesch): we shouldn't need to stick SYSROOT/lib onto the linker search
  45. # path once https://github.com/rust-lang/rust/issues/66140 is fixed.
  46. RUSTFLAGS+=" -Z sanitizer=$MZ_DEV_SANITIZER -L$(rustc +nightly --print sysroot)/lib"
  47. export RUSTFLAGS
  48. run "$CARGO" +nightly "${option_args[@]}" "${command[@]}" "${target_args[@]}" "$@"