0

I have a project in which I have two different 'sub-projects', the main library and a compiler for a programming language in a directory structure like this:

- build.gradle
- src
  - library
  - compiler
  - ...

My build.gradle looks like this:

sourceSets
{
    library
    {
        java
        {
            srcDir 'src/library'
            srcDir 'src/asm'
        }
    }
    compiler
    {
        java
        {
            srcDir 'src/compiler'
        }
    }
    //...
}

// ...

task buildLib(type: Jar, dependsOn: 'classes') {
    from sourceSets.library.output
    classifier = 'dyvil-library'
}

task buildCompiler(type: Jar, dependsOn: 'classes') {
    from sourceSets.compiler.output
    classifier = 'dyvil-compiler'
}

My problem is that whenever I try to run the buildCompiler task, the build fails and the Java Compiler gives me hundreds of errors in places where the compiler source code references classes in the library. I already tried to make the compiler dependant on the library classes like this:

task buildCompiler(type: Jar, dependsOn: [ 'classes', 'libraryClasses' ]) {
    from sourceSets.compiler.output
    classifier = 'dyvil-compiler'
}

But that did not help. I know that I could theoretically copy the srcDirs from sourceSets.library into sourceSets.compiler like this:

compiler
{
    java
    {
        srcDir 'src/library'
        srcDir 'src/asm'
        srcDir 'src/compiler'
    }
}

But that seems like a bad idea for obvious reasons.

Is there another way to include the source files from the library when compiling the compiler (duh)?

Clashsoft
  • 11,553
  • 5
  • 40
  • 79

1 Answers1

1

There's a similar question on SO here.

I replicated your situation locally and the fix was to add a line to the sourceSets for compiler as follows:

compiler
{
    java {
        srcDir 'src/compiler'
    }
    compileClasspath += library.output
}
Community
  • 1
  • 1
Mark Fisher
  • 9,838
  • 3
  • 32
  • 38