Different compile flags for shared deps depending on leaf targets

97 views
Skip to first unread message

Ivan Inozemtsev

unread,
Nov 6, 2024, 11:33:15 AM11/6/24
to gn-...@chromium.org, Ryan Macnak, Slava Egorov
Hi,

I'm new to GN and I want to add a shared library to the Dart SDK repo (https://dart.googlesource.com/sdk), which depends on existing targets (used by executables).

To make a shared library usable, I need to compile it with -fPIC, but currently the build is configured to use -fPIE, so I want to compile shared dependencies with different flags depending on which leaf target I build.

If I understand correctly from https://groups.google.com/a/chromium.org/g/gn-dev/c/lS_w_NGuXhk and Fuchsia docs, toolchains look like the right way to do this, so I have a draft change https://dart-review.googlesource.com/c/sdk/+/392661, which changes each toolchain definition into a definition of two toolchains: regular and "_shared".

Does this look like a reasonable approach? Are there any caveats or better solutions?

Thanks
Ivan

David Turner

unread,
Nov 6, 2024, 11:58:37 AM11/6/24
to Ivan Inozemtsev, gn-...@chromium.org, Ryan Macnak, Slava Egorov
Yes, toolchains are the only way to get these compiler/linker flags to flow to dependencies, and that's exactly how it is implemented in the Fuchsia build.

Note that getting executables to find the shared libraries at runtime can be challenging because they are now located in a different root_out_dir. We solved with a shared_library() template wrapper that copies (hard-links) the files from xxx_shared/ to the root_out_dir/ where the executable binaries are located.

Thanks
Ivan

To unsubscribe from this group and stop receiving emails from it, send an email to gn-dev+un...@chromium.org.

Nico Weber

unread,
Nov 6, 2024, 2:54:24 PM11/6/24
to David Turner, Ivan Inozemtsev, gn-...@chromium.org, Ryan Macnak, Slava Egorov
I would try doing this with a default config that adds fPIC, and then in executable targets remove that config and pass fPIE instead.

This means that your shared libraries are always fPIC, but in return you get a much simpler (and faster -- need to build shared libs just once) build.

David Turner

unread,
Nov 6, 2024, 3:09:40 PM11/6/24
to Nico Weber, Ivan Inozemtsev, gn-...@chromium.org, Ryan Macnak, Slava Egorov
On Wed, Nov 6, 2024 at 8:54 PM Nico Weber <tha...@chromium.org> wrote:
I would try doing this with a default config that adds fPIC, and then in executable targets remove that config and pass fPIE instead.

This means that your shared libraries are always fPIC, but in return you get a much simpler (and faster -- need to build shared libs just once) build.


This is actually much better for most use cases. The only drawback is that your executables will be slightly slower and slightly larger (by a couple percent).
For Fuchsia, which targets embedded systems, this is really important, but if you don't have this requirement, I suggest trying's Nico's method first!
Message has been deleted

Ivan Inozemtsev

unread,
Nov 13, 2024, 2:53:16 AM11/13/24
to gn-dev, David Turner, Ivan Inozemtsev, gn-...@chromium.org, Ryan Macnak, Slava Egorov, tha...@chromium.org
Thanks a lot! For now I've decided to go toolchains route as I think it can simplify other configs, and things almost go well: in https://dart-review.googlesource.com/c/sdk/+/392661/comment/1503fa7f_23e76284/ I've create a template which either defines a group in a regular toolchain, or an actual shared library in a shared toolchain, and I've hit an issue with a different output dir David mentioned.

David, could you please point out where hardlinking happens in Fuchsia build rules, did you mean https://cs.opensource.google/fuchsia/fuchsia/+/86dcff138700c76441db8592b39de7b3997a0050:build/config/BUILDCONFIG.gn;l=3156-3158?

David Turner

unread,
Nov 13, 2024, 5:44:17 AM11/13/24
to Ivan Inozemtsev, gn-dev, Ryan Macnak, Slava Egorov, tha...@chromium.org
On Wed, Nov 13, 2024 at 8:53 AM Ivan Inozemtsev <iinoz...@google.com> wrote:
Thanks a lot! For now I've decided to go toolchains route as I think it can simplify other configs, and things almost go well: in https://dart-review.googlesource.com/c/sdk/+/392661/comment/1503fa7f_23e76284/ I've create a template which either defines a group in a regular toolchain, or an actual shared library in a shared toolchain, and I've hit an issue with a different output dir David mentioned.

David, could you please point out where hardlinking happens in Fuchsia build rules, did you mean https://cs.opensource.google/fuchsia/fuchsia/+/86dcff138700c76441db8592b39de7b3997a0050:build/config/BUILDCONFIG.gn;l=3156-3158?

Sure. Our GN toolchain definition use a custom command for the copy tool, which invokes a script that will perform hard-linking by default, falling back to regular copy if that fails, which also deals with MacOS shenanigans.
Apart from that, our ELF shared library toolchain redirection implementation is documented here with additional comments found in our BUILDCONFIG.gn file.

Hope this helps.

- Digit
Reply all
Reply to author
Forward
0 new messages