Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: usage of copts for SWIFT_PACKAGE #1260

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
luispadron marked this conversation as resolved.
Show resolved Hide resolved
"-Xcc",
"-DSWIFT_PACKAGE",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am confused. When are we building a C/C++ file with the swift compiler? We have separate targets for Swift and C/C++ source code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-Xcc passes a flag through to all C compiler invocations

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. Perhaps, I am missing something. We create cc_xxx, objc_xxx, and swift_xxx targets separating the source files into their respective targets. How is a C/C++ file being compiled by the Swift compiler? The cc_xxx and objc_xxx should be compiled with clang.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When Swift depends on a clang module (modulemap and headers), with implicit modules it compiles that clang module. -Xcc is a way to pass compiler flags to that compilation.

]

# 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
Loading