lld_runner_test.cpp 7.2 KB

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