Skip to content

[Release Tooling] Add resources, if any, to bundle within xcframework platform slices (approach 1) #12114

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 17 additions & 28 deletions ReleaseTooling/Sources/ZipBuilder/FrameworkBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ struct FrameworkBuilder {
/// - Parameter logsOutputDir: The path to the directory to place build logs.
/// - Parameter setCarthage: Set Carthage diagnostics flag in build.
/// - Parameter moduleMapContents: Module map contents for all frameworks in this pod.
/// - Returns: A path to the newly compiled frameworks, and Resources.
/// - Returns: A path to the newly compiled frameworks.
func compileFrameworkAndResources(withName framework: String,
logsOutputDir: URL? = nil,
setCarthage: Bool,
podInfo: CocoaPodUtils.PodInfo) -> ([URL], URL?) {
podInfo: CocoaPodUtils.PodInfo) -> [URL] {
let fileManager = FileManager.default
let outputDir = fileManager.temporaryDirectory(withName: "frameworks_being_built")
let logsDir = logsOutputDir ?? fileManager.temporaryDirectory(withName: "build_logs")
Expand All @@ -74,8 +74,7 @@ struct FrameworkBuilder {
}

if dynamicFrameworks {
return (buildDynamicFrameworks(withName: framework, logsDir: logsDir, outputDir: outputDir),
nil)
return buildDynamicFrameworks(withName: framework, logsDir: logsDir, outputDir: outputDir)
} else {
return buildStaticFrameworks(
withName: framework,
Expand Down Expand Up @@ -330,7 +329,7 @@ struct FrameworkBuilder {
logsDir: URL,
outputDir: URL,
setCarthage: Bool,
podInfo: CocoaPodUtils.PodInfo) -> ([URL], URL) {
podInfo: CocoaPodUtils.PodInfo) -> [URL] {
// Build every architecture and save the locations in an array to be assembled.
let slicedFrameworks = buildFrameworksForAllPlatforms(withName: framework, logsDir: logsDir,
setCarthage: setCarthage)
Expand Down Expand Up @@ -383,15 +382,6 @@ struct FrameworkBuilder {

// TODO: copy PrivateHeaders directory as well if it exists. SDWebImage is an example pod.

// Move all the Resources into .bundle directories in the destination Resources dir. The
// Resources live are contained within the folder structure:
// `projectDir/arch/Release-platform/FrameworkName`.
// The Resources are stored at the top-level of the .framework or .xcframework directory.
// For Firebase distributions, they are propagated one level higher in the final distribution.
let resourceContents = projectDir.appendingPathComponents([anyPlatform.buildName,
anyPlatform.buildDirName,
framework])

guard let moduleMapContentsTemplate = podInfo.moduleMapContents else {
fatalError("Module map contents missing for framework \(frameworkName)")
}
Expand All @@ -417,7 +407,7 @@ struct FrameworkBuilder {
""")
}
}
return (frameworks, resourceContents)
return frameworks
}

/// Parses CocoaPods config files or uses the passed in `moduleMapContents` to write the
Expand Down Expand Up @@ -656,6 +646,17 @@ struct FrameworkBuilder {
fatalError("Could not create framework directory needed to build \(framework): \(error)")
}

// Copy resource bundles (if any)
try? fileManager.contentsOfDirectory(
at: frameworkPath.deletingLastPathComponent(),
includingPropertiesForKeys: nil
)
.filter { $0.pathExtension == "bundle" }
.forEach { try! fileManager.copyItem(
at: $0,
to: platformFrameworkDir.appendingPathComponent($0.lastPathComponent)
) }

// Copy the binary to the right location.
let binaryName = frameworkPath.lastPathComponent.replacingOccurrences(of: ".framework",
with: "")
Expand All @@ -682,11 +683,9 @@ struct FrameworkBuilder {
/// - Parameter withName: The framework name.
/// - Parameter frameworks: The grouped frameworks.
/// - Parameter xcframeworksDir: Location at which to build the xcframework.
/// - Parameter resourceContents: Location of the resources for this xcframework.
static func makeXCFramework(withName name: String,
frameworks: [URL],
xcframeworksDir: URL,
resourceContents: URL?) -> URL {
xcframeworksDir: URL) -> URL {
let xcframework = xcframeworksDir
.appendingPathComponent(frameworkBuildName(name) + ".xcframework")

Expand All @@ -711,16 +710,6 @@ struct FrameworkBuilder {
case .success:
print("XCFramework for \(name) built successfully at \(xcframework).")
}
// xcframework resources are packaged at top of xcframework.
if let resourceContents = resourceContents {
let resourceDir = xcframework.appendingPathComponent("Resources")
do {
try ResourcesManager.moveAllBundles(inDirectory: resourceContents, to: resourceDir)
} catch {
fatalError("Could not move bundles into Resources directory while building \(name): " +
"\(error)")
}
}
return xcframework
}
}
23 changes: 8 additions & 15 deletions ReleaseTooling/Sources/ZipBuilder/ZipBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,12 @@ struct ZipBuilder {
var carthageGoogleUtilitiesFrameworks: [URL] = []
var podsBuilt: [String: CocoaPodUtils.PodInfo] = [:]
var xcframeworks: [String: [URL]] = [:]
var resources: [String: URL] = [:]

for platform in platforms {
let projectDir = FileManager.default.temporaryDirectory(withName: "project-" + platform.name)
CocoaPodUtils.podInstallPrepare(inProjectDir: projectDir, templateDir: paths.templateDir)

let platformPods = podsToInstall.filter { $0.platforms.contains(platform.name) }

CocoaPodUtils.installPods(platformPods,
inDir: projectDir,
platform: platform,
Expand Down Expand Up @@ -228,24 +226,21 @@ struct ZipBuilder {
let builder = FrameworkBuilder(projectDir: projectDir,
targetPlatforms: platform.platformTargets,
dynamicFrameworks: dynamicFrameworks)
let (frameworks, resourceContents) =
let frameworks =
builder.compileFrameworkAndResources(withName: podName,
logsOutputDir: paths.logsOutputDir,
setCarthage: false,
podInfo: podInfo)
groupedFrameworks[podName] = (groupedFrameworks[podName] ?? []) + frameworks

if includeCarthage, podName == "GoogleUtilities" {
let (cdFrameworks, _) = builder.compileFrameworkAndResources(withName: podName,
logsOutputDir: paths
.logsOutputDir,
setCarthage: true,
podInfo: podInfo)
let cdFrameworks = builder.compileFrameworkAndResources(withName: podName,
logsOutputDir: paths
.logsOutputDir,
setCarthage: true,
podInfo: podInfo)
carthageGoogleUtilitiesFrameworks += cdFrameworks
}
if resourceContents != nil {
resources[podName] = resourceContents
}
} else if podsBuilt[podName] == nil {
// Binary pods need to be collected once, since the platforms should already be merged.
let binaryFrameworks = collectBinaryFrameworks(fromPod: podName, podInfo: podInfo)
Expand Down Expand Up @@ -274,8 +269,7 @@ struct ZipBuilder {
let name = groupedFramework.key
let xcframework = FrameworkBuilder.makeXCFramework(withName: name,
frameworks: groupedFramework.value,
xcframeworksDir: xcframeworksDir,
resourceContents: resources[name])
xcframeworksDir: xcframeworksDir)
xcframeworks[name] = [xcframework]
}
for (framework, paths) in xcframeworks {
Expand All @@ -296,8 +290,7 @@ struct ZipBuilder {
let carthageGoogleUtilitiesXcframework = FrameworkBuilder.makeXCFramework(
withName: "GoogleUtilities",
frameworks: carthageGoogleUtilitiesFrameworks,
xcframeworksDir: xcframeworksCarthageDir,
resourceContents: nil
xcframeworksDir: xcframeworksCarthageDir
)
return (podsBuilt, xcframeworks, carthageGoogleUtilitiesXcframework)
}
Expand Down