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 javac options to Kapt javacArguments so the javac_options warn='off' option works for annotation processors that print warnings #1079

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -53,6 +53,7 @@ class KotlinBuilder
) : Flag {
TARGET_LABEL("--target_label"),
CLASSPATH("--classpath"),
JAVAC_OPTS("--javacopts"),
DIRECT_DEPENDENCIES("--direct_dependencies"),
DEPS_ARTIFACTS("--deps_artifacts"),
SOURCES("--sources"),
Expand Down Expand Up @@ -288,6 +289,8 @@ class KotlinBuilder
?.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 @@ -119,6 +119,35 @@ internal fun JvmCompilationTask.preProcessingSteps(
context: CompilationTaskContext,
): JvmCompilationTask = 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 @@ -141,10 +170,15 @@ internal fun JvmCompilationTask.kaptArgs(
aptMode: String,
): CompilationArgs {
val javacArgs =
mapOf<String, String>(
"-target" to info.toolchainInfo.jvm.jvmTarget,
"-source" to info.toolchainInfo.jvm.jvmTarget,
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