enso/project/BuildInfo.scala

165 lines
5.2 KiB
Scala
Raw Normal View History

import sbt._
2020-07-10 13:57:42 +03:00
import sbt.internal.util.ManagedLogger
import scala.sys.process._
object BuildInfo {
2020-09-09 16:37:26 +03:00
/** Writes build-time information to a Java class that can be used by the
2020-09-09 16:37:26 +03:00
* components.
*
* If the `ENSO_RELEASE_MODE` environment variable is set to `true`, will set
* an `isRelease` flag to true. This flag can be used to disable
* development-specific features.
*
* @param file location where to write the Scala code
* @param log a logger instance for diagnostics
2022-02-15 18:34:33 +03:00
* @param defaultDevEnsoVersion the default version used for dev builds
2020-09-09 16:37:26 +03:00
* @param ensoVersion Enso version
* @param scalacVersion Scala compiler version used in the project
* @param graalVersion GraalVM version used in the project
--jvm tries to find Java executable system-wide. (#11500) Fixes `--jvm` option, given to the native image. This was failing on my machine, because when given `--jvm` option, the runner was trying to find the `java` executable from the distribution manager's runtime (on my system located in `~/.local/share/enso/runtime`) and it used the first runtime found. But the first runtime on my system is JDK 17. The `--jvm` option now tries to: - Find a JDK from the distribution manager that has the same version as the JDK used for building the engine. - If there is not an exact version match, it tries to find a runtime from distribution manager that is *newer*. - If none, fallback to system-wide search - System-wide search tries to find `java` from `$JAVA_HOME` and from `$PATH`. But this is just a fallback. # Important Notes - Added test to Engine CI jobs that pass `--jvm` argument to a native image of engine-runner - ea3af5ffbca6dc439690142251426b4c167bca98 - `runtime-version-manager` sbt project migrated to a JPMS module - `engine-runner` now depends on `runtime-version-manager`. - Removed unnecessary stuff in `runtime-version-manager` dealing with outdated `gu` Graal Updater utility. - Extracted [GraalVersionManager](https://github.com/enso-org/enso/blob/1455b025cb8e5141409ea96b75577e8884265a70/lib/scala/runtime-version-manager/src/main/java/org/enso/runtimeversionmanager/components/GraalVersionManager.java) from [RuntimeVersionManager](https://github.com/enso-org/enso/blob/d2e8994700fd36228f7873f040668013e643190a/lib/scala/runtime-version-manager/src/main/scala/org/enso/runtimeversionmanager/components/RuntimeVersionManager.scala)
2024-11-19 01:44:54 +03:00
* @param javaVersion Java language version used in the project.
2021-07-08 16:38:20 +03:00
* @param currentEdition name of the edition associated with the Enso
* version; this should be removed once #1831 is
* implemented
2020-09-09 16:37:26 +03:00
* @return sequence of modified files
*/
def writeBuildInfoFile(
file: File,
2020-07-10 13:57:42 +03:00
log: ManagedLogger,
2022-02-15 18:34:33 +03:00
defaultDevEnsoVersion: String,
ensoVersion: String,
scalacVersion: String,
2021-07-08 16:38:20 +03:00
graalVersion: String,
--jvm tries to find Java executable system-wide. (#11500) Fixes `--jvm` option, given to the native image. This was failing on my machine, because when given `--jvm` option, the runner was trying to find the `java` executable from the distribution manager's runtime (on my system located in `~/.local/share/enso/runtime`) and it used the first runtime found. But the first runtime on my system is JDK 17. The `--jvm` option now tries to: - Find a JDK from the distribution manager that has the same version as the JDK used for building the engine. - If there is not an exact version match, it tries to find a runtime from distribution manager that is *newer*. - If none, fallback to system-wide search - System-wide search tries to find `java` from `$JAVA_HOME` and from `$PATH`. But this is just a fallback. # Important Notes - Added test to Engine CI jobs that pass `--jvm` argument to a native image of engine-runner - ea3af5ffbca6dc439690142251426b4c167bca98 - `runtime-version-manager` sbt project migrated to a JPMS module - `engine-runner` now depends on `runtime-version-manager`. - Removed unnecessary stuff in `runtime-version-manager` dealing with outdated `gu` Graal Updater utility. - Extracted [GraalVersionManager](https://github.com/enso-org/enso/blob/1455b025cb8e5141409ea96b75577e8884265a70/lib/scala/runtime-version-manager/src/main/java/org/enso/runtimeversionmanager/components/GraalVersionManager.java) from [RuntimeVersionManager](https://github.com/enso-org/enso/blob/d2e8994700fd36228f7873f040668013e643190a/lib/scala/runtime-version-manager/src/main/scala/org/enso/runtimeversionmanager/components/RuntimeVersionManager.scala)
2024-11-19 01:44:54 +03:00
javaVersion: String,
currentEdition: String
): Seq[File] = {
2020-09-09 16:37:26 +03:00
val gitInfo = getGitInformation(log).getOrElse(fallbackGitInformation)
val isRelease = isReleaseMode
val className = file.getName.stripSuffix(".java")
val fileContents =
s"""
|package org.enso.version;
|
|final class ${className} {
| private GeneratedVersion() {}
|
| static String defaultDevEnsoVersion() {
| return "${defaultDevEnsoVersion}";
| }
|
| static String ensoVersion() {
| return "${ensoVersion}";
| }
2020-09-09 16:37:26 +03:00
|
| static String scalacVersion() {
| return "${scalacVersion}";
| }
|
| static String graalVersion() {
| return "${graalVersion}";
| }
|
--jvm tries to find Java executable system-wide. (#11500) Fixes `--jvm` option, given to the native image. This was failing on my machine, because when given `--jvm` option, the runner was trying to find the `java` executable from the distribution manager's runtime (on my system located in `~/.local/share/enso/runtime`) and it used the first runtime found. But the first runtime on my system is JDK 17. The `--jvm` option now tries to: - Find a JDK from the distribution manager that has the same version as the JDK used for building the engine. - If there is not an exact version match, it tries to find a runtime from distribution manager that is *newer*. - If none, fallback to system-wide search - System-wide search tries to find `java` from `$JAVA_HOME` and from `$PATH`. But this is just a fallback. # Important Notes - Added test to Engine CI jobs that pass `--jvm` argument to a native image of engine-runner - ea3af5ffbca6dc439690142251426b4c167bca98 - `runtime-version-manager` sbt project migrated to a JPMS module - `engine-runner` now depends on `runtime-version-manager`. - Removed unnecessary stuff in `runtime-version-manager` dealing with outdated `gu` Graal Updater utility. - Extracted [GraalVersionManager](https://github.com/enso-org/enso/blob/1455b025cb8e5141409ea96b75577e8884265a70/lib/scala/runtime-version-manager/src/main/java/org/enso/runtimeversionmanager/components/GraalVersionManager.java) from [RuntimeVersionManager](https://github.com/enso-org/enso/blob/d2e8994700fd36228f7873f040668013e643190a/lib/scala/runtime-version-manager/src/main/scala/org/enso/runtimeversionmanager/components/RuntimeVersionManager.scala)
2024-11-19 01:44:54 +03:00
| static String javaVersion() {
| return "${javaVersion}";
| }
|
| static String currentEdition() {
| return "${currentEdition}";
| }
|
| static String commit() {
| return "${gitInfo.commitHash}";
| }
|
| static String ref() {
| return "${gitInfo.ref}";
| }
|
| static boolean isDirty() {
| return ${gitInfo.isDirty};
| }
|
| static String latestCommitDate() {
| return "${gitInfo.latestCommitDate}";
| }
|
| static boolean isRelease() {
| return ${isRelease};
| }
|}
|""".stripMargin
IO.write(file, fileContents)
2020-07-10 13:57:42 +03:00
log.debug("Build info updated.")
Seq(file)
}
def isReleaseMode: Boolean =
2022-02-15 18:34:33 +03:00
sys.env.get("ENSO_RELEASE_MODE").contains("true")
2020-09-09 16:37:26 +03:00
/** Information regarding the Git repository that was used in the build.
2020-09-09 16:37:26 +03:00
*
* @param ref if available, name of the branch that was checked out; if a
* branch is not available, but the current commit is tagged, name
* of that tag is used, otherwise falls back to `HEAD`
* @param commitHash hash of the currently checked out commit
* @param isDirty indicates if there are any uncommitted changes
* @param latestCommitDate date of the current commit
*/
private case class GitInformation(
ref: String,
2020-09-09 16:37:26 +03:00
commitHash: String,
isDirty: Boolean,
latestCommitDate: String
)
2020-09-09 16:37:26 +03:00
private def getGitInformation(log: ManagedLogger): Option[GitInformation] =
try {
val hash = "git rev-parse HEAD".!!.trim
val ref =
try {
val branchCommand = "git symbolic-ref -q --short HEAD"
val tagCommand = "git describe --tags --exact-match"
val refCommand =
branchCommand #|| tagCommand
refCommand.!!.trim
} catch {
case e: Exception =>
log.warn(
"Cannot get name of git branch/tag, defaulting to \"HEAD\". " +
s"(Caused by: $e)"
)
"HEAD"
}
val isDirty = "git status --porcelain".!!.trim.nonEmpty
val latestCommitDate = "git log HEAD -1 --format=%cd".!!.trim
2020-09-09 16:37:26 +03:00
Some(
GitInformation(
ref = ref,
commitHash = hash,
isDirty = isDirty,
latestCommitDate = latestCommitDate
)
)
} catch {
case e: Exception =>
log.warn(
"Could not get any git information. The build will proceed but it " +
s"will not contain the git metadata. (Caused by: $e)"
)
None
}
2020-09-09 16:37:26 +03:00
/** Fallback instance of [[GitInformation]] that can be used if the build is
2020-09-09 16:37:26 +03:00
* outside of a repository or the git information cannot be obtained for
* other reasons.
*/
private def fallbackGitInformation: GitInformation =
GitInformation(
"<built outside of a git repository>",
"<built outside of a git repository>",
isDirty = false,
"<built outside of a git repository>"
)
}