Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
Add a user settable build setting to specify additional swiftcopts fo…
Browse files Browse the repository at this point in the history
…r a swift_libray target.

The flag accepts a comma separated list of "target=copts" pairs, where "target" is a fully qualified target label and "copts" is a colon separated list of Swift copts.

PiperOrigin-RevId: 371728552
  • Loading branch information
Googler authored and swiple-rules-gardener committed May 3, 2021
1 parent 06322f1 commit de9c4e1
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
11 changes: 11 additions & 0 deletions swift/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//rules:common_settings.bzl", "bool_setting")
load(
"//swift/internal:build_settings.bzl",
"per_module_swiftcopt_flag",
)

package(default_visibility = ["//visibility:public"])

Expand Down Expand Up @@ -35,6 +39,13 @@ filegroup(
],
)

# User settable flag that specifies additional Swift copts on a per-swiftmodule basis.
per_module_swiftcopt_flag(
name = "per_module_swiftcopt",
build_setting_default = "",
visibility = ["//visibility:public"],
)

# Configuration setting for enabling the generation of swiftinterface files.
bool_setting(
name = "emit_swiftinterface",
Expand Down
6 changes: 6 additions & 0 deletions swift/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ bzl_library(
],
deps = [
":build_defs",
":build_settings",
":swift_common",
"@bazel_skylib//lib:dicts",
"@bazel_skylib//lib:new_sets",
Expand Down Expand Up @@ -90,6 +91,11 @@ bzl_library(
],
)

bzl_library(
name = "build_settings",
srcs = ["build_settings.bzl"],
)

# Consumed by Bazel integration tests.
filegroup(
name = "for_bazel_tests",
Expand Down
3 changes: 3 additions & 0 deletions swift/internal/attrs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ def swift_config_attrs():
"_config_emit_swiftinterface": attr.label(
default = "@build_bazel_rules_swift//swift:emit_swiftinterface",
),
"_per_module_swiftcopt": attr.label(
default = "@build_bazel_rules_swift//swift:per_module_swiftcopt",
),
}

def swift_deps_attr(doc, **kwargs):
Expand Down
85 changes: 85 additions & 0 deletions swift/internal/build_settings.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Copyright 2021 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

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

PerModuleSwiftCoptSettingInfo = provider(
doc = "A provider for the parsed per-swiftmodule swift copts.",
fields = {
"value": "A map of target labels to lists of additional Swift copts to apply to" +
"the target.",
},
)

def additional_per_module_swiftcopts(label, provider):
"""Returns additional swiftcopts to apply to a target named `label`.
Args:
label: The label of the target.
provider: The `PerModuleSwiftCoptsSettingInfo` provider from the calling
context.
Returns:
A list of additional swiftcopts to use when builing the target.
"""
per_module_copts = provider.value
if per_module_copts:
target_label = str(label)
return per_module_copts.get(target_label, [])
return []

def _per_module_swiftcopt_flag_impl(ctx):
# Each item in this list should of the form
# "<target label>=<comma separated copts>".
module_and_copts_list = ctx.build_setting_value
value = dict()
for item in module_and_copts_list:
if not item:
continue
contents = item.split("=", 1)
if len(contents) != 2:
fail("""\
--per_module_swiftcopt must be written as a target label, an equal sign \
("="), and a comma-delimited list of compiler flags. For example, \
"//package:target=-copt,-copt,...".""")

# Canonicalize the label using the `Label` constructor.
label = str(Label(contents[0]))
raw_copts = contents[1]

# TODO(b/186875113): This breaks if any of the copts actually use
# commas. Switch to a more selective approach to splitting,
# respecting an escape sequence for commas inside of copts.
copts = raw_copts.split(",")
if len(copts) > 0:
existing_copts = value.get(label)
if existing_copts:
existing_copts.extend(copts)
else:
value[label] = copts
return PerModuleSwiftCoptSettingInfo(value = value)

per_module_swiftcopt_flag = rule(
build_setting = config.string(
flag = True,
allow_multiple = True,
),
# TODO(b/186869451): Support adding swiftcopts by module name in addition
# to the target label.
doc = """\
A string list build setting that can be set on the command line. Each item in
the list is expected to be of the form: <//target-package:name>=<copts> where
copts is a colon separated list of Swift copts.
""",
implementation = _per_module_swiftcopt_flag_impl,
)
11 changes: 11 additions & 0 deletions swift/internal/swift_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
"""Implementation of the `swift_library` rule."""

load(":attrs.bzl", "swift_deps_attr")
load(
":build_settings.bzl",
"PerModuleSwiftCoptSettingInfo",
"additional_per_module_swiftcopts",
)
load(
":compiling.bzl",
"new_objc_provider",
Expand Down Expand Up @@ -104,6 +109,12 @@ def _swift_library_impl(ctx):
linkopts = expand_locations(ctx, ctx.attr.linkopts, ctx.attr.swiftc_inputs)
srcs = ctx.files.srcs

module_copts = additional_per_module_swiftcopts(
ctx.label,
ctx.attr._per_module_swiftcopt[PerModuleSwiftCoptSettingInfo],
)
copts.extend(module_copts)

extra_features = []
if ctx.attr._config_emit_swiftinterface[BuildSettingInfo].value:
extra_features.append(SWIFT_FEATURE_ENABLE_LIBRARY_EVOLUTION)
Expand Down

0 comments on commit de9c4e1

Please sign in to comment.