|
| 1 | +diff --git a/bazel/BUILD b/bazel/BUILD |
| 2 | +index a15adee..66f99d2 100644 |
| 3 | +--- a/bazel/BUILD |
| 4 | ++++ b/bazel/BUILD |
| 5 | +@@ -124,18 +124,12 @@ cc_library( |
| 6 | + }), |
| 7 | + ) |
| 8 | + |
| 9 | +-# shim old library as it is still referenced in src/sta, implementing the |
| 10 | +-# old behavior. |
| 11 | ++# Stub library kept for src/sta:opensta compatibility. |
| 12 | ++# Tcl setup is now handled by //bazel:tcl_library_init. |
| 13 | + cc_library( |
| 14 | + name = "runfiles", |
| 15 | + srcs = ["InitRunFiles.cpp"], |
| 16 | +- data = [":tcl_resources_dir"], |
| 17 | + visibility = ["//visibility:public"], |
| 18 | +- deps = [ |
| 19 | +- "@rules_cc//cc/runfiles", |
| 20 | +- "@tcl_lang//:tcl", |
| 21 | +- ], |
| 22 | +- alwayslink = True, |
| 23 | + ) |
| 24 | + |
| 25 | + # small build test to check if "bazel build //src/sta:StaTclInitVar" works |
| 26 | +diff --git a/bazel/InitRunFiles.cpp b/bazel/InitRunFiles.cpp |
| 27 | +index 0651774..ece50b0 100644 |
| 28 | +--- a/bazel/InitRunFiles.cpp |
| 29 | ++++ b/bazel/InitRunFiles.cpp |
| 30 | +@@ -1,86 +1,3 @@ |
| 31 | +-// SPDX-License-Identifier: BSD-3-Clause |
| 32 | +-// Copyright (c) 2026, The OpenROAD Authors |
| 33 | +- |
| 34 | +-// This file to go away as soon as we use the tcl_library_init initialization |
| 35 | +-// in OpenSTA. |
| 36 | +- |
| 37 | +-#include <unistd.h> // readlink() |
| 38 | +- |
| 39 | +-#include <climits> |
| 40 | +-#include <cstdlib> |
| 41 | +-#include <filesystem> |
| 42 | +-#include <iostream> |
| 43 | +-#include <memory> |
| 44 | +-#include <optional> |
| 45 | +-#include <string> |
| 46 | +-#include <system_error> |
| 47 | +- |
| 48 | +-#if defined(__APPLE__) |
| 49 | +-#include <mach-o/dyld.h> |
| 50 | +-#include <sys/param.h> |
| 51 | +-#endif |
| 52 | +- |
| 53 | +-#include "rules_cc/cc/runfiles/runfiles.h" |
| 54 | +- |
| 55 | +-namespace { |
| 56 | +-// Avoid adding any dependencies like boost.filesystem |
| 57 | +-// Returns path to running binary if possible, otherwise nullopt. |
| 58 | +-static std::optional<std::string> GetProgramLocation() |
| 59 | +-{ |
| 60 | +-#if defined(_WIN32) |
| 61 | +- char result[MAX_PATH + 1] = {'\0'}; |
| 62 | +- auto path_len = GetModuleFileNameA(NULL, result, MAX_PATH); |
| 63 | +-#elif defined(__APPLE__) |
| 64 | +- char result[MAXPATHLEN + 1] = {'\0'}; |
| 65 | +- uint32_t path_len = MAXPATHLEN; |
| 66 | +- if (_NSGetExecutablePath(result, &path_len) != 0) { |
| 67 | +- path_len = readlink(result, result, MAXPATHLEN); |
| 68 | +- } |
| 69 | +-#else |
| 70 | +- char result[PATH_MAX + 1] = {'\0'}; |
| 71 | +- ssize_t path_len = readlink("/proc/self/exe", result, PATH_MAX); |
| 72 | +-#endif |
| 73 | +- if (path_len > 0) { |
| 74 | +- return result; |
| 75 | +- } |
| 76 | +- return std::nullopt; |
| 77 | +-} |
| 78 | +- |
| 79 | +-std::string GetTclLibraryBaseLocation() |
| 80 | +-{ |
| 81 | +- using rules_cc::cc::runfiles::Runfiles; |
| 82 | +- std::string error; |
| 83 | +- std::unique_ptr<Runfiles> runfiles(Runfiles::Create( |
| 84 | +- *GetProgramLocation(), BAZEL_CURRENT_REPOSITORY, &error)); |
| 85 | +- if (!runfiles) { |
| 86 | +- std::cerr << "[Warning] Failed to create bazel runfiles: " << error << "\n"; |
| 87 | +- return ""; |
| 88 | +- } |
| 89 | +- |
| 90 | +- std::error_code ec; |
| 91 | +- for (const std::string loc : {"openroad", "opensta", "_main"}) { |
| 92 | +- const std::string check_loc = loc + "/bazel/tcl_resources_dir"; |
| 93 | +- const std::string path = runfiles->Rlocation(check_loc); |
| 94 | +- if (!path.empty() && std::filesystem::exists(path, ec)) { |
| 95 | +- return path; |
| 96 | +- } |
| 97 | +- } |
| 98 | +- return ""; |
| 99 | +-} |
| 100 | +- |
| 101 | +-class BazelInitializer |
| 102 | +-{ |
| 103 | +- public: |
| 104 | +- BazelInitializer() |
| 105 | +- { |
| 106 | +- const std::string lib_mount_point = GetTclLibraryBaseLocation(); |
| 107 | +- if (!lib_mount_point.empty()) { |
| 108 | +- const std::string tcl_path = lib_mount_point + "/library"; |
| 109 | +- setenv("TCL_LIBRARY", tcl_path.c_str(), true); |
| 110 | +- } |
| 111 | +- } |
| 112 | +-}; |
| 113 | +- |
| 114 | +-// Provide via global constructor. |
| 115 | +-static BazelInitializer bazel_initializer; |
| 116 | +-} // namespace |
| 117 | ++// Intentionally empty: tcl_library_init now handles Tcl setup. |
| 118 | ++// This file is kept because src/sta:opensta still references |
| 119 | ++// //bazel:runfiles in its srcs. |
| 120 | +diff --git a/bazel/tcl_library_init.cc b/bazel/tcl_library_init.cc |
| 121 | +index 4be9671..8306978 100644 |
| 122 | +--- a/bazel/tcl_library_init.cc |
| 123 | ++++ b/bazel/tcl_library_init.cc |
| 124 | +@@ -35,6 +35,16 @@ static std::optional<std::string> TclLibraryMountPoint(Tcl_Interp* interp) |
| 125 | + return Tcl_GetStringResult(interp); |
| 126 | + #else |
| 127 | + using rules_cc::cc::runfiles::Runfiles; |
| 128 | ++ |
| 129 | ++ // Clear inherited RUNFILES_* env vars: when OpenROAD is invoked by |
| 130 | ++ // a build system that previously ran another Bazel binary (e.g. a |
| 131 | ++ // Python wrapper), those variables point to the *other* binary's |
| 132 | ++ // runfiles tree. Runfiles::Create() checks env vars first, so it |
| 133 | ++ // would resolve paths in the wrong tree. Unsetting forces it to |
| 134 | ++ // fall back to Tcl_GetNameOfExecutable(), which is always correct. |
| 135 | ++ unsetenv("RUNFILES_DIR"); |
| 136 | ++ unsetenv("RUNFILES_MANIFEST_FILE"); |
| 137 | ++ |
| 138 | + std::string error; |
| 139 | + std::unique_ptr<Runfiles> runfiles(Runfiles::Create( |
| 140 | + Tcl_GetNameOfExecutable(), BAZEL_CURRENT_REPOSITORY, &error)); |
0 commit comments