Skip to content

Commit 0e2d771

Browse files
committed
Merge branch 'master' of https://github.com/The-OpenROAD-Project/OpenROAD into grt_fill_via
2 parents 75a5c58 + ee85037 commit 0e2d771

42 files changed

Lines changed: 1122 additions & 315 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

BUILD.bazel

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,6 @@ cc_binary(
166166
"src/Main.cc",
167167
],
168168
copts = OPENROAD_COPTS,
169-
data = [
170-
"@tclreadline//:tclreadline_scripts",
171-
],
172169
features = ["-use_header_modules"],
173170
malloc = select({
174171
"@platforms//os:linux": "@tcmalloc//tcmalloc",
@@ -180,13 +177,13 @@ cc_binary(
180177
":openroad_version",
181178
":opt_notification",
182179
":ord",
183-
"//bazel:runfiles",
180+
"//bazel:tcl_library_init",
181+
"@rules_cc//cc/runfiles", # sets BAZEL_CURRENT_REPOSITORY
184182
"//src/cut",
185183
"//src/gui",
186184
"//src/sta:opensta_lib",
187185
"//src/utl",
188186
"@boost.stacktrace",
189-
"@rules_cc//cc/runfiles",
190187
"@tcl_lang//:tcl",
191188
# tclreadline: provides ENABLE_READLINE define + links libtclreadline.
192189
# On systems without tclreadline these are empty stub targets (no-op).

MODULE.bazel

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ bazel_dep(name = "platforms", version = "0.0.11")
1515
bazel_dep(name = "rules_bison", version = "0.3.1")
1616
bazel_dep(name = "rules_cc", version = "0.2.17")
1717
bazel_dep(name = "rules_flex", version = "0.3.1")
18+
bazel_dep(name = "rules_pkg", version = "1.2.0")
1819
bazel_dep(name = "rules_python", version = "1.8.5")
1920
bazel_dep(name = "rules_shell", version = "0.6.1")
2021
bazel_dep(name = "swig", version = "4.3.0.bcr.2")
@@ -65,6 +66,7 @@ bazel_dep(name = "or-tools", version = "9.15")
6566
bazel_dep(name = "readline", version = "8.2.bcr.3")
6667
bazel_dep(name = "spdlog", version = "1.15.1")
6768
bazel_dep(name = "tcl_lang", version = "8.6.16.bcr.1")
69+
bazel_dep(name = "tclreadline", version = "2.4.1")
6870
bazel_dep(name = "tcmalloc", version = "0.0.0-20250927-12f2552")
6971
bazel_dep(name = "yaml-cpp", version = "0.9.0")
7072
bazel_dep(name = "zlib", version = "1.3.1.bcr.5")
@@ -97,7 +99,6 @@ bazel_dep(name = "toolchains_llvm", version = "1.5.0")
9799

98100
bazel_dep(name = "bant", version = "0.2.4", dev_dependency = True)
99101
bazel_dep(name = "googletest", version = "1.17.0.bcr.2", dev_dependency = True)
100-
bazel_dep(name = "rules_pkg", version = "1.2.0", dev_dependency = True)
101102
bazel_dep(name = "rules_verilator", version = "0.1.0", dev_dependency = True)
102103
bazel_dep(name = "verilator", version = "5.036.bcr.3", dev_dependency = True)
103104

@@ -249,16 +250,6 @@ use_repo(maven, "maven")
249250

250251
http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
251252

252-
http_archive(
253-
name = "tclreadline",
254-
build_file = "//bazel:tclreadline.BUILD",
255-
sha256 = "a64e0faed5957b8e1ac16f179948e21cdd6d3b8313590b7ab049a3192ab864fb",
256-
strip_prefix = "tclreadline-2.3.8",
257-
urls = [
258-
"https://github.com/flightaware/tclreadline/archive/refs/tags/v2.3.8.tar.gz",
259-
],
260-
)
261-
262253
http_archive(
263254
name = "circt",
264255
build_file_content = """

MODULE.bazel.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bazel/BUILD

Lines changed: 93 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
load("@bazel_skylib//rules:build_test.bzl", "build_test")
2+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
3+
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
24
load("@rules_cc//cc:cc_library.bzl", "cc_library")
5+
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
6+
load("@rules_pkg//pkg:zip.bzl", "pkg_zip")
37
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
48
load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")
9+
load("//bazel:extract_zip.bzl", "extract_zip")
510

611
package(features = ["layering_check"])
712

@@ -36,17 +41,99 @@ compile_pip_requirements(
3641
requirements_txt = "requirements_lock_3_13.txt",
3742
)
3843

39-
cc_library(
40-
name = "runfiles",
44+
# Various tcl libraries that we want to embed into the binary.
45+
pkg_files(
46+
name = "tcl_core_files",
47+
srcs = ["@tcl_lang//:tcl_core"],
48+
strip_prefix = strip_prefix.from_root(),
49+
)
50+
51+
pkg_files(
52+
name = "tclreadline_files",
53+
srcs = ["@tclreadline//:tclreadline_scripts"],
54+
prefix = "tclreadline",
55+
strip_prefix = strip_prefix.from_root(),
56+
)
57+
58+
# Packaging up all tcl resources to one complete zip file. It can be
59+
# directly embedded in Tcl9 in a //zipfs:/ vfs.
60+
pkg_zip(
61+
name = "tcl_resources_zip",
4162
srcs = [
42-
"InitRunFiles.cpp",
43-
],
44-
data = [
45-
"@tcl_lang//:tcl_core",
63+
":tcl_core_files",
64+
":tclreadline_files",
4665
],
66+
)
67+
68+
# ... however, Tcl8 still needs access to a directory, so unpack in that case.
69+
extract_zip(
70+
name = "tcl_resources_dir",
71+
src = ":tcl_resources_zip",
72+
)
73+
74+
genrule(
75+
name = "tcl_resources_zip_data_h",
76+
srcs = [":tcl_resources_zip"],
77+
outs = ["tcl_resources_zip_data.h"],
78+
cmd = "$(location :embed) kTclResourceZip $< $@",
79+
tools = [":embed"],
80+
)
81+
82+
cc_binary(
83+
name = "embed",
84+
srcs = ["embed.cc"],
85+
)
86+
87+
# As long as we support tcl8 (using runfiles) we need this flag.
88+
bool_flag(
89+
name = "use_zipfs",
90+
build_setting_default = False, # False until we can use tcl9
91+
)
92+
93+
config_setting(
94+
name = "zipfs_config",
95+
flag_values = {":use_zipfs": "true"},
96+
)
97+
98+
# Library initialization is a bit different for tcl8 and tcl9.
99+
# In tcl9, we can use a zipped version of the needed include files, but
100+
# for tcl8, we have to use the unpacked zip file and point our runfiles
101+
# to it.
102+
cc_library(
103+
name = "tcl_library_init",
104+
srcs = ["tcl_library_init.cc"] +
105+
select({
106+
":zipfs_config": ["tcl_resources_zip_data.h"],
107+
"//conditions:default": [],
108+
}),
109+
hdrs = ["tcl_library_init.h"],
110+
copts = select({
111+
":zipfs_config": [],
112+
"//conditions:default": ["-DUSE_TCL_RUNFILE_INIT"],
113+
}),
114+
data = select({
115+
":zipfs_config": [],
116+
"//conditions:default": [":tcl_resources_dir"],
117+
}),
118+
visibility = ["//visibility:public"],
119+
deps = [
120+
"@tcl_lang//:tcl",
121+
] + select({
122+
":zipfs_config": [],
123+
"//conditions:default": ["@rules_cc//cc/runfiles"],
124+
}),
125+
)
126+
127+
# shim old library as it is still referenced in src/sta, implementing the
128+
# old behavior.
129+
cc_library(
130+
name = "runfiles",
131+
srcs = ["InitRunFiles.cpp"],
132+
data = [":tcl_resources_dir"],
47133
visibility = ["//visibility:public"],
48134
deps = [
49135
"@rules_cc//cc/runfiles",
136+
"@tcl_lang//:tcl",
50137
],
51138
alwayslink = True,
52139
)

bazel/InitRunFiles.cpp

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
#include <unistd.h>
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
// Copyright (c) 2026, The OpenROAD Authors
3+
4+
// This file to go away as soon as we use the tcl_library_init initialization
5+
// in OpenSTA.
6+
7+
#include <unistd.h> // readlink()
28

39
#include <climits>
410
#include <cstdlib>
11+
#include <filesystem>
512
#include <iostream>
613
#include <memory>
714
#include <optional>
815
#include <string>
16+
#include <system_error>
917

1018
#if defined(__APPLE__)
1119
#include <mach-o/dyld.h>
@@ -14,9 +22,10 @@
1422

1523
#include "rules_cc/cc/runfiles/runfiles.h"
1624

25+
namespace {
1726
// Avoid adding any dependencies like boost.filesystem
1827
// Returns path to running binary if possible, otherwise nullopt.
19-
static std::optional<std::string> getProgramLocation()
28+
static std::optional<std::string> GetProgramLocation()
2029
{
2130
#if defined(_WIN32)
2231
char result[MAX_PATH + 1] = {'\0'};
@@ -37,41 +46,41 @@ static std::optional<std::string> getProgramLocation()
3746
return std::nullopt;
3847
}
3948

40-
// Global constructor class to initialize Bazel runfiles and environment
41-
// variables
49+
std::string GetTclLibraryBaseLocation()
50+
{
51+
using rules_cc::cc::runfiles::Runfiles;
52+
std::string error;
53+
std::unique_ptr<Runfiles> runfiles(Runfiles::Create(
54+
*GetProgramLocation(), BAZEL_CURRENT_REPOSITORY, &error));
55+
if (!runfiles) {
56+
std::cerr << "[Warning] Failed to create bazel runfiles: " << error << "\n";
57+
return "";
58+
}
59+
60+
std::error_code ec;
61+
for (const std::string loc : {"openroad", "opensta", "_main"}) {
62+
const std::string check_loc = loc + "/bazel/tcl_resources_dir";
63+
const std::string path = runfiles->Rlocation(check_loc);
64+
if (!path.empty() && std::filesystem::exists(path, ec)) {
65+
return path;
66+
}
67+
}
68+
return "";
69+
}
70+
4271
class BazelInitializer
4372
{
4473
public:
4574
BazelInitializer()
4675
{
47-
using rules_cc::cc::runfiles::Runfiles;
48-
49-
std::string error;
50-
std::string program_location = getProgramLocation().value();
51-
std::unique_ptr<Runfiles> runfiles(
52-
Runfiles::Create(program_location, BAZEL_CURRENT_REPOSITORY, &error));
53-
if (!runfiles) {
54-
std::cerr << "Error initializing Bazel runfiles: " << error << std::endl;
55-
std::exit(1);
56-
}
57-
58-
// Set the TCL_LIBRARY environment variable
59-
const std::string tcl_path = runfiles->Rlocation("tcl_lang/library/");
60-
if (!tcl_path.empty()) {
76+
const std::string lib_mount_point = GetTclLibraryBaseLocation();
77+
if (!lib_mount_point.empty()) {
78+
const std::string tcl_path = lib_mount_point + "/library";
6179
setenv("TCL_LIBRARY", tcl_path.c_str(), true);
62-
} else {
63-
std::cerr << "Error: Could not locate 'tcl_lang/library/' in runfiles."
64-
<< std::endl;
65-
std::exit(1);
6680
}
67-
68-
// Setup env variables for any other libraries that use runfiles
69-
std::string manifest = program_location + ".runfiles/MANIFEST";
70-
std::string runfiles_dir = program_location + ".runfiles";
71-
setenv("RUNFILES_MANIFEST_FILE", manifest.c_str(), /*__replace=*/true);
72-
setenv("RUNFILES_DIR", runfiles_dir.c_str(), /*__replace=*/true);
7381
}
7482
};
7583

76-
// Instantiate the global constructor
84+
// Provide via global constructor.
7785
static BazelInitializer bazel_initializer;
86+
} // namespace

bazel/embed.cc

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
// Copyright (c) 2026, The OpenROAD Authors
3+
4+
#include <cstddef>
5+
#include <cstdint>
6+
#include <cstdio>
7+
#include <cstdlib>
8+
9+
int main(int argc, char* argv[])
10+
{
11+
if (argc != 4) {
12+
fprintf(stderr,
13+
"Usage: %s <variable-name> <input-file> <output_file>\n",
14+
argv[0]);
15+
return 1;
16+
}
17+
18+
const char* variable_name = argv[1];
19+
const char* input_file = argv[2];
20+
const char* output_file = argv[3];
21+
22+
FILE* in_file = fopen(input_file, "rb");
23+
if (!in_file) {
24+
perror("Could not open input.");
25+
return EXIT_FAILURE;
26+
}
27+
28+
FILE* out_file = fopen(output_file, "wb");
29+
if (!out_file) {
30+
fclose(in_file);
31+
perror("Could not open output.");
32+
return EXIT_FAILURE;
33+
}
34+
35+
fprintf(out_file,
36+
"static inline constexpr unsigned char %s[] = {\n ",
37+
variable_name);
38+
uint8_t buffer[100 * 12];
39+
size_t nread;
40+
while ((nread = fread(buffer, 1, sizeof(buffer), in_file)) > 0) {
41+
for (int i = 0; i < nread; ++i) {
42+
fprintf(out_file, "0x%02x,%s", buffer[i], i % 12 == 11 ? "\n " : " ");
43+
}
44+
}
45+
const bool any_issue = ferror(in_file) || ferror(out_file);
46+
if (any_issue) {
47+
perror("trouble reading file.");
48+
}
49+
fprintf(out_file, "};\n");
50+
fclose(in_file);
51+
fclose(out_file);
52+
53+
return any_issue ? EXIT_FAILURE : EXIT_SUCCESS;
54+
}

bazel/extract_zip.bzl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
# Copyright (c) 2026, The OpenROAD Authors
3+
4+
# We need this rule as long as we support Tcl 8
5+
6+
"""
7+
Unzip a zip file into a directory.
8+
9+
Since this creates a directory, we need to have it as separate rule
10+
and can't just have a genrule().
11+
"""
12+
13+
def _extract_zip_impl(ctx):
14+
out_dir = ctx.actions.declare_directory(ctx.attr.name)
15+
ctx.actions.run(
16+
inputs = [ctx.file.src],
17+
outputs = [out_dir],
18+
executable = ctx.executable._zipper,
19+
arguments = ["x", ctx.file.src.path, "-d", out_dir.path],
20+
)
21+
return [DefaultInfo(
22+
files = depset([out_dir]),
23+
runfiles = ctx.runfiles(files = [out_dir]),
24+
)]
25+
26+
extract_zip = rule(
27+
implementation = _extract_zip_impl,
28+
attrs = {
29+
"src": attr.label(allow_single_file = [".zip", ".jar"]),
30+
"_zipper": attr.label(
31+
default = Label("@bazel_tools//tools/zip:zipper"),
32+
cfg = "exec",
33+
executable = True,
34+
),
35+
},
36+
)

0 commit comments

Comments
 (0)