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

Commit

Permalink
Cobble together a semanticdb plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
andyscott committed Nov 23, 2018
1 parent 5c6004c commit 56e2c39
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 3 deletions.
30 changes: 29 additions & 1 deletion 3rdparty/maven.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,10 @@ def list_dependencies():
},
"import_args": {
"default_visibility": ["//visibility:public"],
"deps": ["@scala_annex_com_lihaoyi_fansi_2_12"],
"deps": [
"@scala_annex_com_lihaoyi_fansi_2_12",
"@scala_annex_com_lihaoyi_sourcecode_2_12",
],
"jar_sha256": "2e18aa0884870537bf5c562255fc759d4ebe360882b5cb2141b30eda4034c71d",
"jar_urls": [
"http://central.maven.org/maven2/com/lihaoyi/pprint_2.12/0.5.3/pprint_2.12-0.5.3.jar",
Expand All @@ -895,6 +898,7 @@ def list_dependencies():
# duplicates in com.lihaoyi:sourcecode_2.12 promoted to 0.1.4
# - ch.epfl.scala:bloop-backend_2.12:1.0.0 wanted version 0.1.4
# - com.lihaoyi:fastparse_2.12:0.4.2 wanted version 0.1.3
# - com.lihaoyi:pprint_2.12:0.5.3 wanted version 0.1.4
{
"bind_args": {
"actual": "@scala_annex_com_lihaoyi_sourcecode_2_12",
Expand Down Expand Up @@ -2462,6 +2466,30 @@ def list_dependencies():
},
"lang": "java",
},
{
"bind_args": {
"actual": "@scala_annex_org_scalameta_semanticdb_scalac_2_12_7",
"name": "jar/scala_annex_org/scalameta/semanticdb_scalac_2_12_7",
},
"import_args": {
"default_visibility": ["//visibility:public"],
"deps": [
"@scala_annex_com_lihaoyi_pprint_2_12",
"@scala_annex_scala_2_12_scala_library//jar",
],
"jar_sha256": "62be6eb517912026e8824f95533a1ed4ae7c886bab5d266ee39ca98dd416a4dc",
"jar_urls": [
"http://central.maven.org/maven2/org/scalameta/semanticdb-scalac_2.12.7/4.0.0/semanticdb-scalac_2.12.7-4.0.0.jar",
],
"licenses": ["notice"],
"name": "scala_annex_org_scalameta_semanticdb_scalac_2_12_7",
"srcjar_sha256": "91970337ec5b6cc5ad0ae0162c452f1bb4a77bf1880644235dc8e62fa3dfd694",
"srcjar_urls": [
"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",
],
},
"lang": "java",
},
{
"bind_args": {
"actual": "@scala_annex_org_scalatest_scalatest_2_12",
Expand Down
5 changes: 5 additions & 0 deletions dependencies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ dependencies:
modules: ["frontend"]
version: "1.0.0"

org.scalameta:
semanticdb-scalac_2.12.7:
lang: java
version: "4.0.0"

net.sourceforge.argparse4j:
argparse4j:
lang: java
Expand Down
6 changes: 5 additions & 1 deletion rules/scala/private/phases.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def run_phases(ctx, phases):
sdeps = java_common.merge(_collect(JavaInfo, scala_configuration.runtime_classpath + ctx.attr.deps))
init = struct(
scala_configuration = scala_configuration,
scalacopts = ctx.attr.scalacopts[:],
# todo: probably can remove this from init
sdeps = sdeps,
)
Expand Down Expand Up @@ -109,6 +110,8 @@ def phase_compile(ctx, g):

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

print(splugins.transitive_runtime_deps.to_list())

zinc_configuration = ctx.attr.scala[_ZincConfiguration]

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

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

scalacopts = runner.scalacopts + g.init.scalacopts
args = ctx.actions.args()
args.add_all(depset(transitive = [zinc.deps for zinc in zincs]), map_each = _compile_analysis)
args.add("--compiler_bridge", zinc_configuration.compiler_bridge)
args.add_all("--compiler_classpath", g.init.scala_configuration.compiler_classpath)
args.add_all("--classpath", compile_classpath)
args.add_all(runner.scalacopts + ctx.attr.scalacopts, format_each = "--compiler_option=%s")
args.add_all(scalacopts, format_each = "--compiler_option=%s")
args.add_all(javacopts, format_each = "--java_compiler_option=%s")
args.add(ctx.label, format = "--label=%s")
args.add("--main_manifest", mains_file)
Expand Down
Empty file added rules/semanticdb.scala
Empty file.
16 changes: 16 additions & 0 deletions rules/semanticdb/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("@rules_scala_annex//rules:scala.bzl", "scala_library")
load(
":private.bzl",
_make_semanticdb_plugin = "make_semanticdb_plugin",
)

scala_library(
name = "lib",
scala = "//external:scala_annex_scala",
)

_make_semanticdb_plugin(
name = "2_12_7",
dep = "@scala_annex_org_scalameta_semanticdb_scalac_2_12_7",
visibility = ["//visibility:public"],
)
49 changes: 49 additions & 0 deletions rules/semanticdb/private.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
load(
"@rules_scala_annex//rules:providers.bzl",
_ScalaRulePhase = "ScalaRulePhase",
)

SemanticDB = provider(
doc = "Scala SemanticDB output",
fields = {
"output": "the semanticdb file",
},
)

def _phase_semanticdb_before_compile(ctx, g):
print("semanticdb before compile phase")
g.init.scalacopts.extend([
"-Xplugin-require:semanticdb",
"-Yrangepos",
#"-P:semanticdb:targetroot:~/Desktop/foo",
])

def _phase_semanticdb_after_compile(ctx, g):
print("semanticdb after compile phase")

g.out.providers.append(SemanticDB(
output = None,
))

def _my_plugin_implementation(ctx):
# TODO: write something intelligent that allows us to pass along
# all providers from the underlying dep
return [
ctx.attr.dep[JavaInfo],
_ScalaRulePhase(
phases = [
("-", "compile", "semanticdb", _phase_semanticdb_before_compile),
("+", "compile", "semanticdb", _phase_semanticdb_after_compile),
],
),
]

make_semanticdb_plugin = rule(
attrs = {
"dep": attr.label(
mandatory = True,
providers = [JavaInfo],
),
},
implementation = _my_plugin_implementation,
)
2 changes: 1 addition & 1 deletion tests/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ scala_repository(

scala_repository(
"scala_2_12",
("org.scala-lang", "2.12.6"),
("org.scala-lang", "2.12.7"),
"@compiler_bridge_2_12//:src",
)

Expand Down
12 changes: 12 additions & 0 deletions tests/semanticdb/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@rules_scala_annex//rules:scala.bzl", "configure_scala", "scala_library")
load(":rules.bzl", "my_plugin")

scala_library(
name = "input",
srcs = ["input.scala"],
plugins = [
"@rules_scala_annex//rules/semanticdb:2_12_7",
],
scala = "@scala_2_12",
tags = ["manual"],
)
5 changes: 5 additions & 0 deletions tests/semanticdb/input.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package anx

object Foo

object Bar
3 changes: 3 additions & 0 deletions tests/semanticdb/plugin.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package annex

object Plugin
41 changes: 41 additions & 0 deletions tests/semanticdb/rules.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
load(
"@rules_scala_annex//rules:providers.bzl",
_ScalaRulePhase = "ScalaRulePhase",
)

def _foo_before_javainfo(ctx, g):
if hasattr(g, "javainfo"):
fail("javainfo shouldn't be in the globals, yet")

def _foo_after_javainfo(ctx, g):
if not hasattr(g, "javainfo"):
fail("javainfo should be in the globals by now")

def _foo_after_coda(ctx, g):
if not hasattr(g, "compile"):
fail("expected to run after compilation")

print("plugin phase success")

def _my_plugin_implementation(ctx):
sdeps = java_common.merge([dep[JavaInfo] for dep in ctx.attr.deps])
return [
sdeps,
_ScalaRulePhase(
phases = [
("-", "javainfo", "foo_before_javainfo", _foo_before_javainfo),
("+", "javainfo", "foo_after_javainfo", _foo_after_javainfo),
("+", "coda", "foo_after_coda", _foo_after_coda),
],
),
]

my_plugin = rule(
attrs = {
"deps": attr.label_list(
mandatory = True,
providers = [JavaInfo],
),
},
implementation = _my_plugin_implementation,
)
4 changes: 4 additions & 0 deletions tests/semanticdb/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash -e
. "$(dirname "$0")"/../common.sh

bazel build :input
3 changes: 3 additions & 0 deletions tests/semanticdb/usage.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package anx

object Usage

0 comments on commit 56e2c39

Please sign in to comment.