-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to package an iOS dynamic framework into a format Xcode c…
…an consume (#928)
- Loading branch information
1 parent
e2b1e1e
commit 572aee2
Showing
27 changed files
with
2,052 additions
and
2 deletions.
There are no files selected for viewing
154 changes: 154 additions & 0 deletions
154
apple/internal/aspects/swift_dynamic_framework_aspect.bzl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
# Copyright 2019 The Bazel Authors. All rights reserved. | ||
# | ||
# 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. | ||
|
||
"""Aspect implementation for Swift dynamic framework support.""" | ||
|
||
load( | ||
"@build_bazel_rules_swift//swift:swift.bzl", | ||
"SwiftInfo", | ||
) | ||
|
||
SwiftDynamicFrameworkInfo = provider( | ||
fields = { | ||
"module_name": "The module name for the single swift_library dependency.", | ||
"swiftinterfaces": """ | ||
Dictionary of architecture to the generated swiftinterface file for that architecture. | ||
""", | ||
"swiftdocs": """ | ||
Dictionary of architecture to the generated swiftdoc file for that architecture. | ||
""", | ||
"generated_header": """ | ||
The generated Objective-C header for the single swift_library dependency. | ||
""", | ||
"swiftmodules": """ | ||
Dictionary of architecture to the generated swiftmodule file for that architecture. | ||
""", | ||
"modulemap": """ | ||
Generated modulemap for that architecture. | ||
""", | ||
}, | ||
doc = """ | ||
Provider that collects artifacts required to build a Swift-based dynamic framework. | ||
""", | ||
) | ||
|
||
def _swift_target_for_dep(dep): | ||
"""Returns the target for which the dependency was compiled. | ||
This is really hacky, but there's no easy way to acquire the Apple CPU for which the target was | ||
built. One option would be to let this aspect propagate transitively through deps and have | ||
another provider that propagates the CPU, but the model there gets a bit more complicated to | ||
follow. With this approach, we avoid propagating the aspect transitively as well. | ||
This should be cleaned up when b/141931700 is fixed (adding support for ctx.rule.split_attr). | ||
""" | ||
for action in dep.actions: | ||
if action.mnemonic == "SwiftCompile": | ||
target_found = False | ||
for arg in action.argv: | ||
if target_found: | ||
return arg | ||
if arg == "-target": | ||
target_found = True | ||
fail("error: Expected at least one Swift compilation action for target {}.".format(dep.label)) | ||
|
||
def _swift_arch_for_dep(dep): | ||
"""Returns the architecture for which the dependency was built.""" | ||
target = _swift_target_for_dep(dep) | ||
return target.split("-", 1)[0] | ||
|
||
def _modulemap_contents(module_name): | ||
"""Returns the contents for the modulemap file for the framework.""" | ||
return """\ | ||
framework module {module_name} {{ | ||
header "{module_name}.h" | ||
requires objc | ||
}} | ||
""".format(module_name = module_name) | ||
|
||
def _swift_dynamic_framework_aspect_impl(target, ctx): | ||
"""Aspect implementation for Swift dynamic framework support.""" | ||
|
||
if not hasattr(ctx.rule.attr, "deps"): | ||
return [] | ||
|
||
swiftdeps = [x for x in ctx.rule.attr.deps if SwiftInfo in x] | ||
ccinfos = [x for x in ctx.rule.attr.deps if CcInfo in x] | ||
# If there are no Swift dependencies, return nothing. | ||
if not swiftdeps: | ||
return [] | ||
|
||
if len(swiftdeps) != len(ctx.rule.attr.deps): | ||
fail( | ||
"""\ | ||
error: Found a mix of swift_library and other rule dependencies. Swift dynamic frameworks expect a \ | ||
single swift_library dependency.\ | ||
""", | ||
) | ||
|
||
# Collect all relevant artifacts for Swift dynamic framework generation. | ||
module_name = None | ||
generated_header = None | ||
swiftdocs = {} | ||
swiftmodules = {} | ||
modulemap_file = None | ||
for dep in swiftdeps: | ||
swiftinfo = dep[SwiftInfo] | ||
module_name = swiftinfo.module_name | ||
arch = _swift_arch_for_dep(dep) | ||
|
||
swiftmodule = None | ||
swiftdoc = None | ||
for module in swiftinfo.transitive_modules.to_list(): | ||
if not module.swift: | ||
continue | ||
swiftmodule = module.swift.swiftmodule | ||
swiftdoc = module.swift.swiftdoc | ||
|
||
swiftdocs[arch] = swiftdoc | ||
swiftmodules[arch] = swiftmodule | ||
modulemap_file = ctx.actions.declare_file("{}_file.modulemap".format(module_name)) | ||
ctx.actions.write(modulemap_file, _modulemap_contents(module_name)) | ||
|
||
# Get the generated_header using the CcInfo provider | ||
for dep in ccinfos: | ||
headers = dep[CcInfo].compilation_context.headers.to_list() | ||
if headers: | ||
generated_header = headers.pop(0) | ||
|
||
# Make sure that all dictionaries contain at least one module before returning the provider. | ||
if all([module_name, generated_header, swiftdocs, swiftmodules, modulemap_file]): | ||
return [ | ||
SwiftDynamicFrameworkInfo( | ||
module_name = module_name, | ||
generated_header = generated_header, | ||
swiftdocs = swiftdocs, | ||
swiftmodules = swiftmodules, | ||
modulemap = modulemap_file, | ||
), | ||
] | ||
else: | ||
fail( | ||
"""\ | ||
error: Could not find all required artifacts and information to build a Swift dynamic framework. \ | ||
Please file an issue with a reproducible error case.\ | ||
""", | ||
) | ||
|
||
swift_dynamic_framework_aspect = aspect( | ||
implementation = _swift_dynamic_framework_aspect_impl, | ||
doc = """ | ||
Aspect that collects Swift information to construct a dynamic framework that supports Swift. | ||
""", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.