From 1cc779c4eefc39430f6b5a5249b691cc829c8e51 Mon Sep 17 00:00:00 2001 From: Ian O Connell Date: Sun, 11 Sep 2016 17:16:05 -0700 Subject: [PATCH] Wip --- scala/scala.bzl | 61 ++++--- .../rulesscala/scalac/ScalaCInvoker.java | 167 ++++++++++-------- src/scala/scripts/BUILD | 16 +- 3 files changed, 142 insertions(+), 102 deletions(-) diff --git a/scala/scala.bzl b/scala/scala.bzl index 8eb20f7fe..b13874c15 100644 --- a/scala/scala.bzl +++ b/scala/scala.bzl @@ -131,12 +131,11 @@ def _compile(ctx, _jars, dep_srcjars, buildijar): cp_resources = _add_resources_cmd( ctx, "{out}_tmp".format(out=ctx.outputs.jar.path) ) - ijar_cmd = "" + ijar_output_path = "" + ijar_cmd_path = "" if buildijar: - ijar_cmd = "\n{ijar} {out} {ijar_out}".format( - ijar=ctx.file._ijar.path, - out=ctx.outputs.jar.path, - ijar_out=ctx.outputs.ijar.path) + ijar_output_path = ctx.outputs.ijar.path + ijar_cmd_path = ctx.file._ijar.path java_srcs = _java_filetype.filter(ctx.files.srcs) sources = _scala_filetype.filter(ctx.files.srcs) + java_srcs @@ -144,7 +143,7 @@ def _compile(ctx, _jars, dep_srcjars, buildijar): all_srcjars = set(srcjars + list(dep_srcjars)) # look for any plugins: plugins = _collect_plugin_paths(ctx.attr.plugins) - plugin_arg = "Plugins: " + ",".join(list(plugins)) + plugin_arg = ",".join(list(plugins)) # Set up the args to pass to scalac because they can be too long for bash scalac_args_file = ctx.new_file(ctx.outputs.jar, ctx.label.name + "_scalac_args") # noqa @@ -157,19 +156,25 @@ def _compile(ctx, _jars, dep_srcjars, buildijar): ) scalac_args = """ -{out} -{manifest} -{scala_opts} -{plugin_arg} -{cp} -{files} +JarOutput: {out} +Manifest: {manifest} +ScalacOpts: {scala_opts} +Plugins: {plugin_arg} +Classpath: {cp} +Files: {files} +EnableIjar: {enableijar} +ijarOutput: {ijar_out} +ijarCmdPath: {ijar_cmd_path} """.format( out=ctx.outputs.jar.path, # 0 manifest=ctx.outputs.manifest.path, # 1 - scala_opts="ScalaOpts: " + ",".join(ctx.attr.scalacopts), # 2 + scala_opts=",".join(ctx.attr.scalacopts), # 2 plugin_arg=plugin_arg, # 3 cp=compiler_classpath, # 4 files=",".join([f.path for f in sources]), # 5 + enableijar=buildijar, + ijar_out=ijar_output_path, + ijar_cmd_path=ijar_cmd_path, ) ctx.file_action(output=scalac_args_file, content=scalac_args) javac_sources_cmd = "" @@ -229,21 +234,21 @@ def _compile(ctx, _jars, dep_srcjars, buildijar): ) ctx.file_action(output=argfile, content=scalac_args) - cmd = """ - cat {scalac_args} > {out}_args/scala_args - {java} -jar {scalac} {jvm_flags} @{out}_args/scala_args""" + javac_sources_cmd + """ - """ + ijar_cmd - cmd = cmd.format( - cp_resources=cp_resources, - java=ctx.file._java.path, - jvm_flags=" ".join(["-J" + flag for flag in ctx.attr.jvm_flags]), - scalac=_get_scalac_jar_path(ctx.files._scalac), - scalac_args=scalac_args_file.path, - out=ctx.outputs.jar.path, - manifest=ctx.outputs.manifest.path, - jar=_get_jar_path(ctx.files._jar), - ijar=ctx.file._ijar.path, - ) + # cmd = """ + # cat {scalac_args} > {out}_args/scala_args + # {java} -jar {scalac} {jvm_flags} @{out}_args/scala_args""" + javac_sources_cmd + """ + # """ + ijar_cmd + # cmd = cmd.format( + # cp_resources=cp_resources, + # java=ctx.file._java.path, + # jvm_flags=" ".join(["-J" + flag for flag in ctx.attr.jvm_flags]), + # scalac=_get_scalac_jar_path(ctx.files._scalac), + # scalac_args=scalac_args_file.path, + # out=ctx.outputs.jar.path, + # manifest=ctx.outputs.manifest.path, + # jar=_get_jar_path(ctx.files._jar), + # ijar=ctx.file._ijar.path, + # ) outs = [ctx.outputs.jar] if buildijar: diff --git a/src/java/io/bazel/rulesscala/scalac/ScalaCInvoker.java b/src/java/io/bazel/rulesscala/scalac/ScalaCInvoker.java index a0b0e33ef..e876a0137 100644 --- a/src/java/io/bazel/rulesscala/scalac/ScalaCInvoker.java +++ b/src/java/io/bazel/rulesscala/scalac/ScalaCInvoker.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.util.Collection; import java.util.Map; +import java.util.HashMap; import java.util.TreeMap; import java.util.jar.Attributes; import java.util.jar.JarOutputStream; @@ -183,53 +184,95 @@ public static String[] merge(String[]... arrays) { // processRequest(ImmutableList.copyOf(args)); // } // } - private static String getOptionalNamedArg(String line) { - String[] lSplit = line.split(" "); - if(lSplit.length > 1) { - return lSplit[1]; - } else { - return ""; + private static HashMap buildArgMap(List 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].substring(0, lSplit[0].length() - 1), lSplit[1]); + } } + return hm; } - private static void processRequest(List args) throws Exception { - System.out.println("\n\n\n___ARGS_START____\n"); + private static String getOrError(Map m, String k, String errorMessage) { + if(m.containsKey(k)) { + return m.get(k); + } else { + throw new RuntimeException(errorMessage); + } + } + private static String getOrEmpty(Map m, String k) { + if(m.containsKey(k)) { + return m.get(k); + } else { + return ""; + } + } - for (int i = 0; i < args.size(); i++) { - System.out.println("'''" + args.get(i) + "'''"); + private static boolean booleanGetOrFalse(Map 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; + } + +// JarOutput: {out} +// Manifest: {manifest} +// ScalacOpts: {scala_opts} +// Plugins: {plugin_arg} +// Classpath: {cp} +// Files: {files} +// EnableIjar: {enableijar} +// ijarOutput: {ijar_out} +// ijarCmdPath: {ijar_cmd_path} + private static Field reporterField; + static { + try { + reporterField = Driver.class.getDeclaredField("reporter"); //NoSuchFieldException + reporterField.setAccessible(true); + } + catch (Exception ex){ + throw new RuntimeException("nope", ex); + } + } + private static void processRequest(List args) throws Exception { if (args.size() == 1 && args.get(0).startsWith("@")) { args = Files.readAllLines(Paths.get(args.get(0).substring(1)), UTF_8); } - List trimmedArgs = new ArrayList(); - for(String arg : args) { - if(arg.trim().length() > 0){ - trimmedArgs.add(arg); - } + Map argMap = buildArgMap(args); + for (Map.Entry entry : argMap.entrySet()) { + System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); } - args = trimmedArgs; - for (int i = 0; i < args.size(); i++) { - System.out.println(args.get(i)); - } + String outputName = getOrError(argMap, "JarOutput", "Missing required arg JarOutput"); + String manifestPath = getOrError(argMap, "Manifest", "Missing required arg Manifest"); - String outputName = args.get(0); - String manifestPath = args.get(1); + String[] scalaOpts = getOrEmpty(argMap, "ScalacOpts").split(","); + String[] pluginArgs = buildPluginArgs(getOrEmpty(argMap, "Plugins")); + String classpath = getOrError(argMap, "Classpath", "Must supply the classpath arg"); + String[] files = getOrError(argMap, "Files", "Must supply files to operate on").split(","); - String[] scalaOpts = getOptionalNamedArg(args.get(2)).split(","); - String[] pluginArgs = buildPluginArgs(getOptionalNamedArg(args.get(3))); - String classpath = args.get(4); - String[] files = args.get(5).split(","); + boolean iJarEnabled = booleanGetOrFalse(argMap, "EnableIjar"); + String ijarOutput = null; + String ijarCmdPath = null; + if(iJarEnabled) { + ijarOutput = getOrError(argMap, "ijarOutput", "Missing required arg ijarOutput when ijar enabled"); + ijarCmdPath = getOrError(argMap, "ijarCmdPath", "Missing required arg ijarCmdPath when ijar enabled"); + } Path outputPath = FileSystems.getDefault().getPath(outputName); Path tmpPath = Files.createTempDirectory(outputPath.getParent(),"tmp"); - System.out.println("Output path will be: " + outputName); - System.out.println("Manifest path will be: " + manifestPath); - System.out.println("tmpPath path will be: " + tmpPath); String[] constParams = { "-classpath", @@ -244,50 +287,34 @@ private static void processRequest(List args) throws Exception { constParams, files); + MainClass comp = new MainClass(); + comp.process(compilerArgs); + + ConsoleReporter reporter = (ConsoleReporter) reporterField.get(comp); + + if (reporter.hasErrors()) { + // reportErrors(reporter); + reporter.flush(); + } else { + // reportSuccess(); + String[] jarCreatorArgs = { + "-m", + manifestPath, + outputPath.toString(), + tmpPath.toString() + }; + JarCreator.buildJar(jarCreatorArgs); - for (int i = 0; i < compilerArgs.length; i++) { - System.out.println(compilerArgs[i]); + if(iJarEnabled) { + Process iostat = new ProcessBuilder().command(ijarCmdPath, outputName, ijarOutput).inheritIO().start(); + int exitCode = iostat.waitFor(); + if(exitCode != 0) { + throw new RuntimeException("ijar process failed!"); + } + System.out.println("exitCode = " + exitCode); } - - - - - // System.out.println("\n\n___ARGS_END____\n"); - - // for (int i = 0; i < newArgs.length; i++) { - // System.out.println(newArgs[i]); - // } - - // System.out.println("SASDF"); - // System.out.println("SASDF"); - // System.out.println("SASDF"); - // System.out.println("SASDF"); - // System.out.println("SASDF"); - MainClass comp = new MainClass(); - comp.process(compilerArgs); - - - // System.out.println("SASDF"); - - Field f = Driver.class.getDeclaredField("reporter"); //NoSuchFieldException - f.setAccessible(true); - ConsoleReporter reporter = (ConsoleReporter) f.get(comp); //IllegalAccessException - - if (reporter.hasErrors()) { - // reportErrors(reporter); - reporter.flush(); - } else { - // reportSuccess(); - String[] jarCreatorArgs = { - "-m", - manifestPath, - outputPath.toString(), - tmpPath.toString() - }; - JarCreator.buildJar(jarCreatorArgs); - - System.out.println("Success"); - } + System.out.println("Success"); + } } public static void main(String[] args) { diff --git a/src/scala/scripts/BUILD b/src/scala/scripts/BUILD index 5230bd0a2..66a47555f 100644 --- a/src/scala/scripts/BUILD +++ b/src/scala/scripts/BUILD @@ -1,9 +1,8 @@ -load("//scala:scala.bzl", "scala_binary") +load("//scala:scala.bzl", "scala_binary", "scala_library") -scala_binary( - name = "generator", +scala_library( + name = "generator_lib", srcs = ["TwitterScroogeGenerator.scala"], - main_class = "scripts.ScroogeGenerator", deps = [ "@scrooge_generator//jar", "@util_core//jar", @@ -13,6 +12,15 @@ scala_binary( visibility = ["//visibility:public"], ) +scala_binary( + name = "generator", + main_class = "scripts.ScroogeGenerator", + deps = [ + ":generator_lib", + ], + visibility = ["//visibility:public"], +) + java_import( name = "scala_parsers", jars = ["@scala//:lib/scala-parser-combinators_2.11-1.0.4.jar"],