Skip to content

Commit

Permalink
Merge pull request #1 from ianoc/oscar/resident-compiler
Browse files Browse the repository at this point in the history
Factor out ScalaCInvoker option parsing
  • Loading branch information
ianoc authored Sep 12, 2016
2 parents 617885e + 30b0efc commit 9bf9087
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 227 deletions.
8 changes: 7 additions & 1 deletion scala/support/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
load("//scala:scala.bzl", "scala_library")


java_import(
name = "scala_xml",
jars = ["@scala//:lib/scala-xml_2.11-1.0.4.jar"]
)

scala_library(name = "test_reporter",
srcs = ["JUnitXmlReporter.scala"],
deps = ["@scalatest//file"],
deps = ["@scalatest//file", ":scala_xml"],
visibility = ["//visibility:public"],
)
2 changes: 1 addition & 1 deletion src/java/io/bazel/rulesscala/scalac/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
java_binary(name = "scalac",
main_class = "io.bazel.rulesscala.scalac.ScalaCInvoker",
srcs = ["ScalaCInvoker.java"],
srcs = ["ScalaCInvoker.java", "CompileOptions.java"],

deps = [
"@scala//:lib/scala-library.jar",
Expand Down
100 changes: 100 additions & 0 deletions src/java/io/bazel/rulesscala/scalac/CompileOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package io.bazel.rulesscala.scalac;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CompileOptions {
final public String outputName;
final public String manifestPath;
final public String[] scalaOpts;
final public String[] pluginArgs;
final public String classpath;
final public String[] files;
final public String[] sourceJars;
final public boolean iJarEnabled;
final public String ijarOutput;
final public String ijarCmdPath;

public CompileOptions(List<String> args) {
Map<String, String> argMap = buildArgMap(args);

outputName = getOrError(argMap, "JarOutput", "Missing required arg JarOutput");
manifestPath = getOrError(argMap, "Manifest", "Missing required arg Manifest");

scalaOpts = getOrEmpty(argMap, "ScalacOpts").split(",");
pluginArgs = buildPluginArgs(getOrEmpty(argMap, "Plugins"));
classpath = getOrError(argMap, "Classpath", "Must supply the classpath arg");
files = getOrEmpty(argMap, "Files").split(",");

sourceJars = getOrEmpty(argMap, "SourceJars").split(",");
iJarEnabled = booleanGetOrFalse(argMap, "EnableIjar");
if(iJarEnabled) {
ijarOutput = getOrError(argMap, "ijarOutput", "Missing required arg ijarOutput when ijar enabled");
ijarCmdPath = getOrError(argMap, "ijarCmdPath", "Missing required arg ijarCmdPath when ijar enabled");
}
else {
ijarOutput = null;
ijarCmdPath = null;
}
}

private static HashMap<String, String> buildArgMap(List<String> lines) {
HashMap hm = new HashMap();
for(String line: lines) {
String[] lSplit = line.split(": ");
if(lSplit.length > 2) {
throw new RuntimeException("Bad arg, should have at most 1 space/2 spans. arg: " + line);
}
if(lSplit.length > 1) {
hm.put(lSplit[0], lSplit[1]);
}
}
return hm;
}

private static String getOrEmpty(Map<String, String> m, String k) {
if(m.containsKey(k)) {
return m.get(k);
} else {
return "";
}
}
private static String getOrError(Map<String, String> m, String k, String errorMessage) {
if(m.containsKey(k)) {
return m.get(k);
} else {
throw new RuntimeException(errorMessage);
}
}

private static boolean booleanGetOrFalse(Map<String, String> m, String k) {
if(m.containsKey(k)) {
String v = m.get(k);
if(v.trim().equals("True") || v.trim().equals("true")) {
return true;
}
}
return false;
}
public static String[] buildPluginArgs(String packedPlugins) {
String[] pluginElements = packedPlugins.split(",");
int numPlugins = 0;
for(int i =0; i< pluginElements.length; i++){
if(pluginElements[i].length() > 0) {
numPlugins += 1;
}
}

String[] result = new String[numPlugins];
int idx = 0;
for(int i =0; i< pluginElements.length; i++){
if(pluginElements[i].length() > 0) {
result[idx] = "-Xplugin:" + pluginElements[i];
idx += 1;
}
}
return result;
}
}
Loading

0 comments on commit 9bf9087

Please sign in to comment.