import gradlebuild.integrationtests.configureTestSourceSetInIde

plugins {
    id("gradlebuild.distribution.implementation-java")
}

description = "Classpath manipulation, instrumentation, and bytecode transformation infrastructure"

// Instrumentation interceptors for tests
// Separated from the test source set since we don't support incremental annotation processor with Java/Groovy joint compilation
val testInterceptors = sourceSets.create("testInterceptors") {
    compileClasspath += sourceSets.main.get().output
    runtimeClasspath += sourceSets.main.get().output
}

configureTestSourceSetInIde(testInterceptors)

sourceSets.test {
    compileClasspath += testInterceptors.output
    runtimeClasspath += testInterceptors.output
}
dependencyAnalysis {
    issues {
        ignoreSourceSet(testInterceptors.name)
    }
}
jvmCompile {
    addCompilationFrom(testInterceptors)
}

val testInterceptorsImplementation = configurations.getByName("testInterceptorsImplementation") {
    extendsFrom(configurations.implementation.get())
}

dependencies {
    api(projects.baseAsm)
    api(projects.buildProcessServices)
    api(projects.classloaders)
    api(projects.concurrent)
    api(projects.coreApi)
    api(projects.execution)
    api(projects.fileTemp)
    api(projects.files)
    api(projects.functional)
    api(projects.hashing)
    api(projects.instrumentationReporting)
    api(projects.internalInstrumentationApi)
    api(projects.modelCore)
    api(projects.persistentCache)
    api(projects.scopedPersistentCache)
    api(projects.snapshots)
    api(projects.stdlibJavaExtensions)
    api(projects.versionedCache)

    api(libs.asm)
    api(libs.asmTree)
    api(libs.groovy)
    api(libs.guava)
    api(libs.inject)
    api(libs.jspecify)

    implementation(projects.baseServices)
    implementation(projects.inputTracking)
    implementation(projects.logging)
    implementation(projects.loggingApi)

    implementation(libs.asmCommons)
    implementation(libs.commonsCompress)
    implementation(libs.commonsLang)
    implementation(libs.fastutil)
    implementation(libs.slf4jApi)

    compileOnly(libs.kotlinStdlib) {
        because("it needs to forward calls from instrumented code to the Kotlin standard library")
    }

    annotationProcessor(projects.internalInstrumentationProcessor)
    annotationProcessor(platform(projects.distributionsDependencies))

    testImplementation(testFixtures(projects.core))
    testImplementation(testFixtures(projects.snapshots))
    testImplementation(testFixtures(projects.hashing))
    testImplementation(testFixtures(projects.baseServices))

    testImplementation(libs.asmCommons)

    testFixturesApi(projects.internalInstrumentationApi)

    testFixturesCompileOnly(libs.jetbrainsAnnotations)

    testFixturesImplementation(libs.asm)

    integTestImplementation(projects.dependencyManagement)
    integTestImplementation(testFixtures(projects.launcher))

    integTestDistributionRuntimeOnly(projects.distributionsJvm) {
        because("BuildScriptClasspathIntegrationSpec uses the 'java' and 'me.champeau.mrjar' plugins")
    }

    testInterceptorsImplementation(platform(projects.distributionsDependencies))
    testInterceptorsImplementation(testFixtures(projects.classpath))
    testInterceptorsImplementation(testFixtures(projects.core))
    "testInterceptorsAnnotationProcessor"(projects.internalInstrumentationProcessor)
    "testInterceptorsAnnotationProcessor"(platform(projects.distributionsDependencies))
}

gradleModule {
    computedRuntimes {
        // Auto-generated by `:checkTargetRuntimes --fix`
        client = true
        daemon = true
        worker = true
    }
}

packageCycles {
    // Pre-existing cycles between classpath subpackages (classpath ↔ intercept, classpath ↔ transforms)
    excludePatterns.add("org/gradle/internal/classpath/**")
}

strictCompile {
    ignoreRawTypes() // raw types used in Groovy MetaClass APIs
    ignoreAnnotationProcessing() // Without this, javac will complain about unclaimed annotations
}
