plugins {
    id "com.peterabeles.gversion" version "1.7.0"
}

gversion {
    srcDir = "main/ejml-core/src"
    classPackage = "org.ejml"
    className = "EjmlVersion"
}

allprojects {
    apply plugin: 'idea'
    apply plugin: 'eclipse'

    group = 'org.ejml'
    version = '0.39'
}

subprojects {
    apply plugin: 'java'
    apply plugin: 'maven'
    apply plugin: 'signing'

    java {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }

    compileJava {
        // Should make single file changes faster
        options.incremental = true
        // Output byte code compatible with Java 8
        options.compilerArgs += ["--release", "8"]
//        options.compilerArgs += '--enable-preview'
    }

    test {
        ignoreFailures true
        reports.html.enabled = false
    }

    repositories {
        mavenCentral()
        mavenLocal()

        maven { url = "https://oss.sonatype.org/content/repositories/snapshots/" }
        maven { url = 'https://jitpack.io' } // Allows annotations past Java 8 to be used
    }

    sourceSets {
        main {
            java {
                srcDir 'src'
            }
            resources {
                srcDir 'resources/src'
            }
        }

        generate {
            java {
                srcDir 'generate'
            }
            compileClasspath += sourceSets.main.runtimeClasspath
        }

        benchmark {
            java {
                srcDir 'benchmarks/src'
            }
            resources {
                srcDir 'benchmark/resources'
            }
            compileClasspath += sourceSets.main.runtimeClasspath
        }

        test {
            java {
                srcDir 'test'
                srcDir 'benchmarks/test'
            }
            resources {
                srcDir 'resources/test'
            }
            compileClasspath += sourceSets.benchmark.runtimeClasspath
        }
    }

    dependencies {
        compile 'com.google.code.findbugs:jsr305:3.0.2' // @Nullable

        testImplementation( 'org.junit.jupiter:junit-jupiter-api:5.4.0' )
        testRuntimeOnly( 'org.junit.jupiter:junit-jupiter-engine:5.4.0' )

        ['core','generator-annprocess'].each { String a->
            benchmarkCompile('org.openjdk.jmh:jmh-'+a+':1.19')
        }
        generateCompile project(':main:autocode')

        // needed to use Java 11 with 8 byte code
        annotationProcessor 'com.github.bsideup.jabel:jabel-javac-plugin:0.2.0'
    }

    javadoc.failOnError = false
    task javadocJar(type: Jar) {
        classifier = 'javadoc'
        from javadoc
    }

    task sourcesJar(type: Jar) {
        classifier = 'sources'
        from sourceSets.main.allSource
    }

    artifacts {
        archives sourcesJar
    }

    // Force uploadArchives to fail if trying to upload a stable release and git is dirty
    uploadArchives.dependsOn failDirtyNotSnapshot

    // if Maven central isn't setup in gradle.properties skip all of this
    if( project.hasProperty('ossrhUsername') ) {
        signing {
            sign configurations.archives
        }

        uploadArchives {
            artifacts {
                archives javadocJar
            }
            repositories {
                mavenDeployer {
                    beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

                    repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
                        authentication(userName: ossrhUsername, password: ossrhPassword)
                    }

                    snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
                        authentication(userName: ossrhUsername, password: ossrhPassword)
                    }

                    pom.project {
                        name 'EJML'
                        packaging 'pom'
                        // optionally artifactId can be defined here
                        description 'A fast and easy to use dense and sparse matrix linear algebra library written in Java.'
                        url 'http://ejml.org/'

                        scm {
                            connection 'git@github.com:lessthanoptimal/ejml.git'
                            developerConnection 'git@github.com:lessthanoptimal/ejml.git'
                            url 'https://github.com/lessthanoptimal/ejml'
                        }

                        licenses {
                            license {
                                name 'The Apache Software License, Version 2.0'
                                url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                            }
                        }

                        developers {
                            developer {
                                id 'pabeles'
                                name 'Peter Abeles'
                                email 'peter.abeles@gmail.com'
                            }
                        }
                    }
                }
            }
        }
    }
}

def allModules = [
        ':main:ejml-core',
        ':main:ejml-cdense',
        ':main:ejml-ddense',
        ':main:ejml-dsparse',
        ':main:ejml-dsparse',
        ':main:ejml-fdense',
        ':main:ejml-zdense',
        ':main:ejml-simple',
        ':main:ejml-experimental',
]

/*
 * Copyright (c) 2009-2020, Peter Abeles. All Rights Reserved.
 *
 * This file is part of Efficient Java Matrix Library (EJML).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Creates a directory with all the comiled jars
task createLibraryDirectory( dependsOn: allModules.collect{ it+":jar"}+allModules.collect{ it+":sourcesJar"}) {

    doLast {
        // Create lists of .class jars and source jars
        ext.listJars = files(allModules.collect{ project(it).tasks.jar.archivePath })
        ext.listSource = files(allModules.collect{ project(it).tasks.sourcesJar.archivePath })

        file('libraries').deleteDir()
        file('libraries').mkdir()

        copy {
            from ext.listJars
            from ext.listSource
            into 'libraries'
        }
    }
}

def javadocProjects = [
        ':main:ejml-core',
        ':main:ejml-ddense',
        ':main:ejml-dsparse',
        ':main:ejml-fdense',
        ':main:ejml-fsparse',
        ':main:ejml-zdense',
        ':main:ejml-cdense',
        ':main:ejml-simple'
]
task alljavadoc(type: Javadoc) {
    // only include source code in src directory to avoid including 3rd party code which some projects do as a hack
    source = javadocProjects.collect { project(it).fileTree('src').include('**/*.java') }
//    source = javadocProjects.collect { project(it).sourceSets.main.allJava }
    classpath = files(javadocProjects.collect { project(it).sourceSets.main.compileClasspath })

    destinationDir = file("${buildDir}/docs/javadoc")

    // Hack for Java 8u121 and beyond. Comment out if running an earlier version of Java
    options.addBooleanOption("-allow-script-in-comments", true)
    // Fixed the undefined search problem
    options.addBooleanOption("-no-module-directories", true)

    // Add a list of uses of a class to javadoc
    options.use = true

    configure(options) {
        failOnError = false
        docTitle = "Efficient Java Matrix Library (EJML) v$project.version"
        links = [ 'http://docs.oracle.com/javase/8/docs/api/' ]
        bottom = file('docs/bottom.txt').text
    }
}

task oneJarBin(type: Jar, dependsOn: javadocProjects.collect { it + ":compileJava" }) {
    baseName = 'EJML'

    from files(javadocProjects.collect { project(it).sourceSets.main.output })
}

wrapper {
    distributionType = Wrapper.DistributionType.BIN
    gradleVersion = '6.3'
}

project(':main:ejml-core').compileJava.dependsOn(createVersionFile)

// Disable the creation of jars for distribution.  If you don't do this it will crash
[':main',':examples',':main:autocode'].each {String a ->
    project(a) {
        if( project.hasProperty('ossrhUsername') ) {
            signArchives.enabled = false
        }
        sourcesJar.enabled = false
        javadocJar.enabled = false
        jar.enabled = false
        uploadArchives.enabled = false
        install.enabled = false
    }
}


