|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -eo pipefail |
| 4 | +cd "$(dirname "$0")/.." |
| 5 | +source ci/_ |
| 6 | +# only nightly is used uniformly as we contain good amount of nightly-only code |
| 7 | +# (benches, frozen abi...) |
| 8 | +source ci/rust-version.sh nightly |
| 9 | + |
| 10 | +# There's a special common feature called `dev-utils` to |
| 11 | +# overcome cargo's issue: https://github.com/rust-lang/cargo/issues/8379 |
| 12 | +# This feature is like `cfg(test)`, which works between crates. |
| 13 | +# |
| 14 | +# Unfortunately, this in turn needs some special checks to avoid common |
| 15 | +# pitfalls of `dev-utils` itself. |
| 16 | +# |
| 17 | +# Firstly, detect any misuse of dev-utils as normal/build dependencies. |
| 18 | +# Also, allow some exceptions for special purpose crates. This white-listing |
| 19 | +# mechanism can be used for core-development-oriented crates like bench bins. |
| 20 | +# |
| 21 | +# Put differently, use of dev-utils is forbidden for non-dev dependencies in |
| 22 | +# general. However, allow its use for non-dev dependencies only if its use |
| 23 | +# is confined under a dep. subgraph with all nodes being marked as dev-utils. |
| 24 | + |
| 25 | +# Add your troubled package which seems to want to use `dev-utils` as normal (not |
| 26 | +# dev) dependencies, only if you're sure that there's good reason to bend |
| 27 | +# dev-util's original intention and that listed package isn't part of released |
| 28 | +# binaries. |
| 29 | +# Note also that dev-utils-ci-marker feature must be added and all of its |
| 30 | +# dependencies should be edited likewise if any. |
| 31 | +declare dev_util_tainted_packages=( |
| 32 | +) |
| 33 | + |
| 34 | +mode=${1:-full} |
| 35 | + |
| 36 | +if [[ $mode = "tree" || $mode = "full" ]]; then |
| 37 | + # Run against the entire workspace dep graph (sans $dev_util_tainted_packages) |
| 38 | + dev_utils_excludes=$(for tainted in "${dev_util_tainted_packages[@]}"; do |
| 39 | + echo "--exclude $tainted" |
| 40 | + done) |
| 41 | + # shellcheck disable=SC2086 # Don't want to double quote $dev_utils_excludes |
| 42 | + _ cargo "+${rust_nightly}" tree --workspace -f "{p} {f}" --edges normal,build \ |
| 43 | + $dev_utils_excludes | ( |
| 44 | + if grep -E -C 3 -m 10 "[, ]dev-utils([, ]|$)"; then |
| 45 | + echo "dev-utils must not be used as normal dependencies" > /dev/stderr |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | + ) |
| 49 | + |
| 50 | + # Sanity-check that tainted packages has undergone the proper tedious rituals |
| 51 | + # to be justified as such. |
| 52 | + for tainted in "${dev_util_tainted_packages[@]}"; do |
| 53 | + # dev-utils-ci-marker is special proxy feature needed only when using |
| 54 | + # dev-utils code as part of normal dependency. dev-utils will be enabled |
| 55 | + # indirectly via this feature only if prepared correctly |
| 56 | + _ cargo "+${rust_nightly}" tree --workspace -f "{p} {f}" --edges normal,build \ |
| 57 | + --invert "$tainted" --features dev-utils-ci-marker | ( |
| 58 | + if grep -E -C 3 -m 10 -v "[, ]dev-utils([, ]|$)"; then |
| 59 | + echo "$tainted: All inverted dependencies must be with dev-utils" \ |
| 60 | + > /dev/stderr |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | + ) |
| 64 | + done |
| 65 | +fi |
| 66 | + |
| 67 | +# Detect possible compilation errors of problematic usage of `dev-utils`-gated code |
| 68 | +# without being explicitly declared as such in respective workspace member |
| 69 | +# `Cargo.toml`s. This cannot be detected with `--workspace --all-targets`, due |
| 70 | +# to unintentional `dev-utils` feature activation by cargo's feature |
| 71 | +# unification mechanism. |
| 72 | +# So, we use `cargo hack` to exhaustively build each individual workspace |
| 73 | +# members in isolation to work around. |
| 74 | +# |
| 75 | +# 1. Check implicit usage of `dev-utils`-gated code in non-dev (= production) code by |
| 76 | +# building without dev dependencies (= tests/benches) for each crate |
| 77 | +# 2. Check implicit usage of `dev-utils`-gated code in dev (= test/benches) code by |
| 78 | +# building in isolation from other crates, which might happen to enable `dev-utils` |
| 79 | +if [[ $mode = "check-bins" || $mode = "full" ]]; then |
| 80 | + _ cargo "+${rust_nightly}" hack check --bins |
| 81 | +fi |
| 82 | +if [[ $mode = "check-all-targets" || $mode = "full" ]]; then |
| 83 | + _ cargo "+${rust_nightly}" hack check --all-targets |
| 84 | +fi |
0 commit comments