// Copyright (c) 2020-2021 The Khronos Group Inc.
//
// SPDX-License-Identifier: Apache-2.0

// Open this directory in Android Studio, or build with Gradle,
// as one way to create an AAR file of the loader.
// The other way is with maintainer-scripts/build-aar.sh which is simpler.
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.3"
    }
}

plugins {
    id "ru.vyarus.use-python" version "2.3.0"
    id "maven-publish"
}

apply plugin: "com.android.library"

// These next few lines are just to make the version match the OpenXR release.
def repoRoot = file("../..")
project.ext.repoRoot = repoRoot
apply from: new File(repoRoot, "src/version.gradle")

repositories {
    google()
    mavenCentral()
}

def baseIncludeDir = new File(buildDir, "intermediates/includes")
def includeDir = layout.buildDirectory.file("intermediates/includes/openxr")
def scriptDir = "${project.repoRoot}/specification/scripts"
def registry = "${project.repoRoot}/specification/registry/xr.xml"

// Python is used to generate header files
python {
    pip "jinja2:2.10.3"
    minPythonVersion = "3.4"

    environment = ["PYTHONPATH": scriptDir]
}

def ensureDirectory = tasks.register("ensureDirectory") {
    doLast {
        mkdir(includeDir)
    }
}

// Copy or generate the OpenXR headers for packaging in the prefab AAR.
// Cannot just use ones from the build here, unfortunately.
tasks.addRule("Pattern: stageHeader_<HeaderName>") { String taskName ->
    if (!taskName.startsWith("stageHeader_")) {
        return
    }
    def headerName = taskName - "stageHeader_"

    def pregenerated = new File(repoRoot, "include/openxr/${headerName}.h")

    if (pregenerated.exists()) {
        // if the file exists, our task just copies it.
        task(taskName, type: Copy) {
            from pregenerated
            into includeDir
            dependsOn ensureDirectory
        }

    } else {

        // if the file does not exist, our task generates it.
        task(taskName, type: PythonTask) {
            command = ["$scriptDir/genxr.py",
                       "-registry",
                       registry,
                       "-quiet",
                       "-o",
                       includeDir.get(),
                       "${headerName}.h"]
            dependsOn ensureDirectory
        }
    }
}


preBuild.dependsOn stageHeader_openxr,
        stageHeader_openxr_platform,
        stageHeader_openxr_reflection,
        stageHeader_openxr_platform_defines

// Used for publishing/deploying
version = project.versionOpenXR

android {
    compileSdkVersion 29
    buildToolsVersion "30.0.3"
    ndkVersion "21.3.6528147"
    defaultConfig {
        // for Vulkan, need at least 24
        minSdkVersion 24
        compileSdkVersion 26

        versionName = project.versionOpenXR.toString() + project.versionQualifier
        versionCode = project.versionOpenXR.getPacked()
        externalNativeBuild {
            cmake {
                arguments "-DANDROID_STL=c++_shared",
                        "-DBUILD_API_LAYERS=OFF",
                        "-DBUILD_TESTS=OFF",
                        "-DBUILD_LOADER=ON",
                        "-DBUILD_CONFORMANCE_TESTS=OFF",
                        "-DBUILD_ALL_EXTENSIONS=ON"
                targets "openxr_loader"
            }
        }
    }
    sourceSets {
        main {
            manifest.srcFile "AndroidManifest.xml"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
        }
    }

    externalNativeBuild {
        cmake {
            path "${project.repoRoot}/CMakeLists.txt"
        }
    }

    buildFeatures {
        prefabPublishing true
    }
    prefab {
        openxr_loader {
            headers "${baseIncludeDir}"
        }
    }
}

// Note: While publishing is available in this build file,
// what seems to be a bug in the Android gradle plugin duplicates
// the files in the .aar file, so the alternate approach is recommended for making/publishing
// artifacts for now.

def siteUrl = "https://github.com/KhronosGroup/OpenXR-SDK-Source"
def gitUrl = "scm:git:https://github.com/KhronosGroup/OpenXR-SDK-Source.git"
publishing {
    publications {
        // Creates a Maven publication called "maven".
        maven(MavenPublication) {

            artifactId "openxr_loader_for_android"
            version project.versionOpenXR.toString() + project.versionQualifier

            pom {
                name = "OpenXR Loader for Android"
                description = "The AAR for the OpenXR Loader as used on Android."
                url = siteUrl

                // Set your license
                licenses {
                    license {
                        name = "The Apache Software License, Version 2.0"
                        url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
                    }
                    // or MIT, but not easy to express clearly in POM.
                }
                developers {
                    developer {
                        name = "The Khronos Group, Inc."
                        email = "openxr-speceditor AT khronos DOT org"
                    }
                }
                scm {
                    connection = gitUrl
                    developerConnection = gitUrl
                    url = siteUrl
                }
            }
            repositories {
                maven {
                    url = uri(layout.buildDirectory.dir("repo"))
                }
            }
        }

    }
}

// Applies the component for the release build variant.
afterEvaluate {
    publishing.publications["maven"].from(components.release)
}

