-
-
Notifications
You must be signed in to change notification settings - Fork 287
WIP: use scala sculpt to automatically break targets #892
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b589549
WIP: use scala sculpt to automatically break targets
johnynek dcccd86
Add basic dag printer
johnynek b2bb7a2
Add bazel generation
johnynek d0af232
add the aspect
johnynek af3cf0c
add plugin support, lint
johnynek 0f753d6
some fixes using it on bosatsu
johnynek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
load(":sculpt.bzl", "sculpt_json") | ||
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_binary", "scala_library") | ||
|
||
sculpt_json( | ||
name = "example", | ||
srcs = [ | ||
"File0.scala", | ||
"File1.scala", | ||
"File2.scala", | ||
], | ||
deps = [ | ||
"@io_bazel_rules_scala_scala_compiler", | ||
"@io_bazel_rules_scala_scala_library", | ||
"@io_bazel_rules_scala_scala_reflect", | ||
], | ||
) | ||
|
||
scala_library( | ||
name = "example0_scala", | ||
srcs = ["File0.scala"], | ||
) | ||
|
||
scala_library( | ||
name = "example_scala", | ||
srcs = [ | ||
"File1.scala", | ||
"File2.scala", | ||
], | ||
exports = [":example0_scala"], | ||
deps = [":example0_scala"], | ||
) | ||
|
||
java_binary( | ||
name = "scalac_runner", | ||
main_class = "scala.tools.nsc.Main", | ||
visibility = ["//visibility:public"], | ||
runtime_deps = [ | ||
"@io_bazel_rules_scala_scala_compiler", | ||
"@io_bazel_rules_scala_scala_library", | ||
"@io_bazel_rules_scala_scala_reflect", | ||
"@io_bazel_rules_scala_spray_json", | ||
], | ||
) | ||
|
||
scala_library( | ||
name = "dagify", | ||
srcs = ["Dagify.scala"], | ||
) | ||
|
||
scala_library( | ||
name = "bazelgen", | ||
srcs = ["BazelGen.scala"], | ||
) | ||
|
||
scala_library( | ||
name = "sculpt_processor", | ||
srcs = ["SculptProcessor.scala"], | ||
deps = [ | ||
":bazelgen", | ||
":dagify", | ||
"@io_bazel_rules_scala_spray_json", | ||
"@sculpt", | ||
], | ||
) | ||
|
||
java_binary( | ||
name = "sculpt_processor_runner", | ||
main_class = "io.bazel.rules_scala.target_split.SculptProcessor", | ||
visibility = ["//visibility:public"], | ||
runtime_deps = [":sculpt_processor"], | ||
) |
28 changes: 28 additions & 0 deletions
28
src/scala/io/bazel/rules_scala/target_split/BazelGen.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.bazel.rules_scala.target_split | ||
|
||
object BazelGen { | ||
final case class ScalaLibrary( | ||
name: String, | ||
srcs: List[String], | ||
deps: List[String], | ||
visibility: List[String], | ||
exports: List[String]) { | ||
|
||
def render: String = { | ||
def q(s: String): String = "\"" + s + "\"" | ||
def list(s: List[String], indent: String): String = | ||
if (s.isEmpty) "[]" | ||
else s.iterator.map(q).mkString(s"[\n$indent", s",\n$indent",s",\n$indent]") | ||
|
||
val ident = " " * 8 | ||
s"""|scala_library( | ||
| name = ${q(name)}, | ||
| srcs = ${list(srcs, ident)}, | ||
| deps = ${list(deps, ident)}, | ||
| visibility = ${list(visibility, ident)}, | ||
| exports = ${list(exports, ident)}, | ||
| )""".stripMargin | ||
} | ||
} | ||
} | ||
|
123 changes: 123 additions & 0 deletions
123
src/scala/io/bazel/rules_scala/target_split/Dagify.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package io.bazel.rules_scala.target_split | ||
|
||
import scala.collection.immutable.SortedSet | ||
|
||
object Graph { | ||
|
||
def dagify[A: Ordering](seed: Set[A])(neighbors: A => Set[A]): Dagify[A] = | ||
new Dagify[A](seed, neighbors) | ||
|
||
def dagifyGraph[A: Ordering](graph: Map[A, Iterable[A]]): Dagify[A] = | ||
dagify(graph.keySet) { a => graph.getOrElse(a, Nil).toSet } | ||
|
||
/** | ||
* Build a DAG by merging individual nodes of type A into merged nodes of type SortedSet[A] | ||
*/ | ||
final class Dagify[A: Ordering](seed: Set[A], val neighbors: A => Set[A]) { | ||
|
||
@annotation.tailrec | ||
private def allNodes(toCheck: List[A], reached: Set[A], acc: SortedSet[A]): SortedSet[A] = | ||
toCheck match { | ||
case Nil => acc | ||
case h :: tail => | ||
if (reached(h)) allNodes(tail, reached, acc) | ||
else allNodes(neighbors(h).toList.sorted ::: tail, reached + h, acc + h) | ||
} | ||
|
||
// all the reachable nodes in a sorted order | ||
val nodes: SortedSet[A] = | ||
allNodes(seed.toList, Set.empty, SortedSet.empty) | ||
|
||
/* | ||
* For each node, build the full set of nodes it can reach by the neighbors function | ||
*/ | ||
@annotation.tailrec | ||
private def reachable(m: List[(A, SortedSet[A])], | ||
acc: List[(A, SortedSet[A])]): Map[A, SortedSet[A]] = | ||
if (m.isEmpty) acc.toMap | ||
else { | ||
// if A -> B, then include all the nodes B can reach | ||
val stepped = m.iterator.map { | ||
case (src, dest0) => | ||
// expand src + dest0 by looking at all the neighbors of this set | ||
val dest1 = dest0.flatMap(neighbors) ++ neighbors(src) | ||
(src, (dest0, dest1)) | ||
}.toList | ||
|
||
// if the expanded set is the same as the initial set, that node has computed its full reach | ||
// and is done, else we need to continue to expand | ||
val (done, notDone) = stepped.partition { case (_, (d0, d1)) => d0 == d1 } | ||
reachable(notDone.map { case (k, (_, d1)) => (k, d1) }, done.map { | ||
case (k, (d0, _)) => (k, d0) | ||
} ::: acc) | ||
} | ||
|
||
private def toSortedSet[T: Ordering](it: Iterator[T]): SortedSet[T] = { | ||
val bldr = SortedSet.newBuilder[T] | ||
bldr ++= it | ||
bldr.result() | ||
} | ||
|
||
// all the reachable nodes from a given node | ||
val reachableMap: Map[A, SortedSet[A]] = | ||
reachable(nodes.iterator.map { a => | ||
(a, SortedSet.empty[A]) | ||
}.toList, Nil) | ||
|
||
type Cluster = SortedSet[A] | ||
implicit val ordCluster: Ordering[Cluster] = Ordering.Iterable[A].on { s: SortedSet[A] => | ||
s | ||
} | ||
|
||
// To make a dag, we group nodes together that are mutually reachable, these larger sets | ||
// become the new nodes in the bigger graph | ||
val clusterMembers: SortedSet[Cluster] = | ||
toSortedSet(reachableMap.iterator.map { | ||
case (n, reach) => | ||
if (reach(n)) { | ||
// we can reach ourselves, so we include everyone in this cluster that can reach us | ||
toSortedSet(reach.iterator.collect { case n1 if reachableMap(n1)(n) => n1 }) | ||
} else SortedSet(n) | ||
}) | ||
|
||
// which cluster is each node in | ||
val clusterMap: Map[A, Cluster] = | ||
nodes.iterator.map { n => | ||
n -> clusterMembers.iterator.filter(_(n)).next | ||
}.toMap | ||
|
||
// this must form a DAG now by construction | ||
val clusterDeps: Map[Cluster, SortedSet[Cluster]] = | ||
clusterMembers.iterator.map { c => | ||
val reach = c.flatMap(neighbors) | ||
val deps = clusterMembers.filter { c1 => | ||
reach.exists(c1) | ||
} - c | ||
(c, deps) | ||
}.toMap | ||
|
||
lazy val topological: List[List[Cluster]] = { | ||
def loop(prev: List[Cluster], rest: List[Cluster], acc: List[List[Cluster]]): List[List[Cluster]] = | ||
if (rest.isEmpty) acc.reverse.map(_.sorted) | ||
else { | ||
// everyone that can reach the prev is in the next group | ||
val (next, notyet) = rest.partition { c => | ||
val cdeps = clusterDeps(c) | ||
// are all of the deps already seen? | ||
prev.forall(cdeps) | ||
} | ||
|
||
loop(next reverse_::: prev, notyet, next :: acc) | ||
} | ||
|
||
loop(Nil, clusterMembers.toList, Nil) | ||
} | ||
|
||
/** | ||
* if the original A graph was a DAG, then all the clusters are singletons | ||
*/ | ||
lazy val originalIsDag: Boolean = | ||
clusterMembers.forall(_.size == 1) | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package example | ||
|
||
object File0 { | ||
def ident[A](a: A): A = a | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package example | ||
|
||
object File1 { | ||
val message: String = File0.ident("this is file 1") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package example | ||
|
||
object File2 { | ||
val message: String = File1.message + " called from File2" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a thought maybe this should be part of sculpt external repository since it’s a runner for that?