Skip to content

Commit

Permalink
fix: specify all defines as local using copts (#1094)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cgrindel authored Jun 2, 2024
1 parent 018e6f0 commit 6501fcc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
36 changes: 19 additions & 17 deletions swiftpkg/internal/swiftpkg_build_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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:
Expand All @@ -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)

Expand Down Expand Up @@ -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
]))
Expand Down Expand Up @@ -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:
Expand Down
36 changes: 17 additions & 19 deletions swiftpkg/tests/swiftpkg_build_files_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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"],
Expand All @@ -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"],
Expand All @@ -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 = [],
Expand All @@ -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 = [
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"],
Expand All @@ -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"],
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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"],
Expand Down

0 comments on commit 6501fcc

Please sign in to comment.