/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

plugins {
  id("com.android.library")
  id("com.facebook.hermes.plugins.internal.hermesUtils")
}

buildDir = "${hermesUtils.hermesWs}/build_android/hermes"
buildDir.mkdirs()

android {
  compileSdk = hermesUtils.compileSdk
  ndkVersion = hermesUtils.ndkVersion

  defaultConfig {
    minSdk = hermesUtils.minSdk
    namespace "com.facebook.hermes"

    externalNativeBuild {
      cmake {
        arguments "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
        arguments "-DHERMES_IS_ANDROID=True"
        arguments "-DHERMES_FACEBOOK_BUILD=${hermesUtils.facebookBuild}"
        arguments "-DANDROID_STL=c++_shared"
        arguments "-DANDROID_PIE=True"
        arguments "-DIMPORT_HOST_COMPILERS=${hermesUtils.hermesC}"
        arguments "-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=True"
        arguments "-DHERMES_BUILD_SHARED_JSI=True"
        targets "hermesvm"
      }
    }
    ndk {
      abiFilters (*hermesUtils.abis)
    }
  }

  externalNativeBuild {
    cmake {
      version hermesUtils.cmakeVersion
      path "../../CMakeLists.txt"
      buildStagingDirectory = "${hermesUtils.hermesWs}/staging/hermes"
      buildStagingDirectory.mkdirs()
    }
  }

  buildTypes {
    debug {
      externalNativeBuild {
        cmake {
          // JS developers aren't VM developers. Give them a faster build.
          arguments "-DCMAKE_BUILD_TYPE=Release"
        }
      }
    }
    release {
      externalNativeBuild {
        cmake {
          arguments "-DHERMES_ENABLE_DEBUGGER=False"
          arguments "-DCMAKE_BUILD_TYPE=MinSizeRel"
        }
      }
    }
  }

  flavorDimensions "featureset"
  productFlavors {
    intl {
      isDefault true
      dimension "featureset"
      externalNativeBuild {
        cmake {
          arguments "-DHERMES_ENABLE_INTL=True"
        }
      }
    }
    nointl {
      dimension "featureset"
      externalNativeBuild {
        cmake {
          arguments "-DHERMES_ENABLE_INTL=False"
        }
      }
    }
  }

  sourceSets {
    intl {
      java {
        srcDirs = [
                "../../lib/Platform/Intl/java",
        ]
      }
    }
  }

  // Allow using prefab so that we can import libfbjni.so.
  buildFeatures {
    prefab true
  }

  dependencies {
    implementation 'com.facebook.fbjni:fbjni:0.3.0'
    intlImplementation 'com.facebook.soloader:soloader:0.10.4'
    intlImplementation 'com.facebook.yoga:proguard-annotations:1.19.0'
    intlImplementation "androidx.annotation:annotation:1.3.0"
  }

  packagingOptions {
    exclude "**/libc++_shared.so"
    exclude "**/libjsi.so"
    exclude "**/libfbjni.so"
  }

  afterEvaluate {
    // Gradle 4/5.0 outputs android-debug.aar and android-release.aar
    // Gradle 5.1 outputs android.aar for both
    // Unify the two by renaming the files afterwards.
    // Neither appear to care whether the original filename actually exists.
    def aarDir = "$buildDir/outputs/aar"
    tasks.named("assembleIntlDebug").configure {
      doLast {
        file("$aarDir/hermes-intl-debug.aar").renameTo("${hermesUtils.outputDir}/hermes-debug.aar")
        file("$aarDir/hermes-intl.aar").renameTo("${hermesUtils.outputDir}/hermes-debug.aar")
      }
    }
    tasks.named("assembleIntlRelease").configure {
      doLast {
        file("$aarDir/hermes-intl-release.aar").renameTo("${hermesUtils.outputDir}/hermes-release.aar")
        file("$aarDir/hermes-intl.aar").renameTo("${hermesUtils.outputDir}/hermes-release.aar")
      }
    }
    tasks.named("assembleNointlDebug").configure {
      doLast {
        file("$aarDir/hermes-nointl-debug.aar").renameTo("${hermesUtils.outputDir}/hermes-debug.aar")
        file("$aarDir/hermes-nointl.aar").renameTo("${hermesUtils.outputDir}/hermes-debug.aar")
      }
    }
    tasks.named("assembleNointlRelease").configure {
      doLast {
        file("$aarDir/hermes-nointl-release.aar").renameTo("${hermesUtils.outputDir}/hermes-release.aar")
        file("$aarDir/hermes-nointl.aar").renameTo("${hermesUtils.outputDir}/hermes-release.aar")
      }
    }
    tasks.named("mergeIntlDebugNativeLibs").configure {
      doLast { task ->
        copy {
          from(task)
          into("${hermesUtils.outputDir}/unstripped-debug")
        }
      }
    }
    tasks.named("mergeIntlReleaseNativeLibs").configure {
      doLast { task ->
        copy {
          from(task)
          into("${hermesUtils.outputDir}/unstripped-release")
        }
      }
    }
    tasks.named("mergeNointlDebugNativeLibs").configure {
      doLast { task ->
        copy {
          from(task)
          into("${hermesUtils.outputDir}/unstripped-debug")
        }
      }
    }
    tasks.named("mergeNointlReleaseNativeLibs").configure {
      doLast { task ->
        copy {
          from(task)
          into("${hermesUtils.outputDir}/unstripped-release")
        }
      }
    }
  }
}
