// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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.

apply plugin: 'com.android.application'

task generateJniHeaders(type: Exec, description: 'Generate JNI Headers') {
    def headerPath = "${project.projectDir}/src/main/jni"
    def classPath = "${project.projectDir}/../../../jvm/core/target/*"
    def filePath = "${project.projectDir}/../../../jvm/core/src/main/java/org/apache/tvm/LibInfo.java"
    commandLine "javac", "-h", headerPath, "-classpath", classPath, filePath
    doLast {
        file("${headerPath}/org_apache_tvm_LibInfo.h").renameTo(file("${headerPath}/org_apache_tvm_native_c_api.h"))
    }
}

task copyFiles(type: Copy, description: 'Copy Sources for ndk-build') {
    dependsOn "generateJniHeaders"
    def ndkFilesPath = "${project.projectDir}/../../../jvm/native/src/main/native"
    def srcPath = "${project.projectDir}/src/main/jni/"

    from "${ndkFilesPath}/org_apache_tvm_native_c_api.cc", "${ndkFilesPath}/jni_helper_func.h"
    into srcPath
}

task deleteLibs(type: Delete, description: "Delete Compiled Libraries") {
    dependsOn "copyFiles"
    def libsPath = "${project.projectDir}/src/main/libs"
    delete libsPath
}

task buildJni(type: Exec, description: 'Build JNI libs') {
    dependsOn "deleteLibs"
    def buildPath = "${project.projectDir}/src/main/jni"
    commandLine "ndk-build", "--directory", buildPath
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn buildJni
}

// gradle.projectsEvaluated {
//     tasks.withType(JavaCompile) {
//         options.compilerArgs << "-Xlint:deprecation"
//     }
// }

android {
    compileSdkVersion 33
    defaultConfig {
        applicationId "org.apache.tvm.tvmrpc"
        minSdkVersion 24
        targetSdkVersion 33
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {
            jni.srcDirs = []
            jniLibs.srcDirs = ['src/main/libs']
        }
    }

    lint {
        disable 'Instantiatable' // MainActivity and RPCActivity must extend android.app.Activity
        disable 'MissingClass' // .RPCWatchdogService was not found in the project or the libraries
        disable 'IconDipSize' // The image ic_launcher.png varies significantly in its density-independent size
    }
    namespace 'org.apache.tvm.tvmrpc'

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'com.google.android.material:material:1.8.0'
    implementation files('../../../jvm/core/target/tvm4j-core-0.0.1-SNAPSHOT.jar')
    testImplementation 'junit:junit:4.13.2'
}
