Skip to content

Commit fd7692b

Browse files
committed
try adding an sh_wrapper around heir-opt to populate env vars
1 parent 1281797 commit fd7692b

File tree

5 files changed

+57
-2
lines changed

5 files changed

+57
-2
lines changed

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ bazel_dep(name = "rules_license", version = "1.0.0")
2727
bazel_dep(name = "rules_foreign_cc", version = "0.14.0")
2828
bazel_dep(name = "rules_cc", version = "0.2.0")
2929
bazel_dep(name = "rules_go", version = "0.53.0")
30+
bazel_dep(name = "rules_shell", version = "0.6.0")
3031
bazel_dep(name = "rules_python", version = "1.2.0")
3132
bazel_dep(name = "googletest", version = "1.16.0")
3233
bazel_dep(name = "google_benchmark", version = "1.9.1")

MODULE.bazel.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ filegroup(
1313
data = [
1414
":lit.cfg.py",
1515
"@heir//tools:heir-opt",
16+
"@heir//tools:heir-opt-bin",
1617
"@heir//tools:heir-translate",
1718
"@llvm-project//llvm:FileCheck",
1819
"@llvm-project//llvm:count",

tools/BUILD

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# HEIR tools
22
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
33
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
4+
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
45

56
package(
67
default_applicable_licenses = ["@heir//:license"],
@@ -12,7 +13,7 @@ WORKSPACE_PATH = ""
1213

1314
# Custom `mlir-opt` replacement that links our dialect and passes
1415
cc_binary(
15-
name = "heir-opt",
16+
name = "heir-opt-bin",
1617
srcs = ["heir-opt.cpp"],
1718
data = select({
1819
"@heir//:config_enable_yosys": [
@@ -198,6 +199,12 @@ cc_binary(
198199
}),
199200
)
200201

202+
sh_binary(
203+
name = "heir-opt",
204+
srcs = ["heir-opt-wrapper.sh"],
205+
data = [":heir-opt-bin"],
206+
)
207+
201208
# Custom `mlir-translate` replacement that adds our custom translations
202209
cc_binary(
203210
name = "heir-translate",

tools/heir-opt-wrapper.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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

Comments
 (0)