| #!/bin/bash | |
| # Presubmit script for CI/CD | |
| # Exit on error, and exit on unset variables. | |
| set -o errexit | |
| set -o nounset | |
| # Create .cargo/config.toml for the musl target | |
| # TODO: add a line for mac. | |
| BASE_DIR=$(dirname "$0") | |
| mkdir -p "$BASE_DIR/.cargo" | |
| cat > "$BASE_DIR/.cargo/config.toml" <<EOL | |
| [target.x86_64-unknown-linux-musl] | |
| rustflags = ["-C", "linker=clang", "-C", "link-arg=-fuse-ld=lld"] | |
| [target.x86_64-unknown-linux-gnu] | |
| rustflags = ["-C", "linker=clang", "-C", "link-arg=-fuse-ld=lld"] | |
| EOL | |
| # Install tools. | |
| . "$(dirname "$0")/infra/env_setup.sh" | |
| # Print commands as they're executed. | |
| set -o xtrace | |
| # Change to the rust directory to run cargo commands. | |
| cd "$(dirname "$0")/rust" | |
| # 1. Build the code. | |
| # `set -e` will cause the script to exit if this fails. | |
| cargo build | |
| # 2. Check formatting. | |
| # `cargo fmt -- --check` will exit with a non-zero status code if any | |
| # files are not formatted correctly. | |
| if ! cargo fmt -- --check; then | |
| { set +x; } 2>/dev/null | |
| echo "Please run \`cargo fmt\` to format your code" | |
| exit 1 | |
| fi | |
| # 3. Run tests. | |
| # `set -e` will cause the script to exit if any tests fail. | |
| cargo test | |
| { set +x; } 2>/dev/null | |
| echo "Presubmit checks passed!" |