|
| 1 | +""" |
| 2 | +Aspect that collects the outputs of all compile actions of a given build. |
| 3 | +Useful to be able to build and cache compile actions on CI, without having to |
| 4 | +link, bundle, or codesign as well (which we don't cache anyway). |
| 5 | +""" |
| 6 | + |
| 7 | +_DOWNSTREAM_VALID_RULE_KINDS = { |
| 8 | + "apple_dynamic_framework_import": None, |
| 9 | + "apple_dynamic_xcframework_import": None, |
| 10 | + "apple_static_framework_import": None, |
| 11 | + "apple_static_xcframework_import": None, |
| 12 | + "cc_binary": None, |
| 13 | + "ios_app_clip": None, |
| 14 | + "ios_application": None, |
| 15 | + "ios_extension": None, |
| 16 | + "ios_framework": None, |
| 17 | + "ios_ui_test": None, |
| 18 | + "ios_unit_test": None, |
| 19 | + "swift_binary": None, |
| 20 | + "swift_test": None, |
| 21 | + "_ios_internal_ui_test_bundle": None, |
| 22 | + "_ios_internal_unit_test_bundle": None, |
| 23 | + "_precompiled_apple_resource_bundle": None, |
| 24 | +} |
| 25 | + |
| 26 | +_SWIFT_LIBRARY_KINDS = [ |
| 27 | + "swift_library", |
| 28 | + "swift_test", |
| 29 | +] |
| 30 | + |
| 31 | +def _compile_only_aspect_impl(target, ctx): |
| 32 | + outs = [] |
| 33 | + deps = [] |
| 34 | + if ctx.rule.kind in _DOWNSTREAM_VALID_RULE_KINDS or CcInfo in target: |
| 35 | + if ctx.rule.kind in _SWIFT_LIBRARY_KINDS: |
| 36 | + for action in target.actions: |
| 37 | + if action.mnemonic == "SwiftCompile": |
| 38 | + outs = [action.outputs] |
| 39 | + break |
| 40 | + elif ctx.rule.kind == "objc_library": |
| 41 | + outs = [ |
| 42 | + action.outputs |
| 43 | + for action in target.actions |
| 44 | + if action.mnemonic == "ObjcCompile" |
| 45 | + ] |
| 46 | + deps = ( |
| 47 | + getattr(ctx.rule.attr, "deps", []) + |
| 48 | + getattr(ctx.rule.attr, "implementation_deps", []) + |
| 49 | + getattr(ctx.rule.attr, "private_deps", []) |
| 50 | + ) |
| 51 | + swift_target = getattr(ctx.rule.attr, "swift_target", None) |
| 52 | + if swift_target: |
| 53 | + deps.append(swift_target) |
| 54 | + clang_target = getattr(ctx.rule.attr, "clang_target", None) |
| 55 | + if clang_target: |
| 56 | + deps.append(clang_target) |
| 57 | + elif ctx.rule.kind == "test_suite": |
| 58 | + deps = ctx.rule.attr.tests |
| 59 | + elif ctx.rule.kind == "ios_build_test": |
| 60 | + deps = ctx.rule.attr.targets |
| 61 | + elif ctx.rule.kind == "xcodeproj": |
| 62 | + deps = ( |
| 63 | + getattr(ctx.rule.attr, "top_level_device_targets", []) + |
| 64 | + getattr(ctx.rule.attr, "top_level_simulator_targets", []) |
| 65 | + ) |
| 66 | + else: |
| 67 | + return [] |
| 68 | + |
| 69 | + for dep in deps: |
| 70 | + if OutputGroupInfo in dep and not hasattr(dep[OutputGroupInfo], "compiles"): |
| 71 | + fail(target, dep) |
| 72 | + |
| 73 | + return [ |
| 74 | + OutputGroupInfo( |
| 75 | + compiles = depset( |
| 76 | + transitive = outs + [ |
| 77 | + dep[OutputGroupInfo].compiles |
| 78 | + for dep in deps |
| 79 | + if OutputGroupInfo in dep |
| 80 | + ], |
| 81 | + ), |
| 82 | + ), |
| 83 | + ] |
| 84 | + |
| 85 | +compile_only_aspect = aspect( |
| 86 | + implementation = _compile_only_aspect_impl, |
| 87 | + attr_aspects = [ |
| 88 | + "deps", |
| 89 | + "implementation_deps", |
| 90 | + "private_deps", |
| 91 | + # from `mixed_language_library` |
| 92 | + "clang_target", |
| 93 | + "swift_target", |
| 94 | + # from `test_suite` |
| 95 | + "targets", |
| 96 | + # from `*_build_test` |
| 97 | + "tests", |
| 98 | + # from `xcodeproj` |
| 99 | + "top_level_device_targets", |
| 100 | + "top_level_simulator_targets", |
| 101 | + ], |
| 102 | +) |
0 commit comments