Skip to content

Commit

Permalink
Pass javac options to Kapt javacArguments so the javac_options warn='…
Browse files Browse the repository at this point in the history
…off' option works for annotation processors that print warnings
  • Loading branch information
cheister committed Nov 30, 2023
1 parent 66c9b48 commit 237d0c4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class KotlinBuilder @Inject internal constructor(
enum class KotlinBuilderFlags(override val flag: String) : Flag {
TARGET_LABEL("--target_label"),
CLASSPATH("--classpath"),
JAVAC_OPTS("--javacopts"),
DIRECT_DEPENDENCIES("--direct_dependencies"),
DEPS_ARTIFACTS("--deps_artifacts"),
SOURCES("--sources"),
Expand Down Expand Up @@ -306,6 +307,8 @@ class KotlinBuilder @Inject internal constructor(
?.also {
addAllSourceJars(it)
}

addAllJavacFlags(argMap.optional(KotlinBuilderFlags.JAVAC_OPTS) ?: emptyList())
}

with(root.infoBuilder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,35 @@ internal fun JvmCompilationTask.preProcessingSteps(
return context.execute("expand sources") { expandWithSourceJarSources() }
}

internal fun parseJavacArgsToMap(args: List<String>): Map<String, String> {
val optionsMap = mutableMapOf<String, String>()
var i = 0

while (i < args.size) {
val arg = args[i]

// map option arguments as key value pairs e.g. --source 8 => ("--source", "8")
// map flag arguments as key with value = "true" e.g. map -nowarn => ("-nowarn", "true")
if (arg.startsWith("-")) {
val hasNext = i + 1 < args.size
val nextArg = if (hasNext) args[i + 1] else null

if (hasNext && !nextArg!!.startsWith("-")) {
optionsMap[arg] = nextArg
i += 2
} else {
optionsMap[arg] = "true"
i++
}
} else {
// Ignore non-option arguments
i++
}
}

return optionsMap
}

internal fun encodeMap(options: Map<String, String>): String {
val os = ByteArrayOutputStream()
val oos = ObjectOutputStream(os)
Expand All @@ -131,10 +160,11 @@ internal fun JvmCompilationTask.kaptArgs(
plugins: InternalCompilerPlugins,
aptMode: String,
): CompilationArgs {
val javacArgs = mapOf<String, String>(
"-target" to info.toolchainInfo.jvm.jvmTarget,
"-source" to info.toolchainInfo.jvm.jvmTarget,
)
val javacArgs = parseJavacArgsToMap(listOf(
"-target", info.toolchainInfo.jvm.jvmTarget,
"-source", info.toolchainInfo.jvm.jvmTarget,
).plus(inputs.javacFlagsList))

return CompilationArgs().apply {
xFlag("plugin", plugins.kapt.jarPath)

Expand Down
2 changes: 1 addition & 1 deletion src/main/starlark/core/options/opts.javac.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ _JOPTS = {
),
type = attr.string,
value_to_flag = {
"off": ["-nowarn"],
"off": ["-nowarn", "-Xlint:none"],
"error": ["-Werror"],
"report": None,
},
Expand Down

0 comments on commit 237d0c4

Please sign in to comment.