Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Use protobuf instead of dot to communicate between parsers and core
Browse files Browse the repository at this point in the history
that is, between the language-specific parsers and the language-agnostic core.
  • Loading branch information
cgrushko committed Oct 6, 2017
1 parent ef5af7f commit ed349e2
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 386 deletions.
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Avoid downloading protobuf twice:
build --proto_toolchain_for_java="@com_google_protobuf//:java_toolchain"
8 changes: 8 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@ load("//thirdparty:workspace.bzl", "maven_dependencies")
load("//thirdparty:load.bzl", "declare_maven")

maven_dependencies(declare_maven)

# Provide dependencies for proto_library and java_proto_library rules.
http_archive(
name = "com_google_protobuf",
urls = ["https://github.com/google/protobuf/releases/download/v3.4.1/protobuf-java-3.4.1.zip"],
strip_prefix = "protobuf-3.4.1",
sha256 = "091e1aa2b64ea6d512ff9294ecc9da95132c3b961a8fb39a3bab3929e5122f50",
)
22 changes: 12 additions & 10 deletions src/main/java/com/google/devtools/build/bfg/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ java_library(
],
)

java_library(
name = "DotFileParser",
srcs = ["DotFileParser.java"],
deps = [
"//thirdparty/jvm/com/google/guava",
"//thirdparty/jvm/com/google/re2j",
],
)

java_library(
name = "GraphProcessor",
srcs = [
Expand Down Expand Up @@ -91,10 +82,10 @@ java_library(
"Bfg.java",
],
deps = [
":bfg_java_proto",
":BuildRule",
":BuildozerCommandCreator",
":ClassToRuleResolver",
":DotFileParser",
":ExternalResolver",
":GraphProcessor",
"//thirdparty/jvm/args4j",
Expand All @@ -117,6 +108,7 @@ java_library(
"JavaSourceFileParserCli.java",
],
deps = [
":bfg_java_proto",
":ReferencedClassesParser",
"//thirdparty/jvm/args4j",
"//thirdparty/jvm/com/google/guava",
Expand Down Expand Up @@ -163,3 +155,13 @@ java_library(
"//thirdparty/jvm/com/google/guava",
],
)

proto_library(
name = "bfg_proto",
srcs = ["bfg.proto"],
)

java_proto_library(
name = "bfg_java_proto"
, deps = [":bfg_proto" ],
)
21 changes: 19 additions & 2 deletions src/main/java/com/google/devtools/build/bfg/Bfg.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
import static com.google.devtools.build.bfg.BuildozerCommandCreator.computeBuildozerCommands;
import static com.google.devtools.build.bfg.BuildozerCommandCreator.getBuildFilesForBuildozer;
import static com.google.devtools.build.bfg.ClassGraphPreprocessor.preProcessClassGraph;
import static com.google.devtools.build.bfg.DotFileParser.getDirectedGraphFromDotFile;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.graph.GraphBuilder;
import com.google.common.graph.ImmutableGraph;
import com.google.common.graph.MutableGraph;
import com.google.re2j.Pattern;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
Expand All @@ -39,9 +40,11 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import protos.com.google.devtools.build.bfg.Bfg.ParserOutput;

/** Entry point to the BUILD file generator. */
public class Bfg {
Expand Down Expand Up @@ -108,6 +111,7 @@ private void run(String[] args) throws Exception {
cmdLineParser.parseArgument(args);

List<String> dotFile = readDotFileFromStdIn();
ParserOutput parserOutput = ParserOutput.parseFrom(System.in);
if (dotFile.isEmpty()) {
explainUsageErrorAndExit(cmdLineParser, "Expected nonempty class graph as input");
}
Expand All @@ -118,7 +122,8 @@ private void run(String[] args) throws Exception {
Pattern blackList = compilePattern(cmdLineParser, blackListRegex);

ImmutableGraph<String> classGraph =
preProcessClassGraph(getDirectedGraphFromDotFile(dotFile), whiteList, blackList);
preProcessClassGraph(
protoMultimapToGraph(parserOutput.getClassToClassMap()), whiteList, blackList);

ImmutableList<Path> contentRoots =
stream(Splitter.on(',').split(contentRootPaths))
Expand Down Expand Up @@ -148,6 +153,18 @@ private void run(String[] args) throws Exception {
executeBuildozerCommands(buildRuleGraph, workspace, isDryRun, buildozerPath);
}

private ImmutableGraph<String> protoMultimapToGraph(
Map<String, protos.com.google.devtools.build.bfg.Bfg.Strings> m) {
MutableGraph<String> result = GraphBuilder.directed().build();
m.forEach(
(u, deps) -> {
for (String s : deps.getSList()) {
result.putEdge(u, s);
}
});
return ImmutableGraph.copyOf(result);
}

private static Pattern compilePattern(CmdLineParser cmdLineParser, String patternString) {
try {
return Pattern.compile(patternString);
Expand Down
92 changes: 0 additions & 92 deletions src/main/java/com/google/devtools/build/bfg/DotFileParser.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import protos.com.google.devtools.build.bfg.Bfg;

/**
* Entry point to BFG java source file parser. Given a list of source files, it prints a graph viz
Expand Down Expand Up @@ -104,15 +107,19 @@ private void run(String[] args) throws Exception {
logger.warning(
String.format("Class Names not found %s", Joiner.on("\n\t").join(unresolvedClassNames)));
}
printDotFileContent(classDepsGraph);
Bfg.ParserOutput.Builder result = Bfg.ParserOutput.newBuilder();
result.putAllClassToClass(writeGraphToProtoMap(classDepsGraph));
result.build().writeTo(System.out);
}

/** Given a directed graph, generates the content of desired dot file */
private static void printDotFileContent(ImmutableGraph<String> depsGraph) {
System.out.println("digraph \"depsgraph\" {");
for (EndpointPair<String> edge : depsGraph.edges()) {
System.out.printf("\t\"%s\" -> \"%s\";%n", edge.nodeU(), edge.nodeV());
private static HashMap<String, Bfg.Strings> writeGraphToProtoMap(
ImmutableGraph<String> graph) {
HashMap<String, Bfg.Strings> result = new HashMap<>();
for (String u : graph.nodes()) {
Bfg.Strings.Builder adj = Bfg.Strings.newBuilder();
adj.addAllS(graph.adjacentNodes(u));
result.put(u, adj.build());
}
System.out.println("}");
return result;
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/google/devtools/build/bfg/bfg.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
syntax = "proto2";

package com.google.devtools.build.bfg;

option java_package = "protos.com.google.devtools.build.bfg";

// ParserOutput is used to communicate between language-specific parsers and the language-agnostic
// BFG core.
message ParserOutput {
// The class dependency graph.
// In the Java case, we have (u, v) if 'u' mentions [1] 'v'.
// [1] - By mention, we mean either imports or references in code, either as a fully qualified
// name, or an implied same-package name.
map<string, Strings> class_to_class = 1;
}

message Strings {
repeated string s = 1;
}
12 changes: 0 additions & 12 deletions src/test/java/com/google/devtools/build/bfg/BUILD
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
licenses(["notice"]) # Apache 2.0

java_test(
name = "DotFileParserTest",
srcs = ["DotFileParserTest.java"],
deps = [
"//src/main/java/com/google/devtools/build/bfg:DotFileParser",
"//thirdparty/jvm/com/google/guava",
"//thirdparty/jvm/com/google/re2j",
"//thirdparty/jvm/com/google/truth",
"//thirdparty/jvm/junit",
],
)

java_test(
name = "GraphProcessorTest",
srcs = ["GraphProcessorTest.java"],
Expand Down
Loading

0 comments on commit ed349e2

Please sign in to comment.