Bazel: Can't use a genrule to pass a .ldscript file to the linker

Created on 14 Mar 2019  路  4Comments  路  Source: bazelbuild/bazel

Description of the problem / feature request:

I'd like to use an autogenerated .ldscript file when building a shared library on Linux with cc_binary(linkshared = True). I've written a genrule to produce it with outs = ["libsavi_exports.ldscript"], but bazel won't let me use the genrule in the cc_binary's deps, though a static .ldscript file is allowed.

ERROR: /home/stephen/git/engine/src/products/unix/BUILD:63:12: in deps attribute of cc_binary rule //products/unix:libsavi.so.3: genrule rule '//products/unix:libsavi_exports' is misplaced here (expected cc_library, objc_library, cc_proto_library or cc_import) and '//products/unix:libsavi_exports' does not have mandatory providers: 'CcInfo'

I can't use the genrule in the cc_binary's srcs either:

ERROR: /home/stephen/git/engine/src/products/unix/BUILD:61:12: in srcs attribute of cc_binary rule //products/unix:libsavi.so.3: '//products/unix:libsavi_exports' does not produce any cc_binary srcs files (expected .cc, .cpp, .cxx, .c++, .C, .c, .h, .hh, .hpp, .ipp, .hxx, .inc, .inl, .H, .S, .s, .asm, .a, .lib, .pic.a, .lo, .lo.lib, .pic.lo, .so, .dylib, .dll, .o, .obj or .pic.o)

Feature requests: what underlying problem are you trying to solve with this feature?

Determine which symbols to export from a shared library, as part of the build.

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

cc_binary(
    name = "libsavi.so.3",
    linkopts = [
        "-Wl,--version-script,$(location :libsavi_exports)",
    ],
    linkshared = True,
    visibility = ["//visibility:public"],
    srcs = [
        #":libsavi_exports",
    ],
    deps = [
        ":libsavi_exports",
    ],
)

genrule(
    name = "libsavi_exports",
    outs = ["libsavi_exports.ldscript"],
    cmd = "echo VERSION { global: exported_function; local: *; }; > $@",
    output_to_bindir = 0,
    executable = 0,
)

What operating system are you running Bazel on?

Linux

What's the output of bazel info release?

release 0.23.2

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

n/a

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

n/a

Have you found anything relevant by searching the web?

Just info on using a static .ldscript file.

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

n/a

Most helpful comment

Put whatever you like in the genrule. Just reference its output rather than its name.

All 4 comments

I recommend directly putting the linker script output, i.e., :libsavi_exports.ldscript, in deps.

Thanks, but how would the cc_binary depend on the genrule without refering to it by name? That's just a minimal example, in our real build it does more than echo a fixed string.

Put whatever you like in the genrule. Just reference its output rather than its name.

That solves it, thanks! I didn't realise I could reference genrules by their output names.

Was this page helpful?
0 / 5 - 0 ratings