Skip to content

Commit e59fb2c

Browse files
committed
Simplify tcl library initialization post switch to Tcl9
In tcl9, we don't need the runfiles anymore as we can use the embedded zipfs. Remove remants that were needed to provide runfile support. Signed-off-by: Henner Zeller <h.zeller@acm.org>
1 parent 03322c9 commit e59fb2c

3 files changed

Lines changed: 6 additions & 184 deletions

File tree

bazel/BUILD

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
load("@bazel_skylib//rules:build_test.bzl", "build_test")
2-
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
32
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
43
load("@rules_cc//cc:cc_library.bzl", "cc_library")
54
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
65
load("@rules_pkg//pkg:zip.bzl", "pkg_zip")
76
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
87
load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")
9-
load("//bazel:extract_zip.bzl", "extract_zip")
108

119
package(features = ["layering_check"])
1210

@@ -68,12 +66,6 @@ pkg_zip(
6866
],
6967
)
7068

71-
# ... however, Tcl8 still needs access to a directory, so unpack in that case.
72-
extract_zip(
73-
name = "tcl_resources_dir",
74-
src = ":tcl_resources_zip",
75-
)
76-
7769
genrule(
7870
name = "tcl_resources_zip_data_h",
7971
srcs = [":tcl_resources_zip"],
@@ -87,44 +79,15 @@ cc_binary(
8779
srcs = ["embed.cc"],
8880
)
8981

90-
# As long as we support tcl8 (using runfiles) we need this flag.
91-
bool_flag(
92-
name = "use_zipfs",
93-
build_setting_default = True, # True as we use tcl9 with zipfs feature
94-
)
95-
96-
config_setting(
97-
name = "zipfs_config",
98-
flag_values = {":use_zipfs": "true"},
99-
)
100-
101-
# Library initialization is a bit different for tcl8 and tcl9.
102-
# In tcl9, we can use a zipped version of the needed include files, but
103-
# for tcl8, we have to use the unpacked zip file and point our runfiles
104-
# to it.
10582
cc_library(
10683
name = "tcl_library_init",
107-
srcs = ["tcl_library_init.cc"] +
108-
select({
109-
":zipfs_config": ["tcl_resources_zip_data.h"],
110-
"//conditions:default": [],
111-
}),
84+
srcs = [
85+
"tcl_library_init.cc",
86+
"tcl_resources_zip_data.h",
87+
],
11288
hdrs = ["tcl_library_init.h"],
113-
copts = select({
114-
":zipfs_config": [],
115-
"//conditions:default": ["-DUSE_TCL_RUNFILE_INIT"],
116-
}),
117-
data = select({
118-
":zipfs_config": [],
119-
"//conditions:default": [":tcl_resources_dir"],
120-
}),
12189
visibility = ["//visibility:public"],
122-
deps = [
123-
"@tcl_lang//:tcl",
124-
] + select({
125-
":zipfs_config": [],
126-
"//conditions:default": ["@rules_cc//cc/runfiles"],
127-
}),
90+
deps = ["@tcl_lang//:tcl"],
12891
)
12992

13093
# small build test to check if "bazel build //src/sta:StaTclInitVar" works

bazel/extract_zip.bzl

Lines changed: 0 additions & 37 deletions
This file was deleted.

bazel/tcl_library_init.cc

Lines changed: 1 addition & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -3,130 +3,26 @@
33

44
#include "bazel/tcl_library_init.h"
55

6-
#include <cstdlib>
7-
#include <filesystem>
86
#include <iostream>
97
#include <optional>
108
#include <string>
11-
#include <system_error>
129

13-
#include "tcl.h"
14-
15-
// In tcl 9, we can use the //zipfs:/ virtual file system (unless
16-
// we specifically disabled with the --//bazel:use_zipfs=False flag)
17-
#if TCL_MAJOR_VERSION >= 9 && !defined(USE_TCL_RUNFILE_INIT)
18-
#define USE_ZIPFS_INIT 1
19-
#else
20-
#define USE_ZIPFS_INIT 0
21-
#endif
22-
23-
#if USE_ZIPFS_INIT
2410
#include "bazel/tcl_resources_zip_data.h"
25-
#else
26-
27-
#include <limits.h>
28-
#include <unistd.h>
29-
30-
#if defined(__APPLE__)
31-
#include <mach-o/dyld.h>
32-
#include <sys/param.h>
33-
#endif
34-
35-
#include <memory>
36-
37-
#include "rules_cc/cc/runfiles/runfiles.h"
38-
#endif
11+
#include "tcl.h"
3912

4013
namespace in_bazel {
4114

42-
#if !USE_ZIPFS_INIT
43-
// Avoid adding any dependencies like boost.filesystem
44-
// Returns path to running binary if possible.
45-
static std::string GetProgramLocation()
46-
{
47-
#if defined(_WIN32)
48-
char result[MAX_PATH + 1] = {'\0'};
49-
auto path_len = GetModuleFileNameA(NULL, result, MAX_PATH);
50-
#elif defined(__APPLE__)
51-
char result[MAXPATHLEN + 1] = {'\0'};
52-
uint32_t path_len = MAXPATHLEN;
53-
if (_NSGetExecutablePath(result, &path_len) != 0) {
54-
path_len = readlink(result, result, MAXPATHLEN);
55-
}
56-
#else
57-
char result[PATH_MAX + 1] = {'\0'};
58-
ssize_t path_len = readlink("/proc/self/exe", result, PATH_MAX);
59-
#endif
60-
if (path_len > 0) {
61-
return result;
62-
}
63-
return Tcl_GetNameOfExecutable();
64-
}
65-
#endif
66-
6715
static std::optional<std::string> TclLibraryMountPoint(Tcl_Interp* interp)
6816
{
6917
// In tcl9, we can use //zipfs:/ otherwise we need to point to the
7018
// directory where the tcl library files are extracted.
71-
#if USE_ZIPFS_INIT
7219
if (TclZipfs_MountBuffer(
7320
interp, kTclResourceZip, sizeof(kTclResourceZip), "/app", 0)
7421
!= TCL_OK) {
7522
std::cerr << "[Warning] Failed to mount Tcl zipfs.\n";
7623
return std::nullopt;
7724
}
7825
return Tcl_GetStringResult(interp);
79-
#else
80-
using rules_cc::cc::runfiles::Runfiles;
81-
82-
auto find_tcl_resources
83-
= [](const Runfiles* runfiles) -> std::optional<std::string> {
84-
std::error_code ec;
85-
for (const std::string loc : {"openroad", "opensta", "_main"}) {
86-
const std::string check_loc = loc + "/bazel/tcl_resources_dir";
87-
const std::string path = runfiles->Rlocation(check_loc);
88-
if (!path.empty() && std::filesystem::exists(path, ec)) {
89-
return path;
90-
}
91-
}
92-
return std::nullopt;
93-
};
94-
95-
// First try with the inherited RUNFILES_* env vars. When OpenROAD
96-
// is the direct Bazel target (or invoked from an sh_test whose
97-
// runfiles tree also contains OpenROAD's data), those env vars
98-
// already point at the correct tree.
99-
std::string error;
100-
std::unique_ptr<Runfiles> runfiles(
101-
Runfiles::Create(GetProgramLocation(), BAZEL_CURRENT_REPOSITORY, &error));
102-
if (runfiles) {
103-
if (auto path = find_tcl_resources(runfiles.get())) {
104-
return path;
105-
}
106-
}
107-
108-
// Fallback: when OpenROAD is invoked by a build system that
109-
// previously ran another Bazel binary (e.g. a Python wrapper), the
110-
// inherited RUNFILES_* variables point to the *other* binary's
111-
// runfiles tree and won't contain OpenROAD's tcl_resources_dir.
112-
// Unset them and retry so Runfiles::Create falls back to the
113-
// exe_path derived from /proc/self/exe.
114-
unsetenv("RUNFILES_DIR");
115-
unsetenv("RUNFILES_MANIFEST_FILE");
116-
unsetenv("RUNFILES_MANIFEST_ONLY");
117-
118-
runfiles.reset(
119-
Runfiles::Create(GetProgramLocation(), BAZEL_CURRENT_REPOSITORY, &error));
120-
if (!runfiles) {
121-
std::cerr << "[Warning] Failed to create bazel runfiles: " << error << "\n";
122-
return std::nullopt;
123-
}
124-
125-
if (auto path = find_tcl_resources(runfiles.get())) {
126-
return path;
127-
}
128-
return std::nullopt;
129-
#endif
13026
}
13127

13228
int SetupTclEnvironment(Tcl_Interp* interp)

0 commit comments

Comments
 (0)