Skip to content

Commit 74aa76e

Browse files
authored
Add generate_static_framework_xcframework test rule
Previously we could generate dynamic framework or static library based xcframeworks, but not static framework based xcframeworks. Right now we do a lot of generation of those manually, but we can replace them with this.
1 parent 29b4df6 commit 74aa76e

1 file changed

Lines changed: 129 additions & 0 deletions

File tree

test/testdata/xcframeworks/generate_xcframework.bzl

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,84 @@ def _generate_static_xcframework_impl(ctx):
427427
),
428428
]
429429

430+
def _generate_static_framework_xcframework_impl(ctx):
431+
"""Implementation of generate_static_framework_xcframework."""
432+
actions = ctx.actions
433+
apple_fragment = ctx.fragments.apple
434+
label = ctx.label
435+
target_dir = paths.join(ctx.bin_dir.path, label.package)
436+
xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig]
437+
438+
srcs = ctx.files.srcs
439+
hdrs = ctx.files.hdrs
440+
platforms = ctx.attr.platforms
441+
minimum_os_versions = ctx.attr.minimum_os_versions
442+
443+
if platforms.keys() != minimum_os_versions.keys():
444+
fail("Attributes: 'platforms' and 'minimum_os_versions' must define the same keys")
445+
446+
frameworks = {}
447+
for platform in platforms:
448+
sdk = _sdk_for_platform(platform)
449+
architectures = platforms[platform]
450+
minimum_os_version = minimum_os_versions[platform]
451+
library_identifier = _platform_to_library_identifier(
452+
platform = platform,
453+
architectures = architectures,
454+
)
455+
456+
# Compile library
457+
binary = generation_support.compile_binary(
458+
actions = actions,
459+
apple_fragment = apple_fragment,
460+
archs = architectures,
461+
hdrs = hdrs,
462+
label = label,
463+
minimum_os_version = minimum_os_version,
464+
sdk = sdk,
465+
srcs = srcs,
466+
xcode_config = xcode_config,
467+
)
468+
469+
dynamic_library = generation_support.create_static_library(
470+
actions = actions,
471+
apple_fragment = apple_fragment,
472+
binary = binary,
473+
xcode_config = xcode_config,
474+
)
475+
476+
framework_files = generation_support.create_framework(
477+
actions = actions,
478+
base_path = paths.join("intermediates", library_identifier),
479+
bundle_name = label.name,
480+
library = dynamic_library,
481+
headers = hdrs,
482+
)
483+
484+
framework_path = paths.join(
485+
target_dir,
486+
"intermediates",
487+
library_identifier,
488+
label.name + ".framework",
489+
)
490+
frameworks[framework_path] = framework_files
491+
492+
# Create xcframework bundle
493+
xcframework_files = _create_xcframework(
494+
actions = actions,
495+
apple_fragment = apple_fragment,
496+
frameworks = frameworks,
497+
label = label,
498+
target_dir = target_dir,
499+
xcode_config = xcode_config,
500+
)
501+
502+
return [
503+
DefaultInfo(
504+
files = depset(xcframework_files),
505+
),
506+
]
507+
430508
generate_dynamic_xcframework = rule(
431509
doc = "Generates XCFramework with dynamic frameworks using Xcode build utilities.",
432510
implementation = _generate_dynamic_xcframework_impl,
@@ -557,3 +635,54 @@ purposes.
557635
),
558636
fragments = ["apple"],
559637
)
638+
639+
generate_static_framework_xcframework = rule(
640+
doc = "Generates XCFramework with static frameworks using Xcode build utilities.",
641+
implementation = _generate_static_framework_xcframework_impl,
642+
attrs = dicts.add(
643+
apple_support.action_required_attrs(),
644+
{
645+
"srcs": attr.label_list(
646+
doc = "List of source files for compiling Objective-C(++) / Swift binaries.",
647+
mandatory = True,
648+
allow_files = True,
649+
),
650+
"hdrs": attr.label_list(
651+
doc = "Header files for generated XCFrameworks.",
652+
mandatory = False,
653+
allow_files = True,
654+
),
655+
"platforms": attr.string_list_dict(
656+
doc = """
657+
A dictionary of strings indicating which platform variants should be built (with the following
658+
format: <platform>[_<environment>]) as keys, and arrays of strings listing which architectures
659+
should be built for those platform variants as their values.
660+
661+
platforms = {
662+
"ios_simulator": [
663+
"x86_64",
664+
"arm64",
665+
],
666+
"ios": ["arm64"],
667+
"watchos_simulator": ["i386"],
668+
},
669+
""",
670+
mandatory = True,
671+
),
672+
"minimum_os_versions": attr.string_dict(
673+
doc = """
674+
A dictionary of strings indicating the minimum OS version supported by each platform variant
675+
represented as a dotted version number as values.
676+
677+
minimum_os_versions = {
678+
"ios_simulator": "11.0",
679+
"ios": "11.0",
680+
"watchos_simulator": "4.0",
681+
},
682+
""",
683+
mandatory = True,
684+
),
685+
},
686+
),
687+
fragments = ["apple"],
688+
)

0 commit comments

Comments
 (0)