main.swift 4.35 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.5.0"
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
    case Platform = "--platform"
Drew's avatar
Drew committed
37
    case Configuration = "--configuration"
Drew's avatar
Drew committed
38 39 40 41 42 43 44
    
    static var allOptions : [Options] { return [
        Overlay, 
        CustomFile, 
        Help, 
        Clean, 
        Toolchain, 
Drew's avatar
Drew committed
45 46
        Platform,
        Configuration
Drew's avatar
Drew committed
47 48
        ] 
    }
49 50
}

51
let defaultPackageFile = Path("build.atpkg")
52

Drew's avatar
Drew committed
53
var focusOnTask : String? = nil
Drew's avatar
Drew committed
54

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

Drew's avatar
Drew committed
80

Drew's avatar
Drew committed
81 82
//build overlays
var overlays : [String] = []
Drew's avatar
Drew committed
83
for (i, x) in CommandLine.arguments.enumerated() {
Drew's avatar
Drew committed
84
    if x == Options.Overlay.rawValue {
Drew's avatar
Drew committed
85
        let overlay = CommandLine.arguments[i+1]
Drew's avatar
Drew committed
86 87 88 89 90
        overlays.append(overlay)
    }
}
overlays.append(contentsOf: Platform.targetPlatform.overlays)

Drew's avatar
Drew committed
91 92
overlays.append("atbuild.configuration.\(currentConfiguration)")

Drew's avatar
Drew committed
93 94
print("enabling overlays \(overlays)")

95
var package: Package! = nil
96

Drew's avatar
Drew committed
97
func usage() {
98 99 100 101 102
    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
103
    print("atbuild [--toolchain (/toolchain/path | xcode | xcode-beta )] [-f packagefile] [task] [--clean]")
104

Drew's avatar
Drew committed
105 106 107 108 109 110 111 112
    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")
113
    }
Drew's avatar
Drew committed
114

115 116 117
    exit(1)
}

Drew's avatar
Drew committed
118 119 120 121 122 123 124 125
do {
    package = try Package(filepath: packageFile, overlay: overlays, focusOnTask: focusOnTask)
} catch {
    print("Could not load package file: \(error)")
    usage()
}

//usage message
Drew's avatar
Drew committed
126
if CommandLine.arguments.contains("--help") {
Drew's avatar
Drew committed
127 128 129
    usage()
}

Drew's avatar
Drew committed
130

Drew's avatar
Drew committed
131
func runTask(taskName: String, package: Package) {
132
    guard let task = package.tasks[taskName] else { fatalError("No \(taskName) task in build configuration.") }
Drew's avatar
Drew committed
133
    TaskRunner.runTask(task: task, package: package)
134 135
}

Drew's avatar
Drew committed
136

137
//choose which task to run
Drew's avatar
Drew committed
138
if CommandLine.arguments.count > 1 {
139
    var i = 1
Drew's avatar
Drew committed
140 141
    while i < CommandLine.arguments.count {
        let arg = CommandLine.arguments[i]
142 143 144 145
        if Options.allOptions.map({$0.rawValue}).contains(arg) {
            i += 1
        }
        else {
Drew's avatar
Drew committed
146
            focusOnTask = arg
147 148 149
            break
        }
        i += 1
Drew's avatar
Drew committed
150
    }
151
}
Drew's avatar
Drew committed
152 153
if focusOnTask == nil {
    focusOnTask = "default"
154 155
}

Drew's avatar
Drew committed
156 157
Platform.toolchain = toolchain

Drew's avatar
Drew committed
158 159
print("Building package \(package.name)...")

160
runTask(taskName: focusOnTask!, package: package)
Drew's avatar
Drew committed
161

162 163
//success message
print("Built package \(package.name).")