main.swift 3.81 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 16 17 18 19 20
#if os(Linux)
import Glibc
#else
import Darwin
#endif

Drew's avatar
Drew committed
21
let version = "1.2"
22

23
import atfoundation
24 25 26
import atpkg
import attools

Johannes Schriewer's avatar
Johannes Schriewer committed
27 28 29
// This is a workaround for jumbled up output from print statements
setbuf(stdout, nil)

30
enum Options: String {
31
    case Overlay = "--use-overlay"
32
    case CustomFile = "-f"
Drew's avatar
Drew committed
33 34
    case Help = "--help"
    case Clean = "--clean"
Drew's avatar
Drew committed
35
    case Toolchain = "--toolchain"
Drew's avatar
Drew committed
36 37 38 39 40 41 42 43 44 45 46
    case Platform = "--platform"
    
    static var allOptions : [Options] { return [
        Overlay, 
        CustomFile, 
        Help, 
        Clean, 
        Toolchain, 
        Platform
        ] 
    }
47 48
}

49
let defaultPackageFile = Path("build.atpkg")
50

Drew's avatar
Drew committed
51
var focusOnTask : String? = nil
Drew's avatar
Drew committed
52

Drew's avatar
Drew committed
53
var packageFile = defaultPackageFile
Drew's avatar
Drew committed
54
var toolchain = Platform.buildPlatform.defaultToolchainPath
55
for (i, x) in Process.arguments.enumerated() {
Drew's avatar
Drew committed
56
    if x == Options.CustomFile.rawValue {
57
        packageFile = Path(Process.arguments[i+1])
58
    }
Drew's avatar
Drew committed
59 60 61 62 63 64
    if x == Options.Toolchain.rawValue {
        toolchain = Process.arguments[i+1]
        if toolchain == "xcode" {
            toolchain = "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain"
        }
    }
Drew's avatar
Drew committed
65 66 67 68
    if x == Options.Platform.rawValue {
        let platformString = Process.arguments[i+1]
        Platform.targetPlatform = Platform(string: platformString)
    }
69
}
Drew's avatar
Drew committed
70 71 72 73 74 75 76 77 78 79 80 81 82

//build overlays
var overlays : [String] = []
for (i, x) in Process.arguments.enumerated() {
    if x == Options.Overlay.rawValue {
        let overlay = Process.arguments[i+1]
        overlays.append(overlay)
    }
}
overlays.append(contentsOf: Platform.targetPlatform.overlays)

print("enabling overlays \(overlays)")

83
var package: Package! = nil
84

Drew's avatar
Drew committed
85
func usage() {
86 87 88 89 90
    print("atbuild - Anarchy Tools Build Tool \(version)")
    print("https://github.com/AnarchyTools")
    print("© 2016 Anarchy Tools Contributors.")
    print("")
    print("Usage:")
Drew's avatar
Drew committed
91
    print("atbuild [--toolchain (/toolchain/path | xcode)] [-f packagefile] [task] [--clean]")
92

Drew's avatar
Drew committed
93 94 95 96 97 98 99 100
    if let p = package {
        print("tasks:")
        for (key, _) in p.tasks {
            print("    \(key)")
        }
    }
    else {
        print("No tasks are available; run --help in a directory with a build.atpkg for project-specific help")
101
    }
Drew's avatar
Drew committed
102

103 104 105
    exit(1)
}

Drew's avatar
Drew committed
106 107 108 109 110 111 112 113 114 115 116 117
do {
    package = try Package(filepath: packageFile, overlay: overlays, focusOnTask: focusOnTask)
} catch {
    print("Could not load package file: \(error)")
    usage()
}

//usage message
if Process.arguments.contains("--help") {
    usage()
}

Drew's avatar
Drew committed
118

Drew's avatar
Drew committed
119
func runTask(taskName: String, package: Package) {
120
    guard let task = package.tasks[taskName] else { fatalError("No \(taskName) task in build configuration.") }
Drew's avatar
Drew committed
121
    TaskRunner.runTask(task: task, package: package, toolchain: toolchain)
122 123
}

Drew's avatar
Drew committed
124

125 126
//choose which task to run
if Process.arguments.count > 1 {
127 128 129 130 131 132 133
    var i = 1
    while i < Process.arguments.count {
        let arg = Process.arguments[i]
        if Options.allOptions.map({$0.rawValue}).contains(arg) {
            i += 1
        }
        else {
Drew's avatar
Drew committed
134
            focusOnTask = arg
135 136 137
            break
        }
        i += 1
Drew's avatar
Drew committed
138
    }
139
}
Drew's avatar
Drew committed
140 141
if focusOnTask == nil {
    focusOnTask = "default"
142 143
}

Drew's avatar
Drew committed
144 145
print("Building package \(package.name)...")

146
runTask(taskName: focusOnTask!, package: package)
Drew's avatar
Drew committed
147

148 149
//success message
print("Built package \(package.name).")