From 3a0c1abb26c43ce34292e7592f35e39002d83f36 Mon Sep 17 00:00:00 2001 From: Ian O Connell Date: Sat, 10 Sep 2016 20:58:10 -0700 Subject: [PATCH] WIP --- scala/scala.bzl | 26 ++-- src/java/io/bazel/rulesscala/scalac/BUILD | 12 ++ .../rulesscala/scalac/ScalaCInvoker.java | 116 ++++++++++++++++++ src/scala/scripts/BUILD | 2 +- test/BUILD | 40 +++--- 5 files changed, 168 insertions(+), 28 deletions(-) create mode 100644 src/java/io/bazel/rulesscala/scalac/BUILD create mode 100644 src/java/io/bazel/rulesscala/scalac/ScalaCInvoker.java diff --git a/scala/scala.bzl b/scala/scala.bzl index 713c0554a..6396aedc9 100644 --- a/scala/scala.bzl +++ b/scala/scala.bzl @@ -51,6 +51,13 @@ def _get_jar_path(paths): return path return None +def _get_scalac_jar_path(paths): + for p in paths: + path = p.path + if path.endswith("/scalac_deploy.jar"): + return path + return None + def _build_nosrc_jar(ctx, buildijar): cp_resources = _add_resources_cmd(ctx, "{out}_tmp".format(out=ctx.outputs.jar.path)) ijar_cmd = "" @@ -71,7 +78,7 @@ mkdir -p {out}_tmp out=ctx.outputs.jar.path, manifest=ctx.outputs.manifest.path, java=ctx.file._java.path, - jar=_get_jar_path(ctx.files._jar)) + jar=_get_jar_path(ctx.files.__deploy_jar)) outs = [ctx.outputs.jar] if buildijar: outs.extend([ctx.outputs.ijar]) @@ -79,7 +86,7 @@ mkdir -p {out}_tmp inputs= ctx.files.resources + ctx.files._jdk + - ctx.files._jar + + ctx.files.__deploy_jar + [ctx.outputs.manifest, ctx.file._java], outputs=outs, command=cmd, @@ -122,8 +129,11 @@ def _compile(ctx, _jars, dep_srcjars, buildijar): # 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") - scalac_args = """{scala_opts} {plugin_arg} -classpath "{jars}" -d {out}_tmp {files}""".format( + scalac_args = """{scala_opts} {plugin_arg} -classpath {scalalib}:{scalacompiler}:{jars} -d {out}_tmp {files}""".format( scala_opts=" ".join(ctx.attr.scalacopts), + scalalib=ctx.file._scalalib.path, + scalacompiler=ctx.file._scalacompiler.path, + scalareflect=ctx.file._scalareflect.path, plugin_arg = plugin_arg, jars=":".join([j.path for j in jars]), files=" ".join([f.path for f in sources]), @@ -169,7 +179,7 @@ mkdir -p {out}_args touch {out}_args/files_from_jar mkdir -p {out}_tmp""" + srcjar_cmd + """ cat {scalac_args} {out}_args/files_from_jar > {out}_args/scala_args -env JAVACMD={java} {scalac} {jvm_flags} @{out}_args/scala_args""" + javac_sources_cmd + """ +{java} -jar {scalac} {jvm_flags} @{out}_args/scala_args""" + javac_sources_cmd + """ # add any resources {cp_resources} {java} -jar {jar} -m {manifest} {out} {out}_tmp @@ -181,7 +191,7 @@ rm -rf {out}_tmp_expand_srcjars cp_resources=cp_resources, java=ctx.file._java.path, jvm_flags=" ".join(["-J" + flag for flag in ctx.attr.jvm_flags]), - scalac=ctx.file._scalac.path, + scalac=_get_scalac_jar_path(ctx.files._scalac), scalac_args=scalac_args_file.path, out=ctx.outputs.jar.path, manifest=ctx.outputs.manifest.path, @@ -199,11 +209,11 @@ rm -rf {out}_tmp_expand_srcjars ctx.files.plugins + ctx.files.resources + ctx.files._jdk + + ctx.files._scalac + ctx.files.__deploy_jar + ctx.files._scalasdk + [ctx.outputs.manifest, ctx.file._ijar, - ctx.file._scalac, ctx.file._java, scalac_args_file]) if compile_java_srcs: @@ -463,8 +473,10 @@ def _scala_test_impl(ctx): _implicit_deps = { "_ijar": attr.label(executable=True, default=Label("@bazel_tools//tools/jdk:ijar"), single_file=True, allow_files=True), "_scala": attr.label(executable=True, default=Label("@scala//:bin/scala"), single_file=True, allow_files=True), - "_scalac": attr.label(executable=True, default=Label("@scala//:bin/scalac"), single_file=True, allow_files=True), + "_scalac": attr.label(executable=True, default=Label("//src/java/io/bazel/rulesscala/scalac:scalac_deploy.jar"), allow_files=True), "_scalalib": attr.label(default=Label("@scala//:lib/scala-library.jar"), single_file=True, allow_files=True), + "_scalareflect": attr.label(default=Label("@scala//:lib/scala-reflect.jar"), single_file=True, allow_files=True), + "_scalacompiler": attr.label(default=Label("@scala//:lib/scala-compiler.jar"), single_file=True, allow_files=True), "_scalaxml": attr.label(default=Label("@scala//:lib/scala-xml_2.11-1.0.4.jar"), single_file=True, allow_files=True), "_scalasdk": attr.label(default=Label("@scala//:sdk"), allow_files=True), "_scalareflect": attr.label(default=Label("@scala//:lib/scala-reflect.jar"), single_file=True, allow_files=True), diff --git a/src/java/io/bazel/rulesscala/scalac/BUILD b/src/java/io/bazel/rulesscala/scalac/BUILD new file mode 100644 index 000000000..5ea7f4447 --- /dev/null +++ b/src/java/io/bazel/rulesscala/scalac/BUILD @@ -0,0 +1,12 @@ +java_binary(name = "scalac", + main_class = "io.bazel.rulesscala.scalac.ScalaCInvoker", + srcs = ["ScalaCInvoker.java"], + + deps = [ + "@scala//:lib/scala-library.jar", + "@scala//:lib/scala-reflect.jar", + "@scala//:lib/scala-compiler.jar", + "@scala//:lib/scala-xml_2.11-1.0.4.jar", + ], + visibility = ["//visibility:public"], +) diff --git a/src/java/io/bazel/rulesscala/scalac/ScalaCInvoker.java b/src/java/io/bazel/rulesscala/scalac/ScalaCInvoker.java new file mode 100644 index 000000000..a9fc529b9 --- /dev/null +++ b/src/java/io/bazel/rulesscala/scalac/ScalaCInvoker.java @@ -0,0 +1,116 @@ +// Copyright 2014 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package io.bazel.rulesscala.scalac; + +import java.io.BufferedOutputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Collection; +import java.util.Map; +import java.util.TreeMap; +import java.util.jar.Attributes; +import java.util.jar.JarOutputStream; +import java.util.jar.Manifest; +import scala.tools.nsc.*; +import java.io.*; +import java.lang.reflect.Field; +import scala.tools.nsc.reporters.ConsoleReporter; + +/** + * A class for creating Jar files. Allows normalization of Jar entries by setting their timestamp to + * the DOS epoch. All Jar entries are sorted alphabetically. + */ +public class ScalaCInvoker { + + public static void main(String[] args) { + try { + // System.out.println("\n\n\n___ARGS_START____\n"); + + if(args.length == 1 && args[0].indexOf("@") == 0) { + String line; + BufferedReader in; + in = new BufferedReader(new FileReader(args[0].substring(1))); + line = in.readLine(); + args = line.split(" "); + } + // for (int i = 0; i < args.length; i++) { + // System.out.println(args[i]); + // } + + + + + // 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(args); + + + // 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(); + System.out.println("Success"); + } +} +catch(NoSuchFieldException ex) { + throw new RuntimeException("nope", ex); +} +catch (IllegalAccessException ex){ + throw new RuntimeException("nope", ex); +} +catch (FileNotFoundException ex){ + throw new RuntimeException("nope", ex); +} + +catch (IOException ex){ + throw new RuntimeException("nope", ex); +} + + +// Settings s = new Settings(); + +// Global g = new Global(s); + +// Global.Run run = g.new Run(); + +// // run.compile(List("test.scala")) // invoke compiler. it creates Test.class. + +// for (int i = 0; i < args.length; i++) { +// System.err.println(i); +// } +// System.err.println("Helloooo world!!!"); +// System.exit(-1); + } +} diff --git a/src/scala/scripts/BUILD b/src/scala/scripts/BUILD index d064db9fd..5230bd0a2 100644 --- a/src/scala/scripts/BUILD +++ b/src/scala/scripts/BUILD @@ -16,4 +16,4 @@ scala_binary( java_import( name = "scala_parsers", jars = ["@scala//:lib/scala-parser-combinators_2.11-1.0.4.jar"], -) \ No newline at end of file +) diff --git a/test/BUILD b/test/BUILD index 2a1f33e06..10dbdee72 100644 --- a/test/BUILD +++ b/test/BUILD @@ -9,25 +9,25 @@ load("//scala:scala.bzl", # The examples below show how to combine Scala and Java rules. # ScalaBinary is the Scala equivalent of JavaBinary. -java_binary( - name = "JavaBinary", - srcs = ["JavaBinary.java"], - main_class = "scala.test.JavaBinary", - deps = [":lib_import"], -) - -# TODO(bazel-team): Allow java rules to depend directly on scala_library. -# For now, we have to use a java_import proxy. -java_import( - name = "lib_import", - # these are the outputs of the scala_library targets - jars = [":HelloLib_deploy.jar", - "OtherLib_deploy.jar", - "Exported_deploy.jar", - "Runtime_deploy.jar", - ], - runtime_deps = ["org_scala_lang__scala_library", "OtherJavaLib"] -) +# java_binary( +# name = "JavaBinary", +# srcs = ["JavaBinary.java"], +# main_class = "scala.test.JavaBinary", +# deps = [":lib_import"], +# ) + +# # TODO(bazel-team): Allow java rules to depend directly on scala_library. +# # For now, we have to use a java_import proxy. +# java_import( +# name = "lib_import", +# # these are the outputs of the scala_library targets +# jars = [":HelloLib_deploy.jar", +# "OtherLib_deploy.jar", +# "Exported_deploy.jar", +# "Runtime_deploy.jar", +# ], +# runtime_deps = ["org_scala_lang__scala_library", "OtherJavaLib"] +# ) scala_export_to_java( name = "lib_import_2", @@ -150,7 +150,7 @@ scala_library( #Mix java scala scala_library( name = "MixJavaScalaLib", - srcs = glob(["src/main/scala/scala/test/mix_java_scala/*.scala"]) + + srcs = glob(["src/main/scala/scala/test/mix_java_scala/*.scala"]) + glob(["src/main/scala/scala/test/mix_java_scala/*.java"]), ) #needed to test java sources are compiled