Skip to content

Commit 49d72a0

Browse files
committed
Pass javac options to Kapt javacArguments so the javac_options warn='off' option works for annotation processors that print warnings
1 parent f14d01e commit 49d72a0

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

src/main/kotlin/io/bazel/kotlin/builder/tasks/KotlinBuilder.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class KotlinBuilder
5353
) : Flag {
5454
TARGET_LABEL("--target_label"),
5555
CLASSPATH("--classpath"),
56+
JAVAC_OPTS("--javacopts"),
5657
DIRECT_DEPENDENCIES("--direct_dependencies"),
5758
DEPS_ARTIFACTS("--deps_artifacts"),
5859
SOURCES("--sources"),
@@ -288,6 +289,8 @@ class KotlinBuilder
288289
?.also {
289290
addAllSourceJars(it)
290291
}
292+
293+
addAllJavacFlags(argMap.optional(KotlinBuilderFlags.JAVAC_OPTS) ?: emptyList())
291294
}
292295

293296
with(root.infoBuilder) {

src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/CompilationTask.kt

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,35 @@ internal fun JvmCompilationTask.preProcessingSteps(
119119
context: CompilationTaskContext,
120120
): JvmCompilationTask = context.execute("expand sources") { expandWithSourceJarSources() }
121121

122+
internal fun parseJavacArgsToMap(args: List<String>): Map<String, String> {
123+
val optionsMap = mutableMapOf<String, String>()
124+
var i = 0
125+
126+
while (i < args.size) {
127+
val arg = args[i]
128+
129+
// map option arguments as key value pairs e.g. --source 8 => ("--source", "8")
130+
// map flag arguments as key with value = "true" e.g. map -nowarn => ("-nowarn", "true")
131+
if (arg.startsWith("-")) {
132+
val hasNext = i + 1 < args.size
133+
val nextArg = if (hasNext) args[i + 1] else null
134+
135+
if (hasNext && !nextArg!!.startsWith("-")) {
136+
optionsMap[arg] = nextArg
137+
i += 2
138+
} else {
139+
optionsMap[arg] = "true"
140+
i++
141+
}
142+
} else {
143+
// Ignore non-option arguments
144+
i++
145+
}
146+
}
147+
148+
return optionsMap
149+
}
150+
122151
internal fun encodeMap(options: Map<String, String>): String {
123152
val os = ByteArrayOutputStream()
124153
val oos = ObjectOutputStream(os)
@@ -141,10 +170,15 @@ internal fun JvmCompilationTask.kaptArgs(
141170
aptMode: String,
142171
): CompilationArgs {
143172
val javacArgs =
144-
mapOf<String, String>(
145-
"-target" to info.toolchainInfo.jvm.jvmTarget,
146-
"-source" to info.toolchainInfo.jvm.jvmTarget,
173+
parseJavacArgsToMap(
174+
listOf(
175+
"-target",
176+
info.toolchainInfo.jvm.jvmTarget,
177+
"-source",
178+
info.toolchainInfo.jvm.jvmTarget,
179+
).plus(inputs.javacFlagsList),
147180
)
181+
148182
return CompilationArgs().apply {
149183
xFlag("plugin", plugins.kapt.jarPath)
150184

src/main/starlark/core/options/opts.javac.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ _JOPTS = {
2424
),
2525
type = attr.string,
2626
value_to_flag = {
27-
"off": ["-nowarn"],
27+
"off": ["-nowarn", "-Xlint:none"],
2828
"error": ["-Werror"],
2929
"report": None,
3030
},

0 commit comments

Comments
 (0)