Merging archives / GCC link issue / access bazel-generated path when using cc_library.

247 views
Skip to first unread message

Jiayuan Meng

unread,
Jul 2, 2015, 12:07:49 AM7/2/15
to bazel-...@googlegroups.com
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

Han-Wen Nienhuys

unread,
Jul 2, 2015, 3:13:04 AM7/2/15
to Jiayuan Meng, bazel-...@googlegroups.com
cc_library just compiles and puts together the .c files mentioned in its srcs

I guess you want to generate a self-contained .so that can be loaded
as a plugin? In that case, you should create a cc_binary, setting
linkshared = 1.
> --
> You received this message because you are subscribed to the Google Groups
> "bazel-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to bazel-discus...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/bazel-discuss/9adb78e9-679d-498d-938b-78b944f7eef8%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Han-Wen Nienhuys
Google Munich
han...@google.com

Jiayuan Meng

unread,
Jul 2, 2015, 12:25:41 PM7/2/15
to Han-Wen Nienhuys, bazel-...@googlegroups.com
Works like a charm. Thanks, Han-Wen!!
Reply all
Reply to author
Forward
0 new messages