I'm trying to merge some .c files with .a archives into a shared library, so that it can be imported from a Swig-generated python module. However, I'm stuck and needs some help..
Here is what I've tried:
----------------------
1. I can create a rule implementation where I define the gcc commandline:
"g++ -shared -o %s -Wl,--whole-archive %s -Wl,--no-whole-archive" % (my_lib.path, archive.path)
When I try to run the plain command manually in the terminal, it works. However, Bazel returns:
g++: fatal error: -fuse-linker-plugin, but liblto_plugin.so not found
But liblto_plugin exists at /usr/lib/gcc/x86_64-linux-gnu/4.8. However, even if I pass the path to the library as below, it still results in the same error. I tried setting LD_LIBRARY_PATH as environment variable, and it didn't work either.
"g++ -shared -o %s -lm -B/usr/bin/ -lstdc++ -Wl,-S -Wl,-L/usr/lib/gcc/x86_64-linux-gnu/4.8 -Wl,--whole-archive %s -Wl,--no-whole-archive" % (my_lib.path, archive.path)
------------------------
2. I can reuse cc_library without manually invoking gcc/g++. However, cc_library doesn't seem to merge the archives, even if I pass the rules producing the archives in "deps". I also tried to manually specify the library to merge using linkopts, as the following shows (libutil.a). However, I have to pass the absolute path to make it work, and the absolute path is bazel generated (e.g. .cache/bazel/adbcd123../...), and I can't access the bazel generated path as I can when defining a rule implementation.
# swig_rules.bzl
def merge_libs(name, hdrs=[], srcs=[], deps=[], copts=[], linkopts=[],
defines=[], data=[], includes=[], input_libs=[]):
native.cc_library(
name = name,
srcs = srcs + input_libs,
deps = deps,
linkopts = ['-shared', "-Wl,--whole-archive /path/to/libutil.a", "-Wl,--no-whole-archive"],
copts = ['-fpic'],
includes = includes,
)
# BUILD
merge_libs(
name = "merge",
srcs = ["lab_wrap.cc", "lab.cc"],
input_libs = ["libutil.a"],
includes=["python2.7"],
deps = [
":util",
]
)
Any suggestions would be very helpful!
Thanks,
Jiayuan