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)
Determine which symbols to export from a shared library, as part of the build.
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,
)
Linux
bazel info release?release 0.23.2
bazel info release returns "development version" or "(@non-git)", tell us how you built Bazel.n/a
git remote get-url origin ; git rev-parse master ; git rev-parse HEAD ?n/a
Just info on using a static .ldscript file.
n/a
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.
Most helpful comment
Put whatever you like in the
genrule. Just reference its output rather than its name.