Skip to content

Commit

Permalink
## Add support for Windows linkstatic computed default
Browse files Browse the repository at this point in the history
This uses the same technique as `cc_binary` with/without aspects.
Essentially we are declaring two rules named cc_test with different private attribute defaults. This is primarily done this way to hide the implementation details of computed defaults until something like
[bazel/issues/14434](#14434) is implemented.

NOTE: When `cc_test` written in Starlark is made the default, this _will_ introduce a behavior change -- notably that we will default linkstatic based on whether we are _targeting_ Windows, not whether the host Platform is Windows (as is done today).
The latter heuristic may have been the best available option before Platforms, but it does not match the actual intent.
PiperOrigin-RevId: 441472088
  • Loading branch information
trybka authored and copybara-github committed Apr 13, 2022
1 parent 591dcc4 commit 8ba5eed
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,16 @@ def _malloc_for_target(ctx, cpp_config):
return ctx.attr.malloc

def _get_link_staticness(ctx, cpp_config):
linkstatic_attr = None
if hasattr(ctx.attr, "_linkstatic_explicitly_set") and not ctx.attr._linkstatic_explicitly_set:
# If we know that linkstatic is not explicitly set, use computed default:
linkstatic_attr = semantics.get_linkstatic_default(ctx)
else:
linkstatic_attr = ctx.attr.linkstatic

if cpp_config.dynamic_mode() == "FULLY":
return _LINKING_DYNAMIC
elif cpp_config.dynamic_mode() == "OFF" or ctx.attr.linkstatic:
elif cpp_config.dynamic_mode() == "OFF" or linkstatic_attr:
return _LINKING_STATIC
else:
return _LINKING_DYNAMIC
Expand Down
5 changes: 5 additions & 0 deletions src/main/starlark/builtins_bzl/common/cc/cc_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ _cc_test_attrs.update(
"@" + paths.join(semantics.get_platforms_root(), "os:watchos"),
],
),
_windows_constraints = attr.label_list(
default = [
"@" + paths.join(semantics.get_platforms_root(), "os:windows"),
],
),
stamp = attr.int(values = [-1, 0, 1], default = 0),
linkstatic = attr.bool(default = False),
malloc = attr.label(
Expand Down
14 changes: 14 additions & 0 deletions src/main/starlark/builtins_bzl/common/cc/semantics.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

"""Semantics for Bazel cc rules"""

load(":common/cc/cc_helper.bzl", "cc_helper")

cc_common = _builtins.toplevel.cc_common

def _should_create_empty_archive():
Expand Down Expand Up @@ -78,6 +80,17 @@ def _check_experimental_cc_shared_library(ctx):
if not cc_common.check_experimental_cc_shared_library():
fail("Pass --experimental_cc_shared_library to use cc_shared_library")

def _get_linkstatic_default(ctx):
if ctx.attr._is_test:
# By default Tests do not link statically. Except on Windows.
if cc_helper.has_target_constraints(ctx, ctx.attr._windows_constraints):
return True
else:
return False
else:
# Binaries link statically.
return True

semantics = struct(
ALLOWED_RULES_IN_DEPS = [
"cc_library",
Expand Down Expand Up @@ -108,4 +121,5 @@ semantics = struct(
get_interface_deps_allowed_attr = _get_interface_deps_allowed_attr,
should_use_interface_deps_behavior = _should_use_interface_deps_behavior,
check_experimental_cc_shared_library = _check_experimental_cc_shared_library,
get_linkstatic_default = _get_linkstatic_default,
)

0 comments on commit 8ba5eed

Please sign in to comment.