Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added GetRunfilesDir() to be able to reliably determine prelude location under various invocation scenarios #1207

Merged
merged 19 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ cc_library(
],
)

cc_library(
name = "runfiles_util",
pk19604014 marked this conversation as resolved.
Show resolved Hide resolved
srcs = ["runfiles_util.cpp"],
hdrs = ["runfiles_util.h"],
deps = [
":check",
],
)

cc_test(
name = "runfiles_util_test",
srcs = ["runfiles_util_test.cpp"],
deps = [
":runfiles_util",
"@com_google_googletest//:gtest_main",
],
)

cc_library(
name = "string_helpers",
srcs = ["string_helpers.cpp"],
Expand Down
46 changes: 46 additions & 0 deletions common/runfiles_util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include "common/runfiles_util.h"

#include <filesystem>

#include "common/check.h"

#if defined(__APPLE__)
#include <mach-o/dyld.h>
#include <sys/param.h>
#include <sys/stat.h>
#endif

namespace Carbon {

static auto GetProgramPath() -> std::string {
pk19604014 marked this conversation as resolved.
Show resolved Hide resolved
std::string program_name;
#if defined(__APPLE__)
char ch = '\0';
uint32_t buf_size = 1;
// First call is to determine required buffer size.
CHECK(_NSGetExecutablePath(&ch, &buf_size) < 0);
std::unique_ptr<char[]> buf(new char[buf_size]);
CHECK(_NSGetExecutablePath(buf.get(), &buf_size) == 0);
program_name = buf.get();
#else
program_name = "/proc/self/exe";
#endif
std::error_code error;
program_name = std::filesystem::canonical(program_name, error);
CHECK(error.value() == 0);
llvm::errs() << "### program name=" << program_name << "\n";
return program_name;
}

auto GetRunfilesDir() -> std::string {
std::string runfiles_dir = GetProgramPath() + ".runfiles";
llvm::errs() << "### runfiles dir=" << runfiles_dir << "\n";
CHECK(std::filesystem::exists(runfiles_dir));
return runfiles_dir;
}

} // namespace Carbon
16 changes: 16 additions & 0 deletions common/runfiles_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#ifndef COMMON_RUNFILES_UTIL_H_
#define COMMON_RUNFILES_UTIL_H_

#include <string>

namespace Carbon {

// Returns bazel's runfiles directory.
auto GetRunfilesDir() -> std::string;

} // namespace Carbon
#endif // COMMON_RUNFILES_UTIL_H_
21 changes: 21 additions & 0 deletions common/runfiles_util_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include "common/runfiles_util.h"

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <string>

namespace Carbon::Testing {
namespace {

TEST(RunfilesUtilTest, GetRunfilesDir) {
EXPECT_THAT(GetRunfilesDir(),
testing::EndsWith("/common/runfiles_util_test.runfiles"));
}

} // namespace
} // namespace Carbon::Testing
1 change: 1 addition & 0 deletions executable_semantics/fuzzing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ cc_fuzz_test(
tags = ["manual"],
deps = [
":fuzzer_util",
"//common:runfiles_util",
"//common/fuzzing:carbon_cc_proto",
"//executable_semantics/interpreter:exec_program",
"//executable_semantics/syntax",
Expand Down
8 changes: 5 additions & 3 deletions executable_semantics/fuzzing/executable_semantics_fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <libprotobuf_mutator/src/libfuzzer/libfuzzer_macro.h>

#include "common/fuzzing/carbon.pb.h"
#include "common/runfiles_util.h"
#include "executable_semantics/fuzzing/fuzzer_util.h"
#include "executable_semantics/interpreter/exec_program.h"
#include "executable_semantics/syntax/parse.h"
Expand All @@ -25,10 +26,11 @@ void ParseAndExecute(const Fuzzing::CompilationUnit& compilation_unit) {
llvm::errs() << "Parsing failed: " << ast.error().message() << "\n";
return;
}
AddPrelude("executable_semantics/data/prelude.carbon", &arena,
&ast->declarations);
AddPrelude(
GetRunfilesDir() + "/carbon/executable_semantics/data/prelude.carbon",
pk19604014 marked this conversation as resolved.
Show resolved Hide resolved
&arena, &ast->declarations);
const ErrorOr<int> result =
ExecProgram(&arena, *ast, /*trace_stream=*/nullptr);
ExecProgram(&arena, *ast, /*trace_stream=*/std::nullopt);
if (!result.ok()) {
llvm::errs() << "Execution failed: " << result.error().message() << "\n";
return;
Expand Down