-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
GradleKotlinCompilerRunner.kt
469 lines (419 loc) · 21 KB
/
GradleKotlinCompilerRunner.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.compilerRunner
import org.gradle.api.Project
import org.gradle.api.invocation.Gradle
import org.gradle.api.logging.Logger
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.bundling.AbstractArchiveTask
import org.gradle.api.tasks.bundling.Zip
import org.gradle.jvm.tasks.Jar
import org.gradle.workers.WorkQueue
import org.gradle.workers.WorkerExecutor
import org.jetbrains.kotlin.build.report.metrics.*
import org.jetbrains.kotlin.cli.common.arguments.*
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.compilerRunner.btapi.GradleBuildToolsApiCompilerRunner
import org.jetbrains.kotlin.daemon.client.CompileServiceSession
import org.jetbrains.kotlin.daemon.common.CompilerId
import org.jetbrains.kotlin.daemon.common.configureDaemonJVMOptions
import org.jetbrains.kotlin.daemon.common.filterExtractProps
import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtensionOrNull
import org.jetbrains.kotlin.gradle.internal.ClassLoadersCachingBuildService
import org.jetbrains.kotlin.gradle.logging.kotlinDebug
import org.jetbrains.kotlin.gradle.logging.kotlinInfo
import org.jetbrains.kotlin.gradle.plugin.BuildFinishedListenerService
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion
import org.jetbrains.kotlin.gradle.plugin.internal.BuildIdService
import org.jetbrains.kotlin.gradle.plugin.internal.state.TaskLoggers
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinWithJavaTarget
import org.jetbrains.kotlin.gradle.plugin.statistics.CompilerArgumentMetrics
import org.jetbrains.kotlin.gradle.tasks.*
import org.jetbrains.kotlin.gradle.utils.*
import org.jetbrains.kotlin.incremental.IncrementalModuleEntry
import org.jetbrains.kotlin.incremental.IncrementalModuleInfo
import org.jetbrains.kotlin.statistics.metrics.StatisticsValuesConsumer
import java.io.File
import java.lang.ref.WeakReference
import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.read
import kotlin.concurrent.write
const val CREATED_CLIENT_FILE_PREFIX = "Created client-is-alive flag file: "
const val EXISTING_CLIENT_FILE_PREFIX = "Existing client-is-alive flag file: "
const val CREATED_SESSION_FILE_PREFIX = "Created session-is-alive flag file: "
const val EXISTING_SESSION_FILE_PREFIX = "Existing session-is-alive flag file: "
const val DELETED_SESSION_FILE_PREFIX = "Deleted session-is-alive flag file: "
const val COULD_NOT_CONNECT_TO_DAEMON_MESSAGE = "Could not connect to Kotlin compile daemon"
internal fun createGradleCompilerRunner(
taskProvider: GradleCompileTaskProvider,
toolsJar: File?,
compilerExecutionSettings: CompilerExecutionSettings,
buildMetricsReporter: BuildMetricsReporter<GradleBuildTime, GradleBuildPerformanceMetric>,
workerExecutor: WorkerExecutor,
runViaBuildToolsApi: Boolean,
cachedClassLoadersService: Property<ClassLoadersCachingBuildService>,
buildFinishedListenerService: Provider<BuildFinishedListenerService>,
buildIdService: Provider<BuildIdService>,
fusMetricsConsumer: StatisticsValuesConsumer?,
): GradleCompilerRunner {
return if (runViaBuildToolsApi) {
GradleBuildToolsApiCompilerRunner(
taskProvider,
toolsJar,
compilerExecutionSettings,
buildMetricsReporter,
workerExecutor,
cachedClassLoadersService,
buildFinishedListenerService,
buildIdService,
fusMetricsConsumer
)
} else {
GradleCompilerRunnerWithWorkers(
taskProvider,
toolsJar,
compilerExecutionSettings,
buildMetricsReporter,
workerExecutor,
fusMetricsConsumer
)
}
}
/*
Using real taskProvider cause "field 'taskProvider' from type 'org.jetbrains.kotlin.compilerRunner.GradleCompilerRunner':
value 'fixed(class org.jetbrains.kotlin.gradle.tasks.KotlinCompile_Decorated, task ':compileKotlin')'
is not assignable to 'org.gradle.api.tasks.TaskProvider'" exception
*/
internal open class GradleCompilerRunner(
protected val taskProvider: GradleCompileTaskProvider,
protected val jdkToolsJar: File?,
protected val compilerExecutionSettings: CompilerExecutionSettings,
protected val buildMetrics: BuildMetricsReporter<GradleBuildTime, GradleBuildPerformanceMetric>,
protected val fusMetricsConsumer: StatisticsValuesConsumer?,
) {
internal val pathProvider = taskProvider.path.get()
internal val loggerProvider = taskProvider.logger.get()
internal val buildDirProvider = taskProvider.buildDir.get().asFile
internal val projectDirProvider = taskProvider.projectDir.get()
internal val sessionDirProvider = taskProvider.sessionsDir.get()
internal val projectNameProvider = taskProvider.projectName.get()
internal val incrementalModuleInfoProvider = taskProvider.buildModulesInfo
internal val errorsFiles = taskProvider.errorsFiles.get()
/**
* Compiler might be executed asynchronously. Do not do anything requiring end of compilation after this function is called.
* @see [GradleKotlinCompilerWork]
*/
fun runJvmCompilerAsync(
args: K2JVMCompilerArguments,
environment: GradleCompilerEnvironment,
jdkHome: File,
taskOutputsBackup: TaskOutputsBackup?,
): WorkQueue? {
if (args.jdkHome == null && !args.noJdk) args.jdkHome = jdkHome.absolutePath
loggerProvider.kotlinInfo("Kotlin compilation 'jdkHome' argument: ${args.jdkHome}")
return runCompilerAsync(KotlinCompilerClass.JVM, args, environment, taskOutputsBackup)
}
/**
* Compiler might be executed asynchronously. Do not do anything requiring end of compilation after this function is called.
* @see [GradleKotlinCompilerWork]
*/
fun runJsCompilerAsync(
args: K2JSCompilerArguments,
environment: GradleCompilerEnvironment,
taskOutputsBackup: TaskOutputsBackup?,
): WorkQueue? {
return runCompilerAsync(KotlinCompilerClass.JS, args, environment, taskOutputsBackup)
}
/**
* Compiler might be executed asynchronously. Do not do anything requiring end of compilation after this function is called.
* @see [GradleKotlinCompilerWork]
*/
fun runMetadataCompilerAsync(
args: K2MetadataCompilerArguments,
environment: GradleCompilerEnvironment,
): WorkQueue? {
return runCompilerAsync(KotlinCompilerClass.METADATA, args, environment)
}
private fun runCompilerAsync(
compilerClassName: String,
compilerArgs: CommonCompilerArguments,
environment: GradleCompilerEnvironment,
taskOutputsBackup: TaskOutputsBackup? = null,
): WorkQueue? {
if (compilerArgs.version) {
loggerProvider.lifecycle(
"Kotlin version " + loadCompilerVersion(environment.compilerClasspath) +
" (JRE " + System.getProperty("java.runtime.version") + ")"
)
compilerArgs.version = false
}
val argsArray = ArgumentUtils.convertArgumentsToStringList(compilerArgs).toTypedArray()
fusMetricsConsumer?.let { metricsConsumer -> CompilerArgumentMetrics.collectMetrics(compilerArgs, argsArray, metricsConsumer) }
val incrementalCompilationEnvironment = environment.incrementalCompilationEnvironment
val modulesInfo = incrementalCompilationEnvironment?.let { incrementalModuleInfoProvider.orNull?.info }
val workArgs = GradleKotlinCompilerWorkArguments(
projectFiles = ProjectFilesForCompilation(
loggerProvider,
projectDirProvider,
buildDirProvider,
projectNameProvider,
sessionDirProvider
),
compilerFullClasspath = environment.compilerFullClasspath(jdkToolsJar),
compilerClassName = compilerClassName,
compilerArgs = argsArray,
isVerbose = compilerArgs.verbose,
incrementalCompilationEnvironment = incrementalCompilationEnvironment,
incrementalModuleInfo = modulesInfo,
outputFiles = environment.outputFiles.toList(),
taskPath = pathProvider,
reportingSettings = environment.reportingSettings,
kotlinScriptExtensions = environment.kotlinScriptExtensions,
allWarningsAsErrors = compilerArgs.allWarningsAsErrors,
compilerExecutionSettings = compilerExecutionSettings,
errorsFiles = errorsFiles,
kotlinPluginVersion = getKotlinPluginVersion(loggerProvider),
//no need to log warnings in MessageCollector hear it will be logged by compiler
kotlinLanguageVersion = compilerArgs.languageVersion?.let { v -> KotlinVersion.fromVersion(v) } ?: KotlinVersion.DEFAULT,
compilerArgumentsLogLevel = environment.compilerArgumentsLogLevel,
)
TaskLoggers.put(pathProvider, loggerProvider)
return runCompilerAsync(
workArgs,
taskOutputsBackup
)
}
protected open fun runCompilerAsync(
workArgs: GradleKotlinCompilerWorkArguments,
taskOutputsBackup: TaskOutputsBackup?,
): WorkQueue? {
try {
buildMetrics.addTimeMetric(GradleBuildPerformanceMetric.CALL_WORKER)
val kotlinCompilerRunnable = GradleKotlinCompilerWork(workArgs)
kotlinCompilerRunnable.run()
} catch (e: FailedCompilationException) {
// Restore outputs only for CompilationErrorException or OOMErrorException (see GradleKotlinCompilerWorkAction.execute)
taskOutputsBackup?.tryRestoringOnRecoverableException(e) { restoreAction ->
buildMetrics.measure(GradleBuildTime.RESTORE_OUTPUT_FROM_BACKUP) {
restoreAction()
}
}
throw e
}
return null
}
companion object {
@Synchronized
internal fun getDaemonConnectionImpl(
clientIsAliveFlagFile: File,
sessionIsAliveFlagFile: File,
compilerFullClasspath: List<File>,
messageCollector: MessageCollector,
daemonJvmArgs: List<String>?,
isDebugEnabled: Boolean,
): CompileServiceSession? {
val compilerId = CompilerId.makeCompilerId(compilerFullClasspath)
val daemonJvmOptions = configureDaemonJVMOptions(
inheritMemoryLimits = true,
inheritOtherJvmOptions = false,
inheritAdditionalProperties = true
).also { opts ->
if (!daemonJvmArgs.isNullOrEmpty()) {
opts.jvmParams.addAll(
daemonJvmArgs.filterExtractProps(opts.mappers, "", opts.restMapper)
)
}
}
return KotlinCompilerRunnerUtils.newDaemonConnection(
compilerId, clientIsAliveFlagFile, sessionIsAliveFlagFile,
messageCollector = messageCollector,
isDebugEnabled = isDebugEnabled,
daemonJVMOptions = daemonJvmOptions
)
}
@Volatile
private var cachedGradle = WeakReference<Gradle>(null)
@Volatile
private var cachedModulesInfo: IncrementalModuleInfo? = null
@Synchronized
internal fun buildModulesInfo(gradle: Gradle): IncrementalModuleInfo {
if (cachedGradle.get() === gradle && cachedModulesInfo != null) return cachedModulesInfo!!
val dirToModule = HashMap<File, IncrementalModuleEntry>()
val nameToModules = HashMap<String, HashSet<IncrementalModuleEntry>>()
val jarToClassListFile = HashMap<File, File>()
val jarToModule = HashMap<File, IncrementalModuleEntry>()
val jarToAbiSnapshot = HashMap<File, File>()
val multiplatformProjectTasks = mutableMapOf<Project, MutableSet<String>>()
gradle.taskGraph.allTasks.forEach { task ->
val project = task.project
/*
Ignoring isolated classpath:
An inaccessible project is not a fatal issue here. Missing information will "only" lead to non-incremental
compilation. We expect the user to be warned about this misconfiguration during configuration phase.
*/
val multiplatformExtension = try {
project.multiplatformExtensionOrNull
} catch (_: IsolatedKotlinClasspathClassCastException) {
null
}
if (multiplatformExtension != null) {
// Just record this, we'll process them later
val tasksInProject = multiplatformProjectTasks[project] ?: mutableSetOf()
tasksInProject.add(task.name)
multiplatformProjectTasks[project] = tasksInProject
}
if (task is AbstractKotlinCompile<*>) {
val module = IncrementalModuleEntry(
project.path,
task.taskModuleName,
project.layout.buildDirectory.get().asFile,
task.buildHistoryFile.get().asFile,
task.abiSnapshotFile.get().asFile
)
dirToModule[task.destinationDirectory.get().asFile] = module
task.javaOutputDir.orNull?.asFile?.let { dirToModule[it] = module }
nameToModules.getOrPut(module.name) { HashSet() }.add(module)
if (task is Kotlin2JsCompile) {
(jarForJavaSourceSet(project, task.sourceSetName.get()) ?: jarForSingleTargetJs(
project,
task.sourceSetName.get()
))?.let {
jarToModule[it] = module
}
}
} else if (task is InspectClassesForMultiModuleIC) {
jarToClassListFile[File(task.archivePath.get())] = task.classesListFile.get().asFile
}
}
for ((project, tasksInProject) in multiplatformProjectTasks) {
project.extensions.findByType(KotlinMultiplatformExtension::class.java)?.let { kotlinExt ->
for (target in kotlinExt.targets) {
val mainCompilation = target.compilations.findByName(KotlinCompilation.MAIN_COMPILATION_NAME) ?: continue
if (mainCompilation.compileKotlinTaskName !in tasksInProject || target.artifactsTaskName !in tasksInProject) {
// tasks are not part of the task graph, skip
continue
}
val kotlinTask = mainCompilation.compileTaskProvider.get() as? AbstractKotlinCompile<*> ?: continue
val module = IncrementalModuleEntry(
project.path,
kotlinTask.taskModuleName,
project.layout.buildDirectory.get().asFile,
kotlinTask.buildHistoryFile.get().asFile,
kotlinTask.abiSnapshotFile.get().asFile
)
val jarTask = project.tasks.findByName(target.artifactsTaskName) as? AbstractArchiveTask ?: continue
jarToModule[jarTask.archivePathCompatible.normalize().absoluteFile] = module
if (target is KotlinWithJavaTarget<*, *>) {
val jar = project.tasks.getByName(target.artifactsTaskName) as Jar
jarToClassListFile[jar.archivePathCompatible.normalize().absoluteFile] =
target.defaultArtifactClassesListFile.get()
//configure abiSnapshot mapping for jars
jarToAbiSnapshot[jar.archivePathCompatible.normalize().absoluteFile] =
target.buildDir.get().file(kotlinTask.abiSnapshotRelativePath).get().asFile
}
}
}
}
return IncrementalModuleInfo(
rootProjectBuildDir = gradle.rootProject.layout.buildDirectory.get().asFile,
dirToModule = dirToModule,
nameToModules = nameToModules,
jarToClassListFile = jarToClassListFile,
jarToModule = jarToModule,
jarToAbiSnapshot = jarToAbiSnapshot
).also {
cachedGradle = WeakReference(gradle)
cachedModulesInfo = it
}
}
private val AbstractKotlinCompile<*>.taskModuleName
get() = when (this) {
is KotlinCompile -> compilerOptions.moduleName.get()
is Kotlin2JsCompile -> compilerOptions.moduleName.get()
is KotlinCompileCommon -> moduleName.get()
else -> throw IllegalStateException("Unknown AbstractKotlinCompile task instance: ${this::class.qualifiedName}")
}
private fun jarForJavaSourceSet(
project: Project,
sourceSetName: String,
): File? {
val sourceSets = project.javaSourceSetsIfAvailable ?: return null
val sourceSet = sourceSets.findByName(sourceSetName) ?: return null
val jarTask = project.tasks.findByName(sourceSet.jarTaskName) as? Jar
return jarTask?.archiveFile?.get()?.asFile
}
private fun jarForSingleTargetJs(
project: Project,
sourceSetName: String,
): File? {
if (sourceSetName != KotlinCompilation.MAIN_COMPILATION_NAME) return null
val jarTaskName = project.extensions.findByType<KotlinJsProjectExtension>()?.js()?.artifactsTaskName
val jarTask = jarTaskName?.let { project.tasks.findByName(jarTaskName) } as? Zip
return jarTask?.archiveFile?.get()?.asFile
}
@Synchronized
internal fun clearBuildModulesInfo() {
cachedGradle = WeakReference<Gradle>(null)
cachedModulesInfo = null
}
// created once per gradle instance
// when gradle daemon dies, kotlin daemon should die too
// however kotlin daemon (if it idles enough) can die before gradle daemon dies
@Volatile
private var clientIsAliveFlagFile: File? = null
@Synchronized
internal fun getOrCreateClientFlagFile(log: Logger, projectName: String): File {
if (clientIsAliveFlagFile == null || !clientIsAliveFlagFile!!.exists()) {
clientIsAliveFlagFile = newTmpFile(prefix = "kotlin-compiler-in-${projectName}-", suffix = ".alive")
log.kotlinDebug { CREATED_CLIENT_FILE_PREFIX + clientIsAliveFlagFile!!.normalize().absoluteFile }
} else {
log.kotlinDebug { EXISTING_CLIENT_FILE_PREFIX + clientIsAliveFlagFile!!.normalize().absoluteFile }
}
return clientIsAliveFlagFile!!
}
internal fun String.normalizeForFlagFile(): String {
val validChars = ('a'..'z') + ('0'..'9') + "-_"
return filter { it in validChars }
}
// session is created per build
private var sessionFlagFile: File? = null
private val sessionFileLock = ReentrantReadWriteLock(true)
// session files are deleted at org.jetbrains.kotlin.gradle.plugin.KotlinGradleBuildServices.buildFinished
internal fun getOrCreateSessionFlagFile(
log: Logger,
sessionsDir: File,
): File {
sessionFileLock.read {
val sessionFlagRead = sessionFlagFile
if (sessionFlagRead != null && sessionFlagRead.exists()) {
return sessionFlagRead.sessionFileFlagExists(log)
}
}
sessionFileLock.write {
val sessionFlagWrite = sessionFlagFile
if (sessionFlagWrite != null && sessionFlagWrite.exists()) {
return sessionFlagWrite.sessionFileFlagExists(log)
}
return newTmpFile(
prefix = "kotlin-compiler-",
suffix = ".salive",
directory = sessionsDir.apply { mkdirs() })
.also {
sessionFlagFile = it
log.kotlinDebug { CREATED_SESSION_FILE_PREFIX + it.absolutePath }
}
}
}
private fun File.sessionFileFlagExists(log: Logger): File {
log.kotlinDebug { EXISTING_SESSION_FILE_PREFIX + absolutePath }
return this
}
}
}