Determine if cc_library dependencies are built with PIC

353 views
Skip to first unread message

Vitaly

unread,
Jan 3, 2019, 12:53:45 PM1/3/19
to bazel-discuss
Hi all,

Hope this is an appropriate place to ask this question.

As the subject says, what would be the correct/idiomatic way to detect whether a dependent cc_library is built with -fPIC? The question stems from https://github.com/bazelbuild/rules_rust/issues/118, which discusses an interop issue between Rust and C. In short, rustc defaults to building position-independent executables (i.e. PIE) on (at least) Linux. If a static lib is being linked in, it needs to have been built with -fPIC. This works perfectly fine with the dbg and fastbuild compilation modes, but fails with opt builds. The difference is opt does not produce PIC archives.

As an experiment, I wanted to see if I could detect the presence/absence of PIC archives of the transitive cc_library deps (or slightly more general, detect if PIC compilation was used or not for them). If PIC archives are absent, the rust rule would automatically turn off PIE generation.

Any suggestions/thoughts?

Thanks

vit...@gmail.com

unread,
Jan 9, 2019, 7:59:03 AM1/9/19
to bazel-discuss
So no takers thus far :).

hlo...@google.com, is this something you may have ideas about?

Marcel Hlopko

unread,
Jan 9, 2019, 8:43:23 AM1/9/19
to bazel-discuss
Hi :)

what a great day to ask this question :) We've cut Bazel 0.22 this week and it will contain new C++ providers, and those will expose library_to_link struct that contains separate entries for pic and nopic static libraries. The docs are not yet awesome but with some digging and with:

https://docs.bazel.build/versions/master/skylark/lib/cc_common.html#create_library_to_link
https://github.com/bazelbuild/bazel/issues/7036

You might be able to get started. Let me know if you need help!

And in the rare case you didn't know, there is --force_pic_flags bazel option, which by default results in adding -pie to cc_binaries. Not great here, but can be useful.

vit...@gmail.com

unread,
Jan 9, 2019, 11:57:54 AM1/9/19
to bazel-discuss
Hi Marcel,

Well, I'm glad I caught you on the right day for this question :).

So the idea is I'd inspect the pic entries of the library_to_link struct hanging off the cpp provider, and if it's empty, build a non-PIE binary via rustc. And if both pic and nopic lists have artifacts, I can choose whether to build PIE or non-PIE. Ok, I think that ought to work.

And yeah - improving the docs would be much appreciated :).

I'm also assuming that there's no good answer to my original question in the context of Bazel 0.20, right?

Thanks!

I'm aware of the force_pic flag, but was hoping to avoid forcing things that way.

Reply all
Reply to author
Forward
0 new messages