Skip to content

Commit

Permalink
fix: usage of copts for SWIFT_PACKAGE
Browse files Browse the repository at this point in the history
This primarily does two changes:

- Passes `-Xcc -DSWIFT_PACKAGE` for the Swift library target generated `copts` attribute.
- Removes de-duplication in `bzl_selects.bzl`
  - This is required because `copts = ["-DSWIFT_PACKAGE", "-Xcc", "-DSWIFT_PACKAGE"]` are valid copts but would get de-duplicated into `copts = ["-DSWIFT_PACKAGE", "-Xcc"]`

Fixes #1259
  • Loading branch information
luispadron committed Sep 25, 2024
1 parent 76b88dc commit 3225617
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 28 deletions.
22 changes: 10 additions & 12 deletions swiftpkg/internal/bzl_selects.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ Module for transforming Swift package manifest conditionals to Bazel select \
statements.\
"""

load("@bazel_skylib//lib:sets.bzl", "sets")
load("@cgrindel_bazel_starlib//bzllib:defs.bzl", "lists")
load(
"//config_settings/spm/configuration:configurations.bzl",
Expand Down Expand Up @@ -155,34 +154,33 @@ def _to_starlark(values, kind_handlers = {}, mutually_inclusive = False):
# dict whose keys are the conditions and the value is the value for the
# condition.
selects_by_kind = {}
no_condition_results = sets.make()
no_condition_results = []

for v in values:
v_type = type(v)
if v_type != "struct":
if v_type == "list":
no_condition_results = sets.union(no_condition_results, sets.make(v))
no_condition_results.extend(v)
else:
sets.insert(no_condition_results, v)
no_condition_results.append(v)
continue

# We are assuming that the select will always result in a list.
# Hence, we wrap the transformed value in a list.
kind_handler = kind_handlers.get(v.kind, _noop_kind_handler)
tvs_set = sets.make(lists.flatten(kind_handler.transform(v.value)))
tvs = lists.flatten(kind_handler.transform(v.value))
if v.condition != None:
# Collect all of the values associted with a condition.
select_dict = selects_by_kind.get(v.kind, {})
condition_values = select_dict.get(v.condition, sets.make())
condition_values = sets.union(condition_values, tvs_set)
condition_values = select_dict.get(v.condition, []) + tvs
select_dict[v.condition] = condition_values
selects_by_kind[v.kind] = select_dict
else:
no_condition_results = sets.union(no_condition_results, tvs_set)
no_condition_results = no_condition_results + tvs

expr_members = []
if sets.length(no_condition_results) > 0:
expr_members.append(sets.to_list(no_condition_results))
if len(no_condition_results) > 0:
expr_members.append(no_condition_results)
for (kind, select_dict) in selects_by_kind.items():
kind_handler = kind_handlers.get(kind, _noop_kind_handler)
sorted_keys = sorted(select_dict.keys())
Expand All @@ -191,13 +189,13 @@ def _to_starlark(values, kind_handlers = {}, mutually_inclusive = False):
# Generate multiple select expressions for each condition.
for k in sorted_keys:
new_dict = {
k: sets.to_list(select_dict[k]),
k: select_dict[k],
}
_append_select(expr_members, kind_handler, new_dict)
else:
# Combine all conditions of the same kind into one select expression.
new_dict = {
k: sets.to_list(select_dict[k])
k: select_dict[k]
for k in sorted_keys
}
_append_select(expr_members, kind_handler, new_dict)
Expand Down
3 changes: 3 additions & 0 deletions swiftpkg/internal/swiftpkg_build_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ def _swift_target_build_file(pkg_ctx, target):
# SPM directive instructing the code to build as if a Swift package.
# https://github.com/apple/swift-package-manager/blob/main/Documentation/Usage.md#packaging-legacy-code
"-DSWIFT_PACKAGE",
# SPM directive instructing the code to build as if a Swift package for any clang modules.
"-Xcc",
"-DSWIFT_PACKAGE",
]

# GH046: Support plugins.
Expand Down
28 changes: 18 additions & 10 deletions swiftpkg/tests/bzl_selects_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,12 @@ def _to_starlark_test(ctx):
struct(
msg = "string values",
khs = {},
vals = ["first", "second", "first"],
vals = ["-DFoo", "-Xcc", "-DFoo"],
exp = """\
[
"first",
"second",
"-DFoo",
"-Xcc",
"-DFoo",
]\
""",
),
Expand Down Expand Up @@ -271,33 +272,40 @@ def _to_starlark_test(ctx):
khs = {},
vals = [
bzl_selects.new(
value = "a",
value = "-DFoo",
kind = "mykind",
condition = "//myconditions:alpha",
),
bzl_selects.new(
value = "b",
value = "-DBar",
kind = "mykind",
condition = "//myconditions:beta",
),
bzl_selects.new(
value = "c",
value = "-DZoo",
kind = "mykind",
condition = "//myconditions:alpha",
),
bzl_selects.new(
value = "a",
value = "-Xcc",
kind = "mykind",
condition = "//myconditions:alpha",
),
bzl_selects.new(
value = "-DFoo",
kind = "mykind",
condition = "//myconditions:alpha",
),
],
exp = """\
select({
"//myconditions:alpha": [
"a",
"c",
"-DFoo",
"-DZoo",
"-Xcc",
"-DFoo",
],
"//myconditions:beta": ["b"],
"//myconditions:beta": ["-DBar"],
"//conditions:default": [],
})\
""",
Expand Down
38 changes: 32 additions & 6 deletions swiftpkg/tests/swiftpkg_build_files_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,11 @@ load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")
swift_library(
name = "RegularSwiftTargetAsLibrary.rspm",
always_include_developer_search_paths = True,
copts = ["-DSWIFT_PACKAGE"],
copts = [
"-DSWIFT_PACKAGE",
"-Xcc",
"-DSWIFT_PACKAGE",
],
module_name = "RegularSwiftTargetAsLibrary",
package_name = "MyPackage",
srcs = ["Source/RegularSwiftTargetAsLibrary/RegularSwiftTargetAsLibrary.swift"],
Expand All @@ -513,7 +517,11 @@ load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")
swift_library(
name = "RegularTargetForExec.rspm",
always_include_developer_search_paths = True,
copts = ["-DSWIFT_PACKAGE"],
copts = [
"-DSWIFT_PACKAGE",
"-Xcc",
"-DSWIFT_PACKAGE",
],
deps = ["@swiftpkg_mypackage//:RegularSwiftTargetAsLibrary.rspm"],
module_name = "RegularTargetForExec",
package_name = "MyPackage",
Expand All @@ -531,7 +539,11 @@ load("@build_bazel_rules_swift//swift:swift.bzl", "swift_test")
swift_test(
name = "RegularSwiftTargetAsLibraryTests.rspm",
copts = ["-DSWIFT_PACKAGE"],
copts = [
"-DSWIFT_PACKAGE",
"-Xcc",
"-DSWIFT_PACKAGE",
],
deps = ["@swiftpkg_mypackage//:RegularSwiftTargetAsLibrary.rspm"],
module_name = "RegularSwiftTargetAsLibraryTests",
package_name = "MyPackage",
Expand All @@ -549,6 +561,8 @@ load("@build_bazel_rules_swift//swift:swift.bzl", "swift_binary")
swift_binary(
name = "SwiftExecutableTarget.rspm",
copts = [
"-DSWIFT_PACKAGE",
"-Xcc",
"-DSWIFT_PACKAGE",
"-enable-experimental-feature",
"BuiltinModule",
Expand Down Expand Up @@ -732,7 +746,11 @@ load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")
swift_library(
name = "SwiftLibraryWithConditionalDep.rspm",
always_include_developer_search_paths = True,
copts = ["-DSWIFT_PACKAGE"],
copts = [
"-DSWIFT_PACKAGE",
"-Xcc",
"-DSWIFT_PACKAGE",
],
deps = ["@swiftpkg_mypackage//:ClangLibrary.rspm"] + select({
"@rules_swift_package_manager//config_settings/spm/platform:ios": ["@swiftpkg_mypackage//:RegularSwiftTargetAsLibrary.rspm"],
"@rules_swift_package_manager//config_settings/spm/platform:tvos": ["@swiftpkg_mypackage//:RegularSwiftTargetAsLibrary.rspm"],
Expand Down Expand Up @@ -802,7 +820,11 @@ generate_modulemap(
swift_library(
name = "SwiftForObjcTarget.rspm",
always_include_developer_search_paths = True,
copts = ["-DSWIFT_PACKAGE"],
copts = [
"-DSWIFT_PACKAGE",
"-Xcc",
"-DSWIFT_PACKAGE",
],
deps = [
"@swiftpkg_mypackage//:ObjcLibraryDep.rspm",
"@swiftpkg_mypackage//:ObjcLibraryDep.rspm_modulemap",
Expand Down Expand Up @@ -845,7 +867,11 @@ resource_bundle_infoplist(
swift_library(
name = "SwiftLibraryWithFilePathResource.rspm",
always_include_developer_search_paths = True,
copts = ["-DSWIFT_PACKAGE"],
copts = [
"-DSWIFT_PACKAGE",
"-Xcc",
"-DSWIFT_PACKAGE",
],
data = [":SwiftLibraryWithFilePathResource.rspm_resource_bundle"],
module_name = "SwiftLibraryWithFilePathResource",
package_name = "MyPackage",
Expand Down

0 comments on commit 3225617

Please sign in to comment.