Linking library fails on big project

I’ve got a project with over 3000 c files.
When linking a static library, the command ar fails on Mac OS X, with the error Argument list too long.
There was another topic with this problem, but no proper workaround was given: How to work around “Argument list too long” error when compiling library (C plugin).

Yes, this came up a couple of months ago:

GRADLE-3363

There’s a workaround attached to the JIRA. Would you be interested in contributing a fix?

Yes, I’d be interested in contributing!

This bug refers to linking dynamic libraries with ld. My problem is with the staticLibArchiver, which in turn uses ar, and this command doesn’t support the -filelist argument.

Thanks, I misread your original post.

It’s a similar problem, but I raised GRADLE-3381 for tracking. I think a similar workaround would work, but you have to use @path/to/file instead of -filelist.

Hi Gradle dev. team, dear Sterling, dear Bencze,
this is an interesting story which also hit us in our Gradle migration.
is there anything going on wrt. this. I see above that himamis wanted to provide a patch. Was there something coming up so far?

Thanks in advance!
BR,
Franz

Hello,

I didn’t provide a patch as I didn’t had the time to dig deep into the Gradle project. Also, as far as I remember, I could make ar work with the @filelist option. So our solution is instead of using ar as the archiver tool, we switched to libtool, which accepts the option -filelist.

This is added inside model.toolChains.clang.target

gccPlatformToolChain.staticLibArchiver.setExecutable("/usr/bin/libtool")
gccPlatformToolChain.staticLibArchiver.withArguments { List<String> args ->
    args.remove("-rcs")
    args.add("-static")
    int idx = args.findIndexOf { it.endsWith(".a")}
    args.add(idx, "-o")

    def filelist = project.file("build/tmp/${args.hashCode()}.txt")
    def objects = args.findAll { it.endsWith(".o") }
    def writer = filelist.newWriter()
    objects.each { o ->
        writer.write(o)
        writer.newLine()
    }
    writer.close()
    args.removeAll(objects)
    args.add("-filelist")
    args.add("$filelist")
}

I hope this helps!

Best Regards,
Balazs