From d361e0f5ea3319867964477d31e5812d26618d09 Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Wed, 14 Feb 2018 15:05:37 +0300 Subject: [PATCH] 181: Try to fix OOM in 181 by rising Xmx --- buildSrc/src/main/kotlin/tasks.kt.181 | 123 ++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 buildSrc/src/main/kotlin/tasks.kt.181 diff --git a/buildSrc/src/main/kotlin/tasks.kt.181 b/buildSrc/src/main/kotlin/tasks.kt.181 new file mode 100644 index 00000000000..8f373894e04 --- /dev/null +++ b/buildSrc/src/main/kotlin/tasks.kt.181 @@ -0,0 +1,123 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed 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. + */ + +@file:Suppress("unused") // usages in build scripts are not tracked properly + +import org.gradle.api.Project +import org.gradle.api.Task +import org.gradle.api.internal.tasks.testing.filter.DefaultTestFilter +import org.gradle.api.plugins.JavaPluginConvention +import org.gradle.api.tasks.testing.Test +import org.gradle.kotlin.dsl.extra +import org.gradle.kotlin.dsl.task +import org.gradle.kotlin.dsl.the +import org.gradle.kotlin.dsl.* +import java.lang.Character.isLowerCase +import java.lang.Character.isUpperCase + +fun Project.projectTest(taskName: String = "test", body: Test.() -> Unit = {}): Test = getOrCreateTask(taskName) { + doFirst { + val patterns = filter.includePatterns + ((filter as? DefaultTestFilter)?.commandLineIncludePatterns ?: emptySet()) + if (patterns.isEmpty() || patterns.any { '*' in it }) return@doFirst + patterns.forEach { pattern -> + val maybeMethodName = pattern.substringAfterLast('.') + val maybeClassFqName = if (maybeMethodName.isFirstChar(::isLowerCase)) + pattern.substringBeforeLast('.') + else + pattern + + if (!maybeClassFqName.substringAfterLast('.').isFirstChar(::isUpperCase)) { + return@forEach + } + + val classFileNameWithoutExtension = maybeClassFqName.replace('.', '/') + val classFileName = classFileNameWithoutExtension + ".class" + + include { + val path = it.path + if (it.isDirectory) { + classFileNameWithoutExtension.startsWith(path) + } else { + path == classFileName || (path.endsWith(".class") && path.startsWith(classFileNameWithoutExtension + "$")) + } + } + } + } + + doFirst { + val agent = tasks.findByPath(":test-instrumenter:jar")!!.outputs.files.singleFile + + val args = project.findProperty("kotlin.test.instrumentation.args")?.let { "=$it" }.orEmpty() + + jvmArgs("-javaagent:$agent$args") + } + + dependsOn(":test-instrumenter:jar") + + jvmArgs("-ea", "-XX:+HeapDumpOnOutOfMemoryError", "-Xmx1600m", "-XX:+UseCodeCacheFlushing", "-XX:ReservedCodeCacheSize=128m", "-Djna.nosys=true") + maxHeapSize = "1600m" + systemProperty("idea.is.unit.test", "true") + systemProperty("idea.home.path", intellijRootDir().canonicalPath) + environment("NO_FS_ROOTS_ACCESS_CHECK", "true") + environment("PROJECT_CLASSES_DIRS", the().sourceSets.getByName("test").output.classesDirs.asPath) + environment("PROJECT_BUILD_DIR", buildDir) + systemProperty("jps.kotlin.home", rootProject.extra["distKotlinHomeDir"]) + systemProperty("kotlin.ni", if (rootProject.hasProperty("newInferenceTests")) "true" else "false") + body() +} + +private inline fun String.isFirstChar(f: (Char) -> Boolean) = isNotEmpty() && f(first()) + +inline fun Project.getOrCreateTask(taskName: String, body: T.() -> Unit): T = + (tasks.findByName(taskName)?.let { it as T } ?: task(taskName)).apply { body() } + +object TaskUtils { + fun useAndroidSdk(task: Task) { + task.useAndroidConfiguration(systemPropertyName = "android.sdk", configName = "androidSdk") + } + + fun useAndroidJar(task: Task) { + task.useAndroidConfiguration(systemPropertyName = "android.jar", configName = "androidJar") + } +} + +private fun Task.useAndroidConfiguration(systemPropertyName: String, configName: String) { + val configuration = with(project) { + configurations.getOrCreate(configName) + .also { + dependencies.add( + configName, + dependencies.project(":custom-dependencies:android-sdk", configuration = configName) + ) + } + } + + dependsOn(configuration) + + if (this is Test) { + doFirst { + systemProperty(systemPropertyName, configuration.singleFile.canonicalPath) + } + } +} + +fun Task.useAndroidSdk() { + TaskUtils.useAndroidSdk(this) +} + +fun Task.useAndroidJar() { + TaskUtils.useAndroidJar(this) +}