diff --git a/xls/common/file/filesystem_test.cc b/xls/common/file/filesystem_test.cc index 8f2e63d067..e62a3ee744 100644 --- a/xls/common/file/filesystem_test.cc +++ b/xls/common/file/filesystem_test.cc @@ -356,18 +356,17 @@ TEST(FilesystemTest, SetTextProtoFileFailsWhenRequiredFieldIsMissing) { } TEST(FilesystemTest, GetCurrentDirectoryReturnsCurrentDirectory) { - absl::StatusOr temp_dir = TempDirectory::Create(); - XLS_ASSERT_OK(temp_dir); - absl::StatusOr initial_cwd = GetCurrentDirectory(); - XLS_ASSERT_OK(initial_cwd); - ASSERT_EQ(0, chdir(temp_dir->path().c_str())); + XLS_ASSERT_OK_AND_ASSIGN(TempDirectory temp_dir, TempDirectory::Create()); + XLS_ASSERT_OK_AND_ASSIGN(std::filesystem::path initial_cwd, + GetCurrentDirectory()); + ASSERT_EQ(0, chdir(temp_dir.path().c_str())); - absl::StatusOr new_cwd = GetCurrentDirectory(); - XLS_ASSERT_OK(new_cwd); - EXPECT_EQ(temp_dir->path(), *new_cwd); + XLS_ASSERT_OK_AND_ASSIGN(std::filesystem::path new_cwd, + GetCurrentDirectory()); + EXPECT_TRUE(std::filesystem::equivalent(temp_dir.path(), new_cwd)); ASSERT_EQ(0, - chdir(initial_cwd->c_str())); // Change back to the original path. + chdir(initial_cwd.c_str())); // Change back to the original path. } TEST(FilesystemTest, GetDirectoryEntriesFailsWhenPathDoesNotExist) { @@ -421,6 +420,9 @@ TEST(FilesystemTest, GetDirectoryEntriesGivesRelativePathsWhenPathIsRelative) { } TEST(FilesystemTest, GetRealPath) { +#ifndef __linux__ + GTEST_SKIP() << "Skipping as procfs only available on linux"; +#endif /* __linux__ */ XLS_ASSERT_OK_AND_ASSIGN(std::filesystem::path link_path, GetRealPath("/proc/self/exe")); XLS_ASSERT_OK_AND_ASSIGN(std::filesystem::path real_path, diff --git a/xls/common/file/get_runfile_path.cc b/xls/common/file/get_runfile_path.cc index 3c16342e99..cd1c09c3e5 100644 --- a/xls/common/file/get_runfile_path.cc +++ b/xls/common/file/get_runfile_path.cc @@ -34,12 +34,37 @@ using ::bazel::tools::cpp::runfiles::Runfiles; static absl::Mutex mutex(absl::kConstInit); static Runfiles* runfiles; +#ifdef __APPLE__ +#include +#include +#endif /* __APPLE__ */ + +absl::StatusOr GetSelfExecutablePath() { +#if __linux__ + return GetRealPath("/proc/self/exe"); +#elif __APPLE__ + char path[PATH_MAX+1]; + uint32_t size = PATH_MAX; + if (_NSGetExecutablePath(path, &size) == 0) { + return std::filesystem::path(path); + } + return absl::InvalidArgumentError("Self path could not fit into buffer"); +#else +#error "Unknown platform" +#endif +} + absl::StatusOr GetRunfiles( - const std::string& argv0 = "/proc/self/exe") { + std::optional argv0 = std::nullopt) { absl::MutexLock lock(&mutex); if (runfiles == nullptr) { // Need to dereference the path, in case it's a link (as with the default). - XLS_ASSIGN_OR_RETURN(auto path, GetRealPath(argv0)); + std::filesystem::path path; + if (argv0.has_value()) { + XLS_ASSIGN_OR_RETURN(path, GetRealPath(std::string(argv0.value()))); + } else { + XLS_ASSIGN_OR_RETURN(path, GetSelfExecutablePath()); + } std::string error; runfiles = Runfiles::Create(path.string(), &error);