lld_runner_test.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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, 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(Testing::CallWithCapturedOutput(
  76. out, err,
  77. [&] {
  78. auto run_result = clang.Run({target_arg, "-fPIE", "-c",
  79. test_a_file.string(), "-o",
  80. test_a_output.string()});
  81. if (!run_result.ok()) {
  82. err = run_result.error().message();
  83. return false;
  84. }
  85. return *run_result;
  86. }),
  87. "Verbose output from runner:\n{0}\nStderr:\n{1}\n",
  88. verbose_out.TakeStr(), err);
  89. verbose_out.clear();
  90. CARBON_CHECK(Testing::CallWithCapturedOutput(
  91. out, err,
  92. [&] {
  93. auto run_result = clang.Run({target_arg, "-fPIE", "-c",
  94. test_b_file.string(), "-o",
  95. test_b_output.string()});
  96. if (!run_result.ok()) {
  97. err = run_result.error().message();
  98. return false;
  99. }
  100. return *run_result;
  101. }),
  102. "Verbose output from runner:\n{0}\nStderr:\n{1}\n",
  103. verbose_out.TakeStr(), err);
  104. verbose_out.clear();
  105. return {test_a_output, test_b_output};
  106. }
  107. TEST(LldRunnerTest, ElfLinkTest) {
  108. const auto install_paths =
  109. InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  110. std::filesystem::path test_a_output;
  111. std::filesystem::path test_b_output;
  112. std::tie(test_a_output, test_b_output) =
  113. CompileTwoSources(install_paths, "aarch64-unknown-linux");
  114. std::filesystem::path test_output = *Testing::WriteTestFile("test.o", "");
  115. RawStringOstream verbose_out;
  116. std::string out;
  117. std::string err;
  118. LldRunner lld(&install_paths, &verbose_out);
  119. // Link the two object files together.
  120. //
  121. // TODO: Currently, this uses a relocatable link, but it would be better to do
  122. // a full link to an executable. For that to work, we need at least the
  123. // C-runtime built artifacts available in the toolchain. We should revisit
  124. // this once we have those in place. This also prevents us from testing a
  125. // failed link easily.
  126. EXPECT_TRUE(Testing::CallWithCapturedOutput(
  127. out, err,
  128. [&] {
  129. return lld.ElfLink({"-m", "aarch64linux", "--relocatable", "-o",
  130. test_output.string(), test_a_output.string(),
  131. test_b_output.string()});
  132. }))
  133. << "Verbose output from runner:\n"
  134. << verbose_out.TakeStr() << "\n";
  135. verbose_out.clear();
  136. // No output should be produced.
  137. EXPECT_THAT(out, StrEq(""));
  138. EXPECT_THAT(err, StrEq(""));
  139. }
  140. TEST(LldRunnerTest, MachOLinkTest) {
  141. const auto install_paths =
  142. InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  143. std::filesystem::path test_a_output;
  144. std::filesystem::path test_b_output;
  145. std::tie(test_a_output, test_b_output) =
  146. CompileTwoSources(install_paths, "arm64-unknown-macosx10.4.0");
  147. std::filesystem::path test_output = *Testing::WriteTestFile("test.o", "");
  148. RawStringOstream verbose_out;
  149. std::string out;
  150. std::string err;
  151. // Link the two object files together.
  152. //
  153. // This is a somewhat arbitrary command line, and is missing the C-runtimes,
  154. // but seems to succeed currently. The goal isn't to test any *particular*
  155. // link, but just than an actual link occurs successfully.
  156. LldRunner lld(&install_paths, &verbose_out);
  157. EXPECT_TRUE(Testing::CallWithCapturedOutput(
  158. out, err,
  159. [&] {
  160. return lld.MachOLink({"-arch", "arm64", "-platform_version", "macos",
  161. "10.4.0", "10.4.0", "-o", test_output.string(),
  162. test_a_output.string(), test_b_output.string()});
  163. }))
  164. << "Verbose output from runner:\n"
  165. << verbose_out.TakeStr() << "\n";
  166. verbose_out.clear();
  167. // No output should be produced.
  168. EXPECT_THAT(out, StrEq(""));
  169. EXPECT_THAT(err, StrEq(""));
  170. // Re-do the link, but with only one of the inputs. This should fail due to an
  171. // unresolved symbol.
  172. EXPECT_FALSE(Testing::CallWithCapturedOutput(
  173. out, err,
  174. [&] {
  175. return lld.MachOLink({"-arch", "arm64", "-platform_version", "macos",
  176. "10.4.0", "10.4.0", "-o", test_output.string(),
  177. test_b_output.string()});
  178. }))
  179. << "Verbose output from runner:\n"
  180. << verbose_out.TakeStr() << "\n";
  181. verbose_out.clear();
  182. // The missing symbol should be diagnosed on `stderr`.
  183. EXPECT_THAT(out, StrEq(""));
  184. EXPECT_THAT(err, HasSubstr("undefined symbol: __Z6test_av"));
  185. }
  186. } // namespace
  187. } // namespace Carbon