Configuration.swift 4.14 KB
Newer Older
Drew's avatar
Drew committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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.

public enum Configuration  {
16 17 18 19 20 21 22 23 24

    public enum DebugInstrumentationType {
        ///Instrumentation should be included in the target
        case Included
        ///No instrumentation for the target
        case Omitted
        ///Instumentation should be generated separately, e.g. DWARF
        case Stripped
    }
Drew's avatar
Drew committed
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    ///Built-in configurations

    ///The default configuration.
    ///Choose settings suitable for interactive debugging, like O0, -g, etc.
    ///Generally, operations that slow down compile times should be avoided.
    case Debug

    ///A configuration appropriate for distribution.
    ///Choose settings suitable for final, production-quality software.
    case Release

    ///A configuration appropriate for unit (correctness) tests.
    case Test

    ///A configuration appropriate for performance-sensitive tests or benchmarks
    case Benchmark

    ///A "plain" or "no-magic" configuration
    ///Do not assume anything about the user's intent
    ///Provide only default behavior
    case None

    ///Like "None", but with a custom name.
    case User(String)

    public init(string: String) {
        if string == "debug" { self = .Debug }
        else if string == "release" { self = .Release }
        else if string == "test" { self = .Test }
        else if string == "bench" { self = .Benchmark }
        else if string == "none" { self = .None }
        else {self = .User(string)}
    }
}

extension Configuration:CustomStringConvertible {
    public var description: String {
        switch(self) {
            case .Debug: return "debug"
            case .Release: return "release"
            case .Test: return "test" 
            case .Benchmark: return "benchmark"
            case .None: return "none"
            case .User(let str): return str
        }
    }
}

/// The default configuration is Debug
///- warning: Switching on this value directly is discouraged; instead switch on one of its ivars.
public var currentConfiguration = Configuration.Debug

///Helper configuration options
///The following options may be more convenient than working with configurations directly
extension Configuration {

    ///Whether tools should produce optimized software
    var optimize: Bool? {
        switch(self) {
            case .Benchmark, .Release: return true
            case .Debug, .Test: return false
            case .User, .None: return nil
        }
    }

    ///Whether tools should prefer compile speed over compile quality
    var fastCompile: Bool? {
        switch(self) {
            case .Debug, .Test: return true
            case .Release, .Benchmark: return false
            case .User, .None: return nil
        }
    }

    ///Whether tools should compile "for testing".
    ///This value is appropriate for deciding whether to compile tests at all, or whether `-enable-testing` should be used
    var testingEnabled: Bool? {
        switch(self) {
            case .Benchmark, .Test: return true
            case .Debug, .Release: return false
            case .User, .None: return nil
        }
    }

    ///Whether tools are explicitly asked to compile "without magic"
    var noMagic: Bool? {
        switch(self) {
            case .Debug, .Release, .Benchmark, .Test: return false
            case .None: return true
            case .User: return nil
        }
    }
117 118 119 120 121 122 123 124 125

    ///Whether debug instrumentation 
    var debugInstrumentation: DebugInstrumentationType {
        switch(self) {
            case .Debug, .Test: return .Included
            case .Release, .Benchmark: return .Stripped
            case .User, .None: return .Omitted
        }
    }
Drew's avatar
Drew committed
126
}