|
| 1 | +//===---------------------- ModuleDeps.cpp --------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "ScanFixture.h" |
| 14 | +#include "swift/Basic/Platform.h" |
| 15 | +#include "llvm/ADT/Triple.h" |
| 16 | +#include "llvm/Support/raw_ostream.h" |
| 17 | +#include "llvm/Support/Host.h" |
| 18 | +#include "llvm/Support/Path.h" |
| 19 | + |
| 20 | +using namespace swift; |
| 21 | +using namespace swift::unittest; |
| 22 | +using namespace swift::dependencies; |
| 23 | + |
| 24 | +static std::string createFilename(StringRef base, StringRef name) { |
| 25 | + SmallString<256> path = base; |
| 26 | + llvm::sys::path::append(path, name); |
| 27 | + return llvm::Twine(path).str(); |
| 28 | +} |
| 29 | + |
| 30 | +static bool emitFileWithContents(StringRef path, StringRef contents, |
| 31 | + std::string *pathOut = nullptr) { |
| 32 | + int FD; |
| 33 | + if (llvm::sys::fs::openFileForWrite(path, FD)) |
| 34 | + return true; |
| 35 | + if (pathOut) |
| 36 | + *pathOut = path.str(); |
| 37 | + llvm::raw_fd_ostream file(FD, /*shouldClose=*/true); |
| 38 | + file << contents; |
| 39 | + return false; |
| 40 | +} |
| 41 | + |
| 42 | +static bool emitFileWithContents(StringRef base, StringRef name, |
| 43 | + StringRef contents, |
| 44 | + std::string *pathOut = nullptr) { |
| 45 | + return emitFileWithContents(createFilename(base, name), contents, pathOut); |
| 46 | +} |
| 47 | + |
| 48 | +TEST_F(ScanTest, TestModuleDeps) { |
| 49 | + // Create test input file |
| 50 | + auto TestFilePath = TemporaryTestWorkspace; |
| 51 | + llvm::sys::path::append(TestFilePath, "foo.swift"); |
| 52 | + std::string TestPathStr = llvm::Twine(TestFilePath).str(); |
| 53 | + |
| 54 | + ASSERT_FALSE(emitFileWithContents(TemporaryTestWorkspace, "foo.swift", |
| 55 | + "import A\n")); |
| 56 | + |
| 57 | + // Create includes |
| 58 | + std::string IncludeDirPath = createFilename(TemporaryTestWorkspace, |
| 59 | + "include"); |
| 60 | + ASSERT_FALSE(llvm::sys::fs::create_directory(IncludeDirPath)); |
| 61 | + |
| 62 | + std::string CHeadersDirPath = createFilename(IncludeDirPath, "CHeaders"); |
| 63 | + ASSERT_FALSE(llvm::sys::fs::create_directory(CHeadersDirPath)); |
| 64 | + std::string SwiftDirPath = createFilename(IncludeDirPath, "Swift"); |
| 65 | + ASSERT_FALSE(llvm::sys::fs::create_directory(SwiftDirPath)); |
| 66 | + |
| 67 | + // Create imported module Swift interface files |
| 68 | + ASSERT_FALSE(emitFileWithContents(SwiftDirPath, "A.swiftinterface", |
| 69 | +"// swift-interface-format-version: 1.0\n\ |
| 70 | +// swift-module-flags: -module-name A\n\ |
| 71 | +import Swift\n\ |
| 72 | +@_exported import A\n\ |
| 73 | +public func overlayFuncA() { }\n")); |
| 74 | + ASSERT_FALSE(emitFileWithContents(SwiftDirPath, "E.swiftinterface", |
| 75 | +"// swift-interface-format-version: 1.0\n\ |
| 76 | +// swift-module-flags: -module-name E\n\ |
| 77 | +import Swift\n\ |
| 78 | +public func funcE()\n")); |
| 79 | + ASSERT_FALSE(emitFileWithContents(SwiftDirPath, "F.swiftinterface", |
| 80 | +"// swift-interface-format-version: 1.0\n\ |
| 81 | +// swift-module-flags: -module-name\n\ |
| 82 | +import Swift\n\ |
| 83 | +@_exported import F\n\ |
| 84 | +public func funcF() { }")); |
| 85 | + ASSERT_FALSE(emitFileWithContents(SwiftDirPath, "G.swiftinterface", |
| 86 | +"// swift-interface-format-version: 1.0\n\ |
| 87 | +// swift-module-flags: -module-name G -swift-version 5 -target x86_64-apple-macosx10.9\n\ |
| 88 | +#if swift(>=5.0)\n\ |
| 89 | +@_exported import G\n\ |
| 90 | +import Swift\n\ |
| 91 | +public func overlayFuncG() { }\n\ |
| 92 | +let stringG : String = \"Build\"\n\ |
| 93 | +#endif")); |
| 94 | + |
| 95 | + // Create imported module C modulemap/headers |
| 96 | + ASSERT_FALSE(emitFileWithContents(CHeadersDirPath, "A.h", |
| 97 | +"void funcA(void);")); |
| 98 | + ASSERT_FALSE(emitFileWithContents(CHeadersDirPath, "B.h", |
| 99 | +"#include \"A.h\"\ |
| 100 | +void funcB(void);")); |
| 101 | + ASSERT_FALSE(emitFileWithContents(CHeadersDirPath, "C.h", |
| 102 | +"#include \"B.h\"\n\ |
| 103 | +void funcC(void);\ |
| 104 | +const char* stringC() { return \"Module\"; }")); |
| 105 | + ASSERT_FALSE(emitFileWithContents(CHeadersDirPath, "D.h", |
| 106 | +"void funcD(void);")); |
| 107 | + ASSERT_FALSE(emitFileWithContents(CHeadersDirPath, "F.h", |
| 108 | +"void funcF(void);")); |
| 109 | + ASSERT_FALSE(emitFileWithContents(CHeadersDirPath, "G.h", |
| 110 | +"#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 110000\n\ |
| 111 | +#include \"X.h\"\n\ |
| 112 | +#endif\n\ |
| 113 | +void funcG(void);")); |
| 114 | + ASSERT_FALSE(emitFileWithContents(CHeadersDirPath, "X.h", |
| 115 | +"void funcX(void);")); |
| 116 | + ASSERT_FALSE(emitFileWithContents(CHeadersDirPath, "Bridging.h", |
| 117 | +"#include \"BridgingOther.h\"\n\ |
| 118 | +int bridging_other(void);")); |
| 119 | + ASSERT_FALSE(emitFileWithContents(CHeadersDirPath, "BridgingOther.h", |
| 120 | +"#include \"F.h\"\n\ |
| 121 | +int bridging_other(void);")); |
| 122 | + |
| 123 | + ASSERT_FALSE(emitFileWithContents(CHeadersDirPath, "module.modulemap", |
| 124 | +"module A {\n\ |
| 125 | +header \"A.h\"\n\ |
| 126 | +export *\n\ |
| 127 | +}\n\ |
| 128 | +module B {\n\ |
| 129 | +header \"B.h\"\n\ |
| 130 | +export *\n\ |
| 131 | +}\n\ |
| 132 | +module C {\n\ |
| 133 | +header \"C.h\"\n\ |
| 134 | +export *\n\ |
| 135 | +}\n\ |
| 136 | +module D {\n\ |
| 137 | +header \"D.h\"\n\ |
| 138 | +export *\n\ |
| 139 | +}\n\ |
| 140 | +module F {\n\ |
| 141 | +header \"F.h\"\n\ |
| 142 | +export *\n\ |
| 143 | +}\n\ |
| 144 | +module G {\n\ |
| 145 | +header \"G.h\"\n\ |
| 146 | +export *\n\ |
| 147 | +}\n\ |
| 148 | +module X {\n\ |
| 149 | +header \"X.h\"\n\ |
| 150 | +export *\n\ |
| 151 | +}")); |
| 152 | + |
| 153 | + // Paths to shims and stdlib |
| 154 | + llvm::SmallString<128> ShimsLibDir = StdLibDir; |
| 155 | + llvm::sys::path::append(ShimsLibDir, "shims"); |
| 156 | + auto Target = llvm::Triple(llvm::sys::getDefaultTargetTriple()); |
| 157 | + llvm::sys::path::append(StdLibDir, getPlatformNameForTriple(Target)); |
| 158 | + |
| 159 | + std::string Command = TestPathStr + " -I " + SwiftDirPath + |
| 160 | + " -I " + CHeadersDirPath + |
| 161 | + " -I " + StdLibDir.str().str() + |
| 162 | + " -I " + ShimsLibDir.str().str(); |
| 163 | + std::string deps = ScannerTool.getFullDependencies(Command.c_str(), |
| 164 | + {TestPathStr.c_str()}, {}); |
| 165 | + |
| 166 | + // TODO: Output/verify dependency graph correctness |
| 167 | + //llvm::dbgs() << "Deps: " << deps << "\n"; |
| 168 | +} |
0 commit comments