Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Gradle toolchains to build ORT with a consistent Java version #8411

Merged
merged 5 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import org.gradle.accessors.dm.LibrariesForLibs

import org.jetbrains.kotlin.gradle.dsl.JvmTarget

private val Project.libs: LibrariesForLibs
get() = extensions.getByType()

Expand All @@ -44,13 +42,3 @@ dependencies {
implementation(libs.plugin.kotlin)
implementation(libs.plugin.mavenPublish)
}

val javaVersion = JavaVersion.current()
val maxKotlinJvmTarget = runCatching { JvmTarget.fromTarget(javaVersion.majorVersion) }
.getOrDefault(enumValues<JvmTarget>().max())

tasks.withType<JavaCompile>().configureEach {
// Align this with Kotlin to avoid errors, see https://youtrack.jetbrains.com/issue/KT-48745.
sourceCompatibility = maxKotlinJvmTarget.target
targetCompatibility = maxKotlinJvmTarget.target
}
28 changes: 15 additions & 13 deletions buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ import io.gitlab.arturbosch.detekt.Detekt
import io.gitlab.arturbosch.detekt.report.ReportMergeTask

import org.gradle.accessors.dm.LibrariesForLibs
import org.gradle.api.JavaVersion
import org.gradle.api.attributes.TestSuiteType
import org.gradle.api.plugins.jvm.JvmTestSuite
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.api.tasks.testing.Test
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.gradle.kotlin.dsl.dependencies
Expand All @@ -40,6 +35,8 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
private val Project.libs: LibrariesForLibs
get() = extensions.getByType()

val javaLanguageVersion: String by project

plugins {
// Apply core plugins.
jacoco
Expand Down Expand Up @@ -110,8 +107,19 @@ detekt {
basePath = rootDir.path
}

val javaVersion = JavaVersion.current()
val maxKotlinJvmTarget = runCatching { JvmTarget.fromTarget(javaVersion.majorVersion) }
java {
toolchain {
languageVersion = JavaLanguageVersion.of(javaLanguageVersion)
}
}

tasks.withType<Jar>().configureEach {
manifest {
attributes["Build-Jdk"] = javaToolchains.compilerFor(java.toolchain).map { it.metadata.jvmVersion }
}
}

val maxKotlinJvmTarget = runCatching { JvmTarget.fromTarget(javaLanguageVersion) }
.getOrDefault(enumValues<JvmTarget>().max())

val mergeDetektReportsTaskName = "mergeDetektReports"
Expand Down Expand Up @@ -152,12 +160,6 @@ tasks.withType<Detekt>().configureEach detekt@{
finalizedBy(mergeDetektReports)
}

tasks.withType<JavaCompile>().configureEach {
// Align this with Kotlin to avoid errors, see https://youtrack.jetbrains.com/issue/KT-48745.
sourceCompatibility = maxKotlinJvmTarget.target
targetCompatibility = maxKotlinJvmTarget.target
}

tasks.withType<KotlinCompile>().configureEach {
val hasSerialization = plugins.hasPlugin(libs.plugins.kotlinSerialization.get().pluginId)

Expand Down
1 change: 1 addition & 0 deletions cli/src/funTest/assets/git-repo-expected-output.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ analyzer:
end_time: "1970-01-01T00:00:00Z"
environment:
ort_version: "HEAD"
build_jdk: "<REPLACE_JDK>"
java_version: "<REPLACE_JAVA>"
os: "<REPLACE_OS>"
processors: "<REPLACE_PROCESSORS>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ analyzer:
end_time: "1970-01-01T00:00:00Z"
environment:
ort_version: "HEAD"
build_jdk: "<REPLACE_JDK>"
java_version: "<REPLACE_JAVA>"
os: "<REPLACE_OS>"
processors: "<REPLACE_PROCESSORS>"
Expand Down
6 changes: 3 additions & 3 deletions cli/src/main/kotlin/OrtMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ class OrtMain : CliktCommand(

cell(
"""
The OSS Review Toolkit, version ${Theme.Default.info(version)}.

Running$command$user under Java ${env.javaVersion} on ${env.os}
The OSS Review Toolkit, version ${Theme.Default.info(version)},
built with JDK ${env.buildJdk}, running under Java ${env.javaVersion}.
Executing$command$user on ${env.os}
with ${env.processors} CPUs and a maximum of $maxMemInMib MiB of memory.
""".trimIndent()
)
Expand Down
3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ org.gradle.parallel = true

kotlin.code.style = official

# The version of the JDK to use for building ORT.
javaLanguageVersion = 11
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, I'd be fine with giving this another / shorter name. Maybe just buildJdk?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with either name, but add javaToolchainVersion to the candidates.


# The version of the SPDX license list which is used to import license texts and generate SPDX enums. Must be a valid
# tag, see https://github.com/spdx/license-list-data/tags.
spdxLicenseListVersion = 3.23
1 change: 1 addition & 0 deletions model/src/test/assets/result-with-issues-graph-old.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ analyzer:
end_time: "1970-01-01T00:00:00Z"
environment:
ort_version: "HEAD"
build_jdk: "<REPLACE_JDK>"
java_version: "<REPLACE_JAVA>"
os: "<REPLACE_OS>"
processors: 4
Expand Down
1 change: 1 addition & 0 deletions model/src/test/assets/result-with-issues-graph.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ analyzer:
end_time: "1970-01-01T00:00:00Z"
environment:
ort_version: "HEAD"
build_jdk: "<REPLACE_JDK>"
java_version: "<REPLACE_JAVA>"
os: "<REPLACE_OS>"
processors: 4
Expand Down
1 change: 1 addition & 0 deletions model/src/test/assets/result-with-issues-scopes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ analyzer:
end_time: "1970-01-01T00:00:00Z"
environment:
ort_version: "HEAD"
build_jdk: "<REPLACE_JDK>"
java_version: "<REPLACE_JAVA>"
os: "<REPLACE_OS>"
processors: 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ analyzer:
end_time: "1970-01-01T00:00:00Z"
environment:
ort_version: "HEAD"
build_jdk: "<REPLACE_JDK>"
java_version: "<REPLACE_JAVA>"
os: "<REPLACE_OS>"
processors: "<REPLACE_PROCESSORS>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ analyzer:
end_time: "1970-01-01T00:00:00Z"
environment:
ort_version: "HEAD"
build_jdk: "<REPLACE_JDK>"
java_version: "<REPLACE_JAVA>"
os: "<REPLACE_OS>"
processors: "<REPLACE_PROCESSORS>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ analyzer:
end_time: "1970-01-01T00:00:00Z"
environment:
ort_version: "HEAD"
build_jdk: "<REPLACE_JDK>"
java_version: "<REPLACE_JAVA>"
os: "<REPLACE_OS>"
processors: "<REPLACE_PROCESSORS>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ analyzer:
end_time: "1970-01-01T00:00:00Z"
environment:
ort_version: "HEAD"
build_jdk: "<REPLACE_JDK>"
java_version: "<REPLACE_JAVA>"
os: "<REPLACE_OS>"
processors: "<REPLACE_PROCESSORS>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ analyzer:
end_time: "1970-01-01T00:00:00Z"
environment:
ort_version: "HEAD"
build_jdk: "<REPLACE_JDK>"
java_version: "<REPLACE_JAVA>"
os: "<REPLACE_OS>"
processors: "<REPLACE_PROCESSORS>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ analyzer:
end_time: "1970-01-01T00:00:00Z"
environment:
ort_version: "HEAD"
build_jdk: "<REPLACE_JDK>"
java_version: "<REPLACE_JAVA>"
os: "<REPLACE_OS>"
processors: "<REPLACE_PROCESSORS>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ analyzer:
end_time: "1970-01-01T00:00:00Z"
environment:
ort_version: "HEAD"
build_jdk: "<REPLACE_JDK>"
java_version: "<REPLACE_JAVA>"
os: "<REPLACE_OS>"
processors: "<REPLACE_PROCESSORS>"
Expand Down Expand Up @@ -188,6 +189,7 @@ scanner:
end_time: "1970-01-01T00:00:00Z"
environment:
ort_version: "HEAD"
build_jdk: "<REPLACE_JDK>"
java_version: "<REPLACE_JAVA>"
os: "<REPLACE_OS>"
processors: "<REPLACE_PROCESSORS>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ analyzer:
end_time: "1970-01-01T00:00:00Z"
environment:
ort_version: "HEAD"
build_jdk: "<REPLACE_JDK>"
java_version: "<REPLACE_JAVA>"
os: "<REPLACE_OS>"
processors: "<REPLACE_PROCESSORS>"
Expand Down Expand Up @@ -186,6 +187,7 @@ scanner:
end_time: "1970-01-01T00:00:00Z"
environment:
ort_version: "HEAD"
build_jdk: "<REPLACE_JDK>"
java_version: "<REPLACE_JAVA>"
os: "<REPLACE_OS>"
processors: "<REPLACE_PROCESSORS>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ analyzer:
end_time: "1970-01-01T00:00:00Z"
environment:
ort_version: "HEAD"
build_jdk: "<REPLACE_JDK>"
java_version: "<REPLACE_JAVA>"
os: "<REPLACE_OS>"
processors: "<REPLACE_PROCESSORS>"
Expand Down Expand Up @@ -105,6 +106,7 @@ scanner:
end_time: "1970-01-01T00:00:00Z"
environment:
ort_version: "HEAD"
build_jdk: "<REPLACE_JDK>"
java_version: "<REPLACE_JAVA>"
os: "<REPLACE_OS>"
processors: "<REPLACE_PROCESSORS>"
Expand Down
18 changes: 18 additions & 0 deletions utils/ort/src/main/kotlin/Environment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package org.ossreviewtoolkit.utils.ort

import java.io.File
import java.lang.Runtime
import java.util.jar.JarFile

import org.ossreviewtoolkit.utils.common.Os

Expand All @@ -33,6 +34,11 @@ data class Environment(
*/
val ortVersion: String = ORT_VERSION,

/**
* The JDK version ORT was built with.
*/
val buildJdk: String = BUILD_JDK,

/**
* The version of Java used.
*/
Expand Down Expand Up @@ -71,6 +77,18 @@ data class Environment(
*/
val ORT_VERSION by lazy { this::class.java.`package`.implementationVersion ?: "IDE-SNAPSHOT" }

/**
* The version of the OSS Review Toolkit as a string.
*/
val BUILD_JDK: String by lazy {
runCatching {
val codeSource = this::class.java.protectionDomain.codeSource
JarFile(codeSource?.location?.file).use {
it.manifest.mainAttributes.getValue("Build-Jdk")
}
}.getOrDefault(System.getProperty("java.version"))
}

/**
* A string that is supposed to be used as the User Agent when using ORT as an HTTP client.
*/
Expand Down
11 changes: 7 additions & 4 deletions utils/test/src/main/kotlin/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,14 @@ fun patchExpectedResult(
definitionFile: File? = null,
custom: Map<String, String> = emptyMap()
): String {
val env = Environment()

val replacements = buildMap {
put("<REPLACE_JAVA>", System.getProperty("java.version"))
put("<REPLACE_OS>", System.getProperty("os.name"))
put("\"<REPLACE_PROCESSORS>\"", Runtime.getRuntime().availableProcessors().toString())
put("\"<REPLACE_MAX_MEMORY>\"", Runtime.getRuntime().maxMemory().toString())
put("<REPLACE_JDK>", env.buildJdk)
put("<REPLACE_JAVA>", env.javaVersion)
put("<REPLACE_OS>", env.os)
put("\"<REPLACE_PROCESSORS>\"", env.processors.toString())
put("\"<REPLACE_MAX_MEMORY>\"", env.maxMemory.toString())

if (definitionFile != null) {
val projectDir = definitionFile.parentFile
Expand Down
Loading