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

Replace --swiftcopt with starlark flag #1139

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 19 additions & 0 deletions swift/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "bool_setting")
load(
"//swift/internal:build_settings.bzl",
"per_module_swiftcopt_flag",
"repeatable_string_flag",
)

package(default_visibility = ["//visibility:public"])
Expand Down Expand Up @@ -81,6 +82,24 @@ filegroup(
],
)

# 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).
repeatable_string_flag(
name = "copt",
build_setting_default = [],
visibility = ["//visibility:public"],
)

# Additional Swift compiler flags that will be applied to all `SwiftCompile`
# actions in execution configurations (but not module interface compile
# actions or Clang module compile actions).
repeatable_string_flag(
name = "exec_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
1 change: 1 addition & 0 deletions swift/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ bzl_library(
bzl_library(
name = "build_settings",
srcs = ["build_settings.bzl"],
deps = ["@bazel_skylib//rules:common_settings"],
)

# Indirection needed to prevent using `objc_library` outside of macOS
Expand Down
19 changes: 19 additions & 0 deletions swift/internal/build_settings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@

"""Custom build settings rules for Swift rules."""

load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")

def _repeatable_string_flag_impl(ctx):
return BuildSettingInfo(value = ctx.build_setting_value)

repeatable_string_flag = rule(
build_setting = config.string_list(
flag = True,
repeatable = True,
),
doc = """\
A string-valued flag that can occur on the command line multiple times, used for
flags like `copt`. This allows flags to be stacked in `--config`s (rather than
overwriting previous occurrences) and also makes no assumption about the values
of the flags (comma-splitting does not occur).
""",
implementation = _repeatable_string_flag_impl,
)

PerModuleSwiftCoptSettingInfo = provider(
doc = "A provider for the parsed per-swiftmodule swift copts.",
fields = {
Expand Down
27 changes: 25 additions & 2 deletions swift/internal/swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ toolchain, see `swift.bzl`.

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")
load(":actions.bzl", "swift_action_names")
load(":attrs.bzl", "swift_toolchain_driver_attrs")
Expand Down Expand Up @@ -292,11 +293,19 @@ def _swift_toolchain_impl(ctx):
toolchain_root,
)

# TODO: b/312204041 - Remove the use of the `swift` fragment once we've
# migrated the `--swiftcopt` flag via `--flag_alias`.
swiftcopts = list(ctx.fragments.swift.copts())
if "-exec-" in ctx.bin_dir.path:
keith marked this conversation as resolved.
Show resolved Hide resolved
swiftcopts.extend(ctx.attr._exec_copts[BuildSettingInfo].value)
else:
swiftcopts.extend(ctx.attr._copts[BuildSettingInfo].value)

# Combine build mode features, autoconfigured features, and required
# features.
requested_features = (
features_for_build_modes(ctx) +
features_from_swiftcopts(swiftcopts = ctx.fragments.swift.copts())
features_from_swiftcopts(swiftcopts = swiftcopts)
)
requested_features.extend([
SWIFT_FEATURE_CACHEABLE_SWIFTMODULES,
Expand Down Expand Up @@ -330,7 +339,7 @@ def _swift_toolchain_impl(ctx):
arch = ctx.attr.arch,
sdkroot = ctx.attr.sdkroot,
xctest_version = ctx.attr.xctest_version,
additional_swiftc_copts = ctx.fragments.swift.copts(),
additional_swiftc_copts = swiftcopts,
)

if ctx.attr.os == "windows":
Expand Down Expand Up @@ -441,6 +450,20 @@ configuration options that are applied to targets on a per-package basis.
doc = """\
The C++ toolchain from which other tools needed by the Swift toolchain (such as
`clang` and `ar`) 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.
""",
),
"_exec_copts": attr.label(
default = Label("@build_bazel_rules_swift//swift:exec_copt"),
doc = """\
The label of the `string_list` containing additional flags that should be passed
to the compiler for exec transition builds.
""",
),
"_worker": attr.label(
Expand Down
27 changes: 25 additions & 2 deletions swift/internal/xcode_swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ toolchain, see `swift.bzl`.
load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@bazel_skylib//lib:partial.bzl", "partial")
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")
load(":actions.bzl", "swift_action_names")
load(":attrs.bzl", "swift_toolchain_driver_attrs")
Expand Down Expand Up @@ -551,6 +552,14 @@ 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 = list(ctx.fragments.swift.copts())
if "-exec-" in ctx.bin_dir.path:
swiftcopts.extend(ctx.attr._exec_copts[BuildSettingInfo].value)
else:
swiftcopts.extend(ctx.attr._copts[BuildSettingInfo].value)

# `--define=SWIFT_USE_TOOLCHAIN_ROOT=<path>` is a rapid development feature
# that lets you build *just* a custom `swift` driver (and `swiftc`
# symlink), rather than a full toolchain, and point compilation actions at
Expand Down Expand Up @@ -589,7 +598,7 @@ def _xcode_swift_toolchain_impl(ctx):
requested_features = features_for_build_modes(
ctx,
cpp_fragment = cpp_fragment,
) + features_from_swiftcopts(swiftcopts = ctx.fragments.swift.copts())
) + features_from_swiftcopts(swiftcopts = swiftcopts)
requested_features.extend(ctx.features)
requested_features.extend([
SWIFT_FEATURE_BUNDLED_XCTESTS,
Expand Down Expand Up @@ -640,7 +649,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 @@ -783,6 +792,20 @@ 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.
""",
),
"_exec_copts": attr.label(
default = Label("@build_bazel_rules_swift//swift:exec_copt"),
doc = """\
The label of the `string_list` containing additional flags that should be passed
to the compiler for exec transition builds.
""",
),
"_worker": attr.label(
Expand Down