From c145fa5805183398d6eea4b7e657c120f35bbe06 Mon Sep 17 00:00:00 2001 From: Alex Light Date: Mon, 7 Jul 2025 14:47:36 -0700 Subject: [PATCH] Allow GetXlsRunfilePath to specify other projects. This allows for using runfiles from eg llvm. PiperOrigin-RevId: 780268269 --- xls/common/file/BUILD | 5 ++++- xls/common/file/get_runfile_path.cc | 4 ++-- xls/common/file/get_runfile_path.h | 2 +- xls/common/file/get_runfile_path_test.cc | 13 +++++++++++++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/xls/common/file/BUILD b/xls/common/file/BUILD index ac420872cf..a1c64ee964 100644 --- a/xls/common/file/BUILD +++ b/xls/common/file/BUILD @@ -120,7 +120,10 @@ cc_library( cc_test( name = "get_runfile_path_test", srcs = ["get_runfile_path_test.cc"], - data = ["get_runfile_path_test.cc"], + data = [ + "get_runfile_path_test.cc", + "@llvm-project//clang:builtin_headers_gen", + ], deps = [ ":filesystem", ":get_runfile_path", diff --git a/xls/common/file/get_runfile_path.cc b/xls/common/file/get_runfile_path.cc index 32185e33f6..bce9765e31 100644 --- a/xls/common/file/get_runfile_path.cc +++ b/xls/common/file/get_runfile_path.cc @@ -82,9 +82,9 @@ absl::StatusOr GetRunfiles( } // namespace absl::StatusOr GetXlsRunfilePath( - const std::filesystem::path& path) { + const std::filesystem::path& path, std::string package) { XLS_ASSIGN_OR_RETURN(Runfiles * runfiles, GetRunfiles()); - return runfiles->Rlocation("com_google_xls" / path); + return runfiles->Rlocation(package / path); } absl::Status InitRunfilesDir(const std::string& argv0) { diff --git a/xls/common/file/get_runfile_path.h b/xls/common/file/get_runfile_path.h index b8285c1b81..b9aa8f1fcd 100644 --- a/xls/common/file/get_runfile_path.h +++ b/xls/common/file/get_runfile_path.h @@ -30,7 +30,7 @@ namespace xls { // If the file does not exist as a runfile, this method may return an empty // path. absl::StatusOr GetXlsRunfilePath( - const std::filesystem::path& path); + const std::filesystem::path& path, std::string package = "com_google_xls"); // Called by InitXls; don't call this directly. Sets up global state for the // other functions in this file. diff --git a/xls/common/file/get_runfile_path_test.cc b/xls/common/file/get_runfile_path_test.cc index b1ee3627d8..8419af4988 100644 --- a/xls/common/file/get_runfile_path_test.cc +++ b/xls/common/file/get_runfile_path_test.cc @@ -28,8 +28,21 @@ namespace xls { namespace { using ::absl_testing::IsOkAndHolds; +using ::testing::AllOf; using ::testing::HasSubstr; +TEST(GetRunfilePathTest, GetXlsRunfilePathInLlvmReturnsTheRightPath) { + XLS_ASSERT_OK_AND_ASSIGN(std::filesystem::path runfile_path, + GetXlsRunfilePath("clang/staging/include/stddef.h", + /*package=*/"llvm-project")); + absl::StatusOr test_h_file_contents = + GetFileContents(runfile_path); + + EXPECT_THAT(test_h_file_contents, + IsOkAndHolds(AllOf(HasSubstr("stddef.h"), + HasSubstr("Part of the LLVM Project")))); +} + TEST(GetRunfilePathTest, GetXlsRunfilePathReturnsTheRightPath) { XLS_ASSERT_OK_AND_ASSIGN( std::filesystem::path runfile_path,