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

Refactor testing exe path and benchmark main handling. #4216

Merged
merged 4 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 1 addition & 15 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,6 @@ cc_library(
],
)

cc_library(
name = "benchmark_main",
srcs = ["benchmark_main.cpp"],
hdrs = ["benchmark_main.h"],
deps = [
":check",
":exe_path",
":init_llvm",
"@abseil-cpp//absl/flags:parse",
"@google_benchmark//:benchmark",
"@llvm-project//llvm:Support",
],
)

cc_library(
name = "command_line",
srcs = ["command_line.cpp"],
Expand Down Expand Up @@ -196,9 +182,9 @@ cc_binary(
testonly = 1,
srcs = ["hashing_benchmark.cpp"],
deps = [
":benchmark_main",
":check",
":hashing",
"//testing/base:benchmark_main",
"@abseil-cpp//absl/hash",
"@abseil-cpp//absl/random",
"@google_benchmark//:benchmark",
Expand Down
48 changes: 39 additions & 9 deletions testing/base/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,28 @@ cc_library(
name = "gtest_main",
testonly = 1,
srcs = ["gtest_main.cpp"],
hdrs = ["gtest_main.h"],
deps = [
"//common:check",
"//common:exe_path",
":testing_exe_path",
"//common:init_llvm",
"@googletest//:gtest",
"@llvm-project//llvm:Support",
],
)

cc_test(
name = "gtest_main_test",
size = "small",
srcs = ["gtest_main_test.cpp"],
# This does extra initialization on top of Google benchmark's main in order to
# provide stack traces and setup LLVM.
#
# This replaces `@google_benchmark//:benchmark_main`;
# `@google_benchmark//:benchmark` should still be used directly.
cc_library(
name = "benchmark_main",
testonly = 1,
srcs = ["benchmark_main.cpp"],
deps = [
":gtest_main",
"@googletest//:gtest",
":testing_exe_path",
"//common:init_llvm",
"@abseil-cpp//absl/flags:parse",
"@google_benchmark//:benchmark",
"@llvm-project//llvm:Support",
],
)
Expand All @@ -62,6 +67,7 @@ cc_test(
deps = [
":gtest_main",
":source_gen_lib",
":testing_exe_path",
"//common:set",
"//toolchain/driver",
"@googletest//:gtest",
Expand Down Expand Up @@ -104,3 +110,27 @@ cc_test(
"@googletest//:gtest",
],
)

cc_library(
name = "testing_exe_path",
testonly = 1,
srcs = ["testing_exe_path.cpp"],
chandlerc marked this conversation as resolved.
Show resolved Hide resolved
hdrs = ["testing_exe_path.h"],
deps = [
"//common:check",
"//common:exe_path",
"@llvm-project//llvm:Support",
],
)

cc_test(
name = "testing_exe_path_test",
size = "small",
srcs = ["testing_exe_path_test.cpp"],
deps = [
":gtest_main",
":testing_exe_path",
"@googletest//:gtest",
"@llvm-project//llvm:Support",
],
)
27 changes: 2 additions & 25 deletions common/benchmark_main.cpp → testing/base/benchmark_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,17 @@
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include "common/benchmark_main.h"

#include <benchmark/benchmark.h>

#include <string>

#include "absl/flags/parse.h"
#include "common/check.h"
#include "common/exe_path.h"
#include "common/init_llvm.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"

static bool after_main = false;
static llvm::StringRef exe_path;

namespace Carbon::Testing {

auto GetBenchmarkExePath() -> llvm::StringRef {
CARBON_CHECK(after_main)
<< "Must not query the executable path until after `main` is entered!";
return exe_path;
}

} // namespace Carbon::Testing
#include "testing/base/testing_exe_path.h"

// TODO: Refactor this to share code with `gtest_main.cpp`.
auto main(int orig_argc, char** orig_argv) -> int {
// Do LLVM's initialization first, this will also transform UTF-16 to UTF-8.
Carbon::InitLLVM init_llvm(orig_argc, orig_argv);

std::string exe_path_storage = Carbon::FindExecutablePath(orig_argv[0]);
exe_path = exe_path_storage;
after_main = true;
Carbon::Testing::SetExePath(orig_argv[0]);

// Inject a flag to override the defaults for benchmarks. This can still be
// disabled by user arguments.
Expand Down
27 changes: 4 additions & 23 deletions testing/base/gtest_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,16 @@
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include "testing/base/gtest_main.h"

#include <gtest/gtest.h>

#include <string>

#include "common/check.h"
#include "common/exe_path.h"
#include "common/init_llvm.h"

static bool after_main = false;
static llvm::StringRef exe_path;

namespace Carbon::Testing {

auto GetTestExePath() -> llvm::StringRef {
CARBON_CHECK(after_main)
<< "Must not query the executable path until after `main` is entered!";
return exe_path;
}

} // namespace Carbon::Testing
#include "testing/base/testing_exe_path.h"

auto main(int argc, char** argv) -> int {
std::string exe_path_storage = Carbon::FindExecutablePath(argv[0]);
exe_path = exe_path_storage;
after_main = true;

// Initialize LLVM first, as that will also handle ensuring UTF-8 encoding.
Carbon::InitLLVM init_llvm(argc, argv);

Carbon::Testing::SetExePath(argv[0]);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
4 changes: 2 additions & 2 deletions testing/base/source_gen_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <gtest/gtest.h>

#include "common/set.h"
#include "testing/base/gtest_main.h"
#include "testing/base/testing_exe_path.h"
#include "toolchain/driver/driver.h"

namespace Carbon::Testing {
Expand Down Expand Up @@ -144,7 +144,7 @@ TEST(SourceGenTest, UniqueIdentifiers) {
auto TestCompile(llvm::StringRef source) -> bool {
llvm::vfs::InMemoryFileSystem fs;
InstallPaths installation(
InstallPaths::MakeForBazelRunfiles(Testing::GetTestExePath()));
InstallPaths::MakeForBazelRunfiles(Testing::GetExePath()));
Driver driver(fs, &installation, llvm::outs(), llvm::errs());

// Load the prelude into our VFS.
Expand Down
14 changes: 10 additions & 4 deletions testing/base/gtest_main.h → testing/base/testing_exe_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#ifndef CARBON_TESTING_BASE_GTEST_MAIN_H_
#define CARBON_TESTING_BASE_GTEST_MAIN_H_
#ifndef CARBON_TESTING_BASE_TESTING_EXE_PATH_H_
chandlerc marked this conversation as resolved.
Show resolved Hide resolved
#define CARBON_TESTING_BASE_TESTING_EXE_PATH_H_

#include "llvm/ADT/StringRef.h"

Expand All @@ -13,8 +13,14 @@
namespace Carbon::Testing {

// The executable path of the test binary.
auto GetTestExePath() -> llvm::StringRef;
auto GetExePath() -> llvm::StringRef;

// Sets the executable path of a test binary from its `argv[0]`.
//
// This function must only be called once for an execution, and before any
// callers to `GetExePath`. Typically, it is called from within `main`.
auto SetExePath(const char* argv_zero) -> void;

} // namespace Carbon::Testing

#endif // CARBON_TESTING_BASE_GTEST_MAIN_H_
#endif // CARBON_TESTING_BASE_TESTING_EXE_PATH_H_
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include "testing/base/gtest_main.h"
#include "testing/base/testing_exe_path.h"

#include <gmock/gmock.h>
#include <gtest/gtest.h>
Expand All @@ -15,7 +15,7 @@ namespace {
using ::testing::StrNe;

TEST(TestExePathTest, Test) {
llvm::StringRef exe_path = GetTestExePath();
llvm::StringRef exe_path = GetExePath();
EXPECT_THAT(exe_path, StrNe(""));
EXPECT_TRUE(llvm::sys::fs::exists(exe_path));
EXPECT_TRUE(llvm::sys::fs::can_execute(exe_path));
Expand Down
6 changes: 5 additions & 1 deletion toolchain/driver/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ cc_test(
"//common:ostream",
"//testing/base:gtest_main",
"//testing/base:test_raw_ostream",
"//testing/base:testing_exe_path",
"@googletest//:gtest",
"@llvm-project//llvm:Object",
"@llvm-project//llvm:Support",
Expand All @@ -58,8 +59,10 @@ cc_binary(
srcs = ["compile_benchmark.cpp"],
deps = [
":driver",
"//common:benchmark_main",
"//testing/base:benchmark_main",
"//testing/base:source_gen_lib",
"//testing/base:testing_exe_path",
"@google_benchmark//:benchmark",
"@llvm-project//llvm:Support",
],
)
Expand Down Expand Up @@ -112,6 +115,7 @@ cc_test(
"//common:all_llvm_targets",
"//testing/base:gtest_main",
"//testing/base:test_raw_ostream",
"//testing/base:testing_exe_path",
"//toolchain/diagnostics:diagnostic_emitter",
"//toolchain/install:install_paths",
"//toolchain/lex:tokenized_buffer_test_helpers",
Expand Down
6 changes: 3 additions & 3 deletions toolchain/driver/clang_runner_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/Program.h"
#include "llvm/TargetParser/Host.h"
#include "testing/base/gtest_main.h"
#include "testing/base/test_raw_ostream.h"
#include "testing/base/testing_exe_path.h"

namespace Carbon {
namespace {
Expand Down Expand Up @@ -56,7 +56,7 @@ static auto RunWithCapturedOutput(std::string& out, std::string& err,
TEST(ClangRunnerTest, Version) {
TestRawOstream test_os;
const auto install_paths =
InstallPaths::MakeForBazelRunfiles(Testing::GetTestExePath());
InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
std::string target = llvm::sys::getDefaultTargetTriple();
ClangRunner runner(&install_paths, target, &test_os);

Expand Down Expand Up @@ -125,7 +125,7 @@ TEST(ClangRunnerTest, LinkCommandEcho) {
std::filesystem::path bar_file = WriteTestFile("bar.o", "");

const auto install_paths =
InstallPaths::MakeForBazelRunfiles(Testing::GetTestExePath());
InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
std::string verbose_out;
llvm::raw_string_ostream verbose_os(verbose_out);
std::string target = llvm::sys::getDefaultTargetTriple();
Expand Down
5 changes: 2 additions & 3 deletions toolchain/driver/compile_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

#include <string>

#include "common/benchmark_main.h"
#include "testing/base/source_gen.h"
#include "testing/base/testing_exe_path.h"
#include "toolchain/driver/driver.h"

namespace Carbon::Testing {
Expand All @@ -20,8 +20,7 @@ namespace {
class CompileBenchmark {
public:
CompileBenchmark()
: installation_(
InstallPaths::MakeForBazelRunfiles(GetBenchmarkExePath())),
: installation_(InstallPaths::MakeForBazelRunfiles(GetExePath())),
driver_(fs_, &installation_, llvm::outs(), llvm::errs()) {
// Load the prelude into our VFS.
//
Expand Down
4 changes: 2 additions & 2 deletions toolchain/driver/driver_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#include "llvm/ADT/ScopeExit.h"
#include "llvm/Object/Binary.h"
#include "llvm/Support/FormatVariadic.h"
#include "testing/base/gtest_main.h"
#include "testing/base/test_raw_ostream.h"
#include "testing/base/testing_exe_path.h"
#include "toolchain/testing/yaml_test_helpers.h"

namespace Carbon {
Expand Down Expand Up @@ -43,7 +43,7 @@ class DriverTest : public testing::Test {
protected:
DriverTest()
: installation_(
InstallPaths::MakeForBazelRunfiles(Testing::GetTestExePath())),
InstallPaths::MakeForBazelRunfiles(Testing::GetExePath())),
driver_(fs_, &installation_, test_output_stream_, test_error_stream_) {
char* tmpdir_env = getenv("TEST_TMPDIR");
CARBON_CHECK(tmpdir_env != nullptr);
Expand Down
1 change: 1 addition & 0 deletions toolchain/install/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ cc_test(
"//common:check",
"//common:ostream",
"//testing/base:gtest_main",
"//testing/base:testing_exe_path",
"@bazel_tools//tools/cpp/runfiles",
"@googletest//:gtest",
"@llvm-project//llvm:Support",
Expand Down
7 changes: 3 additions & 4 deletions toolchain/install/install_paths_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/Path.h"
#include "testing/base/gtest_main.h"
#include "testing/base/testing_exe_path.h"
#include "tools/cpp/runfiles/runfiles.h"

namespace Carbon {
Expand All @@ -28,8 +28,7 @@ class InstallPathsTest : public ::testing::Test {
protected:
InstallPathsTest() {
std::string error;
test_runfiles_.reset(
Runfiles::Create(Testing::GetTestExePath().str(), &error));
test_runfiles_.reset(Runfiles::Create(Testing::GetExePath().str(), &error));
CARBON_CHECK(test_runfiles_ != nullptr) << error;
}

Expand Down Expand Up @@ -102,7 +101,7 @@ TEST_F(InstallPathsTest, PrefixRootExplicit) {
}

TEST_F(InstallPathsTest, TestRunfiles) {
auto paths = InstallPaths::MakeForBazelRunfiles(Testing::GetTestExePath());
auto paths = InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
ASSERT_THAT(paths.error(), Eq(std::nullopt)) << *paths.error();
TestInstallPaths(paths);
}
Expand Down
Loading
Loading