1#!/bin/sh 2 3set -eux 4 5[ -n "${CI:-}" ] && check="--check" 6 7cargo test --manifest-path libc-test/Cargo.toml --test style -- --nocapture 8 9command -v rustfmt 10rustfmt -V 11 12# Run once to cover everything that isn't in `src/` 13cargo fmt 14 15# Save a list of all source files 16tmpfile="file-list~" # trailing tilde for gitignore 17find src -name '*.rs' > "$tmpfile" 18 19# Before formatting, replace all macro identifiers with a function signature. 20# This allows `rustfmt` to format it. 21while IFS= read -r file; do 22 if [ "$file" = "src/macros.rs" ]; then 23 # Too much special syntax in `macros.rs` that we don't want to format 24 continue 25 fi 26 27 # Turn all braced macro `foo! { /* ... */ }` invocations into 28 # `fn foo_fmt_tmp() { /* ... */ }`. 29 perl -pi -e 's/(?!macro_rules|c_enum)\b(\w+)!\s*\{/fn $1_fmt_tmp() {/g' "$file" 30 31 # Replace `if #[cfg(...)]` within `cfg_if` with `if cfg_tmp!([...])` which 32 # `rustfmt` will format. We put brackets within the parens so it is easy to 33 # match (trying to match parentheses would catch the first closing `)` which 34 # wouldn't be correct for something like `all(any(...), ...)`). 35 perl -pi -0777 -e 's/if #\[cfg\((.*?)\)\]/if cfg_tmp!([$1])/gms' "$file" 36 37 # We have some instances of `{const}` that make macros happy but aren't 38 # valid syntax. Replace this with just the keyword, plus an indicator 39 # comment on the preceding line (which is where rustfmt puts it. Also 40 # rust-lang/rustfmt#5464). 41 perl -pi -e 's/^(\s*)(.*)\{const\}/$1\/\* FMT-CONST \*\/\n$1$2const/g' "$file" 42 43 # Format the file. We need to invoke `rustfmt` directly since `cargo fmt` 44 # can't figure out the module tree with the hacks in place. 45 failed=false 46 rustfmt --config-path rustfmt.toml "$file" ${check:+"$check"} || failed=true 47 48 # Restore all changes to the files. 49 perl -pi -e 's/fn (\w+)_fmt_tmp\(\)/$1!/g' "$file" 50 perl -pi -0777 -e 's/cfg_tmp!\(\[(.*?)\]\)/#[cfg($1)]/gms' "$file" 51 perl -pi -0777 -e 's/\/\* FMT-CONST \*\/(?:\n\s*)?(.*?)const/$1\{const\}/gms' "$file" 52 53 # Defer emitting the failure until after the files get reset 54 if [ "$failed" != "false" ]; then 55 echo "Formatting failed" 56 exit 1 57 fi 58done < "$tmpfile" 59 60rm "$tmpfile" 61 62# Run once from workspace root to get everything that wasn't handled as an 63# individual file. 64cargo fmt 65 66# Ensure that `sort` output is not locale-dependent 67export LC_ALL=C 68 69for file in libc-test/semver/*.txt; do 70 case "$file" in 71 *TODO*) continue ;; 72 esac 73 74 if ! sort -C "$file"; then 75 echo "Unsorted semver file $file" 76 exit 1 77 fi 78 79 duplicates=$(uniq -d "$file") 80 if [ -n "$duplicates" ]; then 81 echo "Semver file $file contains duplicates:" 82 echo "$duplicates" 83 84 exit 1 85 fi 86done 87 88if shellcheck --version ; then 89 find . -name '*.sh' -print0 | xargs -0 shellcheck 90else 91 echo "shellcheck not found" 92 exit 1 93fi 94