Shared library built only from static library dependencies, no sources

391 views
Skip to first unread message

Andrew Paprocki

unread,
Jul 25, 2012, 11:14:16 AM7/25/12
to gyp-de...@googlegroups.com
How can I get a shared library target to build purely from static library dependencies without any additional 'sources'?

If I create a 'shared_library' target and only list 'dependencies', the generated xcodeproj will not build anything.

If I add an empty .c file to 'sources', then all of a sudden it begins to work as expected, linking the shared library from all of its .a pieces. This seems like a hack, though. There should be no requirement for 'sources' for a shared library to build. Is there something else to specify to get this to work?

-Andrew

Dirk Pranke

unread,
Jul 25, 2012, 2:41:33 PM7/25/12
to Andrew Paprocki, gyp-de...@googlegroups.com
Generally speaking, this doesn't work (and it's not a gyp-specific
thing). The linker will only attempt to use static libraries to find
missing symbols, and since in your case there aren't any symbols at
all, there aren't any missing symbols.

Most linkers have a way to force all of the symbols/archive members to
be included, but this is something of a hack and can have
unanticipated side effects, like getting multiple definitions of a
symbol from different archives.

The approach I've taken in the past with GYP is to define the lists of
archive members and then pull them in directly to the shared library
as necessary. If you are at all familiar with chromium, you can see
this in the way we build the "content" target in
src/content/content.gyp.

-- Dirk

Dirk Pranke

unread,
Jul 25, 2012, 8:56:59 PM7/25/12
to Andrew Paprocki, gyp-de...@googlegroups.com
On Wed, Jul 25, 2012 at 5:12 PM, Andrew Paprocki
<andrew.m...@gmail.com> wrote:
> On Wed, Jul 25, 2012 at 2:41 PM, Dirk Pranke <dpr...@chromium.org> wrote:
>> Most linkers have a way to force all of the symbols/archive members to
>> be included, but this is something of a hack and can have
>
> I'll take a look at the files you mentioned.
>
> gyp does exactly what you describe by specifying --whole-archive. The
> link would actually work if gyp would run the link command with only
> dependencies specified and no source files.
>
> http://code.google.com/p/gyp/source/browse/trunk/pylib/gyp/generator/make.py#141
>

So, that's an example of "most linkers have a way", and if I knew that
gyp told make to use this flag, I'd forgotten it :)

However, relying on this behavior has the drawbacks that I described
(the potential for multiple definitions, the lack of dead code
elimination), so I don't recommend it, and I'd be surprised if
Chromium relied on this anywhere (Nico or Scott can correct me if I'm
wrong here).

That said, it could be that it would work for you in the situation
you're dealing with. I defer to the other committers if they think
this should "just work" or should be a configurable flag or something.

-- Dirk

Andrew Paprocki

unread,
Jul 25, 2012, 8:12:47 PM7/25/12
to Dirk Pranke, gyp-de...@googlegroups.com
On Wed, Jul 25, 2012 at 2:41 PM, Dirk Pranke <dpr...@chromium.org> wrote:
> Most linkers have a way to force all of the symbols/archive members to
> be included, but this is something of a hack and can have

I'll take a look at the files you mentioned.

gyp does exactly what you describe by specifying --whole-archive. The
link would actually work if gyp would run the link command with only
dependencies specified and no source files.

http://code.google.com/p/gyp/source/browse/trunk/pylib/gyp/generator/make.py#141

"cmd_solink = $(LINK.$(TOOLSET)) -shared $(GYP_LDFLAGS)
$(LDFLAGS.$(TOOLSET)) -Wl,-soname=$(@F) -o $@ -Wl,--whole-archive
$(LD_INPUTS) -Wl,--no-whole-archive $(LIBS)"

-Andrew

Evan Martin

unread,
Jul 26, 2012, 2:13:37 PM7/26/12
to Dirk Pranke, Andrew Paprocki, gyp-de...@googlegroups.com
I believe the "shared_library" support in gyp is underspecified a bit.  We don't use it in Chrome except for the components build where some strangeness in behavior is acceptable.

I think Andrew's original desire is reasonable in the sense of "I want the shared library to include all the stuff of its dependents".  I think it wouldn't be too hard to fix in the Makefile or Ninja generators. But I don't know much about Xcode; if it doesn't handle this case gracefully either perhaps it's better to just add an empty source file to appease every system simultaneously.

Torne (Richard Coles)

unread,
Jul 26, 2012, 2:38:33 PM7/26/12
to Evan Martin, gyp-de...@googlegroups.com, Andrew Paprocki, Dirk Pranke

For reference the Android port is using shared_library to build its jni libraries, and for this purpose we absolutely don't want it to include the whole archive of every dependency :)

Evan Martin

unread,
Jul 26, 2012, 2:43:14 PM7/26/12
to Torne (Richard Coles), gyp-de...@googlegroups.com, Andrew Paprocki, Dirk Pranke

Torne (Richard Coles)

unread,
Jul 26, 2012, 3:51:55 PM7/26/12
to Evan Martin, gyp-de...@googlegroups.com, Andrew Paprocki, Dirk Pranke
Hm. That does look like it does. I should probably experiment with that sometime :)
--
Torne (Richard Coles)
to...@google.com
Reply all
Reply to author
Forward
0 new messages