diff --git a/kotlin/internal/BUILD b/kotlin/internal/BUILD index 4228a3447..df3bf1180 100644 --- a/kotlin/internal/BUILD +++ b/kotlin/internal/BUILD @@ -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() diff --git a/kotlin/internal/BUILD.release.bazel b/kotlin/internal/BUILD.release.bazel index d03419764..c152368bc 100644 --- a/kotlin/internal/BUILD.release.bazel +++ b/kotlin/internal/BUILD.release.bazel @@ -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", +) diff --git a/kotlin/internal/jvm/compile.bzl b/kotlin/internal/jvm/compile.bzl index 6917fe817..51c18cfa6 100644 --- a/kotlin/internal/jvm/compile.bzl +++ b/kotlin/internal/jvm/compile.bzl @@ -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 ##################################################################################################### @@ -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") @@ -334,6 +334,30 @@ 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) @@ -341,6 +365,9 @@ def _run_kt_builder_action(ctx, rule_kind, toolchains, dirs, srcs, friend, compi 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) @@ -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( diff --git a/kotlin/internal/opts.bzl b/kotlin/internal/opts.bzl new file mode 100644 index 000000000..9bb55080c --- /dev/null +++ b/kotlin/internal/opts.bzl @@ -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()}, +) diff --git a/kotlin/internal/toolchains.bzl b/kotlin/internal/toolchains.bzl index a123e9b74..6333d10d3 100644 --- a/kotlin/internal/toolchains.bzl +++ b/kotlin/internal/toolchains.bzl @@ -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", @@ -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), @@ -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], @@ -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( @@ -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( diff --git a/src/main/kotlin/io/bazel/kotlin/builder/tasks/KotlinBuilder.kt b/src/main/kotlin/io/bazel/kotlin/builder/tasks/KotlinBuilder.kt index 5b1a03e90..59f2d7bb5 100644 --- a/src/main/kotlin/io/bazel/kotlin/builder/tasks/KotlinBuilder.kt +++ b/src/main/kotlin/io/bazel/kotlin/builder/tasks/KotlinBuilder.kt @@ -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) @@ -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) { diff --git a/src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/JavaCompiler.kt b/src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/JavaCompiler.kt index 0b26204f1..16f18a05b 100644 --- a/src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/JavaCompiler.kt +++ b/src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/JavaCompiler.kt @@ -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 -> diff --git a/src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/compilation_task.kt b/src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/compilation_task.kt index 81022ff05..64b461d87 100644 --- a/src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/compilation_task.kt +++ b/src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/compilation_task.kt @@ -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) { @@ -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 { diff --git a/src/main/kotlin/io/bazel/kotlin/builder/utils/ArgMap.kt b/src/main/kotlin/io/bazel/kotlin/builder/utils/ArgMap.kt index fc650e2f5..f48fc60f8 100644 --- a/src/main/kotlin/io/bazel/kotlin/builder/utils/ArgMap.kt +++ b/src/main/kotlin/io/bazel/kotlin/builder/utils/ArgMap.kt @@ -39,7 +39,7 @@ class ArgMap(private val map: Map>) { 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") } } diff --git a/src/main/protobuf/kotlin_model.proto b/src/main/protobuf/kotlin_model.proto index 33e15632e..7824e2f85 100644 --- a/src/main/protobuf/kotlin_model.proto +++ b/src/main/protobuf/kotlin_model.proto @@ -67,7 +67,7 @@ message CompilationTaskInfo { // The name of the module being compiled. string module_name = 4; // Flags to be passed straight through to the compiler. - string passthrough_flags = 5; + repeated string passthrough_flags = 5; // Toolchain info for this build. KotlinToolchainInfo toolchain_info = 6; // Paths to Jars that the kotlin compiler will allow package private access to. @@ -106,28 +106,30 @@ message JvmCompilationTask { } message Inputs { - // The full classpath + // The full classpath repeated string classpath = 1; - // Direct dependencies of the target. + // Direct dependencies of the target. map direct_dependencies = 2; - // Indirect dependencies of the target. + // Indirect dependencies of the target. map indirect_dependencies = 3; - // Partitioned from the builder flags and could have been augmented with sources discovered by expanding the - // source_jars and from annotation processing. - repeated string kotlin_sources = 4; - // Partitioned from the builder flags and could have been augmented with sources discovered by expanding the - // source_jars and from annotation processing. - repeated string java_sources = 5; - // Jars containing additional sources. - repeated string source_jars = 6; - // Annotation processor FQNames - repeated string processors = 7; - // Annotation processor classpath. - repeated string processorpaths = 8; - // Kotlin compiler plugin options - repeated string plugin_options = 9; - // Kotlin compiler plugin classpath - repeated string pluginpaths = 10; + // Partitioned from the builder flags and could have been augmented with sources discovered by expanding the + // source_jars and from annotation processing. + repeated string kotlin_sources = 4; + // Partitioned from the builder flags and could have been augmented with sources discovered by expanding the + // source_jars and from annotation processing. + repeated string java_sources = 5; + // Jars containing additional sources. + repeated string source_jars = 6; + // Annotation processor FQNames + repeated string processors = 7; + // Annotation processor classpath. + repeated string processorpaths = 8; + // Kotlin compiler plugin options + repeated string plugin_options = 9; + // Kotlin compiler plugin classpath + repeated string pluginpaths = 10; + // Java opts to be passed to java compiler + repeated string javac_flags = 11; } CompilationTaskInfo info = 1; diff --git a/src/test/kotlin/io/bazel/kotlin/builder/tasks/jvm/KotlinWorkerTest.kt b/src/test/kotlin/io/bazel/kotlin/builder/tasks/jvm/KotlinWorkerTest.kt index ca6500cdc..cbbd5257a 100644 --- a/src/test/kotlin/io/bazel/kotlin/builder/tasks/jvm/KotlinWorkerTest.kt +++ b/src/test/kotlin/io/bazel/kotlin/builder/tasks/jvm/KotlinWorkerTest.kt @@ -115,7 +115,9 @@ class KotlinWorkerTest { flag(JavaBuilderFlags.TEMPDIR, "tmp") flag(JavaBuilderFlags.SOURCEGEN_DIR, "generated_sources") cp(JavaBuilderFlags.CLASSPATH, KotlinJvmTestBuilder.KOTLIN_STDLIB.singleCompileJar()) - flag(KotlinBuilderFlags.PASSTHROUGH_FLAGS, info.passthroughFlags) + info.passthroughFlagsList.forEach { pf -> + flag(KotlinBuilderFlags.PASSTHROUGH_FLAGS, pf) + } flag(KotlinBuilderFlags.DEBUG, info.debugList.joinToString(",")) flag(KotlinBuilderFlags.JVM_TARGET, info.toolchainInfo.jvm.jvmTarget) init() diff --git a/third_party/BUILD b/third_party/BUILD index 8a2536381..f19a4b2b2 100644 --- a/third_party/BUILD +++ b/third_party/BUILD @@ -69,7 +69,6 @@ java_import( visibility = ["//visibility:public"], ) - bzl_library( name = "java_tools_bzl", srcs = ["@bazel_tools//tools:bzl_srcs"],