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

Version kotlinc plugins by major version to handle API changes. #1011

Open
wants to merge 9 commits 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
18 changes: 18 additions & 0 deletions docs/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -567,3 +567,21 @@ versions.use_repository(<a href="#versions.use_repository-name">name</a>, <a hre
| <a id="versions.use_repository-kwargs"></a>kwargs | <p align="center"> - </p> | none |


<a id="versions.get_major"></a>

## versions.get_major

<pre>
versions.get_major(<a href="#versions.get_major-version">version</a>)
</pre>



**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="versions.get_major-version"></a>version | <p align="center"> - </p> | none |


14 changes: 7 additions & 7 deletions kotlin/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ release_archive(

genrule(
name = "stardoc",
srcs = [doc for doc in [
"js",
"jvm",
"lint",
"core",
"repositories.doc",
]],
srcs = [
":js",
":jvm",
":lint",
":core",
":repositories.doc",
],
outs = ["kotlin.md"],
cmd = """
for md in $(SRCS); do
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jar_jar(

jar_jar(
name = "jdeps-gen",
input_jar = "//src/main/kotlin/io/bazel/kotlin/plugin/jdeps:jdeps-gen_deploy.jar",
input_jar = "//src/main/kotlin/io/bazel/kotlin/plugin:jdeps-gen_deploy.jar",
rules = "shade.jarjar",
visibility = ["//visibility:public"],
)
Expand Down
41 changes: 27 additions & 14 deletions src/main/kotlin/bootstrap.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ def _resolve_dep_label(d):
else:
return d

def kt_bootstrap_library(name, srcs, visibility = [], deps = [], neverlink_deps = [], runtime_deps = []):
def kt_bootstrap_library(
name,
srcs,
visibility = [],
deps = [],
neverlink_deps = [],
runtime_deps = [],
kotlinc_repository_name = "com_github_jetbrains_kotlin"):
"""
Simple compilation of a kotlin library using a non-persistent worker. The target is a JavaInfo provider.

Expand All @@ -41,6 +48,7 @@ def kt_bootstrap_library(name, srcs, visibility = [], deps = [], neverlink_deps
deps: the dependenices, the are setup as runtime_deps of the library.
neverlink_deps: deps that won't be linked. These deps are added to the `"for_ide"` target.
"""
kotlinc_repository = "@" + kotlinc_repository_name
jar_label = name + "_jar"
dep_label = name + "_deps"
native.filegroup(
Expand All @@ -50,7 +58,7 @@ def kt_bootstrap_library(name, srcs, visibility = [], deps = [], neverlink_deps
visibility = ["//visibility:private"],
)
command = """
function join_by { local IFS="$$1"; shift; echo "$$*"; }
function join_by {{ local IFS="$$1"; shift; echo "$$*"; }}
case "$$(uname -s)" in
CYGWIN*|MINGW32*|MSYS*)
SEP=";"
Expand All @@ -59,14 +67,14 @@ case "$$(uname -s)" in
SEP=":"
;;
esac
NAME=%s
CP="%s"
ARGS="%s"
NAME={name}
CP="{classpath}"
ARGS="{args}"

CMD="$(JAVA) -Xmx256M -Xms32M -noverify \
-cp $(location @com_github_jetbrains_kotlin//:kotlin-preloader) org.jetbrains.kotlin.preloading.Preloader \
-cp $(location @com_github_jetbrains_kotlin//:kotlin-compiler) org.jetbrains.kotlin.cli.jvm.K2JVMCompiler \
$$CP -d $(@D)/$${NAME}_temp.jar $${ARGS} $(SRCS)"
-cp $(location {kotlinc_repository}//:kotlin-preloader) org.jetbrains.kotlin.preloading.Preloader \
-cp $(location {kotlinc_repository}//:kotlin-compiler) org.jetbrains.kotlin.cli.jvm.K2JVMCompiler \
$$CP -d $(@D)/$${{NAME}}_temp.jar $${{ARGS}} $(SRCS)"

$$CMD

Expand All @@ -82,17 +90,22 @@ esac
$$SJ \
--normalize \
--compression \
--sources $(@D)/$${NAME}_temp.jar \
--sources $(@D)/$${{NAME}}_temp.jar \
--output $(OUTS)

rm $(@D)/$${NAME}_temp.jar
""" % (name, "-cp $$(join_by $$SEP $(locations :%s)) " % dep_label if deps + neverlink_deps else "", " ".join(_BOOTSTRAP_LIB_ARGS))
rm $(@D)/$${{NAME}}_temp.jar
""".format(
name = name,
classpath = "-cp $$(join_by $$SEP $(locations :%s)) " % dep_label if deps + neverlink_deps else "",
args = " ".join(_BOOTSTRAP_LIB_ARGS),
kotlinc_repository = kotlinc_repository,
)
native.genrule(
name = jar_label,
tools = [
"@com_github_jetbrains_kotlin//:home",
"@com_github_jetbrains_kotlin//:kotlin-preloader",
"@com_github_jetbrains_kotlin//:kotlin-compiler",
"%s//:home" % kotlinc_repository,
"%s//:kotlin-preloader" % kotlinc_repository,
"%s//:kotlin-compiler" % kotlinc_repository,
"@bazel_tools//tools/jdk:singlejar",
dep_label,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ kt_bootstrap_library(
visibility = ["//src:__subpackages__"],
deps = [
"//src/main/kotlin/io/bazel/kotlin/builder/utils",
"//src/main/kotlin/io/bazel/kotlin/plugin:jdeps-gen-lib",
"//src/main/kotlin/io/bazel/kotlin/plugin:skip-code-gen-lib",
"//src/main/kotlin/io/bazel/kotlin/plugin/jdeps:jdeps-gen-lib",
"//src/main/protobuf:kotlin_model_java_proto",
"@com_github_jetbrains_kotlin//:kotlin-preloader",
"@kotlin_rules_maven//:com_google_protobuf_protobuf_java",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//src/main/starlark/core/repositories/kotlin:releases.bzl", "KOTLINC_INDEX")
load("//src/main/kotlin:bootstrap.bzl", "kt_bootstrap_library")

kt_bootstrap_library(
Expand All @@ -6,6 +7,8 @@ kt_bootstrap_library(
"*.kt",
"**/*.kt",
]),
# Need for the 1.6 jdeps-gen plugin
kotlinc_repository_name = KOTLINC_INDEX["1.6"].repository_name,
visibility = ["//src:__subpackages__"],
deps = [
"//src/main/protobuf:deps_java_proto",
Expand Down
103 changes: 86 additions & 17 deletions src/main/kotlin/io/bazel/kotlin/plugin/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,104 @@
# 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("//src/main/starlark/core/repositories/kotlin:releases.bzl", "KOTLINC_INDEX")
load("@rules_java//java:defs.bzl", "java_binary")
load("//src/main/kotlin:bootstrap.bzl", "kt_bootstrap_library")
load("//kotlin/internal/utils:generate_jvm_service.bzl", "generate_jvm_service")
load("@com_github_jetbrains_kotlin//:version.bzl", KOTLINC_MAJOR_VERSION = "MAJOR_VERSION")

# The compiler binary, this is co-located in the kotlin compiler classloader.
kt_bootstrap_library(
name = "skip-code-gen-lib",
srcs = glob(["*.kt"]),
# Generate a set of plugins for each major revision of the kotlinc compiler plugin api
[
(
# jdeps generator plugin
kt_bootstrap_library(
name = "jdeps-gen-lib-%s" % version,
srcs = glob(["%s/jdeps/*.kt" % release.repository_name]),
kotlinc_repository_name = release.repository_name,
visibility = ["//src:__subpackages__"],
deps = [
"//src/main/kotlin/io/bazel/kotlin/builder/utils/jars",
"//src/main/protobuf:deps_java_proto",
"@%s//:kotlin-compiler" % release.repository_name,
"@kotlin_rules_maven//:com_google_protobuf_protobuf_java",
],
),

# services to integrate with the plugin.
generate_jvm_service(
name = "jdeps-gen-services-%s" % version,
services = {
"io.bazel.kotlin.plugin.%s.jdeps.JdepsGenComponentRegistrar" % release.repository_name: "org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar",
"io.bazel.kotlin.plugin.%s.jdeps.JdepsGenCommandLineProcessor" % release.repository_name: "org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor",
},
),
# The plugin binary.
java_binary(
name = "jdeps-gen-%s" % version,
visibility = ["//src:__subpackages__"],
runtime_deps = [
":jdeps-gen-lib-%s" % version,
":jdeps-gen-services-%s" % version,
],
),
# SkipCodeGen utility plugin
kt_bootstrap_library(
name = "skip-code-gen-lib-%s" % version,
srcs = glob(["%s/*.kt" % release.repository_name]),
kotlinc_repository_name = release.repository_name,
visibility = ["//src:__subpackages__"],
deps = [
"@%s//:kotlin-compiler" % release.repository_name,
],
),
# services to integrate with the plugin.
generate_jvm_service(
name = "skip-code-gen-services-%s" % version,
services = {
"io.bazel.kotlin.plugin.%s.SkipCodeGen" % release.repository_name: "org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar",
},
),
# The plugin binary.
java_binary(
name = "skip-code-gen-%s" % version,
visibility = ["//src:__subpackages__"],
runtime_deps = [
":skip-code-gen-lib-%s" % version,
":skip-code-gen-services-%s" % version,
],
),
)
for version, release in KOTLINC_INDEX.items()
]

# Deploy jars are not aliased, so repack in a java_binary.
java_binary(
name = "skip-code-gen",
visibility = ["//src:__subpackages__"],
deps = [
"@com_github_jetbrains_kotlin//:kotlin-compiler",
runtime_deps = [
":jdeps-gen-lib-%s" % KOTLINC_MAJOR_VERSION,
":jdeps-gen-services-%s" % KOTLINC_MAJOR_VERSION,
],
)

# services to integrate with the plugin.
generate_jvm_service(
name = "skip-code-gen-services",
services = {
"io.bazel.kotlin.plugin.SkipCodeGen": "org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar",
},
alias(
name = "skip-code-gen-lib",
actual = "skip-code-gen-lib-%s" % KOTLINC_MAJOR_VERSION,
visibility = ["//src:__subpackages__"],
)

# The plugin binary.
# Deploy jars are not aliased, so repack in a java_binary.
java_binary(
name = "skip-code-gen",
name = "jdeps-gen",
visibility = ["//src:__subpackages__"],
runtime_deps = [
":skip-code-gen-lib",
":skip-code-gen-services",
":jdeps-gen-lib-%s" % KOTLINC_MAJOR_VERSION,
":jdeps-gen-services-%s" % KOTLINC_MAJOR_VERSION,
],
)

alias(
name = "jdeps-gen-lib",
actual = "jdeps-gen-lib-%s" % KOTLINC_MAJOR_VERSION,
visibility = ["//src:__subpackages__"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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.
*
*/
@file:Suppress("ktlint:standard:package-name")

package io.bazel.kotlin.plugin.com_github_jetbrains_kotlin_1_6

import com.google.common.base.Preconditions
import com.intellij.mock.MockProject
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.analyzer.AnalysisResult
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.container.ComponentProvider
import org.jetbrains.kotlin.context.ProjectContext
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.BindingTrace
import org.jetbrains.kotlin.resolve.jvm.extensions.AnalysisHandlerExtension

/**
* SkipCodeGen registers an extension to skip code generation. Must be the last compiler plugin.
*/
class SkipCodeGen : ComponentRegistrar {

companion object {
val COMPILER_PLUGIN_ID = "io.bazel.kotlin.plugin.kt_1_8.SkipCodeGen"
}

override fun registerProjectComponents(
project: MockProject,
configuration: CompilerConfiguration,
) {
AnalysisHandlerExtension.registerExtension(
project,
SkipCodeGen,
)
}

/**
* SkipCodeGen ends the compilation
*/
private object SkipCodeGen : AnalysisHandlerExtension {

override fun doAnalysis(
project: Project,
module: ModuleDescriptor,
projectContext: ProjectContext,
files: Collection<KtFile>,
bindingTrace: BindingTrace,
componentProvider: ComponentProvider,
): AnalysisResult? {
return null
}

// analysisCompleted generates the module jvm abi and requests code generation to be skipped.
override fun analysisCompleted(
project: Project,
module: ModuleDescriptor,
bindingTrace: BindingTrace,
files: Collection<KtFile>,
): AnalysisResult? {
// Ensure this is the last plugin, as it will short circuit any other plugin analysisCompleted
// calls.
Preconditions.checkState(
AnalysisHandlerExtension.getInstances(project).last() == this,
"SkipCodeGen must be the last plugin: ${AnalysisHandlerExtension.getInstances(project)}",
)
return AnalysisResult.Companion.success(bindingTrace.bindingContext, module, false)
}
}
}
Loading