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

Build config/build scans and build cache #451

Merged
merged 8 commits into from
Jul 8, 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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,19 @@ See [gradle.properties](gradle.properties) in AtomicFU project for its `kotlin_v

Available Kotlin/Native targets are based on non-deprecated official targets [Tier list](https://kotlinlang.org/docs/native-target-support.html)
with the corresponding compatibility guarantees.

### Gradle Build Scans

[Gradle Build Scans](https://scans.gradle.com/) can provide insights into an Atomicfu Build.
JetBrains runs a [Gradle Develocity server](https://ge.jetbrains.com/scans?search.rootProjectNames=kotlinx-atomicfu).
that can be used to automatically upload reports.

To automatically opt in add the following to `$GRADLE_USER_HOME/gradle.properties`.

```properties
org.jetbrains.atomicfu.build.scan.enabled=true
# optionally provide a username that will be attached to each report
org.jetbrains.atomicfu.build.scan.username=John Wick
```

A Build Scan may contain identifiable information. See the Terms of Use https://gradle.com/legal/terms-of-use/.
12 changes: 12 additions & 0 deletions build-settings-logic/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
`kotlin-dsl`
}

repositories {
mavenCentral()
gradlePluginPortal()
}

dependencies {
implementation(libs.gradle.develocity)
}
16 changes: 16 additions & 0 deletions build-settings-logic/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
rootProject.name = "build-settings-logic"

dependencyResolutionManagement {
repositories {
mavenCentral()
gradlePluginPortal()
}

versionCatalogs {
create("libs") {
from(files("../gradle/libs.versions.toml"))
}
}
}

apply(from = "src/main/kotlin/atomicfu-gradle-build-cache.settings.gradle.kts")
34 changes: 34 additions & 0 deletions build-settings-logic/src/main/kotlin/Utils.kt
Copy link
Member

Choose a reason for hiding this comment

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

These utils are only used in gradle-build-scan.settings.gradle.kts - maybe they should be moved into the same file?

Alternatively, I think a more descriptive name would be nice. What about BuildScanProperties.kt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would leave it as is for the future, if we have some shared logic, it could be placed in Utils.kt file

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2014-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
import org.gradle.api.initialization.Settings
import org.gradle.api.provider.Provider


val buildingOnTeamCity: Boolean = System.getenv("TEAMCITY_VERSION") != null
val buildingOnGitHub: Boolean = System.getenv("GITHUB_ACTION") != null
val buildingOnCi: Boolean = System.getenv("CI") != null || buildingOnTeamCity || buildingOnGitHub

// NOTE: build scan properties are documented in README.md
val Settings.buildScanEnabled: Provider<Boolean>
get() =
atomicfuProperty("build.scan.enabled", String::toBoolean)
.orElse(buildingOnCi)

internal const val DEFAULT_ATOMICFU_USER_NAME = "<default>"

/**
* Optionaly override the default name attached to a Build Scan.
*/
val Settings.buildScanUsername: Provider<String>
get() =
atomicfuProperty("build.scan.username")
.orElse(DEFAULT_ATOMICFU_USER_NAME)
.map(String::trim)

internal fun Settings.atomicfuProperty(name: String): Provider<String> =
providers.gradleProperty("org.jetbrains.atomicfu.$name")

internal fun <T : Any> Settings.atomicfuProperty(name: String, convert: (String) -> T): Provider<T> =
atomicfuProperty(name).map(convert)

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Gradle Build Cache conventions.
*
* Because Atomicfu uses Composite Builds, Build Cache must be configured consistently on
* - the root settings.gradle.kts,
* - and the settings.gradle.kts of any projects added with `pluginManagement { includedBuild("...") }`
*
* See https://docs.gradle.org/8.8/userguide/build_cache.html#sec:build_cache_composite
*
* ⚠️ This file _must_ be applicable as a script plugin and so _must not_ depend on other source files.
*
* Based on https://github.com/JetBrains/kotlin/blob/2675531624d42851af502a993bbefd65ee3e38ef/repo/gradle-settings-conventions/build-cache/src/main/kotlin/build-cache.settings.gradle.kts
*/

// Don't remove or move to *.kt files these functions and variables
// they are required for applying this plugin as convention plugin to `build-settings-logic` settings file
internal fun Settings.atomicfuProperty(name: String): Provider<String> =
providers.gradleProperty("org.jetbrains.atomicfu.$name")

internal fun <T : Any> Settings.atomicfuProperty(name: String, convert: (String) -> T): Provider<T> =
atomicfuProperty(name).map(convert)

val buildingOnTeamCity: Boolean = System.getenv("TEAMCITY_VERSION") != null
val buildingOnGitHub: Boolean = System.getenv("GITHUB_ACTION") != null
val buildingOnCi: Boolean = System.getenv("CI") != null || buildingOnTeamCity || buildingOnGitHub

/**
* Disable Local Cache on CI, because CI machines are short-lived, so local caching doesn't help a lot.
* Also, to force CI machines to update the remote cache.
*/
val buildCacheLocalEnabled: Provider<Boolean> =
atomicfuProperty("build.cache.local.enabled", String::toBoolean)
.orElse(!buildingOnCi)

val buildCacheLocalDirectory: Provider<String> =
atomicfuProperty("build.cache.local.directory")

val buildCachePushEnabled: Provider<Boolean> =
atomicfuProperty("build.cache.push", String::toBoolean)
.orElse(buildingOnTeamCity)

val buildCacheUser: Provider<String> =
providers.gradleProperty("build.cache.user")

val buildCachePassword: Provider<String> =
providers.gradleProperty("build.cache.password")

buildCache {
local {
isEnabled = buildCacheLocalEnabled.get()
if (buildCacheLocalDirectory.orNull != null) {
directory = buildCacheLocalDirectory.get()
}
}
remote<HttpBuildCache> {
url = uri("https://ge.jetbrains.com/cache/")
isPush = buildCachePushEnabled.get()

if (buildCacheUser.isPresent &&
buildCachePassword.isPresent
) {
credentials.username = buildCacheUser.get()
credentials.password = buildCachePassword.get()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// this is a settings convention plugin for Gradle Develocity

plugins {
id("com.gradle.develocity")
}

develocity {
if (buildScanEnabled.get()) {
val overriddenName = buildScanUsername.orNull
buildScan {
server = "https://ge.jetbrains.com/"
publishing.onlyIf { true }
capture {
fileFingerprints = true
buildLogging = true
uploadInBackground = true
}
obfuscation {
ipAddresses { _ -> listOf("0.0.0.0") }
hostname { _ -> "concealed" }
username { originalUsername ->
when {
buildingOnTeamCity -> "TeamCity"
buildingOnGitHub -> "GitHub"
buildingOnCi -> "CI"
!overriddenName.isNullOrBlank() && overriddenName != DEFAULT_ATOMICFU_USER_NAME -> overriddenName
overriddenName == DEFAULT_ATOMICFU_USER_NAME -> originalUsername
else -> "unknown"
}
}
}
}
}
}
2 changes: 2 additions & 0 deletions buildSrc/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
rootProject.name = "buildSrc"

dependencyResolutionManagement {

@Suppress("UnstableApiUsage")
Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ systemProp.org.gradle.internal.publish.checksums.insecure=true
org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=2g

# we can remove this flag, when move to kotlin-gradle-plugin `2.0.0+`
kotlin.native.distribution.downloadFromMaven=true
kotlin.native.distribution.downloadFromMaven=true
org.gradle.caching=true
dkrasnoff marked this conversation as resolved.
Show resolved Hide resolved

4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ maven-pluginTools = "3.5"
node-gradle = "3.1.1"
rhino = "1.7.10"
gradle-plugin-publish = "1.2.1"
gradle-develocity = "3.17.5"

[libraries]

Expand All @@ -22,6 +23,9 @@ kotlin-testJunit = { group = "org.jetbrains.kotlin", name = "kotlin-test-junit",
kotlin-scriptRuntime = { group = "org.jetbrains.kotlin", name = "kotlin-script-runtime", version.ref = "kotlin" }
kotlin-metadataJvm = { group = "org.jetbrains.kotlin", name = "kotlin-metadata-jvm", version.ref = "kotlin" }

#gradle
gradle-develocity = {group = "com.gradle", name= "develocity-gradle-plugin", version.ref = "gradle-develocity"}

# ASM dependencies
asm = { group = "org.ow2.asm", name = "asm", version.ref = "asm" }
asm-commons = { group = "org.ow2.asm", name = "asm-commons", version.ref = "asm" }
Expand Down
48 changes: 27 additions & 21 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
pluginManagement {
includeBuild("build-settings-logic")
repositories {
repositories {
mavenCentral()
gradlePluginPortal()

val additionalRepositoryProperty = providers.gradleProperty("community.project.kotlin.repo")
.orElse(providers.gradleProperty("kotlin_repo_url"))
if (additionalRepositoryProperty.isPresent) {
maven(url = uri(additionalRepositoryProperty.get()))
logger.info("A custom Kotlin repository ${additionalRepositoryProperty.get()} was added")
}
mavenCentral()
gradlePluginPortal()

/*
* This property group is used to build kotlinx.atomicfu against Kotlin compiler snapshots.
* When build_snapshot_train is set to true, kotlin_version property is overridden with kotlin_snapshot_version.
* Additionally, mavenLocal and Sonatype snapshots are added to repository list
* (the former is required for AFU and public, the latter is required for compiler snapshots).
* DO NOT change the name of these properties without adapting kotlinx.train build chain.
*/
val buildSnapshotTrainGradleProperty = providers.gradleProperty("build_snapshot_train")
if (buildSnapshotTrainGradleProperty.isPresent) {
maven(url = uri("https://oss.sonatype.org/content/repositories/snapshots"))
}
val additionalRepositoryProperty = providers.gradleProperty("community.project.kotlin.repo")
.orElse(providers.gradleProperty("kotlin_repo_url"))
if (additionalRepositoryProperty.isPresent) {
maven(url = uri(additionalRepositoryProperty.get()))
logger.info("A custom Kotlin repository ${additionalRepositoryProperty.get()} was added")
}

/*
* This property group is used to build kotlinx.atomicfu against Kotlin compiler snapshots.
* When build_snapshot_train is set to true, kotlin_version property is overridden with kotlin_snapshot_version.
* Additionally, mavenLocal and Sonatype snapshots are added to repository list
* (the former is required for AFU and public, the latter is required for compiler snapshots).
* DO NOT change the name of these properties without adapting kotlinx.train build chain.
*/
val buildSnapshotTrainGradleProperty = providers.gradleProperty("build_snapshot_train")
if (buildSnapshotTrainGradleProperty.isPresent) {
maven(url = uri("https://oss.sonatype.org/content/repositories/snapshots"))
}

}
}

Expand Down Expand Up @@ -105,6 +104,13 @@ dependencyResolutionManagement {
}
}

rootProject.name = "kotlinx-atomicfu"

plugins {
id("atomicfu-gradle-build-scan")
id("atomicfu-gradle-build-cache")
}

include("atomicfu")
include("atomicfu-transformer")
include("atomicfu-gradle-plugin")
Expand Down