group 'com.flet.serious_python_android'
version '0.9.2'

def python_version = '3.12'

buildscript {
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        // The Android Gradle Plugin knows how to build native code with the NDK.
        classpath 'com.android.tools.build:gradle:7.3.0'
        classpath 'de.undercouch:gradle-download-task:4.1.2'
    }
}

rootProject.allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

apply plugin: 'com.android.library'
apply plugin: 'de.undercouch.download'

android {
    namespace "com.flet.serious_python_android"
    
    // Bumping the plugin compileSdkVersion requires all clients of this plugin
    // to bump the version in their app.
    compileSdkVersion 31

    // Invoke the shared CMake build with the Android Gradle Plugin.
    externalNativeBuild {
        cmake {
            path "../src/CMakeLists.txt"

            // The default CMake version for the Android Gradle Plugin is 3.10.2.
            // https://developer.android.com/studio/projects/install-ndk#vanilla_cmake
            //
            // The Flutter tooling requires that developers have CMake 3.10 or later
            // installed. You should not increase this version, as doing so will cause
            // the plugin to fail to compile for some customers of the plugin.
            // version "3.10.2"
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        minSdkVersion 16

        ndk {
            abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86_64'
        }
    }

    packagingOptions {
        doNotStrip "*/arm64-v8a/libpython*.so"
        doNotStrip "*/armeabi-v7a/libpython*.so"
        doNotStrip "*/x86/libpython*.so"
        doNotStrip "*/x86_64/libpython*.so"
    }
}

import de.undercouch.gradle.tasks.download.Download

task copyBuildDist(type: Copy) {
    def srcDir = System.getenv('SERIOUS_PYTHON_BUILD_DIST')
    if (srcDir != null) {
        from srcDir
        into 'src/main/jniLibs'
    }
}

// Loop through abiFilters
def packageTasks = []
android.defaultConfig.ndk.abiFilters.each { abi ->

    def srcDir = System.getenv('SERIOUS_PYTHON_SITE_PACKAGES')
    if (srcDir == null || srcDir.allWhitespace) {
        throw new InvalidUserDataException("SERIOUS_PYTHON_SITE_PACKAGES environment variable is not set.")
    }

    packageTasks.add("zipSitePackages_$abi")
    packageTasks.add("copyOpt_$abi")

    tasks.register("jniCleanUp_$abi", Delete) {
        delete "src/main/jniLibs/$abi"
    }

    tasks.register("downloadDistArchive_$abi", Download) {
        src "https://github.com/flet-dev/python-build/releases/download/v${python_version}/python-android-dart-${python_version}-${abi}.tar.gz"
        dest new File(buildDir, "python-android-${abi}.tar.gz")
    }
    tasks.register("untarFile_$abi", Copy) {
        from tarTree(tasks.named("downloadDistArchive_$abi").get().dest)
        into "src/main/jniLibs/$abi"
        dependsOn "jniCleanUp_$abi", "downloadDistArchive_$abi"
    }

    tasks.register("copyOpt_$abi", Copy) {
        from fileTree(dir: "$srcDir/$abi/opt", include: ["**/*.so"])
        into "src/main/jniLibs/$abi"
        eachFile {
            path = name
        }
        includeEmptyDirs = false
        dependsOn "jniCleanUp_$abi"
    }

    tasks.register("zipSitePackages_$abi", Zip) {
        from fileTree(dir: "$srcDir/$abi")
        archiveFileName = "libpythonsitepackages.so"
        destinationDirectory = file("src/main/jniLibs/$abi")
        dependsOn "jniCleanUp_$abi", "untarFile_$abi"
    }
}

if (System.getenv('SERIOUS_PYTHON_BUILD_DIST')) {
    task copyOrUntar(dependsOn: 'copyBuildDist')
} else {
    task copyOrUntar(dependsOn: packageTasks)
}

preBuild.dependsOn copyOrUntar