From 6501fcc07bed60879b4eb49cec4b4d22756c6ee4 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Sat, 1 Jun 2024 19:02:22 -0600 Subject: [PATCH] fix: specify all defines as local using `copts` (#1094) In an attempt to localize defines to specific targets, we specify defines/macros for all target types (e.g., `swift_library`, `objc_library`, and `cc_library`) targets using `copts`. Fixes #1093. --- swiftpkg/internal/swiftpkg_build_files.bzl | 36 ++++++++++--------- swiftpkg/tests/swiftpkg_build_files_tests.bzl | 36 +++++++++---------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/swiftpkg/internal/swiftpkg_build_files.bzl b/swiftpkg/internal/swiftpkg_build_files.bzl index 3696caf63..3042f8f6b 100644 --- a/swiftpkg/internal/swiftpkg_build_files.bzl +++ b/swiftpkg/internal/swiftpkg_build_files.bzl @@ -71,12 +71,13 @@ def _swift_target_build_file(pkg_ctx, target): deps_without_plugins = [dep for dep in deps if dep.value[0] not in plugins] attrs["deps"] = bzl_selects.to_starlark(deps_without_plugins) - defines = [ + # NOTE: We specify defines using copts so that they stay local to the + # target. Specifying them using the defines attribute will propagate them. + copts = [ # 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 - "SWIFT_PACKAGE", + "-DSWIFT_PACKAGE", ] - copts = [] # GH046: Support plugins. @@ -89,8 +90,11 @@ def _swift_target_build_file(pkg_ctx, target): if target.swift_settings != None: if len(target.swift_settings.defines) > 0: - defines.extend(lists.flatten([ - bzl_selects.new_from_build_setting(bs) + copts.extend(lists.flatten([ + bzl_selects.new_from_build_setting( + bs, + values_map_fn = lambda v: "-D" + v, + ) for bs in target.swift_settings.defines ])) if len(target.swift_settings.unsafe_flags) > 0: @@ -103,8 +107,6 @@ def _swift_target_build_file(pkg_ctx, target): copts.append("-enable-experimental-feature") copts.extend(lists.flatten(bzl_selects.new_from_build_setting(bs))) - if len(defines) > 0: - attrs["defines"] = bzl_selects.to_starlark(defines) if len(copts) > 0: attrs["copts"] = bzl_selects.to_starlark(copts) @@ -233,29 +235,31 @@ def _clang_target_build_file(repository_ctx, pkg_ctx, target): if res_build_file: all_build_files.append(res_build_file) - defines = [ - # The SWIFT_PACKAGE define is a magical value that SPM uses when it - # builds clang libraries that will be used as Swift modules. - "SWIFT_PACKAGE=1", - ] - # The copts may be updated by functions that were executed before this # point. Use whatever has been set. copts = attrs.get("copts", []) + # The SWIFT_PACKAGE define is a magical value that SPM uses when it + # builds clang libraries that will be used as Swift modules. + copts.append("-DSWIFT_PACKAGE=1") + local_includes = [ bzl_selects.new(value = p, kind = _condition_kinds.private_includes) for p in clang_src_info.private_includes ] + def _normalize_and_create_copt_define(value): + normalized = scg.normalize_define_value(value) + return "-D" + normalized + all_settings = lists.compact([target.clang_settings, target.cxx_settings]) for settings in all_settings: - defines.extend(lists.flatten([ + copts.extend(lists.flatten([ bzl_selects.new_from_build_setting( bs, # Define values can contain spaces. Bazel requires that they # are already escaped. - values_map_fn = scg.normalize_define_value, + values_map_fn = _normalize_and_create_copt_define, ) for bs in settings.defines ])) @@ -336,8 +340,6 @@ def _clang_target_build_file(repository_ctx, pkg_ctx, target): }, ) - attrs["defines"] = bzl_selects.to_starlark(defines) - bzl_target_name = target.label.name if target.objc_src_info != None: diff --git a/swiftpkg/tests/swiftpkg_build_files_tests.bzl b/swiftpkg/tests/swiftpkg_build_files_tests.bzl index 489ba879e..04d493ff9 100644 --- a/swiftpkg/tests/swiftpkg_build_files_tests.bzl +++ b/swiftpkg/tests/swiftpkg_build_files_tests.bzl @@ -578,7 +578,7 @@ load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library") swift_library( name = "RegularSwiftTargetAsLibrary.rspm", always_include_developer_search_paths = True, - defines = ["SWIFT_PACKAGE"], + copts = ["-DSWIFT_PACKAGE"], deps = [], module_name = "RegularSwiftTargetAsLibrary", srcs = ["Source/RegularSwiftTargetAsLibrary/RegularSwiftTargetAsLibrary.swift"], @@ -599,7 +599,7 @@ load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library") swift_library( name = "RegularTargetForExec.rspm", always_include_developer_search_paths = True, - defines = ["SWIFT_PACKAGE"], + copts = ["-DSWIFT_PACKAGE"], deps = ["@swiftpkg_mypackage//:RegularSwiftTargetAsLibrary.rspm"], module_name = "RegularTargetForExec", srcs = ["Source/RegularTargetForExec/main.swift"], @@ -616,7 +616,7 @@ load("@build_bazel_rules_swift//swift:swift.bzl", "swift_test") swift_test( name = "RegularSwiftTargetAsLibraryTests.rspm", - defines = ["SWIFT_PACKAGE"], + copts = ["-DSWIFT_PACKAGE"], deps = ["@swiftpkg_mypackage//:RegularSwiftTargetAsLibrary.rspm"], module_name = "RegularSwiftTargetAsLibraryTests", srcs = ["Tests/RegularSwiftTargetAsLibraryTests/RegularSwiftTargetAsLibraryTests.swift"], @@ -633,15 +633,15 @@ load("@build_bazel_rules_swift//swift:swift.bzl", "swift_binary") swift_binary( name = "SwiftExecutableTarget.rspm", copts = [ + "-DSWIFT_PACKAGE", "-enable-experimental-feature", "BuiltinModule", ] + select({ - "@rules_swift_package_manager//config_settings/spm/configuration:release": ["-cross-module-optimization"], + "@rules_swift_package_manager//config_settings/spm/platform:ios": ["-DFOOBAR"], + "@rules_swift_package_manager//config_settings/spm/platform:tvos": ["-DFOOBAR"], "//conditions:default": [], - }), - defines = ["SWIFT_PACKAGE"] + select({ - "@rules_swift_package_manager//config_settings/spm/platform:ios": ["FOOBAR"], - "@rules_swift_package_manager//config_settings/spm/platform:tvos": ["FOOBAR"], + }) + select({ + "@rules_swift_package_manager//config_settings/spm/configuration:release": ["-cross-module-optimization"], "//conditions:default": [], }), deps = [], @@ -663,16 +663,14 @@ cc_library( "-fobjc-arc", "-fPIC", "-fmodule-name=ClangLibrary", + "-DSWIFT_PACKAGE=1", + "-DPLATFORM_POSIX=1", "-Iexternal/bzlmodmangled~swiftpkg_mypackage/src", "-Iexternal/bzlmodmangled~swiftpkg_mypackage", ] + select({ "@rules_swift_package_manager//config_settings/spm/configuration:release": ["-danger"], "//conditions:default": [], }), - defines = [ - "SWIFT_PACKAGE=1", - "PLATFORM_POSIX=1", - ], hdrs = ["include/external.h"], includes = ["include"], srcs = [ @@ -707,9 +705,9 @@ objc_library( "-fobjc-arc", "-fPIC", "-fmodule-name=ObjcLibrary", + "-DSWIFT_PACKAGE=1", "-Iexternal/bzlmodmangled~swiftpkg_mypackage/src", ], - defines = ["SWIFT_PACKAGE=1"], deps = [ "@swiftpkg_mypackage//:ObjcLibraryDep.rspm", "@swiftpkg_mypackage//:ObjcLibraryDep.rspm_modulemap", @@ -766,9 +764,9 @@ objc_library( "-fobjc-arc", "-fPIC", "-fmodule-name=ObjcLibraryWithModulemap", + "-DSWIFT_PACKAGE=1", "-Iexternal/bzlmodmangled~swiftpkg_mypackage/src", ], - defines = ["SWIFT_PACKAGE=1"], deps = [ "@swiftpkg_mypackage//:ObjcLibraryDep.rspm", "@swiftpkg_mypackage//:ObjcLibraryDep.rspm_modulemap", @@ -812,7 +810,7 @@ load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library") swift_library( name = "SwiftLibraryWithConditionalDep.rspm", always_include_developer_search_paths = True, - defines = ["SWIFT_PACKAGE"], + copts = ["-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"], @@ -837,9 +835,9 @@ cc_library( "-fobjc-arc", "-fPIC", "-fmodule-name=ClangLibraryWithConditionalDep", + "-DSWIFT_PACKAGE=1", "-Iexternal/bzlmodmangled~swiftpkg_mypackage/src", ], - defines = ["SWIFT_PACKAGE=1"], deps = select({ "@rules_swift_package_manager//config_settings/spm/platform:ios": ["@swiftpkg_mypackage//:ClangLibrary.rspm"], "@rules_swift_package_manager//config_settings/spm/platform:tvos": ["@swiftpkg_mypackage//:ClangLibrary.rspm"], @@ -875,7 +873,7 @@ generate_modulemap( swift_library( name = "SwiftForObjcTarget.rspm", always_include_developer_search_paths = True, - defines = ["SWIFT_PACKAGE"], + copts = ["-DSWIFT_PACKAGE"], deps = [ "@swiftpkg_mypackage//:ObjcLibraryDep.rspm", "@swiftpkg_mypackage//:ObjcLibraryDep.rspm_modulemap", @@ -917,8 +915,8 @@ resource_bundle_infoplist( swift_library( name = "SwiftLibraryWithFilePathResource.rspm", always_include_developer_search_paths = True, + copts = ["-DSWIFT_PACKAGE"], data = [":SwiftLibraryWithFilePathResource.rspm_resource_bundle"], - defines = ["SWIFT_PACKAGE"], deps = [], module_name = "SwiftLibraryWithFilePathResource", srcs = [ @@ -962,10 +960,10 @@ objc_library( "-fPIC", "-fmodule-name=ObjcLibraryWithResources", "-include$(location :ObjcLibraryWithResources.rspm_objc_resource_bundle_accessor_hdr)", + "-DSWIFT_PACKAGE=1", "-Iexternal/bzlmodmangled~swiftpkg_mypackage/src", ], data = [":ObjcLibraryWithResources.rspm_resource_bundle"], - defines = ["SWIFT_PACKAGE=1"], enable_modules = True, hdrs = ["include/external.h"], includes = ["include"],