Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit 56e2c39

Browse files
committed
Cobble together a semanticdb plugin
1 parent 5c6004c commit 56e2c39

File tree

13 files changed

+173
-3
lines changed

13 files changed

+173
-3
lines changed

3rdparty/maven.bzl

+29-1
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,10 @@ def list_dependencies():
878878
},
879879
"import_args": {
880880
"default_visibility": ["//visibility:public"],
881-
"deps": ["@scala_annex_com_lihaoyi_fansi_2_12"],
881+
"deps": [
882+
"@scala_annex_com_lihaoyi_fansi_2_12",
883+
"@scala_annex_com_lihaoyi_sourcecode_2_12",
884+
],
882885
"jar_sha256": "2e18aa0884870537bf5c562255fc759d4ebe360882b5cb2141b30eda4034c71d",
883886
"jar_urls": [
884887
"http://central.maven.org/maven2/com/lihaoyi/pprint_2.12/0.5.3/pprint_2.12-0.5.3.jar",
@@ -895,6 +898,7 @@ def list_dependencies():
895898
# duplicates in com.lihaoyi:sourcecode_2.12 promoted to 0.1.4
896899
# - ch.epfl.scala:bloop-backend_2.12:1.0.0 wanted version 0.1.4
897900
# - com.lihaoyi:fastparse_2.12:0.4.2 wanted version 0.1.3
901+
# - com.lihaoyi:pprint_2.12:0.5.3 wanted version 0.1.4
898902
{
899903
"bind_args": {
900904
"actual": "@scala_annex_com_lihaoyi_sourcecode_2_12",
@@ -2462,6 +2466,30 @@ def list_dependencies():
24622466
},
24632467
"lang": "java",
24642468
},
2469+
{
2470+
"bind_args": {
2471+
"actual": "@scala_annex_org_scalameta_semanticdb_scalac_2_12_7",
2472+
"name": "jar/scala_annex_org/scalameta/semanticdb_scalac_2_12_7",
2473+
},
2474+
"import_args": {
2475+
"default_visibility": ["//visibility:public"],
2476+
"deps": [
2477+
"@scala_annex_com_lihaoyi_pprint_2_12",
2478+
"@scala_annex_scala_2_12_scala_library//jar",
2479+
],
2480+
"jar_sha256": "62be6eb517912026e8824f95533a1ed4ae7c886bab5d266ee39ca98dd416a4dc",
2481+
"jar_urls": [
2482+
"http://central.maven.org/maven2/org/scalameta/semanticdb-scalac_2.12.7/4.0.0/semanticdb-scalac_2.12.7-4.0.0.jar",
2483+
],
2484+
"licenses": ["notice"],
2485+
"name": "scala_annex_org_scalameta_semanticdb_scalac_2_12_7",
2486+
"srcjar_sha256": "91970337ec5b6cc5ad0ae0162c452f1bb4a77bf1880644235dc8e62fa3dfd694",
2487+
"srcjar_urls": [
2488+
"http://central.maven.org/maven2/org/scalameta/semanticdb-scalac_2.12.7/4.0.0/semanticdb-scalac_2.12.7-4.0.0-sources.jar",
2489+
],
2490+
},
2491+
"lang": "java",
2492+
},
24652493
{
24662494
"bind_args": {
24672495
"actual": "@scala_annex_org_scalatest_scalatest_2_12",

dependencies.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ dependencies:
1919
modules: ["frontend"]
2020
version: "1.0.0"
2121

22+
org.scalameta:
23+
semanticdb-scalac_2.12.7:
24+
lang: java
25+
version: "4.0.0"
26+
2227
net.sourceforge.argparse4j:
2328
argparse4j:
2429
lang: java

rules/scala/private/phases.bzl

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def run_phases(ctx, phases):
1515
sdeps = java_common.merge(_collect(JavaInfo, scala_configuration.runtime_classpath + ctx.attr.deps))
1616
init = struct(
1717
scala_configuration = scala_configuration,
18+
scalacopts = ctx.attr.scalacopts[:],
1819
# todo: probably can remove this from init
1920
sdeps = sdeps,
2021
)
@@ -109,6 +110,8 @@ def phase_compile(ctx, g):
109110

110111
splugins = java_common.merge(_collect(JavaInfo, ctx.attr.plugins + g.init.scala_configuration.global_plugins))
111112

113+
print(splugins.transitive_runtime_deps.to_list())
114+
112115
zinc_configuration = ctx.attr.scala[_ZincConfiguration]
113116

114117
srcs = [file for file in ctx.files.srcs if file.extension.lower() in ["java", "scala"]]
@@ -138,12 +141,13 @@ def phase_compile(ctx, g):
138141

139142
zincs = [dep[_ZincInfo] for dep in ctx.attr.deps if _ZincInfo in dep]
140143

144+
scalacopts = runner.scalacopts + g.init.scalacopts
141145
args = ctx.actions.args()
142146
args.add_all(depset(transitive = [zinc.deps for zinc in zincs]), map_each = _compile_analysis)
143147
args.add("--compiler_bridge", zinc_configuration.compiler_bridge)
144148
args.add_all("--compiler_classpath", g.init.scala_configuration.compiler_classpath)
145149
args.add_all("--classpath", compile_classpath)
146-
args.add_all(runner.scalacopts + ctx.attr.scalacopts, format_each = "--compiler_option=%s")
150+
args.add_all(scalacopts, format_each = "--compiler_option=%s")
147151
args.add_all(javacopts, format_each = "--java_compiler_option=%s")
148152
args.add(ctx.label, format = "--label=%s")
149153
args.add("--main_manifest", mains_file)

rules/semanticdb.scala

Whitespace-only changes.

rules/semanticdb/BUILD

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
load("@rules_scala_annex//rules:scala.bzl", "scala_library")
2+
load(
3+
":private.bzl",
4+
_make_semanticdb_plugin = "make_semanticdb_plugin",
5+
)
6+
7+
scala_library(
8+
name = "lib",
9+
scala = "//external:scala_annex_scala",
10+
)
11+
12+
_make_semanticdb_plugin(
13+
name = "2_12_7",
14+
dep = "@scala_annex_org_scalameta_semanticdb_scalac_2_12_7",
15+
visibility = ["//visibility:public"],
16+
)

rules/semanticdb/private.bzl

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
load(
2+
"@rules_scala_annex//rules:providers.bzl",
3+
_ScalaRulePhase = "ScalaRulePhase",
4+
)
5+
6+
SemanticDB = provider(
7+
doc = "Scala SemanticDB output",
8+
fields = {
9+
"output": "the semanticdb file",
10+
},
11+
)
12+
13+
def _phase_semanticdb_before_compile(ctx, g):
14+
print("semanticdb before compile phase")
15+
g.init.scalacopts.extend([
16+
"-Xplugin-require:semanticdb",
17+
"-Yrangepos",
18+
#"-P:semanticdb:targetroot:~/Desktop/foo",
19+
])
20+
21+
def _phase_semanticdb_after_compile(ctx, g):
22+
print("semanticdb after compile phase")
23+
24+
g.out.providers.append(SemanticDB(
25+
output = None,
26+
))
27+
28+
def _my_plugin_implementation(ctx):
29+
# TODO: write something intelligent that allows us to pass along
30+
# all providers from the underlying dep
31+
return [
32+
ctx.attr.dep[JavaInfo],
33+
_ScalaRulePhase(
34+
phases = [
35+
("-", "compile", "semanticdb", _phase_semanticdb_before_compile),
36+
("+", "compile", "semanticdb", _phase_semanticdb_after_compile),
37+
],
38+
),
39+
]
40+
41+
make_semanticdb_plugin = rule(
42+
attrs = {
43+
"dep": attr.label(
44+
mandatory = True,
45+
providers = [JavaInfo],
46+
),
47+
},
48+
implementation = _my_plugin_implementation,
49+
)

tests/WORKSPACE

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ scala_repository(
3838

3939
scala_repository(
4040
"scala_2_12",
41-
("org.scala-lang", "2.12.6"),
41+
("org.scala-lang", "2.12.7"),
4242
"@compiler_bridge_2_12//:src",
4343
)
4444

tests/semanticdb/BUILD

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
load("@rules_scala_annex//rules:scala.bzl", "configure_scala", "scala_library")
2+
load(":rules.bzl", "my_plugin")
3+
4+
scala_library(
5+
name = "input",
6+
srcs = ["input.scala"],
7+
plugins = [
8+
"@rules_scala_annex//rules/semanticdb:2_12_7",
9+
],
10+
scala = "@scala_2_12",
11+
tags = ["manual"],
12+
)

tests/semanticdb/input.scala

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package anx
2+
3+
object Foo
4+
5+
object Bar

tests/semanticdb/plugin.scala

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package annex
2+
3+
object Plugin

tests/semanticdb/rules.bzl

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
load(
2+
"@rules_scala_annex//rules:providers.bzl",
3+
_ScalaRulePhase = "ScalaRulePhase",
4+
)
5+
6+
def _foo_before_javainfo(ctx, g):
7+
if hasattr(g, "javainfo"):
8+
fail("javainfo shouldn't be in the globals, yet")
9+
10+
def _foo_after_javainfo(ctx, g):
11+
if not hasattr(g, "javainfo"):
12+
fail("javainfo should be in the globals by now")
13+
14+
def _foo_after_coda(ctx, g):
15+
if not hasattr(g, "compile"):
16+
fail("expected to run after compilation")
17+
18+
print("plugin phase success")
19+
20+
def _my_plugin_implementation(ctx):
21+
sdeps = java_common.merge([dep[JavaInfo] for dep in ctx.attr.deps])
22+
return [
23+
sdeps,
24+
_ScalaRulePhase(
25+
phases = [
26+
("-", "javainfo", "foo_before_javainfo", _foo_before_javainfo),
27+
("+", "javainfo", "foo_after_javainfo", _foo_after_javainfo),
28+
("+", "coda", "foo_after_coda", _foo_after_coda),
29+
],
30+
),
31+
]
32+
33+
my_plugin = rule(
34+
attrs = {
35+
"deps": attr.label_list(
36+
mandatory = True,
37+
providers = [JavaInfo],
38+
),
39+
},
40+
implementation = _my_plugin_implementation,
41+
)

tests/semanticdb/test

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash -e
2+
. "$(dirname "$0")"/../common.sh
3+
4+
bazel build :input

tests/semanticdb/usage.scala

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package anx
2+
3+
object Usage

0 commit comments

Comments
 (0)