Skip to content

Commit 4e5b5cb

Browse files
Bencodesrestingbull
authored andcommitted
Revert "[kt_compiler_plugin] Add kt_plugin_cfg for reusable and composable op…" (#1131)
This reverts commit b9380cd.
1 parent f4de4ab commit 4e5b5cb

File tree

21 files changed

+102
-870
lines changed

21 files changed

+102
-870
lines changed

examples/ksp/BUILD

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ load("@rules_java//java:defs.bzl", "java_binary", "java_plugin")
1414
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
17-
load("@rules_kotlin//kotlin:core.bzl", "define_kt_toolchain", "kt_compiler_plugin", "kt_ksp_plugin", "kt_plugin_cfg")
17+
load("@rules_kotlin//kotlin:core.bzl", "define_kt_toolchain", "kt_ksp_plugin")
1818
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
1919

2020
package(default_visibility = ["//visibility:public"])
@@ -81,73 +81,3 @@ build_test(
8181
"//:coffee_app_deploy.jar",
8282
],
8383
)
84-
85-
kt_compiler_plugin(
86-
name = "ksp",
87-
compile_phase = True,
88-
id = "com.google.devtools.ksp.symbol-processing",
89-
options = {
90-
"apclasspath": "{classpath}",
91-
# projectBaseDir shouldn't matter because incremental is disabled
92-
"projectBaseDir": "{temp}",
93-
# Disable incremental mode
94-
"incremental": "false",
95-
# Directory where class files are written to. Files written to this directory are class
96-
# files being written directly from the annotation processor, not Kotlinc
97-
"classOutputDir": "{generatedClasses}",
98-
# Directory where generated Java sources files are written to
99-
"javaOutputDir": "{generatedSources}",
100-
# Directory where generated Kotlin sources files are written to
101-
"kotlinOutputDir": "{generatedSources}",
102-
# Directory where META-INF data is written to. This might not be the most ideal place to
103-
# write this. Maybe just directly to the classes directory?
104-
"resourceOutputDir": "{generatedSources}",
105-
# TODO(bencodes) Not sure what this directory is yet.
106-
"kspOutputDir": "{temp}",
107-
# Directory to write KSP caches. Shouldn't matter because incremental is disabled
108-
"cachesDir": "{temp}",
109-
# Include in compilation as an example. This should be processed in the stubs phase.
110-
"withCompilation": "true",
111-
# Set returnOkOnError to false because we want to fail the build if there are any errors
112-
"returnOkOnError": "false",
113-
"allWarningsAsErrors": "false",
114-
},
115-
deps = [
116-
"@rules_kotlin//kotlin/compiler:symbol-processing-api",
117-
"@rules_kotlin//kotlin/compiler:symbol-processing-cmdline",
118-
],
119-
)
120-
121-
kt_plugin_cfg(
122-
name = "ksp_moshi",
123-
options = {
124-
},
125-
plugin = ":ksp",
126-
deps = [
127-
"@maven//:com_squareup_moshi_moshi",
128-
"@maven//:com_squareup_moshi_moshi_kotlin",
129-
"@maven//:com_squareup_moshi_moshi_kotlin_codegen",
130-
],
131-
)
132-
133-
kt_jvm_library(
134-
name = "raw_ksp_coffee_app_lib",
135-
srcs = ["CoffeeAppModel.kt"],
136-
plugins = [
137-
"//:ksp",
138-
"//:ksp_moshi",
139-
],
140-
deps = [
141-
"@maven//:com_google_auto_service_auto_service_annotations",
142-
"@maven//:com_google_auto_value_auto_value_annotations",
143-
"@maven//:com_squareup_moshi_moshi",
144-
"@maven//:com_squareup_moshi_moshi_kotlin",
145-
],
146-
)
147-
148-
build_test(
149-
name = "raw_ksp_lib_test",
150-
targets = [
151-
"//:raw_ksp_coffee_app_lib",
152-
],
153-
)

examples/ksp/MODULE.bazel

Lines changed: 0 additions & 6 deletions
This file was deleted.

kotlin/core.bzl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ load(
1212
"//kotlin/internal/jvm:jvm.bzl",
1313
_kt_compiler_plugin = "kt_compiler_plugin",
1414
_kt_ksp_plugin = "kt_ksp_plugin",
15-
_kt_plugin_cfg = "kt_plugin_cfg",
1615
)
1716

1817
define_kt_toolchain = _define_kt_toolchain
@@ -21,4 +20,3 @@ kt_javac_options = _kt_javac_options
2120
kt_kotlinc_options = _kt_kotlinc_options
2221
kt_compiler_plugin = _kt_compiler_plugin
2322
kt_ksp_plugin = _kt_ksp_plugin
24-
kt_plugin_cfg = _kt_plugin_cfg

kotlin/internal/compiler_plugins.bzl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
load(
2+
"//kotlin/internal:defs.bzl",
3+
_KtCompilerPluginInfo = "KtCompilerPluginInfo",
4+
)
5+
6+
def plugins_to_classpaths(providers_list):
7+
flattened_files = []
8+
for providers in providers_list:
9+
if _KtCompilerPluginInfo in providers:
10+
provider = providers[_KtCompilerPluginInfo]
11+
for e in provider.classpath:
12+
flattened_files.append(e)
13+
return flattened_files
14+
15+
def plugins_to_options(providers_list):
16+
kt_compiler_plugin_providers = [providers[_KtCompilerPluginInfo] for providers in providers_list if _KtCompilerPluginInfo in providers]
17+
flattened_options = []
18+
for provider in kt_compiler_plugin_providers:
19+
for option in provider.options:
20+
flattened_options.append("%s:%s" % (option.id, option.value))
21+
return flattened_options

kotlin/internal/defs.bzl

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.#
14-
load(
15-
"//src/main/starlark/core/plugin:providers.bzl",
16-
_KspPluginInfo = "KspPluginInfo",
17-
_KtCompilerPluginInfo = "KtCompilerPluginInfo",
18-
_KtCompilerPluginOption = "KtCompilerPluginOption",
19-
_KtPluginConfiguration = "KtPluginConfiguration",
20-
)
2114

2215
# The Kotlin Toolchain type.
2316
TOOLCHAIN_TYPE = "%s" % Label("//kotlin/internal:kt_toolchain_type")
@@ -57,10 +50,18 @@ KtJsInfo = provider(
5750
},
5851
)
5952

60-
KtCompilerPluginInfo = _KtCompilerPluginInfo
61-
62-
KspPluginInfo = _KspPluginInfo
63-
64-
KtCompilerPluginOption = _KtCompilerPluginOption
53+
KtCompilerPluginInfo = provider(
54+
fields = {
55+
"plugin_jars": "List of plugin jars.",
56+
"classpath": "The kotlin compiler plugin classpath.",
57+
"stubs": "Run this plugin during kapt stub generation.",
58+
"compile": "Run this plugin during koltinc compilation.",
59+
"options": "List of plugin options, represented as structs with an id and a value field, to be passed to the compiler",
60+
},
61+
)
6562

66-
KtPluginConfiguration = _KtPluginConfiguration
63+
KspPluginInfo = provider(
64+
fields = {
65+
"plugins": "List of JavaPLuginInfo providers for the plugins to run with KSP",
66+
},
67+
)

kotlin/internal/jvm/compile.bzl

Lines changed: 37 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
load(
2+
"@bazel_tools//tools/jdk:toolchain_utils.bzl",
3+
"find_java_runtime_toolchain",
4+
"find_java_toolchain",
5+
)
6+
17
# Copyright 2018 The Bazel Authors. All rights reserved.
28
#
39
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -11,21 +17,11 @@
1117
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1218
# See the License for the specific language governing permissions and
1319
# limitations under the License.
14-
load(
15-
"@bazel_tools//tools/jdk:toolchain_utils.bzl",
16-
"find_java_runtime_toolchain",
17-
"find_java_toolchain",
18-
)
19-
load(
20-
"@rules_java//java:defs.bzl",
21-
"JavaInfo",
22-
"java_common",
23-
)
20+
load("@rules_java//java:defs.bzl", "JavaInfo", "java_common")
2421
load(
2522
"//kotlin/internal:defs.bzl",
2623
_KtCompilerPluginInfo = "KtCompilerPluginInfo",
2724
_KtJvmInfo = "KtJvmInfo",
28-
_KtPluginConfiguration = "KtPluginConfiguration",
2925
_TOOLCHAIN_TYPE = "TOOLCHAIN_TYPE",
3026
)
3127
load(
@@ -182,83 +178,9 @@ def _adjust_resources_path(path, resource_strip_prefix):
182178
else:
183179
return _adjust_resources_path_by_default_prefixes(path)
184180

185-
def _format_compile_plugin_options(o):
186-
"""Format compiler option into id:value for cmd line."""
187-
return [
188-
"%s:%s" % (o.id, o.value),
189-
]
190-
191-
def _new_plugins_from(targets):
192-
"""Returns a struct containing the plugin metadata for the given targets.
193-
194-
Args:
195-
targets: A list of targets.
196-
Returns:
197-
A struct containing the plugins for the given targets in the format:
198-
{
199-
stubs_phase = {
200-
classpath = depset,
201-
options= List[KtCompilerPluginOption],
202-
),
203-
compile = {
204-
classpath = depset,
205-
options = List[KtCompilerPluginOption],
206-
},
207-
}
208-
"""
209-
210-
all_plugins = {}
211-
plugins_without_phase = []
212-
for t in targets:
213-
if _KtCompilerPluginInfo not in t:
214-
continue
215-
plugin = t[_KtCompilerPluginInfo]
216-
if not (plugin.stubs or plugin.compile):
217-
plugins_without_phase.append("%s: %s" % (t.label, plugin.id))
218-
if plugin.id in all_plugins:
219-
# This need a more robust error messaging.
220-
fail("has multiple plugins with the same id: %s." % plugin.id)
221-
all_plugins[plugin.id] = plugin
222-
223-
if plugins_without_phase:
224-
fail("has plugin without a phase defined: %s" % cfgs_without_plugin)
225-
226-
all_plugin_cfgs = {}
227-
cfgs_without_plugin = []
228-
for t in targets:
229-
if _KtPluginConfiguration not in t:
230-
continue
231-
cfg = t[_KtPluginConfiguration]
232-
if cfg.id not in all_plugins:
233-
cfgs_without_plugin.append("%s: %s" % (t.label, cfg.id))
234-
all_plugin_cfgs[cfg.id] = cfg
235-
236-
if cfgs_without_plugin:
237-
fail("has plugin configurations without corresponding plugins: %s" % cfgs_without_plugin)
238-
239-
return struct(
240-
stubs_phase = _new_plugin_from(all_plugin_cfgs, [p for p in all_plugins.values() if p.stubs]),
241-
compile_phase = _new_plugin_from(all_plugin_cfgs, [p for p in all_plugins.values() if p.compile]),
242-
)
243-
244-
def _new_plugin_from(all_cfgs, plugins_for_phase):
245-
classpath = []
246-
data = []
247-
options = []
248-
for p in plugins_for_phase:
249-
classpath.append(p.classpath)
250-
options.extend(p.options)
251-
if p.id in all_cfgs:
252-
cfg = all_cfgs[p.id]
253-
classpath.append(cfg.classpath)
254-
data.append(cfg.data)
255-
options.extend(cfg.options)
256-
257-
return struct(
258-
classpath = depset(transitive = classpath),
259-
data = depset(transitive = data),
260-
options = options,
261-
)
181+
def _format_compile_plugin_options(options):
182+
"""Format options into id:value for cmd line."""
183+
return ["%s:%s" % (o.id, o.value) for o in options]
262184

263185
# INTERNAL ACTIONS #####################################################################################################
264186
def _fold_jars_action(ctx, rule_kind, toolchains, output_jar, input_jars, action_type = ""):
@@ -496,28 +418,49 @@ def _run_kt_builder_action(
496418
uniquify = True,
497419
)
498420

421+
compiler_plugins = [
422+
p[_KtCompilerPluginInfo]
423+
for p in plugins
424+
if _KtCompilerPluginInfo in p and p[_KtCompilerPluginInfo]
425+
]
426+
427+
stubs_compiler_plugins = [
428+
kcp
429+
for kcp in compiler_plugins
430+
if kcp.stubs
431+
]
432+
433+
compiler_compiler_plugins = [
434+
ccp
435+
for ccp in compiler_plugins
436+
if ccp.compile
437+
]
438+
439+
if compiler_plugins and not (stubs_compiler_plugins or compiler_compiler_plugins):
440+
fail("plugins but no phase plugins: %s" % compiler_plugins)
441+
499442
args.add_all(
500443
"--stubs_plugin_classpath",
501-
plugins.stubs_phase.classpath,
444+
depset(transitive = [p.classpath for p in stubs_compiler_plugins]),
502445
omit_if_empty = True,
503446
)
504447

505448
args.add_all(
506449
"--stubs_plugin_options",
507-
plugins.stubs_phase.options,
450+
[p.options for p in stubs_compiler_plugins],
508451
map_each = _format_compile_plugin_options,
509452
omit_if_empty = True,
510453
)
511454

512455
args.add_all(
513456
"--compiler_plugin_classpath",
514-
plugins.compile_phase.classpath,
457+
depset(transitive = [p.classpath for p in compiler_compiler_plugins]),
515458
omit_if_empty = True,
516459
)
517460

518461
args.add_all(
519462
"--compiler_plugin_options",
520-
plugins.compile_phase.options,
463+
[p.options for p in compiler_compiler_plugins],
521464
map_each = _format_compile_plugin_options,
522465
omit_if_empty = True,
523466
)
@@ -536,13 +479,7 @@ def _run_kt_builder_action(
536479
mnemonic = mnemonic,
537480
inputs = depset(
538481
srcs.all_srcs + srcs.src_jars + generated_src_jars,
539-
transitive = [
540-
compile_deps.compile_jars,
541-
transitive_runtime_jars,
542-
deps_artifacts,
543-
plugins.stubs_phase.classpath,
544-
plugins.compile_phase.classpath,
545-
],
482+
transitive = [compile_deps.compile_jars, transitive_runtime_jars, deps_artifacts] + [p.classpath for p in compiler_plugins],
546483
),
547484
tools = [
548485
toolchains.kt.kotlinbuilder.files_to_run,
@@ -584,12 +521,10 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind):
584521
deps = ctx.attr.deps,
585522
runtime_deps = ctx.attr.runtime_deps,
586523
)
587-
588524
annotation_processors = _plugin_mappers.targets_to_annotation_processors(ctx.attr.plugins + ctx.attr.deps)
589525
ksp_annotation_processors = _plugin_mappers.targets_to_ksp_annotation_processors(ctx.attr.plugins + ctx.attr.deps)
590526
transitive_runtime_jars = _plugin_mappers.targets_to_transitive_runtime_jars(ctx.attr.plugins + ctx.attr.deps)
591-
plugins = _new_plugins_from(ctx.attr.plugins + _exported_plugins(deps = ctx.attr.deps))
592-
527+
plugins = ctx.attr.plugins + _exported_plugins(deps = ctx.attr.deps)
593528
deps_artifacts = _deps_artifacts(toolchains, ctx.attr.deps + associates.targets)
594529

595530
generated_src_jars = []

0 commit comments

Comments
 (0)