From b7f50aeaf25e934eb71757b8701fd95648e51073 Mon Sep 17 00:00:00 2001 From: Yahor Berdnikau Date: Thu, 9 Feb 2023 13:36:46 +0100 Subject: [PATCH] Introduce repo settings conventions First convention is to configure Gradle build cache. --- buildSrc/settings.gradle | 23 ++++---- repo/gradle-settings-conventions/Readme.md | 7 +++ .../build-cache/build.gradle.kts | 16 ++++++ .../kotlin/build-cache.settings.gradle.kts | 24 +++++++++ .../gradle.properties | 3 ++ .../settings.gradle.kts | 52 +++++++++++++++++++ .../kotlin-bootstrap.settings.gradle.kts | 18 +++++-- settings.gradle | 30 ++--------- 8 files changed, 129 insertions(+), 44 deletions(-) create mode 100644 repo/gradle-settings-conventions/Readme.md create mode 100644 repo/gradle-settings-conventions/build-cache/build.gradle.kts create mode 100644 repo/gradle-settings-conventions/build-cache/src/main/kotlin/build-cache.settings.gradle.kts create mode 100644 repo/gradle-settings-conventions/gradle.properties create mode 100644 repo/gradle-settings-conventions/settings.gradle.kts diff --git a/buildSrc/settings.gradle b/buildSrc/settings.gradle index a98e09a6af4..5881b6e3017 100644 --- a/buildSrc/settings.gradle +++ b/buildSrc/settings.gradle @@ -1,6 +1,13 @@ +import org.gradle.api.internal.GradleInternal + pluginManagement { apply from: '../repo/scripts/cache-redirector.settings.gradle.kts' apply from: '../repo/scripts/kotlin-bootstrap.settings.gradle.kts' + + if (((GradleInternal) gradle).isRootBuild()) { + includeBuild '../repo/gradle-settings-conventions' + } + repositories { maven { url "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/kotlin-dependencies" } mavenCentral() @@ -8,6 +15,10 @@ pluginManagement { } } +plugins { + id "build-cache" +} + File versionPropertiesFile = new File(rootProject.projectDir.parentFile, "gradle/versions.properties") def versionProperties = new Properties() versionPropertiesFile.withInputStream { @@ -30,16 +41,4 @@ dependencyResolutionManagement { } } -buildscript { - repositories { - maven { url "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/kotlin-dependencies" } - mavenCentral() - } - - def buildGradlePluginVersion = ext["kotlin.build.gradlePlugin.version"] - dependencies { - classpath("org.jetbrains.kotlin:kotlin-build-gradle-plugin:$buildGradlePluginVersion") - } -} - include "prepare-deps" \ No newline at end of file diff --git a/repo/gradle-settings-conventions/Readme.md b/repo/gradle-settings-conventions/Readme.md new file mode 100644 index 00000000000..2b1c5a472b0 --- /dev/null +++ b/repo/gradle-settings-conventions/Readme.md @@ -0,0 +1,7 @@ +## Description + +Provides common settings convention plugins for the repo. + +### List of plugins + +- "build-cache" - local and remote build cache configuration \ No newline at end of file diff --git a/repo/gradle-settings-conventions/build-cache/build.gradle.kts b/repo/gradle-settings-conventions/build-cache/build.gradle.kts new file mode 100644 index 00000000000..df047596e50 --- /dev/null +++ b/repo/gradle-settings-conventions/build-cache/build.gradle.kts @@ -0,0 +1,16 @@ +plugins { + `kotlin-dsl` + id("org.jetbrains.kotlin.jvm") +} + +repositories { + maven(url = "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/kotlin-dependencies") + mavenCentral() +} + +kotlin.jvmToolchain(8) + +val buildGradlePluginVersion = extra.get("kotlin.build.gradlePlugin.version") +dependencies { + implementation("org.jetbrains.kotlin:kotlin-build-gradle-plugin:$buildGradlePluginVersion") +} diff --git a/repo/gradle-settings-conventions/build-cache/src/main/kotlin/build-cache.settings.gradle.kts b/repo/gradle-settings-conventions/build-cache/src/main/kotlin/build-cache.settings.gradle.kts new file mode 100644 index 00000000000..64b4c35516e --- /dev/null +++ b/repo/gradle-settings-conventions/build-cache/src/main/kotlin/build-cache.settings.gradle.kts @@ -0,0 +1,24 @@ +val buildProperties = getKotlinBuildPropertiesForSettings(settings) + +buildCache { + local { + isEnabled = buildProperties.localBuildCacheEnabled + if (buildProperties.localBuildCacheDirectory != null) { + directory = buildProperties.localBuildCacheDirectory + } + } + + val remoteBuildCacheUrl = buildProperties.buildCacheUrl + if (remoteBuildCacheUrl != null) { + remote { + url = uri(remoteBuildCacheUrl) + isPush = buildProperties.pushToBuildCache + if (buildProperties.buildCacheUser != null && + buildProperties.buildCachePassword != null + ) { + credentials.username = buildProperties.buildCacheUser + credentials.password = buildProperties.buildCachePassword + } + } + } +} diff --git a/repo/gradle-settings-conventions/gradle.properties b/repo/gradle-settings-conventions/gradle.properties new file mode 100644 index 00000000000..7562948443a --- /dev/null +++ b/repo/gradle-settings-conventions/gradle.properties @@ -0,0 +1,3 @@ +kotlin.build.gradlePlugin.version=0.0.39 +kotlin.options.suppressFreeCompilerArgsModificationWarning=true +cacheRedirectorEnabled=true diff --git a/repo/gradle-settings-conventions/settings.gradle.kts b/repo/gradle-settings-conventions/settings.gradle.kts new file mode 100644 index 00000000000..b77a1b474ef --- /dev/null +++ b/repo/gradle-settings-conventions/settings.gradle.kts @@ -0,0 +1,52 @@ +pluginManagement { + apply(from = "../scripts/cache-redirector.settings.gradle.kts") + apply(from = "../scripts/kotlin-bootstrap.settings.gradle.kts") + + repositories { + maven(url = "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/kotlin-dependencies") + mavenCentral() + gradlePluginPortal() + } +} + +buildscript { + repositories { + maven(url = "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/kotlin-dependencies") + mavenCentral() + } + + val buildGradlePluginVersion = extra.get("kotlin.build.gradlePlugin.version") + dependencies { + classpath("org.jetbrains.kotlin:kotlin-build-gradle-plugin:$buildGradlePluginVersion") + } +} + +include(":build-cache") + +// Unfortunately it is not possible to apply build-cache.settings.gradle.kts as script compilation +// could not then find types from "kotlin-build-gradle-plugin" +// Sync below to the content of settings plugin +val buildProperties = getKotlinBuildPropertiesForSettings(settings) + +buildCache { + local { + isEnabled = buildProperties.localBuildCacheEnabled + if (buildProperties.localBuildCacheDirectory != null) { + directory = buildProperties.localBuildCacheDirectory + } + } + + val remoteBuildCacheUrl = buildProperties.buildCacheUrl + if (remoteBuildCacheUrl != null) { + remote { + url = uri(remoteBuildCacheUrl) + isPush = buildProperties.pushToBuildCache + if (buildProperties.buildCacheUser != null && + buildProperties.buildCachePassword != null + ) { + credentials.username = buildProperties.buildCacheUser + credentials.password = buildProperties.buildCachePassword + } + } + } +} diff --git a/repo/scripts/kotlin-bootstrap.settings.gradle.kts b/repo/scripts/kotlin-bootstrap.settings.gradle.kts index cd4ecf5ef44..0d0aee31084 100644 --- a/repo/scripts/kotlin-bootstrap.settings.gradle.kts +++ b/repo/scripts/kotlin-bootstrap.settings.gradle.kts @@ -58,12 +58,19 @@ fun getRootSettings( // Gradle interface neither exposes flag if it is a root of composite-build hierarchy // nor gives access to Settings object. Fortunately it is availaibe inside GradleInternal internal api // used by build scan plugin. + // + // Included builds for build logic are evaluated earlier then root settings leading to error that root settings object is not yet + // available. For such cases we fallback to included build settings object and later manual mapping for kotlinRootDir val gradleInternal = (gradle as GradleInternal) - return if (gradleInternal.isRootBuild()) { - settings - } else { - val gradleParent = gradle.parent ?: error("Could not get includedBuild parent build for ${settings.rootDir}!") - getRootSettings(gradle.parent!!.settings, gradle.parent!!) + return when { + gradleInternal.isRootBuild() || + settings.rootProject.name == "gradle-settings-conventions" -> { + settings + } + else -> { + val gradleParent = gradle.parent ?: error("Could not get includedBuild parent build for ${settings.rootDir}!") + getRootSettings(gradle.parent!!.settings, gradle.parent!!) + } } } @@ -83,6 +90,7 @@ val kotlinRootDir: File = when (rootSettings.rootProject.name) { } } "benchmarksAnalyzer", "performance-server" -> rootSettings.rootDir.parentFile.parentFile.parentFile + "gradle-settings-conventions" -> rootSettings.rootDir.parentFile.parentFile "performance" -> rootSettings.rootDir.parentFile.parentFile "ui" -> rootSettings.rootDir.parentFile.parentFile.parentFile.parentFile else -> rootSettings.rootDir diff --git a/settings.gradle b/settings.gradle index ff21b734377..9ae7ad28dcc 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,6 @@ pluginManagement { + includeBuild("repo/gradle-settings-conventions") + apply from: 'repo/scripts/cache-redirector.settings.gradle.kts' apply from: 'repo/scripts/kotlin-bootstrap.settings.gradle.kts' @@ -13,17 +15,11 @@ pluginManagement { } } -buildscript { - def buildGradlePluginVersion = ext["kotlin.build.gradlePlugin.version"] - dependencies { - classpath("org.jetbrains.kotlin:kotlin-build-gradle-plugin:${buildGradlePluginVersion}") - } -} - plugins { id "com.gradle.enterprise" version "3.11.2" id "com.gradle.common-custom-user-data-gradle-plugin" version "1.8.1" apply false id "org.gradle.toolchains.foojay-resolver-convention" version "0.4.0" + id "build-cache" } def buildProperties = BuildPropertiesKt.getKotlinBuildPropertiesForSettings(settings) @@ -54,26 +50,6 @@ gradleEnterprise { } } -buildCache { - local { - enabled = buildProperties.localBuildCacheEnabled - if (buildProperties.localBuildCacheDirectory != null) { - directory = buildProperties.localBuildCacheDirectory - } - } - - if (buildProperties.buildCacheUrl != null) { - remote(HttpBuildCache) { - url = buildProperties.buildCacheUrl - push = buildProperties.pushToBuildCache - if (buildProperties.buildCacheUser != null && buildProperties.buildCachePassword != null) { - credentials.username = buildProperties.buildCacheUser - credentials.password = buildProperties.buildCachePassword - } - } - } -} - // modules include ":benchmarks", ":compiler",