bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
22.69k stars 3.98k forks source link

cc_shared_library ignores linkopts from header only cc_library deps #21884

Open keith opened 3 months ago

keith commented 3 months ago

Description of the bug:

With this example:

cc_library(
    name = "foo",
    srcs = ["foo.cpp"],
    linkopts = ["-lcompression"],
)

cc_library(
    name = "baz",
    hdrs = ["baz.hpp"],
    linkopts = ["-blah"],
)

cc_binary(
    name = "main",
    srcs = ["main.cpp"],
    deps = [
        ":baz",
        ":foo",
    ],
)

cc_shared_library(
    name = "bar",
    deps = [
        ":baz",
        ":foo",
    ],
)

The cc_binary target fails to build, but the cc_shared_library doesn't, it ignores the invalid -blah linkopt. If you add an empty cpp file to baz it works correctly.

Which category does this issue belong to?

No response

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

No response

Which operating system are you running Bazel on?

macOS

What is the output of bazel info release?

7.1.1

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

No response

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

No response

Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.

No response

Have you found anything relevant by searching the web?

No response

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

No response

comius commented 2 months ago

cc @pzembrod for triage cc @oquenchil for potential insights

oquenchil commented 2 months ago

Not sure what's going on, I'd set a breakpoint (or print) here: https://cs.opensource.google/bazel/bazel/+/master:src/main/starlark/builtins_bzl/common/cc/cc_shared_library.bzl;drc=2abc982ca5a8d1e829613629c8e3b44cf61b58c5;l=295

to see whether the wrapped linker_input has the flag -blah. A different question which I won't go into is whether it makes sense to do this at all. Headers do not actually contribute anything here since cc_shared_library only does linking, no compilation and therefore the flag could be added to the cc_shared_library. The rule doesn't provide CcInfo either so that flag won't be propagated downstream.

In any case, if you want this to throw an error I'd look first around that code I linked.