rules_go icon indicating copy to clipboard operation
rules_go copied to clipboard

Header files produced by linkmode = "c-shared" not part of the go rule outputs

Open GabrielCFigueira opened this issue 1 year ago • 2 comments

What version of rules_go are you using?

0.40.1

What version of gazelle are you using?

0.28.0

What version of Bazel are you using?

7.2.1

Does this issue reproduce with the latest releases of all the above?

Yes (with version 0.49.0 of rules_go and version 0.37.0 of gazelle)

What operating system and processor architecture are you using?

Linux, x86_64

Any other potentially useful information about your toolchain?

Also reproduced the error with remote execution.

What did you do?

I have the following bazel targets:

go_library(
    name = "go_default_library",
    srcs = ["file.go"],
    cgo = True,
    importpath = "path/to/target",
)

go_binary(
    name = "target",
    embed = [":go_default_library"],
    linkmode = "c-shared",
)

It is of my understanding that adding linkmode = "c-shared" creates an additional .so and _cgo_install.h file. So then I have a genrule that consumes this .h file:

genrule(
    name = "rename_header_file",
    outs = ["new_name.h"],
    cmd = """
    cp $$(dirname $(location //path/to/target:target))/_cgo_install.h $@
    """,
    srcs = ["//path/to/target:target"],
)

What did you expect to see?

I expected to be able to run the genrule above, and rename the header file

What did you see instead?

When the --spawn_strategy is local, the outputs of //path/to/target:target are correctly produced: a .so and _cgo_install.h. However, when using --spawn_strategy=processwrapper-sandbox or remote execution, only the .so shows up as an output of //path/to/target:target, and header file is missing, causing the genrule to fail with file not found. Could it be due to the header file not being part of the outputs of the go_binary rule? Not sure how to proceed here

GabrielCFigueira avatar Jul 24 '24 09:07 GabrielCFigueira

The header file should be part of the CcInfo provider returned by the go_binary target. You can inspect it with bazel cquery --output=starlark --starlark:expr='providers(target)' //:target.

The header file is not a standard "file to build" that you could access via a regular genrule. What do you want to do with the file?

fmeum avatar Jul 24 '24 10:07 fmeum

Hey, thanks for the quick answer. The file is then supposed to be used by a cc_library, like this:

cc_library(
    name ="something",
    hdrs = [":new_name.h"]
    includes = ["."],
    deps = ["//path/to/target:target"],
)

cc_library(
    name = "another",
    srcs = glob(["*.cpp"]),
    hdrs = glob(["*.h"]),
    deps = [
        ":something",
    ],
)

GabrielCFigueira avatar Jul 24 '24 10:07 GabrielCFigueira