Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip building static frameworks by checking Mach-O type #894

Merged
merged 4 commits into from
Nov 6, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 68 additions & 12 deletions Source/CarthageKit/Xcode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,52 @@ public enum ProductType: String {
}
}

/// Describes the type of Mach-O files.
/// See https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html#//apple_ref/doc/uid/TP40003931-CH3-SW73.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$5 says this link won't work in a year. 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll remove it. 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, you should leave it in. I was just joking because it's frustrating how Apple breaks these links all the time. ☺️

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, forgive my misunderstanding. 😝

public enum MachOType: String {
/// Executable binary.
case Executable = "mh_executable"

/// Bundle binary.
case Bundle = "mh_bundle"

/// Relocatable object file.
case Object = "mh_object"

/// Dynamic library binary.
case Dylib = "mh_dylib"

/// Static library binary.
case Staticlib = "staticlib"

/// Attempts to parse a Mach-O type from a string returned from `xcodebuild`.
public static func fromString(string: String) -> Result<MachOType, CarthageError> {
return Result(self.init(rawValue: string), failWith: .ParseError(description: "unexpected Mach-O type \"\(string)\""))
}
}

/// Describes the type of frameworks.
private enum FrameworkType {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced this should be private or public.

/// A dynamic framework.
case Dynamic

/// A static framework.
case Static

init?(productType: ProductType, machOType: MachOType) {
switch (productType, machOType) {
case (.Framework, .Dylib):
self = .Dynamic

case (.Framework, .Staticlib):
self = .Static

case _:
return nil
}
}
}

/// A map of build settings and their values, as generated by Xcode.
public struct BuildSettings {
/// The target to which these settings apply.
Expand Down Expand Up @@ -586,6 +632,16 @@ public struct BuildSettings {
}
}

/// Attempts to determine the MachOType specified in these build settings.
public var machOType: Result<MachOType, CarthageError> {
return self["MACH_O_TYPE"].flatMap(MachOType.fromString)
}

/// Attempts to determine the FrameworkType identified by these build settings.
private var frameworkType: Result<FrameworkType?, CarthageError> {
return (productType &&& machOType).map(FrameworkType.init)
}

/// Attempts to determine the URL to the built products directory.
public var builtProductsDirectoryURL: Result<NSURL, CarthageError> {
return self["BUILT_PRODUCTS_DIR"].map { productsDir in
Expand Down Expand Up @@ -728,31 +784,31 @@ private func mergeModuleIntoModule(sourceModuleDirectoryURL: NSURL, _ destinatio
}
}

/// Determines whether the specified product type should be built automatically.
private func shouldBuildProductType(productType: ProductType) -> Bool {
return productType == .Framework
/// Determines whether the specified framework type should be built automatically.
private func shouldBuildFrameworkType(frameworkType: FrameworkType?) -> Bool {
return frameworkType == .Dynamic
}

/// Determines whether the given scheme should be built automatically.
private func shouldBuildScheme(buildArguments: BuildArguments, _ forPlatforms: Set<Platform>) -> SignalProducer<Bool, CarthageError> {
precondition(buildArguments.scheme != nil)

return BuildSettings.loadWithArguments(buildArguments)
.flatMap(.Concat) { settings -> SignalProducer<ProductType, CarthageError> in
let productType = SignalProducer(result: settings.productType)
.flatMap(.Concat) { settings -> SignalProducer<FrameworkType?, CarthageError> in
let frameworkType = SignalProducer(result: settings.frameworkType)

if forPlatforms.isEmpty {
return productType
return frameworkType
.flatMapError { _ in .empty }
} else {
return settings.buildSDKs
.filter { forPlatforms.contains($0.platform) }
.flatMap(.Merge) { _ in productType }
.flatMap(.Merge) { _ in frameworkType }
.flatMapError { _ in .empty }
}
}
.filter(shouldBuildProductType)
// If we find any framework target, we should indeed build this scheme.
.filter(shouldBuildFrameworkType)
// If we find any dynamic framework target, we should indeed build this scheme.
.map { _ in true }
// Otherwise, nope.
.concat(SignalProducer(value: false))
Expand Down Expand Up @@ -908,9 +964,9 @@ public func buildScheme(scheme: String, withConfiguration configuration: String,

return BuildSettings.loadWithArguments(argsForLoading)
.filter { settings in
// Only copy build products for the product types we care about.
if let productType = settings.productType.value {
return shouldBuildProductType(productType)
// Only copy build products for the framework type we care about.
if let frameworkType = settings.frameworkType.value {
return shouldBuildFrameworkType(frameworkType)
} else {
return false
}
Expand Down