bazelbuild/bazel

cc_common Starlark examples don't work with layering_check feature

Open

#16,243 创建于 2022年9月9日

在 GitHub 查看
 (0 评论) (0 反应) (0 负责人)Java (4,465 fork)batch import
P3help wantedteam-Documentationteam-Rules-CPPtype: documentation (cleanup)

仓库指标

Star
 (25,384 star)
PR 合并指标
 (平均合并 22天 20小时) (30 天内合并 77 个 PR)

描述

Description of the bug:

It looks like the common flow of using cc_common.configure_features, cc_common.create_compile_variables and cc_common.get_memory_inefficient_command_line does not work when layering_check feature is enabled for the package.

An indication that this is a known but undocumented issue is the line in the example in the rules_cc repo: https://github.com/bazelbuild/rules_cc/blob/807a87190a117d5798f9f2c69427e9037e9891d3/examples/my_c_compile/my_c_compile.bzl#L23

Because layering_check feature is enabled only for clang on unix, this is only observed when compiling with clang on unix systems (not osx, which is a special case).

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

touch hello.cc
touch WORKSPACE

cat >BUILD <<EOF
package(features = ["layering_check"])

cc_library(
    name = "hello",
    srcs = ["hello.cc"],
)
EOF

cat >aspects.bzl <<EOF
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
load(
    "@bazel_tools//tools/build_defs/cc:action_names.bzl",
    "CPP_COMPILE_ACTION_NAME",
)

def _my_aspect_impl(target, ctx):
    cc_toolchain = find_cpp_toolchain(ctx)
    feature_configuration = cc_common.configure_features(
        ctx = ctx,
        cc_toolchain = cc_toolchain,
        requested_features = ctx.features,
        unsupported_features = ctx.disabled_features,
    )

    compile_variables = cc_common.create_compile_variables(
        feature_configuration = feature_configuration,
        cc_toolchain = cc_toolchain,
        user_compile_flags = ctx.fragments.cpp.cxxopts +
                             ctx.fragments.cpp.copts,
    )

    compiler_options = cc_common.get_memory_inefficient_command_line(
        feature_configuration = feature_configuration,
        action_name = CPP_COMPILE_ACTION_NAME,
        variables = compile_variables,
    )

    return []

my_aspect = aspect(
    attr_aspects = ["srcs"],
    attrs = {
        "_cc_toolchain": attr.label(
            default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
        ),
    },
    fragments = ["cpp"],
    toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
    implementation = _my_aspect_impl,
)
EOF

export CC=clang
bazel build --aspects=aspects.bzl%my_aspect "//:hello" 

Which operating system are you running Bazel on?

Ubuntu 20.04

What is the output of bazel info release?

release 5.3.0

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

NA

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

NA

Have you found anything relevant by searching the web?

Another issue that discusses this in a specific context is https://github.com/grailbio/bazel-compilation-database/issues/101, where an aspect does not work when used with the grpc package. grpc package has the layering_check feature enabled.

Any other information, logs, or outputs that you want to share?

No response

贡献者指南