Shell.swift 3.49 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
#if os(Linux)
import Glibc
#else
import Darwin
#endif
20

21
import atfoundation
22 23 24 25 26 27 28 29
import atpkg

/**
 * The shell tool forks a new process with `/bin/sh -c`. Any arguments specified
 * within the task will also be sent across.
 * If the tool returns with an error code of non-zero, the tool will fail.
 */
final class Shell : Tool {
Drew's avatar
Drew committed
30

Drew's avatar
Drew committed
31 32 33 34
    static private func mysetEnv(_ key: String, _ val: String) {
        setenv(key,val,1)
    }

Drew's avatar
Drew committed
35 36
    ///Sets the environment for the specified task.
    static func environvironment(task: Task, block: () -> ()) {
Drew's avatar
Drew committed
37
        setenv("ATBUILD_PLATFORM", "\(Platform.targetPlatform)", 1)
38
        setenv("ATBUILD_USER_PATH", userPath().description, 1)
39 40 41
        if let version = task.package.version {
            setenv("ATBUILD_PACKAGE_VERSION", version, 1)
        }
42

Drew's avatar
Drew committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56
        setenv("ATBUILD_CONFIGURATION", "\(currentConfiguration)",1)
        if let o = currentConfiguration.optimize {
            mysetEnv("ATBUILD_CONFIGURATION_OPTIMIZE", o ? "1":"0")
        }
        if let o = currentConfiguration.fastCompile {
            mysetEnv("ATBUILD_CONFIGURATION_FAST_COMPILE", o ? "1":"0")
        }
        if let o = currentConfiguration.testingEnabled {
            mysetEnv("ATBUILD_CONFIGURATION_TESTING_ENABLED", o ? "1":"0")
        }
        if let o = currentConfiguration.noMagic {
            mysetEnv("ATBUILD_CONFIGURATION_NO_MAGIC", o ? "1":"0")
        }

57 58 59 60 61 62 63 64 65 66 67 68 69
        //expose debug configuration info
        let conf: String
        switch (currentConfiguration.debugInstrumentation) {
            case .Included:
            conf = "included"
            case .Omitted:
            conf = "omitted"
            case .Stripped:
            conf = "stripped"
        }
        mysetEnv("ATBUILD_CONFIGURATION_DEBUG_INSTRUMENTATION", conf)


Drew's avatar
Drew committed
70 71 72 73 74 75 76
        //does bin path not exist?
        //let's create it!
        let binPath = try! FS.getWorkingDirectory().appending("bin")
        if !FS.fileExists(path: binPath) {
            try! FS.createDirectory(path: binPath)
        }
        setenv("ATBUILD_BIN_PATH",binPath.description,1)
77

Drew's avatar
Drew committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
        //deal with directory nonsense
        let oldPath = try! FS.getWorkingDirectory()
        defer {
            do {
                try FS.changeWorkingDirectory(path: oldPath)
            } catch {
                print("Can not revert to previous working directory '\(oldPath)': \(error)")
                exit(42)
            }
        }
        do {
            try FS.changeWorkingDirectory(path: task.importedPath)
        }
        catch {
            print("Cannot change working directory to \(task.importedPath)")
        }
        block()
    }
    func run(task: Task, toolchain: String) {
        guard var script = task["script"]?.string else { fatalError("Invalid 'script' argument to shell tool.") }
        script = evaluateSubstitutions(input: script, package: task.package)
        Shell.environvironment(task: task) {
            anarchySystem(script)
101 102 103
        }
    }
}