Skip to content

Commit 3fdce47

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 66c9b48 commit 3fdce47

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

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class KotlinBuilder @Inject internal constructor(
5252
enum class KotlinBuilderFlags(override val flag: String) : Flag {
5353
TARGET_LABEL("--target_label"),
5454
CLASSPATH("--classpath"),
55+
JAVAC_OPTS("--javacopts"),
5556
DIRECT_DEPENDENCIES("--direct_dependencies"),
5657
DEPS_ARTIFACTS("--deps_artifacts"),
5758
SOURCES("--sources"),
@@ -306,6 +307,8 @@ class KotlinBuilder @Inject internal constructor(
306307
?.also {
307308
addAllSourceJars(it)
308309
}
310+
311+
addAllJavacFlags(argMap.optional(KotlinBuilderFlags.JAVAC_OPTS) ?: emptyList())
309312
}
310313

311314
with(root.infoBuilder) {

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

+37-3
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,35 @@ internal fun JvmCompilationTask.preProcessingSteps(
111111
return context.execute("expand sources") { expandWithSourceJarSources() }
112112
}
113113

114+
internal fun parseJavacArgsToMap(args: List<String>): Map<String, String> {
115+
val optionsMap = mutableMapOf<String, String>()
116+
var i = 0
117+
118+
while (i < args.size) {
119+
val arg = args[i]
120+
121+
// map option arguments as key value pairs e.g. --source 8 => ("--source", "8")
122+
// map flag arguments as key with value = "true" e.g. map -nowarn => ("-nowarn", "true")
123+
if (arg.startsWith("-")) {
124+
val hasNext = i + 1 < args.size
125+
val nextArg = if (hasNext) args[i + 1] else null
126+
127+
if (hasNext && !nextArg!!.startsWith("-")) {
128+
optionsMap[arg] = nextArg
129+
i += 2
130+
} else {
131+
optionsMap[arg] = "true"
132+
i++
133+
}
134+
} else {
135+
// Ignore non-option arguments
136+
i++
137+
}
138+
}
139+
140+
return optionsMap
141+
}
142+
114143
internal fun encodeMap(options: Map<String, String>): String {
115144
val os = ByteArrayOutputStream()
116145
val oos = ObjectOutputStream(os)
@@ -131,10 +160,15 @@ internal fun JvmCompilationTask.kaptArgs(
131160
plugins: InternalCompilerPlugins,
132161
aptMode: String,
133162
): CompilationArgs {
134-
val javacArgs = mapOf<String, String>(
135-
"-target" to info.toolchainInfo.jvm.jvmTarget,
136-
"-source" to info.toolchainInfo.jvm.jvmTarget,
163+
val javacArgs = parseJavacArgsToMap(
164+
listOf(
165+
"-target",
166+
info.toolchainInfo.jvm.jvmTarget,
167+
"-source",
168+
info.toolchainInfo.jvm.jvmTarget,
169+
).plus(inputs.javacFlagsList),
137170
)
171+
138172
return CompilationArgs().apply {
139173
xFlag("plugin", plugins.kapt.jarPath)
140174

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

+1-1
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)