Skip to content

Commit

Permalink
Add malloc attribute to swift_binary.
Browse files Browse the repository at this point in the history
RELNOTES: Like `cc_{binary,test}`, `swift_{binary,test}` now supports a `malloc` attribute that lets the user substitute an alternate `malloc` implementation to be linked into the binary.
PiperOrigin-RevId: 244659591
  • Loading branch information
allevato authored and swiple-rules-gardener committed Apr 22, 2019
1 parent 20f107b commit f53747c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
10 changes: 10 additions & 0 deletions swift/internal/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ def collect_link_libraries(target):

if SwiftCcLibsInfo in target:
depsets.append(target[SwiftCcLibsInfo].libraries)
elif CcInfo in target:
# TODO(b/124371696): This edge case occurs for the "malloc" target of binaries, which is
# passed directly to the link action and does not pass through swift_cc_libs_aspect. This
# can be removed when all linking logic is consolidated into `CcInfo`.
linking_context = target[CcInfo].linking_context
for library in linking_context.libraries_to_link:
if library.pic_static_library:
depsets.append(depset(direct = [library.pic_static_library]))
elif library.static_library:
depsets.append(depset(direct = [library.static_library]))

return [depset(transitive = depsets, order = "topological")]

Expand Down
60 changes: 35 additions & 25 deletions swift/internal/swift_binary_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,34 @@ load(":providers.bzl", "SwiftToolchainInfo")
load(":swift_c_module_aspect.bzl", "swift_c_module_aspect")
load(":utils.bzl", "expand_locations")

# Attributes common to both `swift_binary` and `swift_test`.
_BINARY_RULE_ATTRS = dicts.add(
swift_common.compilation_attrs(additional_deps_aspects = [swift_c_module_aspect]),
{
"linkopts": attr.string_list(
doc = """
Additional linker options that should be passed to `clang`. These strings are subject to
`$(location ...)` expansion.
""",
mandatory = False,
),
"malloc": attr.label(
default = Label("@bazel_tools//tools/cpp:malloc"),
doc = """
Override the default dependency on `malloc`.
By default, Swift binaries are linked against `@bazel_tools//tools/cpp:malloc"`, which is an empty
library and the resulting binary will use libc's `malloc`. This label must refer to a `cc_library`
rule.
""",
mandatory = False,
providers = [[CcInfo]],
),
# Do not add references; temporary attribute for C++ toolchain Skylark migration.
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
},
)

def _configure_features_for_binary(ctx, requested_features = [], unsupported_features = []):
"""Creates and returns the feature configuration for binary linking.
Expand Down Expand Up @@ -151,11 +179,15 @@ def _swift_linking_rule_impl(
)
link_args.add_all(link_cpp_toolchain_flags)

deps_to_link = ctx.attr.deps + toolchain.implicit_deps
if ctx.attr.malloc:
deps_to_link.append(ctx.attr.malloc)

register_link_action(
actions = ctx.actions,
action_environment = toolchain.action_environment,
clang_executable = toolchain.clang_executable,
deps = ctx.attr.deps + toolchain.implicit_deps,
deps = deps_to_link,
expanded_linkopts = linkopts,
inputs = additional_inputs_to_linker,
mnemonic = "SwiftLinkExecutable",
Expand Down Expand Up @@ -295,20 +327,7 @@ def _swift_test_impl(ctx):
]

swift_binary = rule(
attrs = dicts.add(
swift_common.compilation_attrs(additional_deps_aspects = [swift_c_module_aspect]),
{
"linkopts": attr.string_list(
doc = """
Additional linker options that should be passed to `clang`. These strings are subject to
`$(location ...)` expansion.
""",
mandatory = False,
),
# Do not add references; temporary attribute for C++ toolchain Skylark migration.
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
},
),
attrs = _BINARY_RULE_ATTRS,
doc = """
Compiles and links Swift code into an executable binary.
Expand All @@ -335,21 +354,12 @@ instead of `swift_binary`.

swift_test = rule(
attrs = dicts.add(
swift_common.compilation_attrs(additional_deps_aspects = [swift_c_module_aspect]),
_BINARY_RULE_ATTRS,
{
"linkopts": attr.string_list(
doc = """
Additional linker options that should be passed to `clang`. These strings are subject to
`$(location ...)` expansion.
""",
mandatory = False,
),
"_apple_coverage_support": attr.label(
cfg = "host",
default = Label("@build_bazel_apple_support//tools:coverage_support"),
),
# Do not add references; temporary attribute for C++ toolchain Skylark migration.
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
"_xctest_runner_template": attr.label(
allow_single_file = True,
default = Label(
Expand Down

0 comments on commit f53747c

Please sign in to comment.