plugins {
    id "cpp"
    id "java"
    id "maven-publish"

    id "com.google.protobuf"
}

description = 'The protoc plugin for gRPC Java'

def artifactStagingPath = "$buildDir/artifacts" as File
// Adds space-delimited arguments from the environment variable env to the
// argList.
def addEnvArgs = { env, argList ->
    def value = System.getenv(env)
    if (value != null) {
        value.split(' +').each() { it -> argList.add(it) }
    }
}

// Adds corresponding "-l" option to the argList if libName is not found in
// LDFLAGS. This is only used for Mac because when building for uploadArchives
// artifacts, we add the ".a" files directly to LDFLAGS and without "-l" in
// order to get statically linked, otherwise we add the libraries through "-l"
// so that they can be searched for in default search paths.
def addLibraryIfNotLinked = { libName, argList ->
    def ldflags = System.env.LDFLAGS
    if (ldflags == null || !ldflags.contains('lib' + libName + '.a')) {
        argList.add('-l' + libName)
    }
}

def String arch = rootProject.hasProperty('targetArch') ? rootProject.targetArch : osdetector.arch
def boolean vcDisable = rootProject.hasProperty('vcDisable') ? rootProject.vcDisable : false
def boolean usingVisualCpp // Whether VisualCpp is actually available and selected

model {
    toolChains {
        // If you have both VC and Gcc installed, VC will be selected, unless you
        // set 'vcDisable=true'
        if (!vcDisable) {
            visualCpp(VisualCpp) {
                // Prefer vcvars-provided environment over registry-discovered environment
                def String vsDir = System.getenv("VSINSTALLDIR")
                def String winDir = System.getenv("WindowsSdkDir")
                if (vsDir != null && winDir != null) {
                    installDir = vsDir
                    windowsSdkDir = winDir
                }
            }
        }
        gcc(Gcc) {
            target("ppcle_64") {
                cppCompiler.executable = 'powerpc64le-linux-gnu-g++'
                linker.executable = 'powerpc64le-linux-gnu-g++'
            }
            target("aarch_64") {
                cppCompiler.executable = 'aarch64-linux-gnu-g++'
                linker.executable = 'aarch64-linux-gnu-g++'
            }
            target("s390_64") {
                cppCompiler.executable = 's390x-linux-gnu-g++'
                linker.executable = 's390x-linux-gnu-g++'
            }
            target("loongarch_64")
        }
        clang(Clang) {
        }
    }

    platforms {
        x86_32 { architecture "x86" }
        x86_64 { architecture "x86_64" }
        ppcle_64 { architecture "ppcle_64" }
        aarch_64 { architecture "aarch_64" }
        s390_64 { architecture "s390_64" }
        loongarch_64 { architecture "loongarch_64" }
    }

    components {
        java_plugin(NativeExecutableSpec) {
            if (arch in [
                'x86_32',
                'x86_64',
                'ppcle_64',
                'aarch_64',
                's390_64',
                'loongarch_64'
            ]) {
                // If arch is not within the defined platforms, we do not specify the
                // targetPlatform so that Gradle will choose what is appropriate.
                targetPlatform arch
            }
            baseName "$protocPluginBaseName"
        }
    }

    binaries {
        all {
            if (toolChain in Gcc || toolChain in Clang) {
                cppCompiler.define("GRPC_VERSION", version)
                cppCompiler.args "--std=c++0x"
                addEnvArgs("CXXFLAGS", cppCompiler.args)
                addEnvArgs("CPPFLAGS", cppCompiler.args)
                if (osdetector.os == "osx") {
                    cppCompiler.args "-mmacosx-version-min=10.7", "-stdlib=libc++"
                    addLibraryIfNotLinked('protoc', linker.args)
                    addLibraryIfNotLinked('protobuf', linker.args)
                } else if (osdetector.os == "windows") {
                    linker.args "-static", "-lprotoc", "-lprotobuf", "-static-libgcc", "-static-libstdc++",
                            "-s"
		  } else if (osdetector.arch == "ppcle_64") {
		      linker.args "-Wl,-Bstatic", "-lprotoc", "-lprotobuf", "-Wl,-Bdynamic", "-lpthread", "-s"
		  } else {
                    // Link protoc, protobuf, libgcc and libstdc++ statically.
                    // Link other (system) libraries dynamically.
                    // Clang under OSX doesn't support these options.
                    linker.args "-Wl,-Bstatic", "-lprotoc", "-lprotobuf", "-static-libgcc",
                            "-static-libstdc++",
                            "-Wl,-Bdynamic", "-lpthread", "-s"
                }
                addEnvArgs("LDFLAGS", linker.args)
            } else if (toolChain in VisualCpp) {
                usingVisualCpp = true
                cppCompiler.define("GRPC_VERSION", version)
                cppCompiler.args "/EHsc", "/MT"
                if (rootProject.hasProperty('vcProtobufInclude')) {
                    cppCompiler.args "/I${rootProject.vcProtobufInclude}"
                }
                linker.args "libprotobuf.lib", "libprotoc.lib"
                if (rootProject.hasProperty('vcProtobufLibs')) {
                    linker.args "/LIBPATH:${rootProject.vcProtobufLibs}"
                }
            }
        }
    }
}

configurations {
    testLiteImplementation
}

dependencies {
    testImplementation project(':grpc-protobuf'),
            project(':grpc-stub'),
            libraries.javax.annotation
    testLiteImplementation project(':grpc-protobuf-lite'),
            project(':grpc-stub'),
            libraries.javax.annotation
}

sourceSets {
    testLite {
        proto { setSrcDirs(['src/test/proto']) }
    }
}

tasks.named("compileTestJava").configure {
    options.errorprone.excludedPaths = ".*/build/generated/source/proto/.*"
}

tasks.named("compileTestLiteJava").configure {
    options.compilerArgs = compileTestJava.options.compilerArgs
    options.compilerArgs += [
        "-Xlint:-cast"
    ]
    options.errorprone.excludedPaths = ".*/build/generated/source/proto/.*"
}

tasks.named("checkstyleTestLite").configure {
    enabled = false
}

protobuf {
    protoc {
        if (project.hasProperty('protoc')) {
            path = project.protoc
        } else {
            artifact = libs.protobuf.protoc.get()
        }
    }
    plugins {
        grpc { path = javaPluginPath }
    }
    generateProtoTasks {
        all().configureEach {
            dependsOn 'java_pluginExecutable'
            inputs.file javaPluginPath
        }
        ofSourceSet('test').configureEach {
            plugins { grpc {} }
        }
        ofSourceSet('testLite').configureEach {
            builtins {
                java { option 'lite' }
            }
            plugins {
                grpc { option 'lite' }
            }
        }
    }
}

println "*** Building codegen requires Protobuf version ${libs.versions.protobuf.get()}"
println "*** Please refer to https://github.com/grpc/grpc-java/blob/master/COMPILING.md#how-to-build-code-generation-plugin"

tasks.register("buildArtifacts", Copy) {
    dependsOn 'java_pluginExecutable'
    from("$buildDir/exe") {
        if (osdetector.os != 'windows') {
            rename 'protoc-gen-grpc-java', '$0.exe'
        }
    }
    into artifactStagingPath
}

archivesBaseName = "$protocPluginBaseName"

def checkArtifacts = tasks.register("checkArtifacts") {
    dependsOn buildArtifacts
    doLast {
        if (!usingVisualCpp) {
            def ret = exec {
                executable 'bash'
                args 'check-artifact.sh', osdetector.os, arch
            }
            if (ret.exitValue != 0) {
                throw new GradleException("check-artifact.sh exited with " + ret.exitValue)
            }
        } else {
            def exeName = "$artifactStagingPath/java_plugin/${protocPluginBaseName}.exe"
            def os = new ByteArrayOutputStream()
            def ret = exec {
                executable 'dumpbin'
                args '/nologo', '/dependents', exeName
                standardOutput = os
            }
            if (ret.exitValue != 0) {
                throw new GradleException("dumpbin exited with " + ret.exitValue)
            }
            def dlls = os.toString() =~ /Image has the following dependencies:\s+(.*)\s+Summary/
            if (dlls[0][1] != "KERNEL32.dll") {
                throw new Exception("unexpected dll deps: " + dlls[0][1]);
            }
            os.reset()
            ret = exec {
                executable 'dumpbin'
                args '/nologo', '/headers', exeName
                standardOutput = os
            }
            if (ret.exitValue != 0) {
                throw new GradleException("dumpbin exited with " + ret.exitValue)
            }
            def machine = os.toString() =~ / machine \(([^)]+)\)/
            def expectedArch = [x86_32: "x86", x86_64: "x64"][arch]
            if (machine[0][1] != expectedArch) {
                throw new Exception("unexpected architecture: " + machine[0][1]);
            }
        }
    }
}

// Exe files are skipped by Maven by default. Override it.
// Also skip jar files that is generated by the java plugin.
publishing {
    publications {
        maven(MavenPublication) {
            // Removes all artifacts since grpc-compiler doesn't generates any Jar
            artifacts = []
            artifactId 'protoc-gen-grpc-java'
            artifact("$artifactStagingPath/java_plugin/${protocPluginBaseName}.exe" as File) {
                classifier osdetector.os + "-" + arch
                extension "exe"
                builtBy checkArtifacts
            }
            pom.withXml {
                // This isn't any sort of Java archive artifact, and OSSRH doesn't enforce
                // javadoc for 'pom' packages. 'exe' would be a more appropriate packaging
                // value, but it isn't clear how that will be interpreted. In addition,
                // 'pom' is typically the value used when building an exe with Maven.
                asNode().project.packaging*.value = 'pom'
            }
        }
    }
}

def configureTestTask(Task task, String dep, String extraPackage, String serviceName) {
    test.dependsOn task
    task.dependsOn "generateTest${dep}Proto"
    if (osdetector.os != 'windows') {
        task.executable "diff"
        task.args "-u"
    } else {
        task.executable "fc"
    }
    // File isn't found on Windows if last slash is forward-slash
    def slash = System.getProperty("file.separator")
    task.args "$buildDir/generated/source/proto/test${dep}/grpc/io/grpc/testing/compiler${extraPackage}${slash}${serviceName}Grpc.java",
            "$projectDir/src/test${dep}/golden/${serviceName}.java.txt"
}

tasks.register("testGolden", Exec) {
    configureTestTask(it, '', '', 'TestService')
}
tasks.register("testLiteGolden", Exec) {
    configureTestTask(it, 'Lite', '', 'TestService')
}
tasks.register("testDeprecatedGolden", Exec) {
    configureTestTask(it, '', '', 'TestDeprecatedService')
}
tasks.register("testDeprecatedLiteGolden", Exec) {
    configureTestTask(it, 'Lite', '', 'TestDeprecatedService')
}
