GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Make Swift compatible with Link Groups · meta/buck2-prelude@38ae42e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apple
    • apple_frameworks.bzl
    • apple_library.bzl
    • swift
      • swift_compilation.bzl
  • cxx
    • cxx_executable.bzl
    • cxx_library.bzl
    • cxx_types.bzl
  • linking
    • link_info.bzl
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apple/apple_frameworks.bzl‎

Copy file name to clipboardExpand all lines: apple/apple_frameworks.bzl
+19-7Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# of this source tree.
77

88
load("@prelude//:paths.bzl", "paths")
9+
load("@prelude//apple/swift:swift_compilation.bzl", "extract_swiftmodule_linkables", "get_swiftmodule_linker_flags", "merge_swiftmodule_linkables")
910
load("@prelude//apple/swift:swift_runtime.bzl", "extract_swift_runtime_linkables", "get_swift_runtime_linker_flags")
1011
load(
1112
"@prelude//linking:link_info.bzl",
@@ -16,6 +17,7 @@ load(
1617
"LinkInfosTSet",
1718
"LinkableType",
1819
"SwiftRuntimeLinkable", # @unused Used as a type
20+
"SwiftmoduleLinkable", # @unused Used as a type
1921
"get_link_args",
2022
"merge_framework_linkables",
2123
"merge_swift_runtime_linkables",
@@ -126,8 +128,9 @@ def apple_build_link_args_with_deduped_flags(
126128
frameworks_linkable: [FrameworksLinkable.type, None],
127129
link_style: "LinkStyle",
128130
prefer_stripped: bool.type = False,
131+
swiftmodule_linkable: [SwiftmoduleLinkable.type, None] = None,
129132
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])
131134
if not link_info:
132135
return get_link_args(info, link_style, prefer_stripped)
133136

@@ -142,8 +145,9 @@ def apple_build_link_args_with_deduped_flags(
142145
def apple_get_link_info_by_deduping_link_infos(
143146
ctx: "context",
144147
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]:
147151
# When building a framework or executable, all frameworks used by the statically-linked
148152
# deps in the subtree need to be linked.
149153
#
@@ -153,10 +157,13 @@ def apple_get_link_info_by_deduping_link_infos(
153157
if framework_linkable:
154158
framework_linkables.append(framework_linkable)
155159

160+
swiftmodule_linkables = extract_swiftmodule_linkables(infos)
161+
swiftmodule_linkables.append(swiftmodule_linkable)
162+
156163
swift_runtime_linkables = extract_swift_runtime_linkables(infos)
157164
swift_runtime_linkables.append(swift_runtime_linkable)
158165

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)
160167

161168
def _extract_framework_linkables(link_infos: [[LinkInfo.type], None]) -> [FrameworksLinkable.type]:
162169
frameworks_type = LinkableType("frameworks")
@@ -169,9 +176,14 @@ def _extract_framework_linkables(link_infos: [[LinkInfo.type], None]) -> [Framew
169176

170177
return linkables
171178

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]:
173184
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))
174186
swift_runtime_link_args = get_swift_runtime_linker_flags(ctx, merge_swift_runtime_linkables(swift_runtime_linkables))
175187
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

‎apple/apple_library.bzl‎

Copy file name to clipboardExpand all lines: apple/apple_library.bzl
+8-7Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ load(
1414
"get_swift_anonymous_targets",
1515
"get_swift_dependency_info",
1616
"get_swift_pcm_uncompile_info",
17+
"get_swiftmodule_linkable",
1718
"uses_explicit_modules",
1819
)
1920
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
139140
else:
140141
exported_pre = None
141142

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
142145
swift_dependency_info = swift_compile.dependency_info if swift_compile else [get_swift_dependency_info(ctx, exported_pre, None)]
143146
swift_argsfile = swift_compile.swift_argsfile if swift_compile else None
144147

@@ -173,12 +176,14 @@ def apple_library_rule_constructor_params_and_swift_providers(ctx: "context", pa
173176
framework_search_path_pre = CPreprocessor(
174177
args = [get_framework_search_path_flags(ctx)],
175178
)
179+
176180
return CxxRuleConstructorParams(
177181
rule_type = params.rule_type,
178182
is_test = (params.rule_type == "apple_test"),
179183
headers_layout = get_apple_cxx_headers_layout(ctx),
180184
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,
182187
extra_link_input = swift_object_files,
183188
extra_link_input_has_external_debug_info = True,
184189
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])
265270
tsets.append(info.external_debug_info)
266271
return tsets
267272

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))
274275

275276
def _xcode_populate_attributes(
276277
ctx,
Collapse file

‎apple/swift/swift_compilation.bzl‎

Copy file name to clipboardExpand all lines: apple/swift/swift_compilation.bzl
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ load(
2727
"cxx_inherited_preprocessor_infos",
2828
"cxx_merge_cpreprocessors",
2929
)
30+
load(
31+
"@prelude//linking:link_info.bzl",
32+
"LinkInfo", # @unused Used as a type
33+
"LinkableType",
34+
"SwiftmoduleLinkable",
35+
)
3036
load(":apple_sdk_modules_utility.bzl", "get_compiled_sdk_deps_tset", "get_uncompiled_sdk_deps", "is_sdk_modules_provided")
3137
load(":swift_module_map.bzl", "write_swift_module_map_with_swift_deps")
3238
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":
604610
def uses_explicit_modules(ctx: "context") -> bool.type:
605611
swift_toolchain = ctx.attrs._apple_toolchain[AppleToolchainInfo].swift_toolchain_info
606612
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

‎cxx/cxx_executable.bzl‎

Copy file name to clipboardExpand all lines: cxx/cxx_executable.bzl
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ def cxx_executable(ctx: "context", impl_params: CxxRuleConstructorParams.type, i
242242
frameworks_linkable,
243243
link_style,
244244
prefer_stripped = impl_params.prefer_stripped_objects,
245+
swiftmodule_linkable = impl_params.swiftmodule_linkable,
245246
swift_runtime_linkable = swift_runtime_linkable,
246247
)
247248
else:
@@ -331,7 +332,7 @@ def cxx_executable(ctx: "context", impl_params: CxxRuleConstructorParams.type, i
331332
# Unfortunately, link_groups does not use MergedLinkInfo to represent the args
332333
# for the resolved nodes in the graph.
333334
# 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)
335336
if additional_links:
336337
filtered_links.append(additional_links)
337338

Collapse file

‎cxx/cxx_library.bzl‎

Copy file name to clipboardExpand all lines: cxx/cxx_library.bzl
+10-4Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ load(
5656
"ObjectsLinkable",
5757
"SharedLibLinkable",
5858
"SwiftRuntimeLinkable", # @unused Used as a type
59+
"SwiftmoduleLinkable", # @unused Used as a type
5960
"create_merged_link_info",
6061
"get_actual_link_style",
6162
"get_link_args",
@@ -363,6 +364,7 @@ def cxx_library_parameterized(ctx: "context", impl_params: "CxxRuleConstructorPa
363364
)
364365

365366
frameworks_linkable = apple_create_frameworks_linkable(ctx)
367+
swiftmodule_linkable = impl_params.swiftmodule_linkable
366368
swift_runtime_linkable = create_swift_runtime_linkable(ctx)
367369
shared_links, link_group_map = _get_shared_library_links(
368370
ctx,
@@ -376,6 +378,7 @@ def cxx_library_parameterized(ctx: "context", impl_params: "CxxRuleConstructorPa
376378
impl_params.force_link_group_linking,
377379
frameworks_linkable,
378380
force_static_follows_dependents = impl_params.link_groups_force_static_follows_dependents,
381+
swiftmodule_linkable = swiftmodule_linkable,
379382
swift_runtime_linkable = swift_runtime_linkable,
380383
)
381384
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
384387
extra_static_linkables = []
385388
if frameworks_linkable:
386389
extra_static_linkables.append(frameworks_linkable)
390+
if swiftmodule_linkable:
391+
extra_static_linkables.append(swiftmodule_linkable)
387392
if swift_runtime_linkable:
388393
extra_static_linkables.append(swift_runtime_linkable)
389394

@@ -776,7 +781,7 @@ def _form_library_outputs(
776781
compiled_srcs: _CxxCompiledSourcesOutput.type,
777782
preferred_linkage: Linkage.type,
778783
shared_links: LinkArgs.type,
779-
extra_static_linkables: [[FrameworksLinkable.type, SwiftRuntimeLinkable.type]],
784+
extra_static_linkables: [[FrameworksLinkable.type, SwiftmoduleLinkable.type, SwiftRuntimeLinkable.type]],
780785
gnu_use_link_groups: bool.type) -> _CxxAllLibraryOutputs.type:
781786
# Build static/shared libs and the link info we use to export them to dependents.
782787
outputs = {}
@@ -914,6 +919,7 @@ def _get_shared_library_links(
914919
force_link_group_linking,
915920
frameworks_linkable: [FrameworksLinkable.type, None],
916921
force_static_follows_dependents: bool.type = True,
922+
swiftmodule_linkable: [SwiftmoduleLinkable.type, None] = None,
917923
swift_runtime_linkable: [SwiftRuntimeLinkable.type, None] = None) -> ("LinkArgs", [DefaultInfo.type, None]):
918924
"""
919925
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(
948954
link,
949955
frameworks_linkable,
950956
LinkStyle(link_style_value),
957+
swiftmodule_linkable = swiftmodule_linkable,
951958
swift_runtime_linkable = swift_runtime_linkable,
952959
), None
953960

@@ -973,8 +980,7 @@ def _get_shared_library_links(
973980

974981
# Unfortunately, link_groups does not use MergedLinkInfo to represent the args
975982
# 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)
978984
if additional_links:
979985
filtered_links.append(additional_links)
980986

@@ -995,7 +1001,7 @@ def _static_library(
9951001
objects: ["artifact"],
9961002
pic: bool.type,
9971003
stripped: bool.type,
998-
extra_linkables: [[FrameworksLinkable.type, SwiftRuntimeLinkable.type]],
1004+
extra_linkables: [[FrameworksLinkable.type, SwiftmoduleLinkable.type, SwiftRuntimeLinkable.type]],
9991005
objects_have_external_debug_info: bool.type = False,
10001006
external_debug_info: [ExternalDebugInfoTSet.type, None] = None) -> (_CxxLibraryOutput.type, LinkInfo.type):
10011007
if len(objects) == 0:
Collapse file

‎cxx/cxx_types.bzl‎

Copy file name to clipboardExpand all lines: cxx/cxx_types.bzl
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
load(
99
"@prelude//linking:link_info.bzl",
1010
"LinkArgs",
11+
"SwiftmoduleLinkable", # @unused Used as a type
1112
)
1213
load(
1314
"@prelude//linking:linkables.bzl",
@@ -129,6 +130,8 @@ CxxRuleConstructorParams = record(
129130
extra_link_input_has_external_debug_info = field(bool.type, False),
130131
# Additional args to be used to link the target.
131132
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),
132135
# The source files to compile as part of this rule. This list can be generated
133136
# from ctx.attrs with the `get_srcs_with_flags` function.
134137
srcs = field([CxxSrcWithFlags.type]),
Collapse file

‎linking/link_info.bzl‎

Copy file name to clipboardExpand all lines: linking/link_info.bzl
+9-3Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ LinkableType = enum(
5252
"frameworks",
5353
"shared",
5454
"objects",
55+
"swiftmodule",
5556
"swift_runtime",
5657
)
5758

@@ -98,6 +99,11 @@ FrameworksLinkable = record(
9899
_type = field(LinkableType.type, LinkableType("frameworks")),
99100
)
100101

102+
SwiftmoduleLinkable = record(
103+
tset = field("SwiftmodulePathsTSet"),
104+
_type = field(LinkableType.type, LinkableType("swiftmodule")),
105+
)
106+
101107
# Represents the Swift runtime as a linker input.
102108
SwiftRuntimeLinkable = record(
103109
# Only store whether the runtime is required, so that linker flags
@@ -106,7 +112,7 @@ SwiftRuntimeLinkable = record(
106112
_type = field(LinkableType.type, LinkableType("swift_runtime")),
107113
)
108114

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]
110116

111117
# Contains the information required to add an item (often corresponding to a single library) to a link command line.
112118
LinkInfo = record(
@@ -197,7 +203,7 @@ def append_linkable_args(args: "cmd_args", linkable: LinkableTypes):
197203
args.add(get_objects_as_library_args(linkable.linker_type, linkable.objects))
198204
else:
199205
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"):
201207
# These flags are handled separately so they can be deduped.
202208
#
203209
# We've seen in apps with larger dependency graphs that failing
@@ -231,7 +237,7 @@ def link_info_filelist(value: LinkInfo.type) -> ["artifact"]:
231237
elif linkable._type == LinkableType("objects"):
232238
if linkable.linker_type == "darwin":
233239
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"):
235241
pass
236242
else:
237243
fail("Encountered unhandled linkable of type {}".format(linkable._type))

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.