PlatformPaths.swift 10.8 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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/**Drew's incredibly dumb string library */

extension String {
    func match(_ b: String) -> ClosedRange<String.CharacterView.Index>? {
        var idx = characters.startIndex
        while idx != endIndex {
            var l_idx_pre = idx
            var l_idx = idx
            var found = true
            for charB in b.characters {
                let charAL = characters[l_idx]
                if charAL != charB { found = false; break }
                l_idx_pre = l_idx
                l_idx = characters.index(after: l_idx)
            }
            if found {
                return ClosedRange(uncheckedBounds: (lower: idx, upper: l_idx_pre))
            }
            idx = characters.index(after: idx)
        }
        return nil
    }
    func prefix(to: String.CharacterView.Index) -> String {
        let notEndIndex = characters.index(before: characters.endIndex)
        let notStartIndex = characters.index(after: to)
        let closedRange = ClosedRange(uncheckedBounds: (lower: notStartIndex, upper: notEndIndex))
        var s = self
        s.removeSubrange(closedRange)
        return s
    }
}


Drew's avatar
Drew committed
50 51 52 53 54 55 56
public enum Architecture {
    case x86_64
    case i386
    case armv7
    case arm64
}

Drew's avatar
Drew committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
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
75 76 77 78 79 80 81 82 83
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
84
public enum Platform {
Drew's avatar
Drew committed
85
    //specific platforms
Drew's avatar
Drew committed
86 87
    case OSX
    case Linux
Drew's avatar
Drew committed
88
    case iOS(Architecture)
Drew's avatar
Drew committed
89

Drew's avatar
Drew committed
90 91
    public static var toolchain: String? = nil

Drew's avatar
Drew committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    private static var isXcode7: Bool {
        #if os(OSX)
        return FS.fileExists(path: Path("\(Platform.developerPath)/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk"))
        #else
        return false //no linux platform uses xcode 7
                     //unless you count Project Marklar 2
        #endif
    }

    private static var developerPath: String { 
        //first, look at the toolchain for clues
        if let match = Platform.toolchain!.match(".app/Contents/Developer") {
            return Platform.toolchain!.prefix(to: match.upperBound)
        }
        //assuming that failed, try xcode-select
        #if os(OSX)
        var xcode_select_path = ""
Drew's avatar
Drew committed
109
        anarchySystem("xcode-select -p", redirectOutput: &xcode_select_path)
Drew's avatar
Drew committed
110 111 112 113 114 115 116 117 118 119
        //lop off the newline
        var notEndIndex = xcode_select_path.characters.index(before: xcode_select_path.characters.endIndex)
        notEndIndex = xcode_select_path.characters.index(before: notEndIndex)
        xcode_select_path = xcode_select_path.prefix(to: notEndIndex)
        return xcode_select_path
        #else
        //on linux, let's just use /Applications/Xcode.app
        return "/Applications/Xcode.app/Contents/Developer"
        #endif
    }
Drew's avatar
Drew committed
120

Drew's avatar
Drew committed
121 122 123
    //generic platforms
    case iOSGeneric

Drew's avatar
Drew committed
124 125 126 127 128 129
    public init(string: String) {
        switch(string) {
            case "osx", "mac":
                self = Platform.OSX
            case "linux":
                self = Platform.Linux
Drew's avatar
Drew committed
130 131 132 133 134 135 136 137
            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
138 139
            case "ios":
                self = Platform.iOSGeneric
Drew's avatar
Drew committed
140

Drew's avatar
Drew committed
141 142 143 144
            default:
                fatalError("Unknown platform \(string)")
        }
    }
Drew's avatar
Drew committed
145

Drew's avatar
Drew committed
146 147 148 149 150 151 152
    ///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
153
            case .iOS, .iOSGeneric:
Drew's avatar
Drew committed
154
                return ["atbuild.platform.ios"]
Drew's avatar
Drew committed
155
        }
Drew's avatar
Drew committed
156 157
    }

Drew's avatar
Drew committed
158 159 160
    ///The typical path to a toolchain binary of the platform
    var defaultToolchainBinaryPath: String {
        switch(self) {
Drew's avatar
Drew committed
161
            case .OSX, .iOS, .iOSGeneric:
Drew's avatar
Drew committed
162 163 164 165 166 167 168 169
            return "\(defaultToolchainPath)/usr/bin/"
            case .Linux:
            return "\(defaultToolchainPath)/usr/local/bin/"
        }
    }

    public var defaultToolchainPath: String {
        switch(self) {
Drew's avatar
Drew committed
170
            case .OSX, .iOS, .iOSGeneric:
Drew's avatar
Drew committed
171 172 173 174 175 176 177 178 179
                return "/Library/Developer/Toolchains/swift-latest.xctoolchain"
            case .Linux:
                return "/"
        }
    }

    var sdkPath: String? {
        switch(self) {
            case .OSX:
Drew's avatar
Drew committed
180
                //look for a current-generation path
Drew's avatar
Drew committed
181
                if Platform.isXcode7 {
Drew's avatar
Drew committed
182
                    return "\(Platform.developerPath)/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk"
Drew's avatar
Drew committed
183
                }
Drew's avatar
Drew committed
184
                return "\(Platform.developerPath)/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk"
Drew's avatar
Drew committed
185 186
            case .Linux:
                return nil
Drew's avatar
Drew committed
187
            case .iOS(.x86_64), .iOS(.i386):
Drew's avatar
Drew committed
188
                if Platform.isXcode7 {
Drew's avatar
Drew committed
189 190 191 192
                    return "\(Platform.developerPath)/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator9.3.sdk"
                }
                else {
                    return "\(Platform.developerPath)/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk"
Drew's avatar
Drew committed
193
                }
Drew's avatar
Drew committed
194
            case .iOS(.armv7), .iOS(.arm64):
Drew's avatar
Drew committed
195
                if Platform.isXcode7 {
Drew's avatar
Drew committed
196 197 198 199
                    return "\(Platform.developerPath)/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.3.sdk"
                }
                else {
                    return "\(Platform.developerPath)/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk"
Drew's avatar
Drew committed
200
                }
Drew's avatar
Drew committed
201 202
            case .iOSGeneric:
                fatalError("No SDK for generic iOS platform; choose a specific platform or use atbin")
Drew's avatar
Drew committed
203 204 205
        }
    }

Drew's avatar
Drew committed
206
    var architecture: Architecture {
Drew's avatar
Drew committed
207 208
        switch(self) {
            case .OSX, .Linux:
Drew's avatar
Drew committed
209 210 211
                return Architecture.x86_64
            case .iOS(let arch):
                return arch
Drew's avatar
Drew committed
212 213
            case .iOSGeneric:
                fatalError("No architecture for generic iOS platform; choose a specific platform or use atbin")
Drew's avatar
Drew committed
214 215 216 217 218
        }
    }

    var dynamicLibraryExtension: String {
        switch(self) {
Drew's avatar
Drew committed
219
            case .OSX, .iOS, .iOSGeneric:
Drew's avatar
Drew committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
                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
244 245 246 247 248 249 250 251 252 253 254

    ///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
255

Drew's avatar
Drew committed
256 257
    var standardDeploymentTarget: String {
        switch(self) {
Drew's avatar
Drew committed
258 259 260
            case .OSX:
            return "10.12"
            case .Linux, .iOSGeneric:
Drew's avatar
Drew committed
261 262 263 264 265 266 267 268 269
            fatalError("Not implemented")

            case .iOS:
            if Platform.isXcode7 { return "9.3" }
            return "10.0"
        }
    }

    var nonDeploymentTargetTargetTriple: String {
Drew's avatar
Drew committed
270
        switch(self) {
Drew's avatar
Drew committed
271 272 273
            case .OSX:
            return "x86_64-apple-macosx"
            case .Linux, .iOSGeneric:
Drew's avatar
Drew committed
274 275 276 277 278
            fatalError("Not implemented")

            case .iOS(let arch):
            switch(arch) {
                case .x86_64:
Drew's avatar
Drew committed
279
                return "x86_64-apple-ios"
Drew's avatar
Drew committed
280
                case .i386:
Drew's avatar
Drew committed
281
                return "i386-apple-ios"
Drew's avatar
Drew committed
282
                case .arm64:
Drew's avatar
Drew committed
283
                return "arm64-apple-ios"
Drew's avatar
Drew committed
284
                case .armv7:
Drew's avatar
Drew committed
285
                return "armv7-apple-ios"
Drew's avatar
Drew committed
286 287 288 289
            }

        }
    }
Drew's avatar
Drew committed
290 291
}

Drew's avatar
Drew committed
292 293 294 295 296 297 298 299 300
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
301 302
            case .iOSGeneric:
            return "ios"
Drew's avatar
Drew committed
303 304 305 306
        }
    }
}

Drew's avatar
Drew committed
307
func findToolPath(toolName: String) -> Path {
Drew's avatar
Drew committed
308 309 310 311

    if Platform.buildPlatform == Platform.hostPlatform {
        //poke around on the filesystem
        //look in /usr/bin
Drew's avatar
Drew committed
312
        let usrBin = Path("\(Platform.toolchain!)/usr/bin/\(toolName)")
313
        if FS.fileExists(path: usrBin) { return usrBin }
Drew's avatar
Drew committed
314
        //look in /usr/local/bin
Drew's avatar
Drew committed
315
        let usrLocalBin = Path("\(Platform.toolchain!)/usr/local/bin/\(toolName)")
316
        if FS.fileExists(path: usrLocalBin) { return usrLocalBin }
Drew's avatar
Drew committed
317 318 319 320

        //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" {
321
            let sbtPath = Path("\(Platform.hostPlatform.defaultToolchainPath)/usr/bin/\(toolName)")
322
            if FS.fileExists(path: sbtPath) { return sbtPath }
Drew's avatar
Drew committed
323 324 325 326
        }
    }
    else {
        //file system isn't live; hope the path is in a typical place
327
        return Path("\(Platform.buildPlatform.defaultToolchainBinaryPath)\(toolName)")
Drew's avatar
Drew committed
328 329 330
    }
    
    fatalError("Can't find a path for \(toolName)")
Drew's avatar
Drew committed
331
}