|
14 | 14 |
|
15 | 15 | """Implementation of the `swift_compiler_plugin` rule."""
|
16 | 16 |
|
| 17 | +load("@build_bazel_apple_support//lib:apple_support.bzl", "apple_support") |
| 18 | +load("@build_bazel_apple_support//lib:lipo.bzl", "lipo") |
| 19 | +load("@build_bazel_apple_support//lib:transitions.bzl", "macos_universal_transition") |
17 | 20 | load(
|
18 | 21 | "@build_bazel_rules_swift//swift/internal:compiling.bzl",
|
19 | 22 | "output_groups_from_other_compilation_outputs",
|
@@ -254,3 +257,121 @@ swift_library(
|
254 | 257 | fragments = ["cpp"],
|
255 | 258 | implementation = _swift_compiler_plugin_impl,
|
256 | 259 | )
|
| 260 | + |
| 261 | +def _universal_swift_compiler_plugin_impl(ctx): |
| 262 | + inputs = [ |
| 263 | + plugin.files.to_list()[0] |
| 264 | + for plugin in ctx.split_attr.plugin.values() |
| 265 | + ] |
| 266 | + |
| 267 | + if not inputs: |
| 268 | + fail("Target (%s) `plugin` label ('%s') does not provide any " + |
| 269 | + "file for universal binary" % (ctx.attr.name, ctx.attr.plugin)) |
| 270 | + |
| 271 | + output = ctx.actions.declare_file(ctx.label.name) |
| 272 | + if len(inputs) > 1: |
| 273 | + lipo.create( |
| 274 | + actions = ctx.actions, |
| 275 | + apple_fragment = ctx.fragments.apple, |
| 276 | + inputs = inputs, |
| 277 | + output = output, |
| 278 | + xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig], |
| 279 | + ) |
| 280 | + else: |
| 281 | + ctx.actions.symlink(target_file = inputs[0], output = output) |
| 282 | + |
| 283 | + cc_infos = [] |
| 284 | + direct_swift_modules = [] |
| 285 | + module_name = None |
| 286 | + swift_infos = [] |
| 287 | + for plugin in ctx.split_attr.plugin.values(): |
| 288 | + cc_infos.append(plugin[SwiftCompilerPluginInfo].cc_info) |
| 289 | + direct_swift_modules.extend(plugin[SwiftCompilerPluginInfo].swift_info.direct_modules) |
| 290 | + module_name = plugin[SwiftCompilerPluginInfo].module_names.to_list()[0] |
| 291 | + swift_infos.append(plugin[SwiftCompilerPluginInfo].swift_info) |
| 292 | + |
| 293 | + first_output_group_info = ctx.split_attr.plugin.values()[0][OutputGroupInfo] |
| 294 | + combined_output_group_info = {} |
| 295 | + for key in first_output_group_info: |
| 296 | + all_values = [] |
| 297 | + for plugin in ctx.split_attr.plugin.values(): |
| 298 | + all_values.append(plugin[OutputGroupInfo][key]) |
| 299 | + combined_output_group_info[key] = depset(transitive = all_values) |
| 300 | + |
| 301 | + transitive_runfiles = [ |
| 302 | + plugin[DefaultInfo].default_runfiles |
| 303 | + for plugin in ctx.split_attr.plugin.values() |
| 304 | + ] |
| 305 | + |
| 306 | + return [ |
| 307 | + DefaultInfo( |
| 308 | + executable = output, |
| 309 | + files = depset([output]), |
| 310 | + runfiles = ctx.runfiles().merge_all(transitive_runfiles), |
| 311 | + ), |
| 312 | + OutputGroupInfo(**combined_output_group_info), |
| 313 | + SwiftCompilerPluginInfo( |
| 314 | + cc_info = cc_common.merge_cc_infos(cc_infos = cc_infos), |
| 315 | + executable = output, |
| 316 | + module_names = depset([module_name]), |
| 317 | + swift_info = swift_common.create_swift_info( |
| 318 | + modules = direct_swift_modules, |
| 319 | + swift_infos = swift_infos, |
| 320 | + ), |
| 321 | + ), |
| 322 | + ] |
| 323 | + |
| 324 | +universal_swift_compiler_plugin = rule( |
| 325 | + attrs = dicts.add( |
| 326 | + apple_support.action_required_attrs(), |
| 327 | + { |
| 328 | + "plugin": attr.label( |
| 329 | + cfg = macos_universal_transition, |
| 330 | + doc = "Target to generate a 'fat' binary from.", |
| 331 | + mandatory = True, |
| 332 | + providers = [SwiftCompilerPluginInfo], |
| 333 | + ), |
| 334 | + "_allowlist_function_transition": attr.label( |
| 335 | + default = "@bazel_tools//tools/allowlists/function_transition_allowlist", |
| 336 | + ), |
| 337 | + }, |
| 338 | + ), |
| 339 | + executable = True, |
| 340 | + fragments = ["cpp", "apple"], |
| 341 | + implementation = _universal_swift_compiler_plugin_impl, |
| 342 | + doc = """\ |
| 343 | +Wraps an existing `swift_compiler_plugin` target to produce a universal binary. |
| 344 | +
|
| 345 | +This is useful to allow sharing of caches between Intel and Apple Silicon Macs |
| 346 | +at the cost of building everything twice. |
| 347 | +
|
| 348 | +Example: |
| 349 | +
|
| 350 | +```bzl |
| 351 | +# The actual macro code, using SwiftSyntax, as usual. |
| 352 | +swift_compiler_plugin( |
| 353 | + name = "Macros", |
| 354 | + srcs = glob(["Macros/*.swift"]), |
| 355 | + deps = [ |
| 356 | + "@SwiftSyntax", |
| 357 | + "@SwiftSyntax//:SwiftCompilerPlugin", |
| 358 | + "@SwiftSyntax//:SwiftSyntaxMacros", |
| 359 | + ], |
| 360 | +) |
| 361 | +
|
| 362 | +# Wrap your compiler plugin in this universal shim. |
| 363 | +universal_swift_compiler_plugin( |
| 364 | + name = "Macros.universal", |
| 365 | + plugin = ":Macros", |
| 366 | +) |
| 367 | +
|
| 368 | +# The library that defines the macro hook for use in your project, this |
| 369 | +# references the universal_swift_compiler_plugin. |
| 370 | +swift_library( |
| 371 | + name = "MacroLibrary", |
| 372 | + srcs = glob(["MacroLibrary/*.swift"]), |
| 373 | + plugins = [":Macros.universal"], |
| 374 | +) |
| 375 | +``` |
| 376 | +""", |
| 377 | +) |
0 commit comments