|
| 1 | +import Basic |
| 2 | +import Utility |
| 3 | +import POSIX |
| 4 | + |
| 5 | +enum DestinationError: Swift.Error { |
| 6 | + /// Couldn't find the Xcode installation. |
| 7 | + case invalidInstallation(String) |
| 8 | + |
| 9 | + /// The schema version is invalid. |
| 10 | + case invalidSchemaVersion |
| 11 | +} |
| 12 | + |
| 13 | +extension DestinationError: CustomStringConvertible { |
| 14 | + var description: String { |
| 15 | + switch self { |
| 16 | + case .invalidSchemaVersion: |
| 17 | + return "unsupported destination file schema version" |
| 18 | + case .invalidInstallation(let problem): |
| 19 | + return problem |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +/// The compilation destination, has information about everything that's required for a certain destination. |
| 25 | +public struct Destination { |
| 26 | + |
| 27 | + /// The clang/LLVM triple describing the target OS and architecture. |
| 28 | + /// |
| 29 | + /// The triple has the general format <arch><sub>-<vendor>-<sys>-<abi>, where: |
| 30 | + /// - arch = x86_64, i386, arm, thumb, mips, etc. |
| 31 | + /// - sub = for ex. on ARM: v5, v6m, v7a, v7m, etc. |
| 32 | + /// - vendor = pc, apple, nvidia, ibm, etc. |
| 33 | + /// - sys = none, linux, win32, darwin, cuda, etc. |
| 34 | + /// - abi = eabi, gnu, android, macho, elf, etc. |
| 35 | + /// |
| 36 | + /// for more information see //https://clang.llvm.org/docs/CrossCompilation.html |
| 37 | + public let target: String |
| 38 | + |
| 39 | + /// The SDK used to compile for the destination. |
| 40 | + public let sdk: AbsolutePath |
| 41 | + |
| 42 | + /// The binDir in the containing the compilers/linker to be used for the compilation. |
| 43 | + public let binDir: AbsolutePath |
| 44 | + |
| 45 | + /// The file extension for dynamic libraries (eg. `.so` or `.dylib`) |
| 46 | + public let dynamicLibraryExtension: String |
| 47 | + |
| 48 | + /// Additional flags to be passed to the C compiler. |
| 49 | + public let extraCCFlags: [String] |
| 50 | + |
| 51 | + /// Additional flags to be passed to the Swift compiler. |
| 52 | + public let extraSwiftCFlags: [String] |
| 53 | + |
| 54 | + /// Additional flags to be passed when compiling with C++. |
| 55 | + public let extraCPPFlags: [String] |
| 56 | + |
| 57 | + /// Returns the bin directory for the host. |
| 58 | + private static func hostBinDir() -> AbsolutePath { |
| 59 | + #if Xcode |
| 60 | + // For Xcode, set bin directory to the build directory containing the fake |
| 61 | + // toolchain created during bootstraping. This is obviously not production ready |
| 62 | + // and only exists as a development utility right now. |
| 63 | + // |
| 64 | + // This also means that we should have bootstrapped with the same Swift toolchain |
| 65 | + // we're using inside Xcode otherwise we will not be able to load the runtime libraries. |
| 66 | + // |
| 67 | + // FIXME: We may want to allow overriding this using an env variable but that |
| 68 | + // doesn't seem urgent or extremely useful as of now. |
| 69 | + return AbsolutePath(#file).parentDirectory |
| 70 | + .parentDirectory.parentDirectory.appending(components: ".build", "debug") |
| 71 | + #endif |
| 72 | + return AbsolutePath( |
| 73 | + CommandLine.arguments[0], relativeTo: currentWorkingDirectory).parentDirectory |
| 74 | + } |
| 75 | + |
| 76 | + /// The destination describing the host OS. |
| 77 | + public static func hostDestination(_ binDir: AbsolutePath? = nil) throws -> Destination { |
| 78 | + // Select the correct binDir. |
| 79 | + let binDir = binDir ?? Destination.hostBinDir() |
| 80 | + |
| 81 | + #if os(macOS) |
| 82 | + // Get the SDK. |
| 83 | + let sdkPath: AbsolutePath |
| 84 | + if let value = lookupExecutablePath(filename: getenv("SYSROOT")) { |
| 85 | + sdkPath = value |
| 86 | + } else { |
| 87 | + // No value in env, so search for it. |
| 88 | + let sdkPathStr = try Process.checkNonZeroExit( |
| 89 | + args: "xcrun", "--sdk", "macosx", "--show-sdk-path").chomp() |
| 90 | + guard !sdkPathStr.isEmpty else { |
| 91 | + throw DestinationError.invalidInstallation("could not find default SDK") |
| 92 | + } |
| 93 | + sdkPath = AbsolutePath(sdkPathStr) |
| 94 | + } |
| 95 | + |
| 96 | + // Compute common arguments for clang and swift. |
| 97 | + // This is currently just frameworks path. |
| 98 | + let commonArgs = Destination.sdkPlatformFrameworkPath().map({ ["-F", $0.asString] }) ?? [] |
| 99 | + |
| 100 | + return Destination( |
| 101 | + target: "x86_64-apple-macosx10.10", |
| 102 | + sdk: sdkPath, |
| 103 | + binDir: binDir, |
| 104 | + dynamicLibraryExtension: "dylib", |
| 105 | + extraCCFlags: ["-arch", "x86_64", "-mmacosx-version-min=10.10"] + commonArgs, |
| 106 | + extraSwiftCFlags: commonArgs, |
| 107 | + extraCPPFlags: ["-lc++"] |
| 108 | + ) |
| 109 | + #else |
| 110 | + return Destination( |
| 111 | + target: "linux-unknown-x86_64", |
| 112 | + sdk: .root, |
| 113 | + binDir: binDir, |
| 114 | + dynamicLibraryExtension: "so", |
| 115 | + extraCCFlags: ["-fPIC"], |
| 116 | + extraSwiftCFlags: [], |
| 117 | + extraCPPFlags: ["-lstdc++"] |
| 118 | + ) |
| 119 | + #endif |
| 120 | + } |
| 121 | + |
| 122 | + /// Returns macosx sdk platform framework path. |
| 123 | + public static func sdkPlatformFrameworkPath() -> AbsolutePath? { |
| 124 | + if let path = _sdkPlatformFrameworkPath { |
| 125 | + return path |
| 126 | + } |
| 127 | + let platformPath = try? Process.checkNonZeroExit( |
| 128 | + args: "xcrun", "--sdk", "macosx", "--show-sdk-platform-path").chomp() |
| 129 | + |
| 130 | + if let platformPath = platformPath, !platformPath.isEmpty { |
| 131 | + _sdkPlatformFrameworkPath = AbsolutePath(platformPath).appending( |
| 132 | + components: "Developer", "Library", "Frameworks") |
| 133 | + } |
| 134 | + return _sdkPlatformFrameworkPath |
| 135 | + } |
| 136 | + /// Cache storage for sdk platform path. |
| 137 | + private static var _sdkPlatformFrameworkPath: AbsolutePath? = nil |
| 138 | + |
| 139 | + #if os(macOS) |
| 140 | + /// Returns the host's dynamic library extension. |
| 141 | + public static let hostDynamicLibraryExtension = "dylib" |
| 142 | + #else |
| 143 | + /// Returns the host's dynamic library extension. |
| 144 | + public static let hostDdynamicLibraryExtension = "so" |
| 145 | + #endif |
| 146 | +} |
| 147 | + |
| 148 | +public extension Destination { |
| 149 | + |
| 150 | + /// Load a Destination description from a JSON representation from disk. |
| 151 | + public init(fromFile path: AbsolutePath, fileSystem: FileSystem = localFileSystem) throws { |
| 152 | + let json = try JSON(bytes: fileSystem.readFileContents(path)) |
| 153 | + try self.init(json: json) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +extension Destination: JSONMappable { |
| 158 | + |
| 159 | + /// The current schema version. |
| 160 | + static let schemaVersion = 1 |
| 161 | + |
| 162 | + public init(json: JSON) throws { |
| 163 | + |
| 164 | + // Check schema version. |
| 165 | + guard try json.get("version") == Destination.schemaVersion else { |
| 166 | + throw DestinationError.invalidSchemaVersion |
| 167 | + } |
| 168 | + |
| 169 | + try self.init(target: json.get("target"), |
| 170 | + sdk: AbsolutePath(json.get("sdk")), |
| 171 | + binDir: AbsolutePath(json.get("toolchain-bin-dir")), |
| 172 | + dynamicLibraryExtension: json.get("dynamic-library-extension"), |
| 173 | + extraCCFlags: json.get("extra-cc-flags"), |
| 174 | + extraSwiftCFlags: json.get("extra-swiftc-flags"), |
| 175 | + extraCPPFlags: json.get("extra-cpp-flags") |
| 176 | + ) |
| 177 | + } |
| 178 | +} |
0 commit comments