Skip to content
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

Add javac support for mixed targets #2

Merged
merged 1 commit into from
Sep 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should these not be separate args to the Process builder rather than a string concat?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do that, it passes empty strings to the javac which then errors
rather than ignoring empty args.

It's great. I'm proud of this. ;)
On Mon, Sep 12, 2016 at 14:39 ianoc notifications@github.com wrote:

In src/java/io/bazel/rulesscala/scalac/ScalaCInvoker.java
#2 (comment):

@@ -255,6 +255,37 @@ private static void processRequest(List 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);
    

should these not be separate args to the Process builder rather than a
string concat?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/ianoc/rules_scala/pull/2/files/d0d6658d1f6ff948949ac47ea1ab7f9f128c8606#r78479922,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAEJduvOcPE3K8mTZEnbhLDkvK0h8Yvkks5qpfDCgaJpZM4J7LKQ
.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Miss filter/flatMap don't you!)

ok this seems fine since it must work. (In some other of these sub process libs they can require that the args are passed to it and the 'process' is a executable to call than a string with args in it... indeed i believe scala's works like this.)

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