Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions bazel/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@openroad_rules_python//python:pip.bzl", "compile_pip_requirements")
load("@rules_cc//cc:cc_library.bzl", "cc_library")

Expand Down Expand Up @@ -29,3 +30,11 @@ cc_library(
],
alwayslink = True,
)

# small build test to check if "bazel build //src/sta:StaTclInitVar" works
# Run with "bazel test //bazel:sta_tcl_encode_test"
# Tests the tcl_encode_sta.bzl rule
build_test(
name = "sta_tcl_encode_test",
targets = ["//src/sta:StaTclInitVar"],
)
19 changes: 17 additions & 2 deletions bazel/tcl_encode_sta.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,23 @@ def _tcl_encode_sta_impl(ctx):
args.add(ctx.attr.char_array_name)
args.add_all(ctx.files.srcs)

# Get the Tcl library files (init.tcl...) from tcl_core
tcl_libfiles = ctx.attr._tcl_library_files[DefaultInfo]

# All tcl_core files live under library/, so split any file's path on
# "/library/" to obtain the library directory for TCL_LIBRARY.
tcl_lib_dir = ctx.files._tcl_library_files[0].path.split("/library/", 1)[0] + "/library"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The calculation of tcl_lib_dir assumes that ctx.files._tcl_library_files is not empty. If the file list is empty, this will cause a list index out of range error during the analysis phase. It is safer to verify that the list contains at least one file before accessing it, even though this is a private attribute with a default value.

    if not ctx.files._tcl_library_files:
        fail("No files found in _tcl_library_files")
    tcl_lib_dir = ctx.files._tcl_library_files[0].path.split("/library/", 1)[0] + "/library"


ctx.actions.run(
outputs = [output_file],
inputs = ctx.files.srcs,
inputs = depset(
direct = ctx.files.srcs + [ctx.file._encode_script],
transitive = [tcl_libfiles.files],
),
arguments = [args],
tools = [ctx.executable._tclsh, ctx.file._encode_script],
tools = [ctx.executable._tclsh],
executable = ctx.executable._tclsh,
env = {"TCL_LIBRARY": tcl_lib_dir},
)
return [DefaultInfo(files = depset([output_file]))]

Expand All @@ -46,6 +57,10 @@ tcl_encode_sta = rule(
default = "//src/sta:etc/TclEncode.tcl",
allow_single_file = True,
),
"_tcl_library_files": attr.label(
default = "@tcl_lang//:tcl_core",
allow_files = True,
),
"_tclsh": attr.label(
default = "@tcl_lang//:tclsh",
executable = True,
Expand Down