lld_runner_test.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/driver/lld_runner.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <filesystem>
  8. #include <fstream>
  9. #include <string>
  10. #include <tuple>
  11. #include <utility>
  12. #include "common/check.h"
  13. #include "common/ostream.h"
  14. #include "common/raw_string_ostream.h"
  15. #include "llvm/ADT/ScopeExit.h"
  16. #include "llvm/Object/Binary.h"
  17. #include "llvm/Support/FormatVariadic.h"
  18. #include "llvm/Support/Program.h"
  19. #include "llvm/TargetParser/Host.h"
  20. #include "testing/base/capture_std_streams.h"
  21. #include "testing/base/file_helpers.h"
  22. #include "testing/base/global_exe_path.h"
  23. #include "toolchain/driver/clang_runner.h"
  24. namespace Carbon {
  25. namespace {
  26. using ::testing::HasSubstr;
  27. using ::testing::Not;
  28. using ::testing::StrEq;
  29. TEST(LldRunnerTest, Version) {
  30. RawStringOstream test_os;
  31. const auto install_paths =
  32. InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  33. LldRunner runner(&install_paths, &test_os);
  34. std::string out;
  35. std::string err;
  36. EXPECT_TRUE(Testing::CallWithCapturedOutput(
  37. out, err, [&] { return runner.ElfLink({"--version"}); }));
  38. // The arguments to LLD should be part of the verbose log.
  39. EXPECT_THAT(test_os.TakeStr(), HasSubstr("--version"));
  40. // Nothing should print to stderr here.
  41. EXPECT_THAT(err, StrEq(""));
  42. // We don't care about any particular version, just that it is printed.
  43. EXPECT_THAT(out, HasSubstr("LLD"));
  44. // Check that it was in fact the GNU linker.
  45. EXPECT_THAT(out, HasSubstr("compatible with GNU linkers"));
  46. // Try the Darwin linker.
  47. EXPECT_TRUE(Testing::CallWithCapturedOutput(
  48. out, err, [&] { return runner.MachOLink({"--version"}); }));
  49. // Again, the arguments to LLD should be part of the verbose log.
  50. EXPECT_THAT(test_os.TakeStr(), HasSubstr("--version"));
  51. // Nothing should print to stderr.
  52. EXPECT_THAT(err, StrEq(""));
  53. // We don't care about any particular version.
  54. EXPECT_THAT(out, HasSubstr("LLD"));
  55. // The Darwin link code path doesn't print anything distinct, so instead check
  56. // that the GNU output isn't repeated.
  57. EXPECT_THAT(out, Not(HasSubstr("GNU")));
  58. }
  59. static auto CompileTwoSources(const InstallPaths& install_paths,
  60. llvm::StringRef target)
  61. -> std::pair<std::filesystem::path, std::filesystem::path> {
  62. std::filesystem::path test_a_file =
  63. *Testing::WriteTestFile("test_a.cpp", "int test_a() { return 0; }");
  64. std::filesystem::path test_b_file = *Testing::WriteTestFile(
  65. "test_b.cpp", "int test_a();\nint main() { return test_a(); }");
  66. std::filesystem::path test_a_output = *Testing::WriteTestFile("test_a.o", "");
  67. std::filesystem::path test_b_output = *Testing::WriteTestFile("test_b.o", "");
  68. // First compile the two source files to `.o` files with Clang.
  69. RawStringOstream verbose_out;
  70. auto vfs = llvm::vfs::getRealFileSystem();
  71. ClangRunner clang(&install_paths, target, vfs, &verbose_out);
  72. std::string target_arg = llvm::formatv("--target={0}", target).str();
  73. std::string out;
  74. std::string err;
  75. CARBON_CHECK(
  76. Testing::CallWithCapturedOutput(
  77. out, err,
  78. [&] {
  79. return clang.Run({target_arg, "-fPIE", "-c", test_a_file.string(),
  80. "-o", test_a_output.string()});
  81. }),
  82. "Verbose output from runner:\n{0}\nStderr:\n{1}\n", verbose_out.TakeStr(),
  83. err);
  84. verbose_out.clear();
  85. CARBON_CHECK(
  86. Testing::CallWithCapturedOutput(
  87. out, err,
  88. [&] {
  89. return clang.Run({target_arg, "-fPIE", "-c", test_b_file.string(),
  90. "-o", test_b_output.string()});
  91. }),
  92. "Verbose output from runner:\n{0}\nStderr:\n{1}\n", verbose_out.TakeStr(),
  93. err);
  94. verbose_out.clear();
  95. return {test_a_output, test_b_output};
  96. }
  97. TEST(LldRunnerTest, ElfLinkTest) {
  98. const auto install_paths =
  99. InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  100. std::filesystem::path test_a_output;
  101. std::filesystem::path test_b_output;
  102. std::tie(test_a_output, test_b_output) =
  103. CompileTwoSources(install_paths, "aarch64-unknown-linux");
  104. std::filesystem::path test_output = *Testing::WriteTestFile("test.o", "");
  105. RawStringOstream verbose_out;
  106. std::string out;
  107. std::string err;
  108. LldRunner lld(&install_paths, &verbose_out);
  109. // Link the two object files together.
  110. //
  111. // TODO: Currently, this uses a relocatable link, but it would be better to do
  112. // a full link to an executable. For that to work, we need at least the
  113. // C-runtime built artifacts available in the toolchain. We should revisit
  114. // this once we have those in place. This also prevents us from testing a
  115. // failed link easily.
  116. EXPECT_TRUE(Testing::CallWithCapturedOutput(
  117. out, err,
  118. [&] {
  119. return lld.ElfLink({"-m", "aarch64linux", "--relocatable", "-o",
  120. test_output.string(), test_a_output.string(),
  121. test_b_output.string()});
  122. }))
  123. << "Verbose output from runner:\n"
  124. << verbose_out.TakeStr() << "\n";
  125. verbose_out.clear();
  126. // No output should be produced.
  127. EXPECT_THAT(out, StrEq(""));
  128. EXPECT_THAT(err, StrEq(""));
  129. }
  130. TEST(LldRunnerTest, MachOLinkTest) {
  131. const auto install_paths =
  132. InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  133. std::filesystem::path test_a_output;
  134. std::filesystem::path test_b_output;
  135. std::tie(test_a_output, test_b_output) =
  136. CompileTwoSources(install_paths, "arm64-unknown-macosx10.4.0");
  137. std::filesystem::path test_output = *Testing::WriteTestFile("test.o", "");
  138. RawStringOstream verbose_out;
  139. std::string out;
  140. std::string err;
  141. // Link the two object files together.
  142. //
  143. // This is a somewhat arbitrary command line, and is missing the C-runtimes,
  144. // but seems to succeed currently. The goal isn't to test any *particular*
  145. // link, but just than an actual link occurs successfully.
  146. LldRunner lld(&install_paths, &verbose_out);
  147. EXPECT_TRUE(Testing::CallWithCapturedOutput(
  148. out, err,
  149. [&] {
  150. return lld.MachOLink({"-arch", "arm64", "-platform_version", "macos",
  151. "10.4.0", "10.4.0", "-o", test_output.string(),
  152. test_a_output.string(), test_b_output.string()});
  153. }))
  154. << "Verbose output from runner:\n"
  155. << verbose_out.TakeStr() << "\n";
  156. verbose_out.clear();
  157. // No output should be produced.
  158. EXPECT_THAT(out, StrEq(""));
  159. EXPECT_THAT(err, StrEq(""));
  160. // Re-do the link, but with only one of the inputs. This should fail due to an
  161. // unresolved symbol.
  162. EXPECT_FALSE(Testing::CallWithCapturedOutput(
  163. out, err,
  164. [&] {
  165. return lld.MachOLink({"-arch", "arm64", "-platform_version", "macos",
  166. "10.4.0", "10.4.0", "-o", test_output.string(),
  167. test_b_output.string()});
  168. }))
  169. << "Verbose output from runner:\n"
  170. << verbose_out.TakeStr() << "\n";
  171. verbose_out.clear();
  172. // The missing symbol should be diagnosed on `stderr`.
  173. EXPECT_THAT(out, StrEq(""));
  174. EXPECT_THAT(err, HasSubstr("undefined symbol: __Z6test_av"));
  175. }
  176. } // namespace
  177. } // namespace Carbon