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); 36 Driver TheDriver("/bin/clang", "arm-linux-gnueabihf", Diags, 37 InMemoryFileSystem); 38 39 const char *EmptyFiles[] = { 40 "foo.cpp", 41 "/bin/clang", 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 61 std::unique_ptr<Compilation> C(TheDriver.BuildCompilation( 62 {"-fsyntax-only", "--gcc-toolchain=", "foo.cpp"})); 63 64 std::string S; 65 { 66 llvm::raw_string_ostream OS(S); 67 C->getDefaultToolChain().printVerboseInfo(OS); 68 } 69 #if LLVM_ON_WIN32 70 std::replace(S.begin(), S.end(), '\\', '/'); 71 #endif 72 EXPECT_EQ( 73 "Found candidate GCC installation: " 74 "/usr/lib/gcc/arm-linux-gnueabihf/4.6.3\n" 75 "Selected GCC installation: /usr/lib/gcc/arm-linux-gnueabihf/4.6.3\n" 76 "Candidate multilib: .;@m32\n" 77 "Selected multilib: .;@m32\n", 78 S); 79 } 80 81 TEST(ToolChainTest, VFSGCCInstallationRelativeDir) { 82 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); 83 84 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 85 struct TestDiagnosticConsumer : public DiagnosticConsumer {}; 86 DiagnosticsEngine Diags(DiagID, &*DiagOpts, new TestDiagnosticConsumer); 87 IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( 88 new vfs::InMemoryFileSystem); 89 Driver TheDriver("/home/test/bin/clang", "arm-linux-gnueabi", Diags, 90 InMemoryFileSystem); 91 92 const char *EmptyFiles[] = { 93 "foo.cpp", "/home/test/lib/gcc/arm-linux-gnueabi/4.6.1/crtbegin.o", 94 "/home/test/include/arm-linux-gnueabi/.keep"}; 95 96 for (const char *Path : EmptyFiles) 97 InMemoryFileSystem->addFile(Path, 0, 98 llvm::MemoryBuffer::getMemBuffer("\n")); 99 100 std::unique_ptr<Compilation> C(TheDriver.BuildCompilation( 101 {"-fsyntax-only", "--gcc-toolchain=", "foo.cpp"})); 102 103 std::string S; 104 { 105 llvm::raw_string_ostream OS(S); 106 C->getDefaultToolChain().printVerboseInfo(OS); 107 } 108 #if LLVM_ON_WIN32 109 std::replace(S.begin(), S.end(), '\\', '/'); 110 #endif 111 EXPECT_EQ("Found candidate GCC installation: " 112 "/home/test/lib/gcc/arm-linux-gnueabi/4.6.1\n" 113 "Selected GCC installation: " 114 "/home/test/bin/../lib/gcc/arm-linux-gnueabi/4.6.1\n" 115 "Candidate multilib: .;@m32\n" 116 "Selected multilib: .;@m32\n", 117 S); 118 } 119 120 } // end anonymous namespace 121