-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmixed_static_framework.bzl
509 lines (460 loc) · 18.2 KB
/
mixed_static_framework.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# Copyright 2020 LINE Corporation
#
# LINE Corporation licenses this file to you 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:
#
# https://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.
"""Rule for creating a multi-architecture static framework for a mixed
Objective-C and Swift module."""
load(
"@build_bazel_rules_swift//swift:swift.bzl",
"SwiftInfo",
"swift_library",
)
load(
"@build_bazel_rules_apple//apple:ios.bzl",
"ios_static_framework",
)
load(
"@build_bazel_rules_apple//apple:providers.bzl",
"AppleBundleInfo",
)
load("@bazel_skylib//lib:paths.bzl", "paths")
load(":defines.bzl", "OBJC_DEFINES", "SWIFT_DEFINES")
load(":headermap_support.bzl", "headermap_support")
load(":module_map.bzl", "module_map")
load(":objc_module_map_config.bzl", "objc_module_map_config")
load(
":common.bzl",
"DEFAULT_MINIMUM_OS_VERSION",
"DEFAULT_VISIBILITY",
"HEADERS_FILE_TYPES",
"OBJC_FILE_TYPES",
"SHARED_COMPILER_OPTIONS",
"SHARED_OBJC_COMPILER_OPTIONS",
"SHARED_SWIFT_COMPILER_OPTIONS",
"SWIFT_FILE_TYPES",
)
load(":zipper_support.bzl", "swiftmodule_zipper_arg_format")
_PLATFORM_TO_SWIFTMODULE = {
"ios_armv7": "arm",
"ios_arm64": "arm64",
"ios_i386": "i386",
"ios_sim_arm64": "arm64",
"ios_x86_64": "x86_64",
}
def _objc_headers_impl(ctx):
# Get all the Obj-C headers
headers = []
for dep in ctx.attr.deps:
objc_headers = dep[CcInfo].compilation_context.headers.to_list()
for hdr in objc_headers:
if hdr.owner == dep.label:
headers.append(hdr)
return [
DefaultInfo(
files = depset(headers),
),
]
_objc_headers = rule(
_objc_headers_impl,
attrs = {
"deps": attr.label_list(
providers = [SwiftInfo],
),
},
)
def _mixed_static_framework_impl(ctx):
bundle_info = ctx.attr.framework[AppleBundleInfo]
framework_name = bundle_info.bundle_name + bundle_info.bundle_extension
new_framework = ctx.actions.declare_file(ctx.label.name + ".zip")
inputs = [
ctx.file.framework,
]
zipper_args = ctx.actions.args()
# Get the `swiftdoc` and `swiftmodule` files for each architecture.
for arch, target in ctx.split_attr.swift_partial_target.items():
cpu = _PLATFORM_TO_SWIFTMODULE[arch]
if not cpu:
continue
direct_module = target[SwiftInfo].direct_modules[0]
swiftdoc = direct_module.swift.swiftdoc
swiftmodule = direct_module.swift.swiftmodule
inputs.extend([swiftmodule, swiftdoc])
module_name = direct_module.name
# There is a bit of hardcoding here, but this would avoid the file
# from needing to be evaluated at analysis phase.
zipper_args.add(
swiftdoc,
format = swiftmodule_zipper_arg_format(
framework = framework_name,
module_name = module_name,
cpu = cpu,
extension = "swiftdoc",
),
)
zipper_args.add(
swiftmodule,
format = swiftmodule_zipper_arg_format(
framework = framework_name,
module_name = module_name,
cpu = cpu,
extension = "swiftmodule",
),
)
ctx.actions.run_shell(
inputs = inputs,
outputs = [new_framework],
mnemonic = "SwiftFrameworkPostProcess",
progress_message = "Postprocessing %s for Swift support" % framework_name,
command = """
{zipper} x {framework}
{zipper} c {new_framework} $(find {framework_name} -type f) $@
rm -rf {framework}
""".format(
framework = ctx.file.framework.path,
framework_name = framework_name,
new_framework = new_framework.path,
zipper = ctx.executable._zipper.path,
),
arguments = [zipper_args],
tools = [
ctx.executable._zipper,
],
)
return [
DefaultInfo(
files = depset([new_framework]),
),
]
_mixed_static_framework = rule(
implementation = _mixed_static_framework_impl,
attrs = dict(
framework = attr.label(
providers = [AppleBundleInfo],
allow_single_file = True,
),
swift_partial_target = attr.label(
mandatory = True,
providers = [SwiftInfo],
cfg = apple_common.multi_arch_split,
),
minimum_os_version = attr.string(
mandatory = True,
),
platform_type = attr.string(
default = str(apple_common.platform_type.ios),
),
_zipper = attr.label(
default = "@bazel_tools//tools/zip:zipper",
cfg = "exec",
executable = True,
),
),
fragments = ["apple"],
outputs = {
"output_file": "%{name}.zip",
},
)
def mixed_static_framework(
name,
srcs,
non_arc_srcs = [],
hdrs = [],
textual_hdrs = [],
enable_modules = True,
includes = [],
copts = [],
objc_copts = [],
swift_copts = [],
use_defines = None,
swiftc_inputs = [],
objc_deps = [],
swift_deps = [],
avoid_deps = None,
deps = [],
data = [],
umbrella_header = None,
visibility = DEFAULT_VISIBILITY,
minimum_os_version = DEFAULT_MINIMUM_OS_VERSION,
features = [],
**kwargs):
"""Builds and bundles a static framework for Xcode consumption or third-party distribution.
This supports Swift only targets and mixed language targets. If your target
only contains Objective-C source code, use `objc_static_framework` rule
instead.
This rule in general is very similar to `build_bazel_rules_apple`'s
`ios_static_framework` rule, with some differences:
* It supports Swift as well as mixed Objective-C and Swift targets.
* It supports bundling a swift_library target that depend transitively on
any other swift_library targets. By default, it will not link any of
its dependencies into the final framework binary - the same way Xcode
does when it builds frameworks - which means you can prebuild your
dependencies as static frameworks for Xcode consumption.
* It supports header maps out of the box--you don't need to change your
imports to make your code build with Bazel.
* It always collects the Swift generated header and bundles a
`module.modulemap` file. For a mixed language target, the module map
file is an extended module map.
* It bundles `swiftmodule` and `swiftdoc` files (`ios_static_framework`
rule bundles `swiftinterface` instead of `swiftmodule` file).
This rule uses the native `objc_library` rule and `rules_swift`'s
`swift_library` in its implementation. Even if you're not building a static
framework for Xcode consumption or third-party distribution, this can still
be used as a convenient way to declare a library target that compiles mixed
Objective-C and Swift source code.
The macro contains 3 underlying rules--given `name` is `Greet`:
* `Greet_swift`: a `swift_library` target. This target has private
visibility by default, hence it can't be a dependency of any other
target. It should not be used directly.
* `GreetModule`: a `module_map` target. This has the same visibility as
`Greet`. The common use-case is using it in an `objc_library`'s
`copts` attribute to import the generated module map file (e.g.
`-fmodule-map-file=$(execpath //path/to/package:GreetModule)`). This
will be done automatically if the dependent target is also a
`mixed_static_framework` target.
* `Greet`: an `objc_library` target. This is the wrapper library target.
This can be depended on any `objc_library` or `swift_library` target.
### Examples
```starlark
load("@rules_apple_line//apple:mixed_static_framework.bzl", "mixed_static_framework")
mixed_static_framework(
name = "Mixed",
srcs = glob([
"*.m",
"*.swift",
]),
hdrs = glob(["*.h"]),
)
```
Args:
name: A unique name for this target. This will be the name of the
library target that the framework depends on. The framework target
will be named `${name}Framework`.
srcs: The list of Objective-C and Swift source files to compile.
non_arc_srcs: The Objective-C source files to compile that do not use
ARC. Provide both `srcs` and `non_arc_srcs` explicitly if both kinds
of source files should be included.
hdrs: The list of C, C++, Objective-C, and Objective-C++ header files
published by this library to be included by sources in dependent
rules. These headers describe the public interface for the library
and will be made available for inclusion by sources in this rule or
in dependent rules. Headers not meant to be included by a client of
this library should be listed in the `srcs` attribute instead. These
will be compiled separately from the source if modules are enabled.
textual_hdrs: The list of C, C++, Objective-C, and Objective-C++ files
that are included as headers by source files in this rule or by users
of this library. Unlike `hdrs`, these will not be compiled separately
from the sources.
enable_modules: Enables clang module support (via `-fmodules`).
Note: This is `True` by default. Changing this to `False` might no
longer work. This attribute is here because there are still targets
which are referencing to it.
includes: List of header search paths to add to this target and all
depending targets. Unlike `copts`, these flags are added for this
rule and every rule that depends on it. (Note: not the rules it
depends upon!) Be very careful, since this may have far-reaching
effects. When in doubt, add "-iquote" flags to `copts` instead.
Usage of this is rarely necessary because all headers will be visible
to their depended targets with the help of header maps.
copts: Additional compiler options that should be passed to `clang` and
`swiftc`.
objc_copts: Additional compiler options that should be passed to `clang`.
swift_copts: Additional compiler options that should be passed to `swiftc`.
swiftc_inputs: Additional files that are referenced using `$(rootpath
...)` and `$(execpath ...)` in attributes that support location
expansion (e.g. `copts`).
objc_deps: Dependencies of the underlying `objc_library` target.
swift_deps: Dependencies of the underlying `swift_library` target.
deps: Dependencies of the both `objc_library` and `swift_library` targets.
avoid_deps: A list of `objc_library` and `swift_library` targets on which
this framework depends in order to compile, but the transitive
closure of which will not be linked into the framework's binary. By
default this is the same as `deps`, that is none of the
depependencies will be linked into the framework's binary. For
example, providing an empty list (`[]`) here will result in a fully
static link binary.
data: The list of files needed by this rule at runtime. These will be
bundled to the top level directory of the bundling target (`.app` or
`.framework`).
umbrella_header: An optional single `.h` file to use as the umbrella
header for this framework. Usually, this header will have the same name
as this target, so that clients can load the header using the #import
`<MyFramework/MyFramework.h>` format. If this attribute is not specified
(the common use case), an umbrella header will be generated under the
same name as this target.
visibility: The visibility specifications for this target.
features: Features of the underlying `swift_library` target.
minimum_os_version: Minimum os version.
**kwargs: Additional arguments being passed through.
"""
swift_srcs = []
objc_srcs = []
private_hdrs = []
for x in srcs:
_, extension = paths.split_extension(x)
if extension in SWIFT_FILE_TYPES:
swift_srcs.append(x)
elif extension in OBJC_FILE_TYPES:
objc_srcs.append(x)
if extension in HEADERS_FILE_TYPES:
private_hdrs.append(x)
module_name = kwargs.get("module_name", name)
objc_library_name = name
swift_library_name = name + "_swift"
objc_deps = objc_deps + deps
swift_deps = swift_deps + deps
headermaps = headermap_support(
name = name,
module_name = module_name,
hdrs = hdrs,
private_hdrs = private_hdrs,
deps = deps,
)
headermap_deps = [
headermaps["public_hmap"],
headermaps["private_hmap"],
headermaps["private_angled_hmap"],
]
objc_deps += headermap_deps
swift_deps += headermap_deps
headermap_copts = headermaps["headermap_copts"]
if use_defines == None:
use_defines = native.repository_name() == "@"
if use_defines:
swift_defines = SWIFT_DEFINES
objc_defines = OBJC_DEFINES
else:
swift_defines = []
objc_defines = []
swift_copts = SHARED_COMPILER_OPTIONS + swift_defines + SHARED_SWIFT_COMPILER_OPTIONS + swift_copts + [
"-Xfrontend",
"-enable-objc-interop",
"-import-underlying-module",
]
for copt in headermap_copts:
swift_copts += [
"-Xcc",
copt,
]
objc_deps = objc_deps + [":" + swift_library_name]
# Add Obj-C includes to Swift header search paths
repository_name = native.repository_name()
for x in includes:
include = x if repository_name == "@" else "external/" + repository_name.lstrip("@") + "/" + x
swift_copts += [
"-Xcc",
"-I{}".format(include),
]
# Generate module map for the underlying module
module_map(
name = name + "_objc_module",
hdrs = hdrs,
textual_hdrs = textual_hdrs,
module_name = module_name,
)
objc_module_map = ":" + name + "_objc_module"
swiftc_inputs = swiftc_inputs + hdrs + textual_hdrs + private_hdrs + [objc_module_map] + headermap_deps
swift_copts += [
"-Xcc",
"-fmodule-map-file=$(execpath {})".format(objc_module_map),
]
swift_features = features + ["swift.no_generated_module_map"]
swift_library(
name = swift_library_name,
srcs = swift_srcs,
swiftc_inputs = swiftc_inputs,
copts = swift_copts,
module_name = module_name,
visibility = ["//visibility:private"],
features = swift_features,
deps = swift_deps,
generates_header = True,
generated_header_name = module_name + "-Swift.h",
)
objc_copts = SHARED_COMPILER_OPTIONS + objc_defines + SHARED_OBJC_COMPILER_OPTIONS + objc_copts + headermap_copts
# Modules is not enabled if a custom module map is present even with
# `enable_modules` set to `True`. This forcibly enables it.
# See https://github.com/bazelbuild/bazel/blob/18d01e7f6d8a3f5b4b4487e9d61a6d4d0f74f33a/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java#L1280
if enable_modules:
objc_copts += ["-fmodules"]
# The extended module map for mixed language modules can't have the name
# "module.modulemap", otherwise it would cause duplicate definition errors.
module_map(
name = name + "Module",
hdrs = hdrs,
deps = [":" + swift_library_name],
module_name = module_name,
visibility = visibility,
)
umbrella_module_map = name + "Module"
objc_deps += [name + "Module"]
objc_module_map_config_name = name + "_module_maps"
objc_module_map_config(
name = objc_module_map_config_name,
deps = deps,
out = name + "_module_map_config.cfg",
)
objc_deps += [":" + objc_module_map_config_name]
if deps:
objc_copts += [
"--config",
"$(execpath {})".format(":" + objc_module_map_config_name),
]
native.objc_library(
name = objc_library_name,
module_map = umbrella_module_map,
enable_modules = enable_modules,
includes = includes,
srcs = objc_srcs,
non_arc_srcs = non_arc_srcs,
hdrs = hdrs + [
# These aren't headers but here is the only place to declare these
# files as the inputs because objc_library doesn't have an attribute
# to declare custom inputs.
":" + objc_module_map_config_name,
":" + umbrella_module_map,
],
textual_hdrs = textual_hdrs,
copts = objc_copts,
deps = objc_deps,
data = data,
pch = kwargs.get("pch", None),
sdk_frameworks = kwargs.get("sdk_frameworks", []),
visibility = visibility,
)
_objc_headers(
name = name + ".hdrs",
deps = [
":" + swift_library_name,
],
)
if avoid_deps == None:
avoid_deps = deps
ios_static_framework(
name = name + ".intermediate",
hdrs = hdrs + textual_hdrs + [
":" + name + ".hdrs",
],
deps = [
":" + objc_library_name,
],
avoid_deps = avoid_deps,
bundle_name = module_name,
minimum_os_version = minimum_os_version,
umbrella_header = umbrella_header,
)
_mixed_static_framework(
name = name + "Framework",
framework = name + ".intermediate",
swift_partial_target = ":" + swift_library_name,
minimum_os_version = minimum_os_version,
visibility = visibility,
)