diff --git a/examples/common/build_ice40.sh b/examples/common/build_ice40.sh index 452089e..323e38a 100755 --- a/examples/common/build_ice40.sh +++ b/examples/common/build_ice40.sh @@ -1,5 +1,11 @@ #!/usr/bin/env bash set -euo pipefail +source "$(dirname "$(readlink -f "$0")")/find_tool.sh" + +# Make sure tools are accessible +YOSYS_CMD=$(find_tool yosys) +NEXTPNR_ICE40_CMD=$(find_tool nextpnr-ice40) +ICEPACK_CMD=$(find_tool icepack) # Generate Verilog source for Manta python3 -m manta gen manta.yaml manta.v @@ -8,6 +14,6 @@ python3 -m manta gen manta.yaml manta.v rm -rf build/ mkdir -p build/ cd build -$YOSYS -p 'synth_ice40 -top top_level -json top_level.json' ../top_level.sv -$NEXTPNR_ICE40 --hx1k --json top_level.json --pcf ../top_level.pcf --asc top_level.asc -$ICEPACK top_level.asc top_level.bin +$YOSYS_CMD -p 'synth_ice40 -top top_level -json top_level.json' ../top_level.sv +$NEXTPNR_ICE40_CMD --hx1k --json top_level.json --pcf ../top_level.pcf --asc top_level.asc +$ICEPACK_CMD top_level.asc top_level.bin diff --git a/examples/common/build_vivado.sh b/examples/common/build_vivado.sh index 689fb6a..1da24e9 100755 --- a/examples/common/build_vivado.sh +++ b/examples/common/build_vivado.sh @@ -1,33 +1,13 @@ #!/usr/bin/env bash set -euo pipefail +source "$(dirname "$(readlink -f "$0")")/find_tool.sh" + +# Make sure Vivado is accessible +VIVADO_CMD=$(find_tool vivado) # Generate Verilog source for Manta python3 -m manta gen manta.yaml manta.v -# Determine where Vivado is located - exactly one of PATH or $VIVADO must be available -vivado_on_path=false -vivado_env_set=false - -if command -v vivado &> /dev/null; then - vivado_on_path=true -fi - -if [[ -v VIVADO ]]; then - vivado_env_set=true -fi - -if $vivado_on_path && $vivado_env_set; then - echo "Error: Both \$VIVADO is set and 'vivado' is on PATH. Please use only one." >&2 - exit 1 -elif $vivado_on_path; then - VIVADO_CMD="vivado" -elif $vivado_env_set; then - VIVADO_CMD="$VIVADO" -else - echo "Error: Vivado not found. Either set \$VIVADO environment variable or add 'vivado' to PATH." >&2 - exit 1 -fi - # Clean build/ directory, and run Vivado from within it rm -rf build/ mkdir -p build/ diff --git a/examples/common/find_tool.sh b/examples/common/find_tool.sh new file mode 100644 index 0000000..60c8a30 --- /dev/null +++ b/examples/common/find_tool.sh @@ -0,0 +1,32 @@ +# Find a tool binary from either the $PATH environment variable, or another +# environment variable named after the tool (ie, $VIVADO or $YOSYS). +# Usage: find_tool +# The environment variable is the uppercased tool name, with any hyphens converted to underscores. +# Prints the command to use, or exits with an error. +find_tool() { + local tool="$1" + local env_var="${tool^^}" + env_var="${env_var//-/_}" + local on_path=false + local env_set=false + + if command -v "$tool" &> /dev/null; then + on_path=true + fi + + if [[ -v "$env_var" ]]; then + env_set=true + fi + + if $on_path && $env_set; then + echo "Error: Both \$$env_var is set and '$tool' is on PATH. Please use only one." >&2 + exit 1 + elif $on_path; then + echo "$tool" + elif $env_set; then + echo "${!env_var}" + else + echo "Error: $tool not found. Either set \$$env_var environment variable or add '$tool' to PATH." >&2 + exit 1 + fi +}