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

Pass compilation args using the official way #601

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
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ class CompilationArgs(
return value(
toArgs(
paths.asSequence()
.map { dfs.getPath(it) })
.map { dfs.getPath(it) }
)
)
}

Expand Down Expand Up @@ -167,11 +168,27 @@ class CompilationArgs(
return this
}

fun xFlag(flag: String, value: String): CompilationArgs {
fun xFlag(
flag: String,
value: String
): CompilationArgs {
args.add("-X$flag=$value")
return this
}

fun repeatFlag(
flag: String,
vararg values: Pair<String, List<String>>,
transform: (option: String, value: String) -> String
): CompilationArgs {
values.forEach { (option, values) ->
values.forEach {
flag(flag, transform(option, it))
}
}
return this
}

fun list(): List<String> = args.toList()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably should stick with the toList() naming pattern.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can't just merge it as we needed the base64 encoding for older versions of kotlinc. Currently working on a way to preserve versionsing without making too much of a mess.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Added branch to cover pre-1.5

Not the way I would like it, but I don't want to hold up progress while I yak shave with one hand.


fun base64Encode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ internal fun JvmCompilationTask.plugins(
}
}

internal fun JvmCompilationTask.preProcessingSteps(context: CompilationTaskContext): JvmCompilationTask {
internal fun JvmCompilationTask.preProcessingSteps(
context: CompilationTaskContext
): JvmCompilationTask {
return context.execute("expand sources") { expandWithSourceJarSources() }
}

Expand Down Expand Up @@ -138,10 +140,10 @@ internal fun JvmCompilationTask.kaptArgs(
"-target" to info.toolchainInfo.jvm.jvmTarget,
"-source" to info.toolchainInfo.jvm.jvmTarget
)
return CompilationArgs()
.xFlag("plugin", plugins.kapt.jarPath)
.base64Encode(
"-P",
return CompilationArgs().apply {
xFlag("plugin", plugins.kapt.jarPath)

val values = arrayOf(
"sources" to listOf(directories.generatedJavaSources),
"classes" to listOf(directories.generatedClasses),
"stubs" to listOf(directories.stubs),
Expand All @@ -152,15 +154,35 @@ internal fun JvmCompilationTask.kaptArgs(
"processors" to listOf(inputs.processorsList.joinToString(",")),
"apclasspath" to inputs.processorpathsList,
"aptMode" to listOf(aptMode)
) { enc -> "plugin:${plugins.kapt.id}:configuration=$enc" }
)

val version = info.toolchainInfo.common.apiVersion.toFloat()
when {
version < 1.5 -> base64Encode(
"-P",
*values
) { enc -> "plugin:${plugins.kapt.id}:configuration=$enc" }
else -> repeatFlag(
"-P",
*values
) { option, value ->
"plugin:${plugins.kapt.id}:$option=$value"
}
}
}
}

internal fun JvmCompilationTask.runPlugins(
context: CompilationTaskContext,
plugins: InternalCompilerPlugins,
compiler: KotlinToolchain.KotlincInvoker
): JvmCompilationTask {
if ((inputs.processorsList.isEmpty() && inputs.stubsPluginsList.isEmpty()) || inputs.kotlinSourcesList.isEmpty()) {
if ((
inputs.processorsList.isEmpty() &&
inputs.stubsPluginsList.isEmpty()
) ||
inputs.kotlinSourcesList.isEmpty()
) {
return this
} else {
return context.execute("kapt (${inputs.processorsList.joinToString(", ")})") {
Expand All @@ -175,14 +197,15 @@ internal fun JvmCompilationTask.runPlugins(
.plus(
kaptArgs(context, plugins, "stubsAndApt")
)
)
)
.flag("-d", directories.generatedClasses)
.values(inputs.kotlinSourcesList)
.values(inputs.javaSourcesList).list().let { args ->
context.executeCompilerTask(
args,
compiler::compile,
printOnSuccess = context.whenTracing { false } ?: true)
printOnSuccess = context.whenTracing { false } ?: true
)
}.let { outputLines ->
// if tracing is enabled the output should be formatted in a special way, if we aren't
// tracing then any compiler output would make it's way to the console as is.
Expand Down Expand Up @@ -215,16 +238,16 @@ internal fun JvmCompilationTask.createOutputJar() =
* Produce the primary output jar.
*/
internal fun JvmCompilationTask.createAbiJar() =
JarCreator(
path = Paths.get(outputs.abijar),
normalize = true,
verbose = false
).also {
it.addDirectory(Paths.get(directories.abiClasses))
it.addDirectory(Paths.get(directories.generatedClasses))
it.setJarOwner(info.label, info.bazelRuleKind)
it.execute()
}
JarCreator(
path = Paths.get(outputs.abijar),
normalize = true,
verbose = false
).also {
it.addDirectory(Paths.get(directories.abiClasses))
it.addDirectory(Paths.get(directories.generatedClasses))
it.setJarOwner(info.label, info.bazelRuleKind)
it.execute()
}

/**
* Produce a jar of sources generated by KAPT.
Expand Down Expand Up @@ -284,10 +307,12 @@ fun JvmCompilationTask.compileKotlin(
writeJdeps(outputs.jdeps, emptyJdeps(info.label))
return emptyList()
} else {
return (args + plugins(
options = inputs.compilerPluginOptionsList,
classpath = inputs.compilerPluginClasspathList
))
return (
args + plugins(
options = inputs.compilerPluginOptionsList,
classpath = inputs.compilerPluginClasspathList
)
)
.values(inputs.javaSourcesList)
.values(inputs.kotlinSourcesList)
.flag("-d", directories.classes)
Expand All @@ -312,7 +337,8 @@ fun JvmCompilationTask.compileKotlin(
.flatMap { walk(it) }
.filter { !isDirectory(it) }
.map { it.toString() }
.collect(toList()))
.collect(toList())
)
}
}
}
Expand Down Expand Up @@ -368,7 +394,8 @@ private fun JvmCompilationTask.expandWithSources(sources: Iterator<String>): Jvm
updateBuilder { builder ->
sources.filterOutNonCompilableSources().partitionJvmSources(
{ builder.inputsBuilder.addKotlinSources(it) },
{ builder.inputsBuilder.addJavaSources(it) })
{ builder.inputsBuilder.addJavaSources(it) }
)
}

private fun JvmCompilationTask.updateBuilder(
Expand All @@ -383,9 +410,9 @@ private fun JvmCompilationTask.updateBuilder(
* Only keep java and kotlin files for the iterator. Filter our all other non-compilable files.
*/
private fun Iterator<String>.filterOutNonCompilableSources(): Iterator<String> {
val result = mutableListOf<String>()
this.forEach {
if (it.endsWith(".kt") or it.endsWith(".java")) result.add(it)
}
val result = mutableListOf<String>()
this.forEach {
if (it.endsWith(".kt") or it.endsWith(".java")) result.add(it)
}
return result.iterator()
}