Skip to content

Commit

Permalink
Apply changes from bazelbuild@5640944 to Starlark cc_library
Browse files Browse the repository at this point in the history
This CL inverts the experimental implementation_deps attribute to
interface_deps.

RELNOTES:none
PiperOrigin-RevId: 430460463
  • Loading branch information
oquenchil authored and copybara-github committed Feb 23, 2022
1 parent e3392cd commit a20797e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -859,9 +859,8 @@ public boolean objcShouldGenerateDotdFiles() {
return cppOptions.objcGenerateDotdFiles;
}

// TODO(plf): Change in separate CL where Starlark cc_library.implementation_deps is renamed.
@StarlarkMethod(
name = "experimental_cc_implementation_deps",
name = "experimental_cc_interface_deps",
documented = false,
useStarlarkThread = true)
public boolean experimentalCcInterfaceDepsForStarlark(StarlarkThread thread)
Expand Down
29 changes: 17 additions & 12 deletions src/main/starlark/builtins_bzl/common/cc/cc_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ cc_internal = _builtins.internal.cc_internal

def _cc_library_impl(ctx):
cc_helper.check_srcs_extensions(ctx, ALLOWED_SRC_FILES, "cc_library")
cpp_config = ctx.fragments.cpp

if (not cpp_config.experimental_cc_implementation_deps() and
len(ctx.attr.implementation_deps) > 0):
fail("requires --experimental_cc_implementation_deps", attr = "implementation_deps")

common = cc_internal.create_common(ctx = ctx)
common.report_invalid_options(ctx = ctx)
Expand All @@ -47,10 +42,18 @@ def _cc_library_impl(ctx):
semantics.validate_attributes(ctx = ctx)
_check_no_repeated_srcs(ctx)

compilation_contexts = cc_helper.get_compilation_contexts_from_deps(ctx.attr.deps)
interface_deps = None
implementation_deps = []
should_use_interface_deps_behavior = semantics.should_use_interface_deps_behavior(ctx)
if should_use_interface_deps_behavior:
interface_deps = cc_helper.get_compilation_contexts_from_deps(ctx.attr.interface_deps)
implementation_deps = cc_helper.get_compilation_contexts_from_deps(ctx.attr.deps)
else:
interface_deps = cc_helper.get_compilation_contexts_from_deps(ctx.attr.deps)

if not _is_stl(ctx.attr.tags) and ctx.attr._stl != None:
compilation_contexts.append(ctx.attr._stl[CcInfo].compilation_context)
implementation_compilation_contexts = cc_helper.get_compilation_contexts_from_deps(ctx.attr.implementation_deps)
interface_deps.append(ctx.attr._stl[CcInfo].compilation_context)

(compilation_context, srcs_compilation_outputs) = cc_common.compile(
actions = ctx.actions,
name = ctx.label.name,
Expand All @@ -67,8 +70,8 @@ def _cc_library_impl(ctx):
private_hdrs = common.private_hdrs,
public_hdrs = common.public_hdrs,
code_coverage_enabled = cc_helper.is_code_coverage_enabled(ctx),
compilation_contexts = compilation_contexts,
implementation_compilation_contexts = implementation_compilation_contexts,
compilation_contexts = interface_deps,
implementation_compilation_contexts = implementation_deps,
hdrs_checking_mode = semantics.determine_headers_checking_mode(ctx),
grep_includes = ctx.executable._grep_includes,
textual_hdrs = ctx.files.textual_hdrs,
Expand Down Expand Up @@ -108,7 +111,7 @@ def _cc_library_impl(ctx):
is_google = True

linking_contexts = cc_helper.get_linking_contexts_from_deps(ctx.attr.deps)
linking_contexts.extend(cc_helper.get_linking_contexts_from_deps(ctx.attr.implementation_deps))
linking_contexts.extend(cc_helper.get_linking_contexts_from_deps(ctx.attr.interface_deps))
if ctx.file.linkstamp != None:
linkstamps = []
linkstamps.append(cc_internal.create_linkstamp(
Expand Down Expand Up @@ -584,7 +587,7 @@ attrs = {
),
"alwayslink": attr.bool(default = False),
"linkstatic": attr.bool(default = False),
"implementation_deps": attr.label_list(providers = [CcInfo], allow_files = False),
"interface_deps": attr.label_list(providers = [CcInfo], allow_files = False),
"hdrs": attr.label_list(
allow_files = True,
flags = ["ORDER_INDEPENDENT", "DIRECT_COMPILE_TIME_INPUT"],
Expand Down Expand Up @@ -628,6 +631,8 @@ attrs = {
attrs.update(semantics.get_licenses_attr())
attrs.update(semantics.get_distribs_attr())
attrs.update(semantics.get_loose_mode_in_hdrs_check_allowed_attr())
attrs.update(semantics.get_interface_deps_allowed_attr())

cc_library = rule(
implementation = _cc_library_impl,
attrs = attrs,
Expand Down
13 changes: 13 additions & 0 deletions src/main/starlark/builtins_bzl/common/cc/semantics.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ def _get_def_parser():
def _get_grep_includes():
return attr.label()

def _get_interface_deps_allowed_attr():
return {}

def _should_use_interface_deps_behavior(ctx):
experimental_cc_interface_deps = ctx.fragments.cpp.experimental_cc_interface_deps()
if (not experimental_cc_interface_deps and
len(ctx.attr.interface_deps) > 0):
fail("requires --experimental_cc_interface_deps", attr = "interface_deps")

return experimental_cc_interface_deps

semantics = struct(
ALLOWED_RULES_IN_DEPS = [
"cc_library",
Expand All @@ -84,4 +95,6 @@ semantics = struct(
get_stl = _get_stl,
should_create_empty_archive = _should_create_empty_archive,
get_grep_includes = _get_grep_includes,
get_interface_deps_allowed_attr = _get_interface_deps_allowed_attr,
should_use_interface_deps_behavior = _should_use_interface_deps_behavior,
)
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,7 @@ public void testSameSymlinkedLibraryDoesNotGiveDuplicateError() throws Exception

@Test
public void testImplementationDepsCompilationContextIsNotPropagated() throws Exception {
setBuildLanguageOptions("--experimental_builtins_injection_override=+cc_library");
useConfiguration("--experimental_cc_interface_deps");
scratch.file(
"foo/BUILD",
Expand Down Expand Up @@ -1959,6 +1960,7 @@ public void testImplementationDepsCompilationContextIsNotPropagated() throws Exc

@Test
public void testImplementationDepsLinkingContextIsPropagated() throws Exception {
setBuildLanguageOptions("--experimental_builtins_injection_override=+cc_library");
useConfiguration("--experimental_cc_interface_deps");
scratch.file(
"foo/BUILD",
Expand Down Expand Up @@ -2030,6 +2032,7 @@ public void testImplementationDepsConfigurationHostSucceeds() throws Exception {

@Test
public void testInterfaceDepsFailsWithoutFlagOrTag() throws Exception {
setBuildLanguageOptions("--experimental_builtins_injection_override=+cc_library");
scratch.file(
"foo/BUILD",
"cc_library(",
Expand All @@ -2055,6 +2058,7 @@ public void testInterfaceDepsFailsWithoutFlagOrTag() throws Exception {

@Test
public void testInterfaceDepsNotInAllowlistThrowsError() throws Exception {
setBuildLanguageOptions("--experimental_builtins_injection_override=+cc_library");
if (analysisMock.isThisBazel()) {
// In OSS usage is controlled only by a flag and not an allowlist.
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6978,7 +6978,7 @@ public void testExpandedCppConfigurationApiBlocked() throws Exception {
"build_test_dwp()",
"grte_top()",
"enable_legacy_cc_provider()",
"experimental_cc_implementation_deps()",
"experimental_cc_interface_deps()",
"share_native_deps()",
"experimental_platform_cc_test()");
scratch.file(
Expand Down

0 comments on commit a20797e

Please sign in to comment.