
import org.gradle.internal.jvm.JavaInfo
import org.gradle.internal.jvm.Jvm
import org.gradle.internal.jvm.inspection.JvmInstallationMetadata
import org.gradle.internal.jvm.inspection.JvmMetadataDetector
import org.gradle.jvm.toolchain.internal.InstallationLocation

/*
 * 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.
 */

// This adds support for compiling and testing against a different Java runtime.
//
// I failed to set it up leveraging Gradle's toolchains because
// a toolchain spec is not flexible enough to provide an exact location of the JVM to be used;
// if you have two identical JVM lang. versions in auto-discovered JVMs, an arbitrary one is used (?).
// This situation is not uncommon when debugging low-level stuff (hand-compiled JVM binaries).
//
// The code below is a workaround using internal gradle classes. It may stop working in the future
// but for now it's fine.

JavaInfo jvmGradle = Jvm.current();
JavaInfo jvmCurrent = {
  def runtimeJavaHome = propertyOrDefault("runtime.java.home", System.getenv('RUNTIME_JAVA_HOME'))
  if (runtimeJavaHome != null) {
    return Jvm.forHome(file(runtimeJavaHome))
  } else {
    return jvmGradle
  }
}()

JvmMetadataDetector jvmDetector = project.services.get(JvmMetadataDetector)

if (jvmGradle != jvmCurrent) {
  configure(rootProject) {
    task altJvmWarning() {
      doFirst {

        def jvmInfo = { JavaInfo javaInfo ->
          JvmInstallationMetadata jvmMetadata = jvmDetector.getMetadata(InstallationLocation.userDefined(javaInfo.javaHome, "specific path"))
          return "${jvmMetadata.languageVersion} (${jvmMetadata.displayName} ${jvmMetadata.runtimeVersion}, home at: ${jvmMetadata.javaHome})"
        }

        logger.warn("""NOTE: Alternative java toolchain will be used for compilation and tests:
  Project will use ${jvmInfo(jvmCurrent)}
  Gradle runs with ${jvmInfo(jvmGradle)}
""")
      }
    }
  }

  allprojects {
    // Any tests
    tasks.withType(Test) {
      dependsOn ":altJvmWarning"
      executable = jvmCurrent.javaExecutable
    }

    // Any javac compilation tasks
    tasks.withType(JavaCompile) {
      dependsOn ":altJvmWarning"
      options.fork = true
      options.forkOptions.javaHome = jvmCurrent.javaHome
    }

    // Javadoc compilation.
    def javadocExecutable = jvmCurrent.javadocExecutable
    tasks.matching { it.name == "renderJavadoc" || it.name == "renderSiteJavadoc" }.configureEach {
      dependsOn ":altJvmWarning"
      executable = javadocExecutable.toString()
    }
  }
}

// Set up root project's properties.
rootProject.ext.runtimeJavaHome = jvmCurrent.javaHome
rootProject.ext.runtimeJavaVersion = jvmDetector.getMetadata(InstallationLocation.userDefined(jvmCurrent.javaHome, "specific path")).getLanguageVersion()
rootProject.ext.usesAltJvm = (jvmGradle != jvmCurrent);

