How to work around "Argument list too long" error when compiling library (C plugin)

We are trying to compile CSpice using the C plugin. The compilation died because it created an ar command line that was too long for the shell since it used absolute paths for every filename. Can the C plugin be configured to use relative (short) paths instead when it calls ar? Or, can you think of a better way to compile this library?

Here’s the relevant line from --info output. I’ve replaced the absolute path to projectDir with

14:38:44.345 [INFO] [org.gradle.process.internal.DefaultExecHandle] Starting process 'command '/usr/bin/ar''. Working directory: <absolute-path> Command: /usr/bin/ar -rcs <absolute-path>/build/binaries/cspiceStaticLibrary/release/libcspice.a <absolute-path>/build/objectFiles/cspiceStaticLibrary/release/cspiceC/abort_.o <absolute-path>/build/objectFiles/cspiceStaticLibrary/release/cspiceC/accept.o …

And here is the excerpt from the --debug output:

14:38:44.413 [ERROR] [org.gradle.BuildExceptionReporter] Caused by: java.io.IOException: Cannot run program "/usr/bin/ar" (in directory "<absolute-path>/spice"): error=7, Argument list too long
14:38:44.413 [ERROR] [org.gradle.BuildExceptionReporter]
      at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:25)

Relevant except from build.gradle:

apply plugin: 'c'
  sourceSets {
    cspice {}
}
  sources {
    cspice {
        c {
            source {
                srcDir "$buildDir/mice/src/cspice"
                include "**/*.c"
            }
        }
    }
}

And finally:

$ gradle --version
------------------------------------------------------------
Gradle 1.11
------------------------------------------------------------
  Build time:
 2014-02-11 11:34:39 UTC
Build number: none
Revision:
   a831fa866d46cbee94e61a09af15f9dd95987421
  Groovy:
     1.8.6
Ant:
        Apache Ant(TM) version 1.9.2 compiled on July 8 2013
Ivy:
        2.2.0
JVM:
        1.6.0_65 (Apple Inc. 20.65-b04-462)
OS:
         Mac OS X 10.7.5 x86_64

Is anybody compiling lots of C files (~2000!) in a single directory by convention successfully? We’re currently doing it as follows, but it would be nicer not to have this task:

task runGcc(type: Exec, dependsOn: [‘extractTar’, sources.cspice]) {

inputs.files fileTree("$buildDir/mice/src/cspice").include("**/*.c").files

inputs.files fileTree("$buildDir/mice/include").include("**/*.h").files

outputs.files file("$buildDir/mice/src/cspice").listFiles({file -> file.getPath().endsWith(".c") } as FileFilter).collect {File file -> file.path.replace(".c", “.o”)}

workingDir = “$buildDir/mice/src/cspice”

commandLine “sh”, “-c”, “gcc -x c -O2 -ansi -m64 -fPIC -DNON_UNIX_STDIO -c -I $buildDir/mice/include *.c”

}

Hello you are welcome You can also use Long Path Tool to sort out this problem.