|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright 2015 - 2016 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See http://swift.org/LICENSE.txt for license information |
| 8 | + See http://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | + |
| 10 | + ----------------------------------------------------------------- |
| 11 | + |
| 12 | + In an effort to provide: |
| 13 | + |
| 14 | + 1. Unique reference identifiers |
| 15 | + 2. Human readable reference identifiers |
| 16 | + 3. Stable reference identifiers |
| 17 | + |
| 18 | + (as opposed to the generated UUIDs Xcode typically generates) |
| 19 | + |
| 20 | + We create identifiers with a constant-length unique prefix and |
| 21 | + a unique suffix where the suffix is the filename or module name |
| 22 | + and since we guarantee uniqueness at the PackageDescription |
| 23 | + layer for these properties we satisfy the above constraints. |
| 24 | +*/ |
| 25 | + |
| 26 | +import struct Utility.Toolchain |
| 27 | +import struct Utility.Path |
| 28 | +import PackageType |
| 29 | + |
| 30 | +let rootObjectReference = "__RootObject_" |
| 31 | +let rootBuildConfigurationListReference = "___RootConfs_" |
| 32 | +let rootBuildConfigurationReference = "_______Debug_" |
| 33 | +let rootGroupReference = "___RootGroup_" |
| 34 | +let productsGroupReference = "____Products_" |
| 35 | +let sourcesGroupReference = "_____Sources_" |
| 36 | +let testsGroupReference = "_______Tests_" |
| 37 | +let linkPhaseFileRefPrefix = "_LinkFileRef_" |
| 38 | +let sourceGroupFileRefPrefix = "__PBXFileRef_" |
| 39 | +let compilePhaseFileRefPrefix = "__src_cc_ref_" |
| 40 | + |
| 41 | +extension Module { |
| 42 | + var dependencyReference: String { return "__Dependency_\(c99name)" } |
| 43 | + var productReference: String { return "_____Product_\(c99name)" } |
| 44 | + var targetReference: String { return "______Target_\(c99name)" } |
| 45 | + var groupReference: String { return "_______Group_\(c99name)" } |
| 46 | + var configurationListReference: String { return "_______Confs_\(c99name)" } |
| 47 | + var configurationReference: String { return "___DebugConf_\(c99name)" } |
| 48 | + var compilePhaseReference: String { return "CompilePhase_\(c99name)" } |
| 49 | + var linkPhaseReference: String { return "___LinkPhase_\(c99name)" } |
| 50 | +} |
| 51 | + |
| 52 | +func fileRef(forLinkPhaseChild module: Module) -> String { |
| 53 | + return linkPhaseFileRefPrefix + module.c99name |
| 54 | +} |
| 55 | + |
| 56 | +private func fileRef(suffixForModuleSourceFile path: String, srcroot: String) -> String { |
| 57 | + let path = Path(path).relative(to: srcroot) |
| 58 | + return path.characters.map{ c -> String in |
| 59 | + switch c { |
| 60 | + case "\\": |
| 61 | + return "\\\\" |
| 62 | + case "'": |
| 63 | + return "\'" |
| 64 | + default: |
| 65 | + return "\(c)" |
| 66 | + } |
| 67 | + }.joinWithSeparator("") |
| 68 | +} |
| 69 | + |
| 70 | +func fileRefs(forModuleSources module: SwiftModule, srcroot: String) -> [(String, String)] { |
| 71 | + return module.sources.relativePaths.map { relativePath in |
| 72 | + let path = Path.join(module.sources.root, relativePath) |
| 73 | + let suffix = fileRef(suffixForModuleSourceFile: path, srcroot: srcroot) |
| 74 | + return ("'\(sourceGroupFileRefPrefix)\(suffix)'", relativePath) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func fileRefs(forCompilePhaseSourcesInModule module: SwiftModule, srcroot: String) -> [(String, String)] { |
| 79 | + return fileRefs(forModuleSources: module, srcroot: srcroot).map { ref1, relativePath in |
| 80 | + let path = Path.join(module.sources.root, relativePath) |
| 81 | + let suffix = fileRef(suffixForModuleSourceFile: path, srcroot: srcroot) |
| 82 | + return (ref1, "'\(compilePhaseFileRefPrefix)\(suffix)'") |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +extension SwiftModule { |
| 87 | + private var isLibrary: Bool { |
| 88 | + return type == .Library |
| 89 | + } |
| 90 | + |
| 91 | + var type: String { |
| 92 | + if self is TestModule { |
| 93 | + return "com.apple.product-type.bundle.unit-test" |
| 94 | + } else if isLibrary { |
| 95 | + return "com.apple.product-type.library.dynamic" |
| 96 | + } else { |
| 97 | + return "com.apple.product-type.tool" |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + var explicitFileType: String { |
| 102 | + func suffix() -> String { |
| 103 | + if self is TestModule { |
| 104 | + return "wrapper.cfbundle" |
| 105 | + } else if isLibrary { |
| 106 | + return "dylib" |
| 107 | + } else { |
| 108 | + return "executable" |
| 109 | + } |
| 110 | + } |
| 111 | + return "compiled.mach-o.\(suffix())" |
| 112 | + } |
| 113 | + |
| 114 | + var productPath: String { |
| 115 | + if self is TestModule { |
| 116 | + return "\(c99name).xctest" |
| 117 | + } else if isLibrary { |
| 118 | + return "\(c99name).dylib" |
| 119 | + } else { |
| 120 | + return name |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + var linkPhaseFileRefs: String { |
| 125 | + return recursiveDependencies.map{ fileRef(forLinkPhaseChild: $0) }.joinWithSeparator(", ") |
| 126 | + } |
| 127 | + |
| 128 | + var nativeTargetDependencies: String { |
| 129 | + return dependencies.map{ $0.dependencyReference }.joinWithSeparator(", ") |
| 130 | + } |
| 131 | + |
| 132 | + var productName: String { |
| 133 | + if isLibrary && !(self is TestModule) { |
| 134 | + // you can go without a lib prefix, but something unexpected will break |
| 135 | + return "'lib$(TARGET_NAME)'" |
| 136 | + } else { |
| 137 | + return "'$(TARGET_NAME)'" |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + var buildSettings: String { |
| 142 | + var buildSettings = ["PRODUCT_NAME": productName] |
| 143 | + buildSettings["PRODUCT_MODULE_NAME"] = c99name |
| 144 | + buildSettings["OTHER_SWIFT_FLAGS"] = "-DXcode" |
| 145 | + buildSettings["MACOSX_DEPLOYMENT_TARGET"] = "'10.10'" |
| 146 | + buildSettings["SWIFT_OPTIMIZATION_LEVEL"] = "-Onone" |
| 147 | + |
| 148 | + // prevents Xcode project upgrade warnings |
| 149 | + buildSettings["COMBINE_HIDPI_IMAGES"] = "YES" |
| 150 | + |
| 151 | + if self is TestModule { |
| 152 | + buildSettings["EMBEDDED_CONTENT_CONTAINS_SWIFT"] = "YES" |
| 153 | + |
| 154 | + //FIXME this should not be required |
| 155 | + buildSettings["LD_RUNPATH_SEARCH_PATHS"] = "'@loader_path/../Frameworks'" |
| 156 | + |
| 157 | + } else { |
| 158 | + |
| 159 | + //FIXME we should not have to set this ourselves, and in fact |
| 160 | + // it is problematic because now you must regenerate the xcodeproj |
| 161 | + // whenever the toolchain changes :( |
| 162 | + // static linking would be better since that is what we are meant |
| 163 | + // to do while swift has no ABI compatability. |
| 164 | + // probably the real solution is to generate frameworks since then |
| 165 | + // Xcode will embed the swift runtime libs |
| 166 | + buildSettings["LD_RUNPATH_SEARCH_PATHS"] = "'\(Toolchain.prefix)/usr/lib/swift/macosx'" |
| 167 | + |
| 168 | + if isLibrary { |
| 169 | + buildSettings["ENABLE_TESTABILITY"] = "YES" |
| 170 | + buildSettings["DYLIB_INSTALL_NAME_BASE"] = "'$(CONFIGURATION_BUILD_DIR)'" |
| 171 | + } else { |
| 172 | + // override default behavior, instead link dynamically |
| 173 | + buildSettings["SWIFT_FORCE_STATIC_LINK_STDLIB"] = "NO" |
| 174 | + buildSettings["SWIFT_FORCE_DYNAMIC_LINK_STDLIB"] = "YES" |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + return buildSettings.map{ "\($0) = \($1);" }.joinWithSeparator(" ") |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | + |
| 183 | +extension SwiftModule { |
| 184 | + var blueprintIdentifier: String { |
| 185 | + return targetReference |
| 186 | + } |
| 187 | + |
| 188 | + var buildableName: String { |
| 189 | + if isLibrary && !(self is TestModule) { |
| 190 | + return "lib\(productPath)" |
| 191 | + } else { |
| 192 | + return productPath |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + var blueprintName: String { |
| 197 | + return name |
| 198 | + } |
| 199 | +} |
0 commit comments