|
| 1 | +#!/bin/bash |
| 2 | +# This script is a wrapper for heir-opt-bin that sets up the environment |
| 3 | +# for standalone execution. |
| 4 | + |
| 5 | +set -e |
| 6 | + |
| 7 | +# When run via `bazel run`, $0 is the path to this script. The runfiles |
| 8 | +# directory is created by bazel as a sibling directory with a .runfiles suffix. |
| 9 | +RUNFILES_DIR="${0}.runfiles" |
| 10 | + |
| 11 | +if [ ! -d "${RUNFILES_DIR}" ]; then |
| 12 | + # We may be operating inside of a test context, in which case the runfiles |
| 13 | + # is in a higher directory. E.g., add_one.mlir.test.runfiles might be in |
| 14 | + # bazel-out/k8-dbg/bin/tests/Transforms/layout_propagation/add_one.mlir.test.runfiles/_main/tools/heir-opt |
| 15 | + # And the runfiles dir we want is |
| 16 | + # bazel-out/k8-dbg/bin/tests/Transforms/layout_propagation/add_one.mlir.test.runfiles |
| 17 | + |
| 18 | + # Check if the current directory contains `.runfiles/` and if so, get the |
| 19 | + # path up to the runfiles part. |
| 20 | + CURRENT_DIR="$(dirname "$0")" |
| 21 | + if [[ "${CURRENT_DIR}" == *".runfiles"* ]]; then |
| 22 | + RUNFILES_DIR="${CURRENT_DIR%%.runfiles*}.runfiles" |
| 23 | + fi |
| 24 | +fi |
| 25 | + |
| 26 | +# If there's a runfiles dir, then we can set the relevant env variables |
| 27 | +if [ -d "${RUNFILES_DIR}" ]; then |
| 28 | + # The main workspace is symlinked as '_main' inside the runfiles dir. |
| 29 | + HEIR_WORKSPACE="_main" |
| 30 | + ABC_WORKSPACE="edu_berkeley_abc" |
| 31 | + |
| 32 | + HEIR_OPT_BIN="${RUNFILES_DIR}/${HEIR_WORKSPACE}/tools/heir-opt-bin" |
| 33 | + |
| 34 | + # These paths correspond to the `data` dependencies of the `heir-opt-bin` target. |
| 35 | + export HEIR_ABC_BINARY="${RUNFILES_DIR}/${ABC_WORKSPACE}/abc" |
| 36 | + export HEIR_YOSYS_SCRIPTS_DIR="${RUNFILES_DIR}/${HEIR_WORKSPACE}/lib/Transforms/YosysOptimizer/yosys" |
| 37 | +fi |
| 38 | + |
| 39 | +# If there's no runfiles dir, we currently rely on the user to have set the |
| 40 | +# relevant env vars. We could also attempt to auto-detect the ABC path, but not |
| 41 | +# the yosys scripts path. If they're in the same directory as this binary, then |
| 42 | +# this should work. |
| 43 | + |
| 44 | +# Use exec to replace this script's process with the actual binary. |
| 45 | +exec "${HEIR_OPT_BIN}" "$@" |
0 commit comments