PlatformPaths.swift 8.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2016 Anarchy Tools Contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15
import atfoundation
16

Drew's avatar
Drew committed
17 18 19 20 21 22 23
public enum Architecture {
    case x86_64
    case i386
    case armv7
    case arm64
}

Drew's avatar
Drew committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
extension Architecture: CustomStringConvertible {
    public var description: String {
        switch(self) {
            case .x86_64:
            return "x86_64"

            case .i386:
            return "i386"

            case .armv7:
            return "armv7"

            case .arm64:
            return "arm64"
        }
    }
}

Drew's avatar
Drew committed
42 43 44 45 46 47 48 49 50
func ==(a: Platform, b: Platform) -> Bool {
    switch(a, b) {
        case (.OSX, .OSX): return true
        case (.Linux, .Linux): return true
        case (.iOS(let a), .iOS(let b)) where a == b: return true
        default: return false
    }
}

Drew's avatar
Drew committed
51
public enum Platform {
Drew's avatar
Drew committed
52
    //specific platforms
Drew's avatar
Drew committed
53 54
    case OSX
    case Linux
Drew's avatar
Drew committed
55
    case iOS(Architecture)
Drew's avatar
Drew committed
56

Drew's avatar
Drew committed
57 58 59 60
    public static var toolchain: String? = nil

    static var isXcode7: Bool { return Platform.toolchain!.contains(string: "Xcode.app") }

Drew's avatar
Drew committed
61 62 63
    //generic platforms
    case iOSGeneric

Drew's avatar
Drew committed
64 65 66 67 68 69
    public init(string: String) {
        switch(string) {
            case "osx", "mac":
                self = Platform.OSX
            case "linux":
                self = Platform.Linux
Drew's avatar
Drew committed
70 71 72 73 74 75 76 77
            case "ios-x86_64":
                self = Platform.iOS(Architecture.x86_64)
            case "ios-i386":
                self = Platform.iOS(Architecture.i386)
            case "ios-armv7":
                self = Platform.iOS(Architecture.armv7)
            case "ios-arm64":
                self = Platform.iOS(Architecture.arm64)
Drew's avatar
Drew committed
78 79
            case "ios":
                self = Platform.iOSGeneric
Drew's avatar
Drew committed
80

Drew's avatar
Drew committed
81 82 83 84
            default:
                fatalError("Unknown platform \(string)")
        }
    }
Drew's avatar
Drew committed
85

Drew's avatar
Drew committed
86 87 88 89 90 91 92
    ///The overlays that should be enabled when building for this platform
    public var overlays: [String] {
        switch(self) {
            case .OSX:
                return ["atbuild.platform.osx", "atbuild.platform.mac"]
            case .Linux:
                return ["atbuild.platform.linux"]
Drew's avatar
Drew committed
93
            case .iOS, .iOSGeneric:
Drew's avatar
Drew committed
94
                return ["atbuild.platform.ios"]
Drew's avatar
Drew committed
95
        }
Drew's avatar
Drew committed
96 97
    }

Drew's avatar
Drew committed
98 99 100
    ///The typical path to a toolchain binary of the platform
    var defaultToolchainBinaryPath: String {
        switch(self) {
Drew's avatar
Drew committed
101
            case .OSX, .iOS, .iOSGeneric:
Drew's avatar
Drew committed
102 103 104 105 106 107 108 109
            return "\(defaultToolchainPath)/usr/bin/"
            case .Linux:
            return "\(defaultToolchainPath)/usr/local/bin/"
        }
    }

    public var defaultToolchainPath: String {
        switch(self) {
Drew's avatar
Drew committed
110
            case .OSX, .iOS, .iOSGeneric:
Drew's avatar
Drew committed
111 112 113 114 115 116 117 118 119
                return "/Library/Developer/Toolchains/swift-latest.xctoolchain"
            case .Linux:
                return "/"
        }
    }

    var sdkPath: String? {
        switch(self) {
            case .OSX:
Drew's avatar
Drew committed
120 121 122 123
                if Platform.isXcode7 {
                    return "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk"
                }
                return "/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk"
Drew's avatar
Drew committed
124 125
            case .Linux:
                return nil
Drew's avatar
Drew committed
126
            case .iOS(.x86_64), .iOS(.i386):
Drew's avatar
Drew committed
127 128 129 130
                if Platform.isXcode7 {
                    return "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator9.3.sdk"
                }
                return "/Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk"
Drew's avatar
Drew committed
131
            case .iOS(.armv7), .iOS(.arm64):
Drew's avatar
Drew committed
132 133 134 135
                if Platform.isXcode7 {
                    return "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.3.sdk"
                }
                return "/Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk"
Drew's avatar
Drew committed
136 137
            case .iOSGeneric:
                fatalError("No SDK for generic iOS platform; choose a specific platform or use atbin")
Drew's avatar
Drew committed
138 139 140
        }
    }

Drew's avatar
Drew committed
141
    var architecture: Architecture {
Drew's avatar
Drew committed
142 143
        switch(self) {
            case .OSX, .Linux:
Drew's avatar
Drew committed
144 145 146
                return Architecture.x86_64
            case .iOS(let arch):
                return arch
Drew's avatar
Drew committed
147 148
            case .iOSGeneric:
                fatalError("No architecture for generic iOS platform; choose a specific platform or use atbin")
Drew's avatar
Drew committed
149 150 151 152 153
        }
    }

    var dynamicLibraryExtension: String {
        switch(self) {
Drew's avatar
Drew committed
154
            case .OSX, .iOS, .iOSGeneric:
Drew's avatar
Drew committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
                return ".dylib"
            case .Linux:
                return ".so"
        }
    }

    ///The platform on which atbuild is currently running
    public static var hostPlatform: Platform {
        #if os(OSX)
        return Platform.OSX
        #elseif os(Linux)
        return Platform.Linux
        #endif
    }

    ///The platform for which atbuild is currently building
    ///By default, we build for the hostPlatform
    public static var targetPlatform: Platform = Platform.hostPlatform

    ///The platform on which the build will take place (e.g. swift-build-tool will run).
    ///Ordinarily we build on the hostPlatform, but in the case of bootstrapping,
    ///we may be only emitting a yaml, which the actual build occuring
    /// on some other platform than either the host or the target.
    public static var buildPlatform: Platform = Platform.hostPlatform
Drew's avatar
Drew committed
179 180 181 182 183 184 185 186 187 188 189

    ///If the platform is "virtual" (such as iOS), this returns all the sub-platforms.
    ///Otherwise, it returns the receiver
    public var allPlatforms: [Platform] {
        switch(self) {
            case .OSX, .Linux, .iOS:
            return [self]
            case .iOSGeneric:
            return [Platform.iOS(Architecture.x86_64), Platform.iOS(Architecture.i386), Platform.iOS(Architecture.armv7), Platform.iOS(Architecture.arm64)]
        }
    }
Drew's avatar
Drew committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

    var targetTriple: String {
        switch(self) {
            case .OSX, .Linux, .iOSGeneric:
            fatalError("Not implemented")

            case .iOS(let arch):
            switch(arch) {
                case .x86_64:
                if Platform.isXcode7 {return "x86_64-apple-ios9.3"}
                return "x86_64-apple-ios10.0"

                case .i386:
                if Platform.isXcode7 {return "i386-apple-ios9.3"}
                return "i386-apple-ios10.0"

                case .arm64:
                if Platform.isXcode7 {return "arm64-apple-ios9.3"}
                return "arm64-apple-ios10.0"

                case .armv7:
                if Platform.isXcode7 {return "armv7-apple-ios9.3"}
                return "armv7-apple-ios10.0"
                
            }

        }
    }
Drew's avatar
Drew committed
218 219
}

Drew's avatar
Drew committed
220 221 222 223 224 225 226 227 228
extension Platform: CustomStringConvertible {
    public var description: String {
        switch(self) {
            case .OSX:
            return "osx"
            case .Linux:
            return "linux"
            case .iOS(let architecture):
            return "ios-\(architecture)"
Drew's avatar
Drew committed
229 230
            case .iOSGeneric:
            return "ios"
Drew's avatar
Drew committed
231 232 233 234
        }
    }
}

Drew's avatar
Drew committed
235
func findToolPath(toolName: String) -> Path {
Drew's avatar
Drew committed
236 237 238 239

    if Platform.buildPlatform == Platform.hostPlatform {
        //poke around on the filesystem
        //look in /usr/bin
Drew's avatar
Drew committed
240
        let usrBin = Path("\(Platform.toolchain!)/usr/bin/\(toolName)")
241
        if FS.fileExists(path: usrBin) { return usrBin }
Drew's avatar
Drew committed
242
        //look in /usr/local/bin
Drew's avatar
Drew committed
243
        let usrLocalBin = Path("\(Platform.toolchain!)/usr/local/bin/\(toolName)")
244
        if FS.fileExists(path: usrLocalBin) { return usrLocalBin }
Drew's avatar
Drew committed
245 246 247 248

        //swift-build-tool isn't available in 2.2.
        //If we're looking for SBT, try in the default location
        if toolName == "swift-build-tool" {
249
            let sbtPath = Path("\(Platform.hostPlatform.defaultToolchainPath)/usr/bin/\(toolName)")
250
            if FS.fileExists(path: sbtPath) { return sbtPath }
Drew's avatar
Drew committed
251 252 253 254 255

        }
    }
    else {
        //file system isn't live; hope the path is in a typical place
256
        return Path("\(Platform.buildPlatform.defaultToolchainBinaryPath)\(toolName)")
Drew's avatar
Drew committed
257 258 259 260
    }
    
    fatalError("Can't find a path for \(toolName)")
}