plugins {
    id 'conventions.common'
    id 'conventions.boringssl'
    id 'conventions.cpp'
    // TODO(prb) generate the Java to a central location so we don't need JVM conventions
    id 'conventions.jvm'
}

description = 'Conscrypt: Constants'

ext {
    genDir = "${project.buildDir}/generated-sources"
}


sourceSets.main {
    java {
        srcDirs = [
            "${genDir}"
        ]
    }
}

dependencies {
    implementation files("${genDir}") {
        builtBy ':conscrypt-constants:runGen'
    }
}

model {
    components {
        // Builds exe/ which generates the content of NativeConstants.java
        gen(NativeExecutableSpec) {
            sources {
                cpp {
                    // Sources assumed to be in src/gen/cpp by default.
                    exportedHeaders {
                        srcDirs "${boringsslIncludeDir}"
                        include "**/*.cc"
                    }
                }
            }

            binaries.all {
                if (toolChain in VisualCpp) {
                    cppCompiler.define "WIN32_LEAN_AND_MEAN"
                    cppCompiler.args "/std:c++17"
                } else if (toolChain in Clang || toolChain in Gcc) {
                    cppCompiler.args "-std=c++17"
                }
            }
        }
    }

    tasks {
        // Runs generateNativeConstants to create build/NativeConstants.java
        runGen(Exec) {
            def gen = $.binaries.get("genExecutable")

            dependsOn gen
            outputs.dir genDir
            File genDir = new File("${genDir}/org/conscrypt")

            executable gen.executable.file

            doFirst {
                genDir.mkdirs()
                standardOutput = new FileOutputStream(new File(genDir, "NativeConstants.java"))
            }
            doLast {
                if (standardOutput != null) {
                    standardOutput.close();
                }
            }
        }
    }
}

// Disable the javadoc task.
tasks.withType(Javadoc).configureEach { enabled = false }
