Skip to content

Commit

Permalink
Add a Starlark copt flag defined in rules_swift to replace the nati…
Browse files Browse the repository at this point in the history
…ve `--swiftcopt` flag in Bazel.

PiperOrigin-RevId: 584323059
  • Loading branch information
allevato authored and swiple-rules-gardener committed Nov 21, 2023
1 parent bb62c89 commit 8be7247
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
10 changes: 10 additions & 0 deletions swift/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load(
"@bazel_skylib//rules:common_settings.bzl",
"bool_setting",
"string_list_flag",
)
load(
"//swift/internal:build_settings.bzl",
Expand Down Expand Up @@ -269,6 +270,15 @@ swift_interop_hint(
visibility = ["//visibility:public"],
)

# Additional Swift compiler flags that will be applied to all `SwiftCompile`
# actions in non-execution configurations (but not module interface compile
# actions or Clang module compile actions).
string_list_flag(
name = "copt",
build_setting_default = [],
visibility = ["//visibility:public"],
)

# User settable flag that specifies additional Swift copts on a per-swiftmodule basis.
per_module_swiftcopt_flag(
name = "per_module_swiftcopt",
Expand Down
17 changes: 15 additions & 2 deletions swift/toolchains/xcode_swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ toolchain, see `doc/rules.md`.

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain")
load(
"@build_bazel_rules_swift//swift:providers.bzl",
Expand Down Expand Up @@ -561,6 +562,11 @@ def _xcode_swift_toolchain_impl(ctx):

xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig]

# TODO: b/312204041 - Remove the use of the `swift` fragment once we've
# migrated the `--swiftcopt` flag via `--flag_alias`.
swiftcopts = (
ctx.fragments.swift.copts() + ctx.attr._copts[BuildSettingInfo].value
)
swift_linkopts_cc_info = _swift_linkopts_cc_info(
apple_toolchain = apple_toolchain,
target_triple = target_triple,
Expand Down Expand Up @@ -591,7 +597,7 @@ def _xcode_swift_toolchain_impl(ctx):
requested_features = features_for_build_modes(
ctx,
cpp_fragment = cpp_fragment,
) + wmo_features_from_swiftcopts(swiftcopts = ctx.fragments.swift.copts())
) + wmo_features_from_swiftcopts(swiftcopts = swiftcopts)
requested_features.extend(ctx.features)
requested_features.extend(default_features_for_toolchain(
ctx = ctx,
Expand Down Expand Up @@ -625,7 +631,7 @@ def _xcode_swift_toolchain_impl(ctx):
ctx.fragments.cpp,
ctx.fragments.objc,
),
additional_swiftc_copts = ctx.fragments.swift.copts(),
additional_swiftc_copts = swiftcopts,
apple_toolchain = apple_toolchain,
generated_header_rewriter = generated_header_rewriter,
needs_resource_directory = swift_executable or toolchain_root,
Expand Down Expand Up @@ -775,6 +781,13 @@ configuration options that are applied to targets on a per-package basis.
doc = """\
The C++ toolchain from which linking flags and other tools needed by the Swift
toolchain (such as `clang`) will be retrieved.
""",
),
"_copts": attr.label(
default = Label("@build_bazel_rules_swift//swift:copt"),
doc = """\
The label of the `string_list` containing additional flags that should be passed
to the compiler.
""",
),
"_module_mapping": attr.label(
Expand Down

2 comments on commit 8be7247

@keith
Copy link
Member

@keith keith commented on 8be7247 Nov 27, 2023

Choose a reason for hiding this comment

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

@brentleyjones
Copy link
Collaborator

Choose a reason for hiding this comment

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

And #1363

Please sign in to comment.