lld_runner_test.cpp 7.9 KB

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