Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ianoc-stripe committed Sep 11, 2016
1 parent 15d02b1 commit 3a0c1ab
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 28 deletions.
26 changes: 19 additions & 7 deletions scala/scala.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand All @@ -71,15 +78,15 @@ 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])
ctx.action(
inputs=
ctx.files.resources +
ctx.files._jdk +
ctx.files._jar +
ctx.files.__deploy_jar +
[ctx.outputs.manifest, ctx.file._java],
outputs=outs,
command=cmd,
Expand Down Expand Up @@ -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]),
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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),
Expand Down
12 changes: 12 additions & 0 deletions src/java/io/bazel/rulesscala/scalac/BUILD
Original file line number Diff line number Diff line change
@@ -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"],
)
116 changes: 116 additions & 0 deletions src/java/io/bazel/rulesscala/scalac/ScalaCInvoker.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 1 addition & 1 deletion src/scala/scripts/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ scala_binary(
java_import(
name = "scala_parsers",
jars = ["@scala//:lib/scala-parser-combinators_2.11-1.0.4.jar"],
)
)
40 changes: 20 additions & 20 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3a0c1ab

Please sign in to comment.