Skip to content

Commit

Permalink
Merge pull request #2 from ianoc/oscar/javac_scala_worker
Browse files Browse the repository at this point in the history
Add javac support for mixed targets
  • Loading branch information
johnynek authored Sep 13, 2016
2 parents 9bf9087 + d0d6658 commit 61b3b57
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
8 changes: 8 additions & 0 deletions scala/scala.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ EnableIjar: {enableijar}
ijarOutput: {ijar_out}
ijarCmdPath: {ijar_cmd_path}
SourceJars: {srcjars}
JavacPath: {javac_path}
JavacOpts: {javac_opts}
JavaFiles: {java_files}
JvmFlags: {jvm_flags}
""".format(
out=ctx.outputs.jar.path, # 0
manifest=ctx.outputs.manifest.path, # 1
Expand All @@ -177,6 +181,10 @@ SourceJars: {srcjars}
ijar_out=ijar_output_path,
ijar_cmd_path=ijar_cmd_path,
srcjars=",".join([f.path for f in all_srcjars]),
javac_opts=" ".join(ctx.attr.javacopts),
javac_path=ctx.file._javac.path,
java_files=",".join([f.path for f in java_srcs]),
jvm_flags=" ".join(["-J" + flag for flag in ctx.attr.jvm_flags]),
)
ctx.file_action(output=scalac_args_file, content=scalac_args)
javac_sources_cmd = ""
Expand Down
30 changes: 27 additions & 3 deletions src/java/io/bazel/rulesscala/scalac/CompileOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,28 @@ public class CompileOptions {
final public boolean iJarEnabled;
final public String ijarOutput;
final public String ijarCmdPath;
final public String[] javaFiles;
final public String javacPath;
final public String javacOpts;
final public String jvmFlags;

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(",");
scalaOpts = getCommaList(argMap, "ScalacOpts");
pluginArgs = buildPluginArgs(getOrEmpty(argMap, "Plugins"));
classpath = getOrError(argMap, "Classpath", "Must supply the classpath arg");
files = getOrEmpty(argMap, "Files").split(",");
files = getCommaList(argMap, "Files");

sourceJars = getOrEmpty(argMap, "SourceJars").split(",");
javaFiles = getCommaList(argMap, "JavaFiles");
javacPath = getOrEmpty(argMap, "JavacPath");
javacOpts = getOrEmpty(argMap, "JavacOpts");
jvmFlags = getOrEmpty(argMap, "JvmFlags");

sourceJars = getCommaList(argMap, "SourceJars");
iJarEnabled = booleanGetOrFalse(argMap, "EnableIjar");
if(iJarEnabled) {
ijarOutput = getOrError(argMap, "ijarOutput", "Missing required arg ijarOutput when ijar enabled");
Expand All @@ -54,13 +63,28 @@ private static HashMap<String, String> buildArgMap(List<String> lines) {
return hm;
}

private static String[] getCommaList(Map<String, String> m, String k) {
if(m.containsKey(k)) {
String v = m.get(k);
if (v == "") {
return new String[]{};
}
else {
return v.split(",");
}
} else {
return new String[]{};
}
}

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);
Expand Down
34 changes: 34 additions & 0 deletions src/java/io/bazel/rulesscala/scalac/ScalaCInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,37 @@ private static void processRequest(List<String> args) throws Exception {
reporter.flush();
throw new RuntimeException("Build failed");
} else {
/**
* See if there are java sources to compile
*/
if (ops.javaFiles.length > 0) {
StringBuilder cmd = new StringBuilder();
cmd.append(ops.javacPath);
if (ops.jvmFlags != "") cmd.append(ops.jvmFlags);
if (ops.javacOpts != "") cmd.append(ops.javacOpts);

StringBuilder files = new StringBuilder();
int cnt = 0;
for(String javaFile : ops.javaFiles) {
if (cnt > 0) files.append(" ");
files.append(javaFile);
cnt += 1;
}
Process iostat = new ProcessBuilder()
.command(cmd.toString(),
"-classpath", ops.classpath + ":" + tmpPath.toString(),
"-d", tmpPath.toString(),
files.toString())
.inheritIO()
.start();
int exitCode = iostat.waitFor();
if(exitCode != 0) {
throw new RuntimeException("javac process failed!");
}
}
/**
* Now build the output jar
*/
String[] jarCreatorArgs = {
"-m",
ops.manifestPath,
Expand All @@ -263,6 +294,9 @@ private static void processRequest(List<String> args) throws Exception {
};
JarCreator.buildJar(jarCreatorArgs);

/**
* Now build the output ijar
*/
if(ops.iJarEnabled) {
Process iostat = new ProcessBuilder()
.command(ops.ijarCmdPath, ops.outputName, ops.ijarOutput)
Expand Down

0 comments on commit 61b3b57

Please sign in to comment.