Skip to content

Commit 5594f42

Browse files
authored
fix: usage of copts for SWIFT_PACKAGE (#1260)
This primarily does two changes: - Passes `-Xcc -DSWIFT_PACKAGE` for the Swift library target generated `copts` attribute. - Removes de-duplication in `bzl_selects.bzl` - Using `sets` enforces unique values for the fields which is problematic, for example: `copts = ["-DSWIFT_PACKAGE", "-Xcc", "-DSWIFT_PACKAGE"]` are valid copts but would get de-duplicated into `copts = ["-DSWIFT_PACKAGE", "-Xcc"]`. - We should not work-around duplicated `deps`, `copts`, etc. in the `BUILD` file generator. If issues arise during the build the `Package.swift` for the package should be fixed if it is a legitimate duplicate. Fixes #1259
1 parent be67cfb commit 5594f42

File tree

4 files changed

+68
-27
lines changed

4 files changed

+68
-27
lines changed

swiftpkg/internal/bzl_selects.bzl

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ def _new_from_target_dependency_condition(kind, labels, condition = None):
102102
for p in spm_platforms.supported(condition.platforms)
103103
]
104104

105+
# Because `spm_platforms.label` may transform two different platforms into
106+
# the same Bazel label (e.g. `macos` and `driverkit`), we need to uniquify
107+
# the conditions.
108+
conditions = sets.to_list(sets.make(conditions))
109+
105110
return [
106111
_new(kind = kind, value = labels, condition = c)
107112
for c in conditions
@@ -155,34 +160,33 @@ def _to_starlark(values, kind_handlers = {}, mutually_inclusive = False):
155160
# dict whose keys are the conditions and the value is the value for the
156161
# condition.
157162
selects_by_kind = {}
158-
no_condition_results = sets.make()
163+
no_condition_results = []
159164

160165
for v in values:
161166
v_type = type(v)
162167
if v_type != "struct":
163168
if v_type == "list":
164-
no_condition_results = sets.union(no_condition_results, sets.make(v))
169+
no_condition_results.extend(v)
165170
else:
166-
sets.insert(no_condition_results, v)
171+
no_condition_results.append(v)
167172
continue
168173

169174
# We are assuming that the select will always result in a list.
170175
# Hence, we wrap the transformed value in a list.
171176
kind_handler = kind_handlers.get(v.kind, _noop_kind_handler)
172-
tvs_set = sets.make(lists.flatten(kind_handler.transform(v.value)))
177+
tvs = lists.flatten(kind_handler.transform(v.value))
173178
if v.condition != None:
174179
# Collect all of the values associted with a condition.
175180
select_dict = selects_by_kind.get(v.kind, {})
176-
condition_values = select_dict.get(v.condition, sets.make())
177-
condition_values = sets.union(condition_values, tvs_set)
181+
condition_values = select_dict.get(v.condition, []) + tvs
178182
select_dict[v.condition] = condition_values
179183
selects_by_kind[v.kind] = select_dict
180184
else:
181-
no_condition_results = sets.union(no_condition_results, tvs_set)
185+
no_condition_results = no_condition_results + tvs
182186

183187
expr_members = []
184-
if sets.length(no_condition_results) > 0:
185-
expr_members.append(sets.to_list(no_condition_results))
188+
if len(no_condition_results) > 0:
189+
expr_members.append(no_condition_results)
186190
for (kind, select_dict) in selects_by_kind.items():
187191
kind_handler = kind_handlers.get(kind, _noop_kind_handler)
188192
sorted_keys = sorted(select_dict.keys())
@@ -191,13 +195,13 @@ def _to_starlark(values, kind_handlers = {}, mutually_inclusive = False):
191195
# Generate multiple select expressions for each condition.
192196
for k in sorted_keys:
193197
new_dict = {
194-
k: sets.to_list(select_dict[k]),
198+
k: select_dict[k],
195199
}
196200
_append_select(expr_members, kind_handler, new_dict)
197201
else:
198202
# Combine all conditions of the same kind into one select expression.
199203
new_dict = {
200-
k: sets.to_list(select_dict[k])
204+
k: select_dict[k]
201205
for k in sorted_keys
202206
}
203207
_append_select(expr_members, kind_handler, new_dict)

swiftpkg/internal/swiftpkg_build_files.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ def _swift_target_build_file(pkg_ctx, target):
106106
# SPM directive instructing the code to build as if a Swift package.
107107
# https://github.com/apple/swift-package-manager/blob/main/Documentation/Usage.md#packaging-legacy-code
108108
"-DSWIFT_PACKAGE",
109+
# SPM directive instructing the code to build as if a Swift package for any clang modules.
110+
"-Xcc",
111+
"-DSWIFT_PACKAGE",
109112
]
110113

111114
# GH046: Support plugins.

swiftpkg/tests/bzl_selects_tests.bzl

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,12 @@ def _to_starlark_test(ctx):
158158
struct(
159159
msg = "string values",
160160
khs = {},
161-
vals = ["first", "second", "first"],
161+
vals = ["-DFoo", "-Xcc", "-DFoo"],
162162
exp = """\
163163
[
164-
"first",
165-
"second",
164+
"-DFoo",
165+
"-Xcc",
166+
"-DFoo",
166167
]\
167168
""",
168169
),
@@ -271,33 +272,40 @@ def _to_starlark_test(ctx):
271272
khs = {},
272273
vals = [
273274
bzl_selects.new(
274-
value = "a",
275+
value = "-DFoo",
275276
kind = "mykind",
276277
condition = "//myconditions:alpha",
277278
),
278279
bzl_selects.new(
279-
value = "b",
280+
value = "-DBar",
280281
kind = "mykind",
281282
condition = "//myconditions:beta",
282283
),
283284
bzl_selects.new(
284-
value = "c",
285+
value = "-DZoo",
285286
kind = "mykind",
286287
condition = "//myconditions:alpha",
287288
),
288289
bzl_selects.new(
289-
value = "a",
290+
value = "-Xcc",
291+
kind = "mykind",
292+
condition = "//myconditions:alpha",
293+
),
294+
bzl_selects.new(
295+
value = "-DFoo",
290296
kind = "mykind",
291297
condition = "//myconditions:alpha",
292298
),
293299
],
294300
exp = """\
295301
select({
296302
"//myconditions:alpha": [
297-
"a",
298-
"c",
303+
"-DFoo",
304+
"-DZoo",
305+
"-Xcc",
306+
"-DFoo",
299307
],
300-
"//myconditions:beta": ["b"],
308+
"//myconditions:beta": ["-DBar"],
301309
"//conditions:default": [],
302310
})\
303311
""",

swiftpkg/tests/swiftpkg_build_files_tests.bzl

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,11 @@ swift_library(
495495
name = "RegularSwiftTargetAsLibrary.rspm",
496496
always_include_developer_search_paths = True,
497497
alwayslink = True,
498-
copts = ["-DSWIFT_PACKAGE"],
498+
copts = [
499+
"-DSWIFT_PACKAGE",
500+
"-Xcc",
501+
"-DSWIFT_PACKAGE",
502+
],
499503
module_name = "RegularSwiftTargetAsLibrary",
500504
package_name = "MyPackage",
501505
srcs = ["Source/RegularSwiftTargetAsLibrary/RegularSwiftTargetAsLibrary.swift"],
@@ -517,7 +521,11 @@ swift_library(
517521
name = "RegularTargetForExec.rspm",
518522
always_include_developer_search_paths = True,
519523
alwayslink = True,
520-
copts = ["-DSWIFT_PACKAGE"],
524+
copts = [
525+
"-DSWIFT_PACKAGE",
526+
"-Xcc",
527+
"-DSWIFT_PACKAGE",
528+
],
521529
deps = ["@swiftpkg_mypackage//:RegularSwiftTargetAsLibrary.rspm"],
522530
module_name = "RegularTargetForExec",
523531
package_name = "MyPackage",
@@ -535,7 +543,11 @@ load("@build_bazel_rules_swift//swift:swift.bzl", "swift_test")
535543
536544
swift_test(
537545
name = "RegularSwiftTargetAsLibraryTests.rspm",
538-
copts = ["-DSWIFT_PACKAGE"],
546+
copts = [
547+
"-DSWIFT_PACKAGE",
548+
"-Xcc",
549+
"-DSWIFT_PACKAGE",
550+
],
539551
deps = ["@swiftpkg_mypackage//:RegularSwiftTargetAsLibrary.rspm"],
540552
module_name = "RegularSwiftTargetAsLibraryTests",
541553
package_name = "MyPackage",
@@ -553,6 +565,8 @@ load("@build_bazel_rules_swift//swift:swift.bzl", "swift_binary")
553565
swift_binary(
554566
name = "SwiftExecutableTarget.rspm",
555567
copts = [
568+
"-DSWIFT_PACKAGE",
569+
"-Xcc",
556570
"-DSWIFT_PACKAGE",
557571
"-enable-experimental-feature",
558572
"BuiltinModule",
@@ -771,7 +785,11 @@ swift_library(
771785
name = "SwiftLibraryWithConditionalDep.rspm",
772786
always_include_developer_search_paths = True,
773787
alwayslink = True,
774-
copts = ["-DSWIFT_PACKAGE"],
788+
copts = [
789+
"-DSWIFT_PACKAGE",
790+
"-Xcc",
791+
"-DSWIFT_PACKAGE",
792+
],
775793
deps = ["@swiftpkg_mypackage//:ClangLibrary.rspm"] + select({
776794
"@rules_swift_package_manager//config_settings/spm/platform:ios": ["@swiftpkg_mypackage//:RegularSwiftTargetAsLibrary.rspm"],
777795
"@rules_swift_package_manager//config_settings/spm/platform:tvos": ["@swiftpkg_mypackage//:RegularSwiftTargetAsLibrary.rspm"],
@@ -853,7 +871,11 @@ swift_library(
853871
name = "SwiftForObjcTarget.rspm",
854872
always_include_developer_search_paths = True,
855873
alwayslink = True,
856-
copts = ["-DSWIFT_PACKAGE"],
874+
copts = [
875+
"-DSWIFT_PACKAGE",
876+
"-Xcc",
877+
"-DSWIFT_PACKAGE",
878+
],
857879
deps = ["@swiftpkg_mypackage//:ObjcLibraryDep.rspm"],
858880
features = ["swift.propagate_generated_module_map"],
859881
generates_header = True,
@@ -895,7 +917,11 @@ swift_library(
895917
name = "SwiftLibraryWithFilePathResource.rspm",
896918
always_include_developer_search_paths = True,
897919
alwayslink = True,
898-
copts = ["-DSWIFT_PACKAGE"],
920+
copts = [
921+
"-DSWIFT_PACKAGE",
922+
"-Xcc",
923+
"-DSWIFT_PACKAGE",
924+
],
899925
data = [":SwiftLibraryWithFilePathResource.rspm_resource_bundle"],
900926
module_name = "SwiftLibraryWithFilePathResource",
901927
package_name = "MyPackage",

0 commit comments

Comments
 (0)