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

Add option rules for kotlinc and javac. #383

Merged
merged 4 commits into from
Nov 10, 2020
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
10 changes: 9 additions & 1 deletion kotlin/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("//kotlin/internal:opts.bzl", "kt_javac_options", "kt_kotlinc_options")
load("//kotlin/internal:toolchains.bzl", "kt_configure_toolchains")
load("//kotlin/internal/utils:packager.bzl", "release_archive")
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

kt_kotlinc_options(
name = "default_kotlinc_options",
)

kt_javac_options(
name = "default_javac_options",
)

# Configures the toolchains
kt_configure_toolchains()

Expand Down
9 changes: 9 additions & 0 deletions kotlin/internal/BUILD.release.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("//kotlin/internal:opts.bzl", "kt_javac_options", "kt_kotlinc_options")
load("//kotlin/internal:toolchains.bzl", "kt_configure_toolchains")

kt_configure_toolchains()

kt_kotlinc_options(
name = "default_kotlinc_options",
)

kt_javac_options(
name = "default_javac_options",
)
35 changes: 31 additions & 4 deletions kotlin/internal/jvm/compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ load(
"//kotlin/internal/utils:utils.bzl",
_utils = "utils",
)

load(
"@bazel_tools//tools/jdk:toolchain_utils.bzl",
"find_java_runtime_toolchain",
"find_java_toolchain"
"find_java_toolchain",
)

# INTERNAL ACTIONS #####################################################################################################
Expand Down Expand Up @@ -268,8 +267,9 @@ def kt_jvm_compile_action(ctx, rule_kind, output_jar, compile_jar):
ctx,
source_files = srcs.java,
output = java_compile_jar,
deps = compile_deps.deps + [JavaInfo(compile_jar=kt_compile_jar, output_jar=kt_compile_jar)],
deps = compile_deps.deps + [JavaInfo(compile_jar = kt_compile_jar, output_jar = kt_compile_jar)],
java_toolchain = toolchains.java,
javac_opts = toolchains.kt.javac_opts,
host_javabase = toolchains.java_runtime,
)
compile_jar = ctx.actions.declare_file(ctx.label.name + ".abi.jar")
Expand Down Expand Up @@ -334,13 +334,40 @@ def kt_jvm_compile_action(ctx, rule_kind, output_jar, compile_jar):
),
)

def _kotlinc_options_provider_to_flags(opts):
if not opts:
return ""
flags = []
if not opts.warn:
flags.append("-nowarn")
if opts.x_use_experimental:
flags.append("-Xuse-experimental=kotlin.Experimental")
return flags

def _javac_options_provider_to_flags(opts):
if not opts:
return ""
flags = []
if not opts.warn:
flags.append("-nowarn")
if opts.x_ep_disable_all_checks:
flags.append("-XepDisableAllChecks")
if opts.x_lint:
flags.extend(["-Xlint:%s" % check for check in opts.x_lint])
if opts.xd_suppress_notes:
flags.append("-XDsuppressNotes")
return flags

def _run_kt_builder_action(ctx, rule_kind, toolchains, dirs, srcs, friend, compile_deps, annotation_processors, transitive_runtime_jars, plugins, outputs, mnemonic = "KotlinCompile"):
"""Creates a KotlinBuilder action invocation."""
args = _utils.init_args(ctx, rule_kind, friend.module_name)

for f, path in dirs.items() + outputs.items():
args.add("--" + f, path)

args.add_all("--kotlin_passthrough_flags", _kotlinc_options_provider_to_flags(toolchains.kt.kotlinc_options))
args.add_all("--javacopts", _javac_options_provider_to_flags(toolchains.kt.javac_options))

args.add_all("--classpath", compile_deps.compile_jars)
args.add_all("--sources", srcs.all_srcs, omit_if_empty = True)
args.add_all("--source_jars", srcs.src_jars, omit_if_empty = True)
Expand Down Expand Up @@ -378,7 +405,7 @@ def _run_kt_builder_action(ctx, rule_kind, toolchains, dirs, srcs, friend, compi
len(srcs.kt),
len(srcs.java),
len(srcs.src_jars),
ctx.var.get("TARGET_CPU", "UNKNOWN CPU")
ctx.var.get("TARGET_CPU", "UNKNOWN CPU"),
)

tools, input_manifests = ctx.resolve_tools(
Expand Down
106 changes: 106 additions & 0 deletions kotlin/internal/opts.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Copyright 2020 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

_KOPTS = {
"warn": struct(
args = dict(
default = True,
doc = "Enable or disable compiler warnings.",
),
type = attr.bool,
),
"x_use_experimental": struct(
args = dict(
default = True,
doc = "Allow the experimental language features.",
),
type = attr.bool,
),
}

KotlincOptions = provider(
fields = {
name: o.args["doc"]
for name, o in _KOPTS.items()
},
)

def _kotlinc_options_impl(ctx):
return struct(
providers = [
KotlincOptions(**{n: getattr(ctx, n, None) for n in _KOPTS}),
],
)

kt_kotlinc_options = rule(
implementation = _kotlinc_options_impl,
doc = "Define kotlin compiler options.",
provides = [KotlincOptions],
attrs = {
n: o.type(**o.args)
for n, o in _KOPTS.items()
},
)

_JOPTS = {
"warn": struct(
args = dict(
default = True,
doc = "Enable or disable compiler warnings.",
),
type = attr.bool,
),
"x_ep_disable_all_checks": struct(
args = dict(
default = False,
doc = "See javac -XepDisableAllChecks documentation",
),
type = attr.bool,
),
"x_lint": struct(
args = dict(
default = [],
doc = "See javac -Xlint: documentation",
),
type = attr.string_list,
),
"xd_suppress_notes": struct(
args = dict(
default = False,
doc = "See javac -XDsuppressNotes documentation",
),
type = attr.bool,
),
}

JavacOptions = provider(
fields = {
name: o.args["doc"]
for name, o in _JOPTS.items()
},
)

def _javac_options_impl(ctx):
return struct(
providers = [
JavacOptions(**{n: getattr(ctx, n, None) for n in _JOPTS}),
],
)

kt_javac_options = rule(
implementation = _javac_options_impl,
doc = "Define java compiler options for kt_jvm_* rules with java sources.",
provides = [JavacOptions],
attrs = {n: o.type(**o.args) for n, o in _JOPTS.items()},
)
30 changes: 25 additions & 5 deletions kotlin/internal/toolchains.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load(
"//kotlin/internal:opts.bzl",
"JavacOptions",
"KotlincOptions",
)
load(
"//kotlin/internal:defs.bzl",
_KT_COMPILER_REPO = "KT_COMPILER_REPO",
Expand Down Expand Up @@ -69,6 +73,8 @@ def _kotlin_toolchain_impl(ctx):
jvm_stdlibs = java_common.merge(compile_time_providers + runtime_providers),
js_stdlibs = ctx.attr.js_stdlibs,
experimental_use_abi_jars = ctx.attr.experimental_use_abi_jars,
javac_options = ctx.attr.javac_options[JavacOptions] if ctx.attr.javac_options else None,
kotlinc_options = ctx.attr.kotlinc_options[KotlincOptions] if ctx.attr.kotlinc_options else None,
)
return [
platform_common.ToolchainInfo(**toolchain),
Expand Down Expand Up @@ -161,10 +167,20 @@ _kt_toolchain = rule(
],
providers = [_KtJsInfo],
),
"experimental_use_abi_jars" : attr.bool(
doc="Compile using abi jars",
"experimental_use_abi_jars": attr.bool(
doc = "Compile using abi jars",
default = False,
)
),
"javac_options": attr.label(
doc = "Compiler options for javac",
providers = [JavacOptions],
default = Label("//kotlin/internal:default_javac_options"),
),
"kotlinc_options": attr.label(
doc = "Compiler options for kotlinc",
providers = [KotlincOptions],
default = Label("//kotlin/internal:default_kotlinc_options"),
),
},
implementation = _kotlin_toolchain_impl,
provides = [platform_common.ToolchainInfo],
Expand All @@ -179,7 +195,9 @@ def define_kt_toolchain(
language_version = None,
api_version = None,
jvm_target = None,
experimental_use_abi_jars = False):
experimental_use_abi_jars = False,
javac_options = None,
kotlinc_options = None):
"""Define the Kotlin toolchain."""
impl_name = name + "_impl"
_kt_toolchain(
Expand All @@ -201,6 +219,8 @@ def define_kt_toolchain(
"@io_bazel_rules_kotlin//kotlin/internal:noexperimental_use_abi_jars": False,
"//conditions:default": experimental_use_abi_jars,
}),
javac_options = javac_options,
kotlinc_options = kotlinc_options,
visibility = ["//visibility:public"],
)
native.toolchain(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ class KotlinBuilder @Inject internal constructor(
moduleName = argMap.mandatorySingle(KotlinBuilderFlags.MODULE_NAME).also {
check(it.isNotBlank()) { "--kotlin_module_name should not be blank" }
}
passthroughFlags = argMap.optionalSingle(KotlinBuilderFlags.PASSTHROUGH_FLAGS)
addAllPassthroughFlags(argMap.optional(KotlinBuilderFlags.PASSTHROUGH_FLAGS) ?: emptyList())

argMap.optional(KotlinBuilderFlags.FRIEND_PATHS)?.let(::addAllFriendPaths)
toolchainInfoBuilder.commonBuilder.apiVersion =
argMap.mandatorySingle(KotlinBuilderFlags.API_VERSION)
Expand Down Expand Up @@ -270,6 +271,8 @@ class KotlinBuilder @Inject internal constructor(
argMap.optional(JavaBuilderFlags.SOURCE_JARS)?.also {
addAllSourceJars(it)
}

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

with(root.infoBuilder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ internal class JavaCompiler @Inject constructor(
"-source", command.info.toolchainInfo.jvm.jvmTarget,
"-target", command.info.toolchainInfo.jvm.jvmTarget
)
it.addAll(command.inputs.javacFlagsList)
it.addAll(i.javaSourcesList)
}
context.executeCompilerTask(args, { a, pw ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fun JvmCompilationTask.codeGenArgs(): CompilationArgs = CompilationArgs()
"-Xfriend-paths=${it.joinToString(X_FRIENDS_PATH_SEPARATOR)}"
}
.flag("-d", directories.classes)
.given(info.passthroughFlags).notEmpty { flags -> values(flags.split(" ")) }
.values(info.passthroughFlagsList)

fun JvmCompilationTask.baseArgs(): CompilationArgs = CompilationArgs()
.flag("-cp").absolutePaths(inputs.classpathList) {
Expand All @@ -57,9 +57,9 @@ fun JvmCompilationTask.baseArgs(): CompilationArgs = CompilationArgs()
internal fun pluginArgs(context: JvmCompilationTask, args: CompilationArgs): CompilationArgs = args
.let {
var r = it
val pluginsPath = context.inputs.pluginpathsList.forEach { pluginPath ->
r = r.value("-Xplugin=${pluginPath}")
}
context.inputs.pluginpathsList.forEach { pluginPath ->
r = r.value("-Xplugin=${pluginPath}")
}
r
}
.let {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/io/bazel/kotlin/builder/utils/ArgMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ArgMap(private val map: Map<String, List<String>>) {
when (it.size) {
0 -> throw IllegalArgumentException("$key did not have a value")
1 -> it[0]
else -> throw IllegalArgumentException("$key should have a single value")
else -> throw IllegalArgumentException("$key should have a single value: $it")
}
}

Expand Down
Loading