Skip to content

Commit

Permalink
Move C++ runfiles library out of Bazel
Browse files Browse the repository at this point in the history
This requires adding `@rules_cc//cc/runfiles` to the list of targets that `BAZEL_REPOSITORY_NAME` is defined for.

Closes bazelbuild#24393.

PiperOrigin-RevId: 699127359
Change-Id: Iff0b961ec5ff27daf8e96e49acc5d57a1b9c4274

(cherry picked from commit 8a5e70c)
  • Loading branch information
fmeum committed Dec 13, 2024
1 parent df9068c commit 8a1f42b
Show file tree
Hide file tree
Showing 32 changed files with 126 additions and 1,725 deletions.
1 change: 0 additions & 1 deletion .bazelci/postsubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ tasks:
- "//tools/aquery_differ/..."
- "//tools/bash/..."
- "//tools/build_defs/..."
- "//tools/cpp/runfiles/..."
- "//tools/java/..."
- "//tools/jdk/..."
- "//tools/python/..."
Expand Down
1 change: 0 additions & 1 deletion .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ tasks:
- "//tools/aquery_differ/..."
- "//tools/bash/..."
- "//tools/build_defs/..."
- "//tools/cpp/runfiles/..."
- "//tools/java/..."
- "//tools/jdk/..."
- "//tools/python/..."
Expand Down
8 changes: 6 additions & 2 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bazel_dep(name = "stardoc", version = "0.7.1", repo_name = "io_bazel_skydoc")
bazel_dep(name = "zstd-jni", version = "1.5.2-3.bcr.1")
bazel_dep(name = "blake3", version = "1.5.1.bcr.1")
bazel_dep(name = "zlib", version = "1.3.1.bcr.3")
bazel_dep(name = "rules_cc", version = "0.0.16")
bazel_dep(name = "rules_cc", version = "0.0.17")
bazel_dep(name = "rules_java", version = "8.6.1")
bazel_dep(name = "rules_graalvm", version = "0.11.1")
bazel_dep(name = "rules_proto", version = "7.0.2")
Expand Down Expand Up @@ -263,7 +263,11 @@ use_repo(
# =========================================

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(python_version = "3.11", is_default = True, ignore_root_user_error = True)
python.toolchain(
ignore_root_user_error = True,
is_default = True,
python_version = "3.11",
)

pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
pip.parse(
Expand Down
3 changes: 2 additions & 1 deletion MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions scripts/bootstrap/compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,8 @@ EOF

# Create @bazel_tools//tools/cpp/runfiles
mkdir -p ${BAZEL_TOOLS_REPO}/tools/cpp/runfiles
link_file "${PWD}/tools/cpp/runfiles/runfiles_src.h" \
link_file "${PWD}/tools/cpp/runfiles/runfiles.h" \
"${BAZEL_TOOLS_REPO}/tools/cpp/runfiles/runfiles.h"
# Transform //tools/cpp/runfiles:runfiles_src.cc to
# @bazel_tools//tools/cpp/runfiles:runfiles.cc
# Keep this transformation logic in sync with the
# //tools/cpp/runfiles:srcs_for_embedded_tools genrule.
sed 's|^#include.*/runfiles_src.h.*|#include \"tools/cpp/runfiles/runfiles.h\"|' \
"${PWD}/tools/cpp/runfiles/runfiles_src.cc" > \
"${BAZEL_TOOLS_REPO}/tools/cpp/runfiles/runfiles.cc"
link_file "${PWD}/tools/cpp/runfiles/BUILD.tools" \
"${BAZEL_TOOLS_REPO}/tools/cpp/runfiles/BUILD"

Expand Down
2 changes: 1 addition & 1 deletion src/MODULE.tools
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use_repo(buildozer_binary, "buildozer_binary")
# See also: --incompatible_autoload_externally, AutoloadSymbols
bazel_dep(name = "protobuf", version = "29.0", repo_name = "com_google_protobuf")
bazel_dep(name = "rules_java", version = "8.6.1")
bazel_dep(name = "rules_cc", version = "0.0.16")
bazel_dep(name = "rules_cc", version = "0.0.17")
bazel_dep(name = "rules_python", version = "0.40.0")
bazel_dep(name = "rules_shell", version = "0.2.0")
# add rules_android
Expand Down
2 changes: 0 additions & 2 deletions src/create_embedded_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
('*tools/j2objc/BUILD.tools', lambda x: 'tools/j2objc/BUILD'),
('*tools/platforms/*', lambda x: 'platforms/' + os.path.basename(x)),
('*tools/cpp/BUILD.tools', lambda x: 'tools/cpp/BUILD'),
('*tools/cpp/runfiles/generated_*',
lambda x: 'tools/cpp/runfiles/' + os.path.basename(x)[len('generated_'):]),
('*launcher.exe', lambda x: 'tools/launcher/launcher.exe'),
('*launcher_maker.exe', lambda x: 'tools/launcher/launcher_maker.exe'),
('*def_parser.exe', lambda x: 'tools/def_parser/def_parser.exe'),
Expand Down
1 change: 0 additions & 1 deletion src/main/cpp/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ cc_library(
"//src/tools/launcher:__subpackages__",
"//src/tools/singlejar:__pkg__",
"//third_party/def_parser:__pkg__",
"//tools/cpp/runfiles:__pkg__",
"//tools/test:__pkg__",
],
deps = [
Expand Down
5 changes: 3 additions & 2 deletions src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -956,11 +956,12 @@ def _is_stamping_enabled_for_aspect(ctx):
stamp = ctx.rule.attr.stamp
return stamp

_RUNFILES_LIBRARY_TARGET = Label("@bazel_tools//tools/cpp/runfiles")
_RUNFILES_LIBRARY_TARGET = Label("@rules_cc//cc/runfiles")
_LEGACY_RUNFILES_LIBRARY_TARGET = Label("@bazel_tools//tools/cpp/runfiles")

def _get_local_defines_for_runfiles_lookup(ctx, all_deps):
for dep in all_deps:
if dep.label == _RUNFILES_LIBRARY_TARGET:
if dep.label == _RUNFILES_LIBRARY_TARGET or dep.label == _LEGACY_RUNFILES_LIBRARY_TARGET:
return ["BAZEL_CURRENT_REPOSITORY=\"{}\"".format(ctx.label.workspace_name)]
return []

Expand Down
2 changes: 1 addition & 1 deletion src/test/native/windows/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ cc_test(
"//src/main/cpp/util:filesystem",
"//src/main/native/windows:lib-file",
"//src/main/native/windows:lib-process",
"@bazel_tools//tools/cpp/runfiles",
"@rules_cc//cc/runfiles",
],
"//conditions:default": [],
}),
Expand Down
6 changes: 3 additions & 3 deletions src/test/native/windows/process_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "gtest/gtest.h"
#include "src/main/cpp/util/path.h"
#include "src/main/native/windows/util.h"
#include "tools/cpp/runfiles/runfiles.h"
#include "rules_cc/cc/runfiles/runfiles.h"

namespace {

Expand All @@ -49,8 +49,8 @@ void AssertSubprocessReceivesArgsAsIntended(

// Create a Runfiles object.
std::string error;
std::unique_ptr<bazel::tools::cpp::runfiles::Runfiles> runfiles(
bazel::tools::cpp::runfiles::Runfiles::CreateForTest(&error));
std::unique_ptr<rules_cc::cc::runfiles::Runfiles> runfiles(
rules_cc::cc::runfiles::Runfiles::CreateForTest(&error));
ASSERT_NE(runfiles.get(), nullptr) << error;

// Look up the path of the printarg.exe utility.
Expand Down
54 changes: 53 additions & 1 deletion src/test/py/bazel/bzlmod/bazel_repo_mapping_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def testBashRunfilesLibraryRepoMapping(self):
env_add={'RUNFILES_LIB_DEBUG': '1'},
)

def testCppRunfilesLibraryRepoMapping(self):
def testLegacyCppRunfilesLibraryRepoMapping(self):
self.main_registry.setModuleBasePath('projects')
projects_dir = self.main_registry.projects

Expand Down Expand Up @@ -311,6 +311,58 @@ def testCppRunfilesLibraryRepoMapping(self):
# Run unsandboxed on all platforms.
self.RunBazel(['run', '@test//:test'])

def testCppRunfilesLibraryRepoMapping(self):
self.main_registry.setModuleBasePath('projects')
projects_dir = self.main_registry.projects

self.main_registry.createLocalPathModule('data', '1.0', 'data')
scratchFile(projects_dir.joinpath('data', 'foo.txt'), ['hello'])
scratchFile(
projects_dir.joinpath('data', 'BUILD'), ['exports_files(["foo.txt"])']
)

self.main_registry.createLocalPathModule(
'test', '1.0', 'test', {'data': '1.0', 'rules_cc': '0.0.17'}
)
scratchFile(
projects_dir.joinpath('test', 'BUILD'),
[
'cc_test(',
' name = "test",',
' srcs = ["test.cpp"],',
' data = ["@data//:foo.txt"],',
' args = ["$(rlocationpath @data//:foo.txt)"],',
' deps = ["@rules_cc//cc/runfiles"],',
')',
],
)
scratchFile(
projects_dir.joinpath('test', 'test.cpp'),
[
'#include <cstdlib>',
'#include <fstream>',
'#include "rules_cc/cc/runfiles/runfiles.h"',
'using rules_cc::cc::runfiles::Runfiles;',
'int main(int argc, char** argv) {',
(
' Runfiles* runfiles = Runfiles::Create(argv[0],'
' BAZEL_CURRENT_REPOSITORY);'
),
' std::ifstream f1(runfiles->Rlocation(argv[1]));',
' if (!f1.good()) std::exit(1);',
' std::ifstream f2(runfiles->Rlocation("data/foo.txt"));',
' if (!f2.good()) std::exit(2);',
'}',
],
)

self.ScratchFile('MODULE.bazel', ['bazel_dep(name="test",version="1.0")'])

# Run sandboxed on Linux and macOS.
self.RunBazel(['test', '@test//:test', '--test_output=errors'])
# Run unsandboxed on all platforms.
self.RunBazel(['run', '@test//:test'])

def testJavaRunfilesLibraryRepoMapping(self):
self.main_registry.setModuleBasePath('projects')
projects_dir = self.main_registry.projects
Expand Down
1 change: 1 addition & 0 deletions src/test/shell/bazel/testdata/embedded_tools_srcs_deps
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@
//tools/cpp/modules_tools:common
//tools/cpp/modules_tools:generate-modmap
//tools/cpp/modules_tools:generate-modmap-lib
@@rules_cc+//cc/runfiles:runfiles
3 changes: 2 additions & 1 deletion src/test/tools/bzlmod/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/tools/launcher/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ win_cc_test(
deps = [
":util",
"//src/main/native/windows:lib-file",
"@bazel_tools//tools/cpp/runfiles",
"@com_google_googletest//:gtest_main",
],
)
Expand Down
4 changes: 1 addition & 3 deletions src/tools/launcher/util/launcher_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@
#include <string>
#include <vector>

#include "gtest/gtest.h"
#include "src/main/cpp/util/path_platform.h"
#include "src/main/cpp/util/strings.h"
#include "src/main/native/windows/util.h"
#include "tools/cpp/runfiles/runfiles.h"
#include "gtest/gtest.h"

namespace bazel {
namespace launcher {

using bazel::tools::cpp::runfiles::Runfiles;
using std::getenv;
using std::ios;
using std::ofstream;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/singlejar/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,8 @@ cc_library(
hdrs = ["test_util.h"],
deps = [
"//src/main/cpp/util",
"@bazel_tools//tools/cpp/runfiles",
"@com_google_googletest//:gtest_main",
"@rules_cc//cc/runfiles",
],
)

Expand Down
2 changes: 1 addition & 1 deletion src/tools/singlejar/combiners_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace {

using bazel::tools::cpp::runfiles::Runfiles;
using rules_cc::cc::runfiles::Runfiles;

static const char kTag1Contents[] = "<tag1>Contents1</tag1>";
static const char kTag2Contents[] = "<tag2>Contents2</tag2>";
Expand Down
2 changes: 1 addition & 1 deletion src/tools/singlejar/input_jar_empty_jar_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#include "src/tools/singlejar/test_util.h"
#include "googletest/include/gtest/gtest.h"

using bazel::tools::cpp::runfiles::Runfiles;
using rules_cc::cc::runfiles::Runfiles;
using singlejar_test_util::AllocateFile;
using singlejar_test_util::OutputFilePath;
using singlejar_test_util::RunCommand;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/singlejar/input_jar_preambled_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

namespace {

using bazel::tools::cpp::runfiles::Runfiles;
using rules_cc::cc::runfiles::Runfiles;

void Verify(const std::string &path) {
InputJar input_jar;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/singlejar/input_jar_scan_jartool_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include "src/tools/singlejar/input_jar_scan_entries_test.h"

using bazel::tools::cpp::runfiles::Runfiles;
using rules_cc::cc::runfiles::Runfiles;

class JartoolCreator {
public:
Expand Down
2 changes: 1 addition & 1 deletion src/tools/singlejar/output_huge_jar_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

namespace {

using bazel::tools::cpp::runfiles::Runfiles;
using rules_cc::cc::runfiles::Runfiles;
using singlejar_test_util::AllocateFile;
using singlejar_test_util::OutputFilePath;
using singlejar_test_util::VerifyZip;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/singlejar/output_jar_simple_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

namespace {

using bazel::tools::cpp::runfiles::Runfiles;
using rules_cc::cc::runfiles::Runfiles;
using singlejar_test_util::CreateTextFile;
using singlejar_test_util::GetEntryContents;
using singlejar_test_util::OutputFilePath;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/singlejar/test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include <string>

#include "tools/cpp/runfiles/runfiles.h"
#include "rules_cc/cc/runfiles/runfiles.h"

namespace singlejar_test_util {
using std::string;
Expand Down
46 changes: 1 addition & 45 deletions tools/cpp/runfiles/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,7 @@ filegroup(
name = "embedded_tools",
srcs = [
"BUILD.tools",
":srcs_for_embedded_tools",
"runfiles.h",
],
visibility = ["//tools/cpp:__pkg__"],
)

# Rewrite the include path for runfiles.h in runfiles_src.cc, and create
# "generated_runfiles.{h,cc}". These files are renamed to "runfiles.{h,cc}" as
# part of creating the embedded tools of Bazel.
#
# We cannot just check in runfiles_src.{h,cc} as runfiles.{h,cc}, because it'd
# cause a header check failure on Windows when building targets in the Bazel
# source tree, if those targets depend on @bazel_tools//tools/cpp/runfiles,
# because due to lack of sandboxing they would accidentally pick up runfiles.h
# from //tools/cpp/runfiles.
genrule(
name = "srcs_for_embedded_tools",
srcs = [
"runfiles_src.cc",
"runfiles_src.h",
],
outs = [
"generated_runfiles.cc",
"generated_runfiles.h",
],
# Keep this transformation logic in sync with the
# //scripts/bootstrap/compile.sh
cmd = ("sed " +
" 's|^#include.*/runfiles_src.h.*|#include \"tools/cpp/runfiles/runfiles.h\"|' " +
" $(location runfiles_src.cc) > $(location generated_runfiles.cc) && " +
"cp $(location runfiles_src.h) $(location generated_runfiles.h)"),
)

cc_library(
name = "runfiles",
testonly = 1,
srcs = ["runfiles_src.cc"],
hdrs = ["runfiles_src.h"],
)

cc_test(
name = "runfiles_test",
srcs = ["runfiles_test.cc"],
visibility = ["//visibility:public"],
deps = [
":runfiles",
"@com_google_googletest//:gtest_main",
],
)
4 changes: 1 addition & 3 deletions tools/cpp/runfiles/BUILD.tools
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# This package will host the C++ runfiles library when it's finally released.

cc_library(
name = "runfiles",
srcs = ["runfiles.cc"],
hdrs = ["runfiles.h"],
deps = ["@rules_cc//cc/runfiles"],
visibility = ["//visibility:public"],
)
Loading

0 comments on commit 8a1f42b

Please sign in to comment.