Benjamin Kramer | d45b205 | 2015-10-07 15:48:01 +0000 | [diff] [blame] | 1 | //===- unittests/Driver/ToolChainTest.cpp --- ToolChain tests -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Unit tests for ToolChains. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Driver/ToolChain.h" |
| 15 | #include "clang/Basic/DiagnosticIDs.h" |
| 16 | #include "clang/Basic/DiagnosticOptions.h" |
| 17 | #include "clang/Basic/LLVM.h" |
| 18 | #include "clang/Basic/VirtualFileSystem.h" |
| 19 | #include "clang/Driver/Compilation.h" |
| 20 | #include "clang/Driver/Driver.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | #include "gtest/gtest.h" |
| 23 | using namespace clang; |
| 24 | using namespace clang::driver; |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | TEST(ToolChainTest, VFSGCCInstallation) { |
| 29 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
| 30 | |
| 31 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 32 | struct TestDiagnosticConsumer : public DiagnosticConsumer {}; |
| 33 | DiagnosticsEngine Diags(DiagID, &*DiagOpts, new TestDiagnosticConsumer); |
| 34 | IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 35 | new vfs::InMemoryFileSystem); |
Benjamin Kramer | f420dda | 2015-10-13 15:19:32 +0000 | [diff] [blame] | 36 | Driver TheDriver("/bin/clang", "arm-linux-gnueabihf", Diags, |
Benjamin Kramer | d45b205 | 2015-10-07 15:48:01 +0000 | [diff] [blame] | 37 | InMemoryFileSystem); |
| 38 | |
| 39 | const char *EmptyFiles[] = { |
| 40 | "foo.cpp", |
Benjamin Kramer | f420dda | 2015-10-13 15:19:32 +0000 | [diff] [blame] | 41 | "/bin/clang", |
Benjamin Kramer | d45b205 | 2015-10-07 15:48:01 +0000 | [diff] [blame] | 42 | "/usr/lib/gcc/arm-linux-gnueabi/4.6.1/crtbegin.o", |
| 43 | "/usr/lib/gcc/arm-linux-gnueabi/4.6.1/crtend.o", |
| 44 | "/usr/lib/gcc/arm-linux-gnueabihf/4.6.3/crtbegin.o", |
| 45 | "/usr/lib/gcc/arm-linux-gnueabihf/4.6.3/crtend.o", |
| 46 | "/usr/lib/arm-linux-gnueabi/crt1.o", |
| 47 | "/usr/lib/arm-linux-gnueabi/crti.o", |
| 48 | "/usr/lib/arm-linux-gnueabi/crtn.o", |
| 49 | "/usr/lib/arm-linux-gnueabihf/crt1.o", |
| 50 | "/usr/lib/arm-linux-gnueabihf/crti.o", |
| 51 | "/usr/lib/arm-linux-gnueabihf/crtn.o", |
| 52 | "/usr/include/arm-linux-gnueabi/.keep", |
| 53 | "/usr/include/arm-linux-gnueabihf/.keep", |
| 54 | "/lib/arm-linux-gnueabi/.keep", |
| 55 | "/lib/arm-linux-gnueabihf/.keep"}; |
| 56 | |
| 57 | for (const char *Path : EmptyFiles) |
| 58 | InMemoryFileSystem->addFile(Path, 0, |
| 59 | llvm::MemoryBuffer::getMemBuffer("\n")); |
| 60 | |
Samuel Antao | 4c1f1c3 | 2015-10-27 22:20:26 +0000 | [diff] [blame] | 61 | std::unique_ptr<Compilation> C(TheDriver.BuildCompilation( |
| 62 | {"-fsyntax-only", "--gcc-toolchain=", "foo.cpp"})); |
Serge Pavlov | b43573b | 2017-05-24 14:57:17 +0000 | [diff] [blame^] | 63 | EXPECT_TRUE(C); |
Benjamin Kramer | d45b205 | 2015-10-07 15:48:01 +0000 | [diff] [blame] | 64 | |
| 65 | std::string S; |
| 66 | { |
| 67 | llvm::raw_string_ostream OS(S); |
| 68 | C->getDefaultToolChain().printVerboseInfo(OS); |
| 69 | } |
Reid Kleckner | 04ab116 | 2015-10-09 16:48:52 +0000 | [diff] [blame] | 70 | #if LLVM_ON_WIN32 |
| 71 | std::replace(S.begin(), S.end(), '\\', '/'); |
| 72 | #endif |
Benjamin Kramer | c5862f0 | 2015-10-09 13:03:18 +0000 | [diff] [blame] | 73 | EXPECT_EQ( |
| 74 | "Found candidate GCC installation: " |
| 75 | "/usr/lib/gcc/arm-linux-gnueabihf/4.6.3\n" |
| 76 | "Selected GCC installation: /usr/lib/gcc/arm-linux-gnueabihf/4.6.3\n" |
| 77 | "Candidate multilib: .;@m32\n" |
| 78 | "Selected multilib: .;@m32\n", |
| 79 | S); |
Benjamin Kramer | d45b205 | 2015-10-07 15:48:01 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Benjamin Kramer | f420dda | 2015-10-13 15:19:32 +0000 | [diff] [blame] | 82 | TEST(ToolChainTest, VFSGCCInstallationRelativeDir) { |
| 83 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
| 84 | |
| 85 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 86 | struct TestDiagnosticConsumer : public DiagnosticConsumer {}; |
| 87 | DiagnosticsEngine Diags(DiagID, &*DiagOpts, new TestDiagnosticConsumer); |
| 88 | IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 89 | new vfs::InMemoryFileSystem); |
| 90 | Driver TheDriver("/home/test/bin/clang", "arm-linux-gnueabi", Diags, |
| 91 | InMemoryFileSystem); |
| 92 | |
| 93 | const char *EmptyFiles[] = { |
| 94 | "foo.cpp", "/home/test/lib/gcc/arm-linux-gnueabi/4.6.1/crtbegin.o", |
| 95 | "/home/test/include/arm-linux-gnueabi/.keep"}; |
| 96 | |
| 97 | for (const char *Path : EmptyFiles) |
| 98 | InMemoryFileSystem->addFile(Path, 0, |
| 99 | llvm::MemoryBuffer::getMemBuffer("\n")); |
| 100 | |
Samuel Antao | 4c1f1c3 | 2015-10-27 22:20:26 +0000 | [diff] [blame] | 101 | std::unique_ptr<Compilation> C(TheDriver.BuildCompilation( |
| 102 | {"-fsyntax-only", "--gcc-toolchain=", "foo.cpp"})); |
Serge Pavlov | b43573b | 2017-05-24 14:57:17 +0000 | [diff] [blame^] | 103 | EXPECT_TRUE(C); |
Benjamin Kramer | f420dda | 2015-10-13 15:19:32 +0000 | [diff] [blame] | 104 | |
| 105 | std::string S; |
| 106 | { |
| 107 | llvm::raw_string_ostream OS(S); |
| 108 | C->getDefaultToolChain().printVerboseInfo(OS); |
| 109 | } |
| 110 | #if LLVM_ON_WIN32 |
| 111 | std::replace(S.begin(), S.end(), '\\', '/'); |
| 112 | #endif |
| 113 | EXPECT_EQ("Found candidate GCC installation: " |
| 114 | "/home/test/lib/gcc/arm-linux-gnueabi/4.6.1\n" |
| 115 | "Selected GCC installation: " |
| 116 | "/home/test/bin/../lib/gcc/arm-linux-gnueabi/4.6.1\n" |
| 117 | "Candidate multilib: .;@m32\n" |
| 118 | "Selected multilib: .;@m32\n", |
| 119 | S); |
| 120 | } |
| 121 | |
Zachary Turner | aff19c3 | 2016-08-12 17:47:52 +0000 | [diff] [blame] | 122 | TEST(ToolChainTest, DefaultDriverMode) { |
| 123 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
| 124 | |
| 125 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 126 | struct TestDiagnosticConsumer : public DiagnosticConsumer {}; |
| 127 | DiagnosticsEngine Diags(DiagID, &*DiagOpts, new TestDiagnosticConsumer); |
| 128 | IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 129 | new vfs::InMemoryFileSystem); |
| 130 | |
| 131 | Driver CCDriver("/home/test/bin/clang", "arm-linux-gnueabi", Diags, |
| 132 | InMemoryFileSystem); |
Serge Pavlov | b43573b | 2017-05-24 14:57:17 +0000 | [diff] [blame^] | 133 | CCDriver.setCheckInputsExist(false); |
Zachary Turner | aff19c3 | 2016-08-12 17:47:52 +0000 | [diff] [blame] | 134 | Driver CXXDriver("/home/test/bin/clang++", "arm-linux-gnueabi", Diags, |
| 135 | InMemoryFileSystem); |
Serge Pavlov | b43573b | 2017-05-24 14:57:17 +0000 | [diff] [blame^] | 136 | CXXDriver.setCheckInputsExist(false); |
Zachary Turner | aff19c3 | 2016-08-12 17:47:52 +0000 | [diff] [blame] | 137 | Driver CLDriver("/home/test/bin/clang-cl", "arm-linux-gnueabi", Diags, |
| 138 | InMemoryFileSystem); |
Serge Pavlov | b43573b | 2017-05-24 14:57:17 +0000 | [diff] [blame^] | 139 | CLDriver.setCheckInputsExist(false); |
Zachary Turner | aff19c3 | 2016-08-12 17:47:52 +0000 | [diff] [blame] | 140 | |
Serge Pavlov | b43573b | 2017-05-24 14:57:17 +0000 | [diff] [blame^] | 141 | std::unique_ptr<Compilation> CC(CCDriver.BuildCompilation( |
| 142 | { "/home/test/bin/clang", "foo.cpp"})); |
| 143 | std::unique_ptr<Compilation> CXX(CXXDriver.BuildCompilation( |
| 144 | { "/home/test/bin/clang++", "foo.cpp"})); |
| 145 | std::unique_ptr<Compilation> CL(CLDriver.BuildCompilation( |
| 146 | { "/home/test/bin/clang-cl", "foo.cpp"})); |
Zachary Turner | aff19c3 | 2016-08-12 17:47:52 +0000 | [diff] [blame] | 147 | |
Serge Pavlov | b43573b | 2017-05-24 14:57:17 +0000 | [diff] [blame^] | 148 | EXPECT_TRUE(CC); |
| 149 | EXPECT_TRUE(CXX); |
| 150 | EXPECT_TRUE(CL); |
Zachary Turner | aff19c3 | 2016-08-12 17:47:52 +0000 | [diff] [blame] | 151 | EXPECT_TRUE(CCDriver.CCCIsCC()); |
| 152 | EXPECT_TRUE(CXXDriver.CCCIsCXX()); |
| 153 | EXPECT_TRUE(CLDriver.IsCLMode()); |
| 154 | } |
| 155 | |
Manoj Gupta | e025ebb | 2017-04-18 17:34:46 +0000 | [diff] [blame] | 156 | } // end anonymous namespace. |