Skip to content

Commit cd24529

Browse files
committed
Add resource support, minor cleanups
1 parent 61b3b57 commit cd24529

File tree

4 files changed

+81
-102
lines changed

4 files changed

+81
-102
lines changed

scala/scala.bzl

Lines changed: 5 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,6 @@ def _collect_plugin_paths(plugins):
128128

129129
def _compile(ctx, _jars, dep_srcjars, buildijar):
130130
jars = _jars
131-
cp_resources = _add_resources_cmd(
132-
ctx, "{out}_tmp".format(out=ctx.outputs.jar.path)
133-
)
134131
ijar_output_path = ""
135132
ijar_cmd_path = ""
136133
if buildijar:
@@ -145,9 +142,6 @@ def _compile(ctx, _jars, dep_srcjars, buildijar):
145142
plugins = _collect_plugin_paths(ctx.attr.plugins)
146143
plugin_arg = ",".join(list(plugins))
147144

148-
# Set up the args to pass to scalac because they can be too long for bash
149-
scalac_args_file = ctx.new_file(ctx.outputs.jar, ctx.label.name + "_scalac_args") # noqa
150-
151145
compiler_classpath = '{scalalib}:{scalacompiler}:{scalareflect}:{jars}'.format( # noqa
152146
scalalib=ctx.file._scalalib.path,
153147
scalacompiler=ctx.file._scalacompiler.path,
@@ -170,6 +164,8 @@ JavacPath: {javac_path}
170164
JavacOpts: {javac_opts}
171165
JavaFiles: {java_files}
172166
JvmFlags: {jvm_flags}
167+
ResourceSrcs: {resource_src}
168+
ResourceDests: {resource_dest}
173169
""".format(
174170
out=ctx.outputs.jar.path, # 0
175171
manifest=ctx.outputs.manifest.path, # 1
@@ -185,81 +181,16 @@ JvmFlags: {jvm_flags}
185181
javac_path=ctx.file._javac.path,
186182
java_files=",".join([f.path for f in java_srcs]),
187183
jvm_flags=" ".join(["-J" + flag for flag in ctx.attr.jvm_flags]),
184+
resource_src=",".join([f.path for f in ctx.files.resources]),
185+
resource_dest=",".join([_adjust_resources_path(f.path)[1] for f in ctx.files.resources]),
188186
)
189-
ctx.file_action(output=scalac_args_file, content=scalac_args)
190-
javac_sources_cmd = ""
191-
compile_java_srcs = len(java_srcs) != 0
192-
if (compile_java_srcs):
193-
# Set up the args to pass to javac because they can be
194-
# too long for bash
195-
javac_args_file = ctx.new_file(
196-
ctx.outputs.jar,
197-
ctx.label.name + "_javac_args")
198-
199-
javac_args = """{javac_opts} -classpath "{jars}:{out}_tmp" -d {out}_tmp {files}""".format( # noqa
200-
javac_opts=" ".join(ctx.attr.javacopts),
201-
jars=":".join([j.path for j in jars]),
202-
files=" ".join([f.path for f in java_srcs]),
203-
out=ctx.outputs.jar.path
204-
)
205-
ctx.file_action(output=javac_args_file, content=javac_args)
206-
javac_sources_cmd = """
207-
cat {javac_args} {{out}}_args/files_from_jar > {{out}}_args/java_args
208-
{javac} {{jvm_flags}} @{{out}}_args/java_args""".format(
209-
javac_args=javac_args_file.path,
210-
javac=ctx.file._javac.path
211-
)
212-
213-
# srcjar_cmd = ""
214-
# if len(all_srcjars) > 0:
215-
# srcjar_cmd = "\nmkdir -p {out}_tmp_expand_srcjars\n"
216-
# for srcjar in all_srcjars:
217-
# # Note: this is double escaped because
218-
# # we need to do one format call
219-
# # per each srcjar, but then we are going to include
220-
# # this in the bigger format
221-
# # call that is done to generate the full command
222-
223-
# # TODO would like to be able to switch >/dev/null, -v,
224-
# # etc based on the user's settings
225-
# srcjar_cmd += """
226-
# unzip -o {srcjar} -d {{out}}_tmp_expand_srcjars >/dev/null
227-
# """.format(
228-
# srcjar=srcjar.path)
229-
# srcjar_cmd += """find {out}_tmp_expand_srcjars """
230-
# srcjar_cmd += """-type f -name "*.scala"""
231-
# srcjar_cmd += """ > {out}_args/files_from_jar\n"""
232-
# TODO ADD RESOURCES SUPPORT!
233-
# # add any resources
234-
# {cp_resources}
235-
236-
# TODO add source jar support
237-
# touch {out}_args/files_from_jar
238-
# mkdir -p {out}_tmp""" + srcjar_cmd + """
239-
# {out}_args/files_from_jar
240-
187+
# TODO why this path here?
241188
argfile = ctx.new_file(
242189
ctx.configuration.bin_dir,
243190
"%s_worker_input" % ctx.label.name
244191
)
245192
ctx.file_action(output=argfile, content=scalac_args)
246193

247-
# cmd = """
248-
# cat {scalac_args} > {out}_args/scala_args
249-
# {java} -jar {scalac} {jvm_flags} @{out}_args/scala_args""" + javac_sources_cmd + """
250-
# """ + ijar_cmd
251-
# cmd = cmd.format(
252-
# cp_resources=cp_resources,
253-
# java=ctx.file._java.path,
254-
# jvm_flags=" ".join(["-J" + flag for flag in ctx.attr.jvm_flags]),
255-
# scalac=_get_scalac_jar_path(ctx.files._scalac),
256-
# scalac_args=scalac_args_file.path,
257-
# out=ctx.outputs.jar.path,
258-
# manifest=ctx.outputs.manifest.path,
259-
# jar=_get_jar_path(ctx.files._jar),
260-
# ijar=ctx.file._ijar.path,
261-
# )
262-
263194
outs = [ctx.outputs.jar]
264195
if buildijar:
265196
outs.extend([ctx.outputs.ijar])
@@ -271,20 +202,14 @@ JvmFlags: {jvm_flags}
271202
ctx.files.plugins +
272203
ctx.files.resources +
273204
ctx.files._jdk +
274-
ctx.files._scalac +
275-
ctx.files._jar +
276205
ctx.files._scalasdk +
277206
[ctx.outputs.manifest,
278207
ctx.file._ijar,
279208
ctx.file._java,
280-
scalac_args_file,
281209
argfile])
282-
if compile_java_srcs:
283-
ins.extend([javac_args_file])
284210
ctx.action(
285211
inputs=ins,
286212
outputs=outs,
287-
# command=cmd,
288213
executable=ctx.executable._scalac,
289214
mnemonic="Scalac",
290215
progress_message="scala %s" % ctx.label,

src/java/io/bazel/rulesscala/scalac/CompileOptions.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class CompileOptions {
2020
final public String javacPath;
2121
final public String javacOpts;
2222
final public String jvmFlags;
23+
final public Map<String, String> resourceFiles;
2324

2425
public CompileOptions(List<String> args) {
2526
Map<String, String> argMap = buildArgMap(args);
@@ -47,6 +48,20 @@ public CompileOptions(List<String> args) {
4748
ijarOutput = null;
4849
ijarCmdPath = null;
4950
}
51+
resourceFiles = getResources(argMap);
52+
}
53+
54+
private static Map<String, String> getResources(Map<String, String> args) {
55+
String[] keys = getCommaList(args, "ResourceSrcs");
56+
String[] vals = getCommaList(args, "ResourceDests");
57+
if (keys.length != vals.length)
58+
throw new RuntimeException(String.format("mismatch in resources: keys: %s vals: %s",
59+
getOrEmpty(args, "ResourceSrcs"), getOrEmpty(args, "ResourceDests")));
60+
HashMap<String, String> res = new HashMap();
61+
for(int idx = 0; idx < keys.length; idx++) {
62+
res.put(keys[idx], vals[idx]);
63+
}
64+
return res;
5065
}
5166

5267
private static HashMap<String, String> buildArgMap(List<String> lines) {

src/java/io/bazel/rulesscala/scalac/ScalaCInvoker.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@
3131
import java.io.IOException;
3232
import java.io.PrintStream;
3333
import java.lang.reflect.Field;
34+
import java.nio.file.attribute.BasicFileAttributes;
3435
import java.nio.file.FileSystems;
36+
import java.nio.file.FileVisitResult;
3537
import java.nio.file.Files;
3638
import java.nio.file.Path;
3739
import java.nio.file.Paths;
40+
import java.nio.file.SimpleFileVisitor;
3841
import java.util.ArrayList;
3942
import java.util.Arrays;
4043
import java.util.Collection;
@@ -222,13 +225,15 @@ public static String[] merge(String[]... arrays) {
222225
}
223226

224227
private static void processRequest(List<String> args) throws Exception {
228+
Path tmpPath = null;
229+
try {
225230
if (args.size() == 1 && args.get(0).startsWith("@")) {
226231
args = Files.readAllLines(Paths.get(args.get(0).substring(1)), UTF_8);
227232
}
228233
CompileOptions ops = new CompileOptions(args);
229234

230235
Path outputPath = FileSystems.getDefault().getPath(ops.outputName);
231-
Path tmpPath = Files.createTempDirectory(outputPath.getParent(), "tmp");
236+
tmpPath = Files.createTempDirectory(outputPath.getParent(), "tmp");
232237
String[] constParams = {
233238
"-classpath",
234239
ops.classpath,
@@ -283,6 +288,10 @@ private static void processRequest(List<String> args) throws Exception {
283288
throw new RuntimeException("javac process failed!");
284289
}
285290
}
291+
/**
292+
* Copy the resources
293+
*/
294+
copyResources(ops.resourceFiles, tmpPath);
286295
/**
287296
* Now build the output jar
288297
*/
@@ -306,10 +315,40 @@ private static void processRequest(List<String> args) throws Exception {
306315
if(exitCode != 0) {
307316
throw new RuntimeException("ijar process failed!");
308317
}
309-
// System.out.println("exitCode = " + exitCode);
310318
}
311-
// System.out.println("Success");
312319
}
320+
}
321+
finally {
322+
removeTmp(tmpPath);
323+
}
324+
}
325+
private static void removeTmp(Path tmp) throws IOException {
326+
if (tmp != null) {
327+
Files.walkFileTree(tmp, new SimpleFileVisitor<Path>() {
328+
@Override
329+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
330+
Files.delete(file);
331+
return FileVisitResult.CONTINUE;
332+
}
333+
334+
@Override
335+
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
336+
Files.delete(dir);
337+
return FileVisitResult.CONTINUE;
338+
}
339+
});
340+
}
341+
}
342+
private static void copyResources(Map<String, String> resources, Path dest) throws IOException {
343+
for(Entry<String, String> e : resources.entrySet()) {
344+
Path source = Paths.get(e.getKey());
345+
String dstr = e.getValue();
346+
if (dstr.charAt(0) == '/') dstr = dstr.substring(1);
347+
Path target = dest.resolve(dstr);
348+
File tfile = target.getParent().toFile();
349+
tfile.mkdirs();
350+
Files.copy(source, target);
351+
}
313352
}
314353

315354
public static void main(String[] args) {

test/BUILD

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ load("//scala:scala.bzl",
99
# The examples below show how to combine Scala and Java rules.
1010
# ScalaBinary is the Scala equivalent of JavaBinary.
1111

12-
# java_binary(
13-
# name = "JavaBinary",
14-
# srcs = ["JavaBinary.java"],
15-
# main_class = "scala.test.JavaBinary",
16-
# deps = [":lib_import"],
17-
# )
18-
19-
# # TODO(bazel-team): Allow java rules to depend directly on scala_library.
20-
# # For now, we have to use a java_import proxy.
21-
# java_import(
22-
# name = "lib_import",
23-
# # these are the outputs of the scala_library targets
24-
# jars = [":HelloLib_deploy.jar",
25-
# "OtherLib_deploy.jar",
26-
# "Exported_deploy.jar",
27-
# "Runtime_deploy.jar",
28-
# ],
29-
# runtime_deps = ["org_scala_lang__scala_library", "OtherJavaLib"]
30-
# )
12+
java_binary(
13+
name = "JavaBinary",
14+
srcs = ["JavaBinary.java"],
15+
main_class = "scala.test.JavaBinary",
16+
deps = [":lib_import"],
17+
)
18+
19+
# TODO(bazel-team): Allow java rules to depend directly on scala_library.
20+
# For now, we have to use a java_import proxy.
21+
java_import(
22+
name = "lib_import",
23+
# these are the outputs of the scala_library targets
24+
jars = [":HelloLib_deploy.jar",
25+
"OtherLib_deploy.jar",
26+
"Exported_deploy.jar",
27+
"Runtime_deploy.jar",
28+
],
29+
runtime_deps = ["org_scala_lang__scala_library", "OtherJavaLib"]
30+
)
3131

3232
scala_export_to_java(
3333
name = "lib_import_2",

0 commit comments

Comments
 (0)