From 322561707e2996d01bea07eef2daaed6b8966490 Mon Sep 17 00:00:00 2001 From: Luis Padron Date: Wed, 25 Sep 2024 16:34:46 -0400 Subject: [PATCH 1/2] fix: usage of `copts` for `SWIFT_PACKAGE` 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 --- swiftpkg/internal/bzl_selects.bzl | 22 +++++------ swiftpkg/internal/swiftpkg_build_files.bzl | 3 ++ swiftpkg/tests/bzl_selects_tests.bzl | 28 +++++++++----- swiftpkg/tests/swiftpkg_build_files_tests.bzl | 38 ++++++++++++++++--- 4 files changed, 63 insertions(+), 28 deletions(-) diff --git a/swiftpkg/internal/bzl_selects.bzl b/swiftpkg/internal/bzl_selects.bzl index cb5e95f5b..493ab807f 100644 --- a/swiftpkg/internal/bzl_selects.bzl +++ b/swiftpkg/internal/bzl_selects.bzl @@ -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", @@ -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()) @@ -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) diff --git a/swiftpkg/internal/swiftpkg_build_files.bzl b/swiftpkg/internal/swiftpkg_build_files.bzl index c8bbf1ac2..a8e58ee0f 100644 --- a/swiftpkg/internal/swiftpkg_build_files.bzl +++ b/swiftpkg/internal/swiftpkg_build_files.bzl @@ -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. diff --git a/swiftpkg/tests/bzl_selects_tests.bzl b/swiftpkg/tests/bzl_selects_tests.bzl index c04c99233..6eb26124a 100644 --- a/swiftpkg/tests/bzl_selects_tests.bzl +++ b/swiftpkg/tests/bzl_selects_tests.bzl @@ -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", ]\ """, ), @@ -271,22 +272,27 @@ 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", ), @@ -294,10 +300,12 @@ def _to_starlark_test(ctx): exp = """\ select({ "//myconditions:alpha": [ - "a", - "c", + "-DFoo", + "-DZoo", + "-Xcc", + "-DFoo", ], - "//myconditions:beta": ["b"], + "//myconditions:beta": ["-DBar"], "//conditions:default": [], })\ """, diff --git a/swiftpkg/tests/swiftpkg_build_files_tests.bzl b/swiftpkg/tests/swiftpkg_build_files_tests.bzl index 7a8ec9e50..ccc432584 100644 --- a/swiftpkg/tests/swiftpkg_build_files_tests.bzl +++ b/swiftpkg/tests/swiftpkg_build_files_tests.bzl @@ -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"], @@ -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", @@ -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", @@ -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", @@ -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"], @@ -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", @@ -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", From ce691a95a1ec5a63b869fc853ef5b6091002ff33 Mon Sep 17 00:00:00 2001 From: Luis Padron Date: Wed, 25 Sep 2024 22:51:52 -0400 Subject: [PATCH 2/2] Update swiftpkg/internal/swiftpkg_build_files.bzl --- swiftpkg/internal/swiftpkg_build_files.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swiftpkg/internal/swiftpkg_build_files.bzl b/swiftpkg/internal/swiftpkg_build_files.bzl index a8e58ee0f..34790ad35 100644 --- a/swiftpkg/internal/swiftpkg_build_files.bzl +++ b/swiftpkg/internal/swiftpkg_build_files.bzl @@ -106,7 +106,7 @@ 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. + # Define the SPM directive through all C compiler invocations "-Xcc", "-DSWIFT_PACKAGE", ]