GeezerVison is showing github.com — Make Swift compatible with Link Groups · meta/buck2-prelude@38ae42e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apple/apple_frameworks.bzl +19-7Lines changed: 19 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | # of this source tree. | 7 | 7 |
| 8 | 8 | load("@prelude//:paths.bzl", "paths") | | 9 | +load("@prelude//apple/swift:swift_compilation.bzl", "extract_swiftmodule_linkables", "get_swiftmodule_linker_flags", "merge_swiftmodule_linkables") | 9 | 10 | load("@prelude//apple/swift:swift_runtime.bzl", "extract_swift_runtime_linkables", "get_swift_runtime_linker_flags") | 10 | 11 | load( | 11 | 12 | "@prelude//linking:link_info.bzl", | | 16 | 17 | "LinkInfosTSet", | 17 | 18 | "LinkableType", | 18 | 19 | "SwiftRuntimeLinkable", # @unused Used as a type | | 20 | + "SwiftmoduleLinkable", # @unused Used as a type | 19 | 21 | "get_link_args", | 20 | 22 | "merge_framework_linkables", | 21 | 23 | "merge_swift_runtime_linkables", | @@ -126,8 +128,9 @@ def apple_build_link_args_with_deduped_flags( | 126 | 128 | frameworks_linkable: [FrameworksLinkable.type, None], | 127 | 129 | link_style: "LinkStyle", | 128 | 130 | prefer_stripped: bool.type = False, | | 131 | + swiftmodule_linkable: [SwiftmoduleLinkable.type, None] = None, | 129 | 132 | swift_runtime_linkable: [SwiftRuntimeLinkable.type, None] = None) -> LinkArgs.type: | 130 | | - link_info = _link_info_from_linkables(ctx, [info.frameworks[link_style], frameworks_linkable], [info.swift_runtime[link_style], swift_runtime_linkable]) | | 133 | + link_info = _link_info_from_linkables(ctx, [info.frameworks[link_style], frameworks_linkable], [swiftmodule_linkable], [info.swift_runtime[link_style], swift_runtime_linkable]) | 131 | 134 | if not link_info: | 132 | 135 | return get_link_args(info, link_style, prefer_stripped) | 133 | 136 |
| @@ -142,8 +145,9 @@ def apple_build_link_args_with_deduped_flags( | 142 | 145 | def apple_get_link_info_by_deduping_link_infos( | 143 | 146 | ctx: "context", | 144 | 147 | infos: [[LinkInfo.type, None]], | 145 | | - framework_linkable: [FrameworksLinkable.type, None], | 146 | | - swift_runtime_linkable: [SwiftRuntimeLinkable.type, None]) -> [LinkInfo.type, None]: | | 148 | + framework_linkable: [FrameworksLinkable.type, None] = None, | | 149 | + swiftmodule_linkable: [SwiftmoduleLinkable.type, None] = None, | | 150 | + swift_runtime_linkable: [SwiftRuntimeLinkable.type, None] = None) -> [LinkInfo.type, None]: | 147 | 151 | # When building a framework or executable, all frameworks used by the statically-linked | 148 | 152 | # deps in the subtree need to be linked. | 149 | 153 | # | @@ -153,10 +157,13 @@ def apple_get_link_info_by_deduping_link_infos( | 153 | 157 | if framework_linkable: | 154 | 158 | framework_linkables.append(framework_linkable) | 155 | 159 |
| | 160 | + swiftmodule_linkables = extract_swiftmodule_linkables(infos) | | 161 | + swiftmodule_linkables.append(swiftmodule_linkable) | | 162 | + | 156 | 163 | swift_runtime_linkables = extract_swift_runtime_linkables(infos) | 157 | 164 | swift_runtime_linkables.append(swift_runtime_linkable) | 158 | 165 |
| 159 | | - return _link_info_from_linkables(ctx, framework_linkables, swift_runtime_linkables) | | 166 | + return _link_info_from_linkables(ctx, framework_linkables, swiftmodule_linkables, swift_runtime_linkables) | 160 | 167 |
| 161 | 168 | def _extract_framework_linkables(link_infos: [[LinkInfo.type], None]) -> [FrameworksLinkable.type]: | 162 | 169 | frameworks_type = LinkableType("frameworks") | @@ -169,9 +176,14 @@ def _extract_framework_linkables(link_infos: [[LinkInfo.type], None]) -> [Framew | 169 | 176 |
| 170 | 177 | return linkables | 171 | 178 |
| 172 | | -def _link_info_from_linkables(ctx: "context", framework_linkables: [[FrameworksLinkable.type, None]], swift_runtime_linkables: [[SwiftRuntimeLinkable.type, None]]) -> [LinkInfo.type, None]: | | 179 | +def _link_info_from_linkables( | | 180 | + ctx: "context", | | 181 | + framework_linkables: [[FrameworksLinkable.type, None]], | | 182 | + swiftmodule_linkables: [[SwiftmoduleLinkable.type, None]] = [], | | 183 | + swift_runtime_linkables: [[SwiftRuntimeLinkable.type, None]] = []) -> [LinkInfo.type, None]: | 173 | 184 | framework_link_args = _get_apple_frameworks_linker_flags(ctx, merge_framework_linkables(framework_linkables)) | | 185 | + swift_module_link_args = get_swiftmodule_linker_flags(merge_swiftmodule_linkables(ctx, swiftmodule_linkables)) | 174 | 186 | swift_runtime_link_args = get_swift_runtime_linker_flags(ctx, merge_swift_runtime_linkables(swift_runtime_linkables)) | 175 | 187 | return LinkInfo( | 176 | | - pre_flags = [framework_link_args, swift_runtime_link_args], | 177 | | - ) if (framework_link_args or swift_runtime_link_args) else None | | 188 | + pre_flags = [framework_link_args, swift_module_link_args, swift_runtime_link_args], | | 189 | + ) if (framework_link_args or swift_module_link_args or swift_runtime_link_args) else None |
Collapse file Copy file name to clipboardExpand all lines: apple/apple_library.bzl +8-7Lines changed: 8 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 14 | 14 | "get_swift_anonymous_targets", | 15 | 15 | "get_swift_dependency_info", | 16 | 16 | "get_swift_pcm_uncompile_info", | | 17 | + "get_swiftmodule_linkable", | 17 | 18 | "uses_explicit_modules", | 18 | 19 | ) | 19 | 20 | load("@prelude//cxx:cxx_library.bzl", "cxx_library_parameterized") | @@ -139,6 +140,8 @@ def apple_library_rule_constructor_params_and_swift_providers(ctx: "context", pa | 139 | 140 | else: | 140 | 141 | exported_pre = None | 141 | 142 |
| | 143 | + # When linking, we expect each linked object to provide the transitively required swiftmodule AST entries for linking. | | 144 | + swiftmodule_linkable = get_swiftmodule_linkable(ctx, swift_compile.dependency_info) if swift_compile else None | 142 | 145 | swift_dependency_info = swift_compile.dependency_info if swift_compile else [get_swift_dependency_info(ctx, exported_pre, None)] | 143 | 146 | swift_argsfile = swift_compile.swift_argsfile if swift_compile else None | 144 | 147 |
| @@ -173,12 +176,14 @@ def apple_library_rule_constructor_params_and_swift_providers(ctx: "context", pa | 173 | 176 | framework_search_path_pre = CPreprocessor( | 174 | 177 | args = [get_framework_search_path_flags(ctx)], | 175 | 178 | ) | | 179 | + | 176 | 180 | return CxxRuleConstructorParams( | 177 | 181 | rule_type = params.rule_type, | 178 | 182 | is_test = (params.rule_type == "apple_test"), | 179 | 183 | headers_layout = get_apple_cxx_headers_layout(ctx), | 180 | 184 | extra_exported_link_flags = params.extra_exported_link_flags, | 181 | | - extra_link_flags = [_get_linker_flags(ctx, swift_dependency_info)], | | 185 | + extra_link_flags = [_get_linker_flags(ctx)], | | 186 | + swiftmodule_linkable = swiftmodule_linkable, | 182 | 187 | extra_link_input = swift_object_files, | 183 | 188 | extra_link_input_has_external_debug_info = True, | 184 | 189 | extra_preprocessors = get_min_deployment_version_target_preprocessor_flags(ctx) + [swift_pre, modular_pre], | @@ -265,12 +270,8 @@ def _get_external_debug_info(swift_dependency_infos: [SwiftDependencyInfo.type]) | 265 | 270 | tsets.append(info.external_debug_info) | 266 | 271 | return tsets | 267 | 272 |
| 268 | | -def _get_linker_flags(ctx: "context", swift_dependency_infos: [SwiftDependencyInfo.type]) -> "cmd_args": | 269 | | - cmd = cmd_args(get_min_deployment_version_target_linker_flags(ctx)) | 270 | | - for info in swift_dependency_infos: | 271 | | - cmd.add(info.transitive_swiftmodule_paths.project_as_args("linker_args")) | 272 | | - | 273 | | - return cmd | | 273 | +def _get_linker_flags(ctx: "context") -> "cmd_args": | | 274 | + return cmd_args(get_min_deployment_version_target_linker_flags(ctx)) | 274 | 275 |
| 275 | 276 | def _xcode_populate_attributes( | 276 | 277 | ctx, | |
Collapse file Copy file name to clipboardExpand all lines: apple/swift/swift_compilation.bzl +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 27 | 27 | "cxx_inherited_preprocessor_infos", | 28 | 28 | "cxx_merge_cpreprocessors", | 29 | 29 | ) | | 30 | +load( | | 31 | + "@prelude//linking:link_info.bzl", | | 32 | + "LinkInfo", # @unused Used as a type | | 33 | + "LinkableType", | | 34 | + "SwiftmoduleLinkable", | | 35 | +) | 30 | 36 | load(":apple_sdk_modules_utility.bzl", "get_compiled_sdk_deps_tset", "get_uncompiled_sdk_deps", "is_sdk_modules_provided") | 31 | 37 | load(":swift_module_map.bzl", "write_swift_module_map_with_swift_deps") | 32 | 38 | load(":swift_pcm_compilation.bzl", "PcmDepTSet", "compile_underlying_pcm", "get_compiled_pcm_deps_tset", "get_swift_pcm_anon_targets") | @@ -604,3 +610,23 @@ def _header_basename(header: ["artifact", "string"]) -> "string": | 604 | 610 | def uses_explicit_modules(ctx: "context") -> bool.type: | 605 | 611 | swift_toolchain = ctx.attrs._apple_toolchain[AppleToolchainInfo].swift_toolchain_info | 606 | 612 | return ctx.attrs.uses_explicit_modules and is_sdk_modules_provided(swift_toolchain) | | 613 | + | | 614 | +def get_swiftmodule_linkable(ctx: "context", dependency_infos: [SwiftDependencyInfo.type]) -> SwiftmoduleLinkable.type: | | 615 | + return SwiftmoduleLinkable(tset = ctx.actions.tset(SwiftmodulePathsTSet, children = [d.transitive_swiftmodule_paths for d in dependency_infos])) | | 616 | + | | 617 | +def extract_swiftmodule_linkables(link_infos: [[LinkInfo.type], None]) -> [SwiftmoduleLinkable.type]: | | 618 | + swift_module_type = LinkableType("swiftmodule") | | 619 | + | | 620 | + linkables = [] | | 621 | + for info in link_infos: | | 622 | + for linkable in info.linkables: | | 623 | + if linkable._type == swift_module_type: | | 624 | + linkables.append(linkable) | | 625 | + | | 626 | + return linkables | | 627 | + | | 628 | +def merge_swiftmodule_linkables(ctx: "context", swiftmodule_linkables: [[SwiftmoduleLinkable.type, None]]) -> SwiftmoduleLinkable.type: | | 629 | + return SwiftmoduleLinkable(tset = ctx.actions.tset(SwiftmodulePathsTSet, children = [linkable.tset for linkable in swiftmodule_linkables if linkable])) | | 630 | + | | 631 | +def get_swiftmodule_linker_flags(swiftmodule_linkable: [SwiftmoduleLinkable.type, None]) -> "cmd_args": | | 632 | + return cmd_args(swiftmodule_linkable.tset.project_as_args("linker_args")) if swiftmodule_linkable else cmd_args() |
Collapse file Copy file name to clipboardExpand all lines: cxx/cxx_executable.bzl +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -242,6 +242,7 @@ def cxx_executable(ctx: "context", impl_params: CxxRuleConstructorParams.type, i | 242 | 242 | frameworks_linkable, | 243 | 243 | link_style, | 244 | 244 | prefer_stripped = impl_params.prefer_stripped_objects, | | 245 | + swiftmodule_linkable = impl_params.swiftmodule_linkable, | 245 | 246 | swift_runtime_linkable = swift_runtime_linkable, | 246 | 247 | ) | 247 | 248 | else: | @@ -331,7 +332,7 @@ def cxx_executable(ctx: "context", impl_params: CxxRuleConstructorParams.type, i | 331 | 332 | # Unfortunately, link_groups does not use MergedLinkInfo to represent the args | 332 | 333 | # for the resolved nodes in the graph. | 333 | 334 | # Thus, we have no choice but to traverse all the nodes to dedupe the framework linker args. | 334 | | - additional_links = apple_get_link_info_by_deduping_link_infos(ctx, filtered_links, frameworks_linkable, swift_runtime_linkable) | | 335 | + additional_links = apple_get_link_info_by_deduping_link_infos(ctx, filtered_links, frameworks_linkable, impl_params.swiftmodule_linkable, swift_runtime_linkable) | 335 | 336 | if additional_links: | 336 | 337 | filtered_links.append(additional_links) | 337 | 338 |
| |
Collapse file Copy file name to clipboardExpand all lines: cxx/cxx_library.bzl +10-4Lines changed: 10 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 56 | 56 | "ObjectsLinkable", | 57 | 57 | "SharedLibLinkable", | 58 | 58 | "SwiftRuntimeLinkable", # @unused Used as a type | | 59 | + "SwiftmoduleLinkable", # @unused Used as a type | 59 | 60 | "create_merged_link_info", | 60 | 61 | "get_actual_link_style", | 61 | 62 | "get_link_args", | @@ -363,6 +364,7 @@ def cxx_library_parameterized(ctx: "context", impl_params: "CxxRuleConstructorPa | 363 | 364 | ) | 364 | 365 |
| 365 | 366 | frameworks_linkable = apple_create_frameworks_linkable(ctx) | | 367 | + swiftmodule_linkable = impl_params.swiftmodule_linkable | 366 | 368 | swift_runtime_linkable = create_swift_runtime_linkable(ctx) | 367 | 369 | shared_links, link_group_map = _get_shared_library_links( | 368 | 370 | ctx, | @@ -376,6 +378,7 @@ def cxx_library_parameterized(ctx: "context", impl_params: "CxxRuleConstructorPa | 376 | 378 | impl_params.force_link_group_linking, | 377 | 379 | frameworks_linkable, | 378 | 380 | force_static_follows_dependents = impl_params.link_groups_force_static_follows_dependents, | | 381 | + swiftmodule_linkable = swiftmodule_linkable, | 379 | 382 | swift_runtime_linkable = swift_runtime_linkable, | 380 | 383 | ) | 381 | 384 | if impl_params.generate_sub_targets.link_group_map and link_group_map: | @@ -384,6 +387,8 @@ def cxx_library_parameterized(ctx: "context", impl_params: "CxxRuleConstructorPa | 384 | 387 | extra_static_linkables = [] | 385 | 388 | if frameworks_linkable: | 386 | 389 | extra_static_linkables.append(frameworks_linkable) | | 390 | + if swiftmodule_linkable: | | 391 | + extra_static_linkables.append(swiftmodule_linkable) | 387 | 392 | if swift_runtime_linkable: | 388 | 393 | extra_static_linkables.append(swift_runtime_linkable) | 389 | 394 |
| @@ -776,7 +781,7 @@ def _form_library_outputs( | 776 | 781 | compiled_srcs: _CxxCompiledSourcesOutput.type, | 777 | 782 | preferred_linkage: Linkage.type, | 778 | 783 | shared_links: LinkArgs.type, | 779 | | - extra_static_linkables: [[FrameworksLinkable.type, SwiftRuntimeLinkable.type]], | | 784 | + extra_static_linkables: [[FrameworksLinkable.type, SwiftmoduleLinkable.type, SwiftRuntimeLinkable.type]], | 780 | 785 | gnu_use_link_groups: bool.type) -> _CxxAllLibraryOutputs.type: | 781 | 786 | # Build static/shared libs and the link info we use to export them to dependents. | 782 | 787 | outputs = {} | @@ -914,6 +919,7 @@ def _get_shared_library_links( | 914 | 919 | force_link_group_linking, | 915 | 920 | frameworks_linkable: [FrameworksLinkable.type, None], | 916 | 921 | force_static_follows_dependents: bool.type = True, | | 922 | + swiftmodule_linkable: [SwiftmoduleLinkable.type, None] = None, | 917 | 923 | swift_runtime_linkable: [SwiftRuntimeLinkable.type, None] = None) -> ("LinkArgs", [DefaultInfo.type, None]): | 918 | 924 | """ | 919 | 925 | Returns LinkArgs with the content to link, and a link group map json output if applicable. | @@ -948,6 +954,7 @@ def _get_shared_library_links( | 948 | 954 | link, | 949 | 955 | frameworks_linkable, | 950 | 956 | LinkStyle(link_style_value), | | 957 | + swiftmodule_linkable = swiftmodule_linkable, | 951 | 958 | swift_runtime_linkable = swift_runtime_linkable, | 952 | 959 | ), None | 953 | 960 |
| @@ -973,8 +980,7 @@ def _get_shared_library_links( | 973 | 980 |
| 974 | 981 | # Unfortunately, link_groups does not use MergedLinkInfo to represent the args | 975 | 982 | # for the resolved nodes in the graph. | 976 | | - # Thus, we have no choice but to traverse all the nodes to dedupe the framework linker args. | 977 | | - additional_links = apple_get_link_info_by_deduping_link_infos(ctx, filtered_links, frameworks_linkable, swift_runtime_linkable) | | 983 | + additional_links = apple_get_link_info_by_deduping_link_infos(ctx, filtered_links, frameworks_linkable, swiftmodule_linkable, swift_runtime_linkable) | 978 | 984 | if additional_links: | 979 | 985 | filtered_links.append(additional_links) | 980 | 986 |
| @@ -995,7 +1001,7 @@ def _static_library( | 995 | 1001 | objects: ["artifact"], | 996 | 1002 | pic: bool.type, | 997 | 1003 | stripped: bool.type, | 998 | | - extra_linkables: [[FrameworksLinkable.type, SwiftRuntimeLinkable.type]], | | 1004 | + extra_linkables: [[FrameworksLinkable.type, SwiftmoduleLinkable.type, SwiftRuntimeLinkable.type]], | 999 | 1005 | objects_have_external_debug_info: bool.type = False, | 1000 | 1006 | external_debug_info: [ExternalDebugInfoTSet.type, None] = None) -> (_CxxLibraryOutput.type, LinkInfo.type): | 1001 | 1007 | if len(objects) == 0: | |
Collapse file Copy file name to clipboardExpand all lines: cxx/cxx_types.bzl +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | load( | 9 | 9 | "@prelude//linking:link_info.bzl", | 10 | 10 | "LinkArgs", | | 11 | + "SwiftmoduleLinkable", # @unused Used as a type | 11 | 12 | ) | 12 | 13 | load( | 13 | 14 | "@prelude//linking:linkables.bzl", | @@ -129,6 +130,8 @@ CxxRuleConstructorParams = record( | 129 | 130 | extra_link_input_has_external_debug_info = field(bool.type, False), | 130 | 131 | # Additional args to be used to link the target. | 131 | 132 | extra_link_args = field([LinkArgs.type], []), | | 133 | + # The swift module linkable that should be included for linking. | | 134 | + swiftmodule_linkable = field([SwiftmoduleLinkable.type, None], None), | 132 | 135 | # The source files to compile as part of this rule. This list can be generated | 133 | 136 | # from ctx.attrs with the `get_srcs_with_flags` function. | 134 | 137 | srcs = field([CxxSrcWithFlags.type]), | |
Collapse file Copy file name to clipboardExpand all lines: linking/link_info.bzl +9-3Lines changed: 9 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -52,6 +52,7 @@ LinkableType = enum( | 52 | 52 | "frameworks", | 53 | 53 | "shared", | 54 | 54 | "objects", | | 55 | + "swiftmodule", | 55 | 56 | "swift_runtime", | 56 | 57 | ) | 57 | 58 |
| @@ -98,6 +99,11 @@ FrameworksLinkable = record( | 98 | 99 | _type = field(LinkableType.type, LinkableType("frameworks")), | 99 | 100 | ) | 100 | 101 |
| | 102 | +SwiftmoduleLinkable = record( | | 103 | + tset = field("SwiftmodulePathsTSet"), | | 104 | + _type = field(LinkableType.type, LinkableType("swiftmodule")), | | 105 | +) | | 106 | + | 101 | 107 | # Represents the Swift runtime as a linker input. | 102 | 108 | SwiftRuntimeLinkable = record( | 103 | 109 | # Only store whether the runtime is required, so that linker flags | @@ -106,7 +112,7 @@ SwiftRuntimeLinkable = record( | 106 | 112 | _type = field(LinkableType.type, LinkableType("swift_runtime")), | 107 | 113 | ) | 108 | 114 |
| 109 | | -LinkableTypes = [ArchiveLinkable.type, SharedLibLinkable.type, ObjectsLinkable.type, FrameworksLinkable.type, SwiftRuntimeLinkable.type] | | 115 | +LinkableTypes = [ArchiveLinkable.type, SharedLibLinkable.type, ObjectsLinkable.type, FrameworksLinkable.type, SwiftmoduleLinkable.type, SwiftRuntimeLinkable.type] | 110 | 116 |
| 111 | 117 | # Contains the information required to add an item (often corresponding to a single library) to a link command line. | 112 | 118 | LinkInfo = record( | @@ -197,7 +203,7 @@ def append_linkable_args(args: "cmd_args", linkable: LinkableTypes): | 197 | 203 | args.add(get_objects_as_library_args(linkable.linker_type, linkable.objects)) | 198 | 204 | else: | 199 | 205 | args.add(linkable.objects) | 200 | | - elif linkable._type == LinkableType("frameworks") or linkable._type == LinkableType("swift_runtime"): | | 206 | + elif linkable._type == LinkableType("frameworks") or linkable._type == LinkableType("swiftmodule") or linkable._type == LinkableType("swift_runtime"): | 201 | 207 | # These flags are handled separately so they can be deduped. | 202 | 208 | # | 203 | 209 | # We've seen in apps with larger dependency graphs that failing | @@ -231,7 +237,7 @@ def link_info_filelist(value: LinkInfo.type) -> ["artifact"]: | 231 | 237 | elif linkable._type == LinkableType("objects"): | 232 | 238 | if linkable.linker_type == "darwin": | 233 | 239 | filelists += linkable.objects | 234 | | - elif linkable._type == LinkableType("frameworks") or linkable._type == LinkableType("swift_runtime"): | | 240 | + elif linkable._type == LinkableType("frameworks") or linkable._type == LinkableType("swiftmodule") or linkable._type == LinkableType("swift_runtime"): | 235 | 241 | pass | 236 | 242 | else: | 237 | 243 | fail("Encountered unhandled linkable of type {}".format(linkable._type)) | |
|