Skip to content

Commit 1ee813e

Browse files
ekueflerdamienmg
authored andcommitted
Fix Groovy rules to work with sandboxing
-- Change-Id: Id6b14c65e5737f31001fcbdd0d8e1cf34f21336b Reviewed-on: https://bazel-review.googlesource.com/1953 MOS_MIGRATED_REVID=102513900
1 parent 8741978 commit 1ee813e

4 files changed

Lines changed: 52 additions & 42 deletions

File tree

tools/build_defs/groovy/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ libraries and vice-versa.
1919
To be able to use the Groovy rules, you must provide bindings for the following
2020
targets:
2121

22-
* `//external:groovy`, pointing at the core Groovy library
23-
* `//external:groovyc`, pointing at the Groovy compiler
22+
* `//external:groovy-sdk`, pointing at the
23+
[Groovy SDK binaries](http://www.groovy-lang.org/download.html)
2424
* `//external:junit`, pointing at JUnit (only required if using `groovy_test`)
2525

2626
The easiest way to do so is by copying the content of `groovy.WORKSPACE` to your
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
filegroup(
2-
name = "groovyc",
3-
srcs = ["groovy-2.4.4/bin/groovyc"],
2+
name = "sdk",
3+
srcs = glob(["groovy-2.4.4/**"], exclude_directories=0),
44
visibility = ["//visibility:public"],
55
)
Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
1-
bind(
2-
name = "groovyc",
3-
actual = "@groovy-bin//:groovyc",
4-
)
51
new_http_archive(
6-
name = "groovy-bin",
2+
name = "groovy-sdk-artifact",
73
url = "http://dl.bintray.com/groovy/maven/apache-groovy-binary-2.4.4.zip",
84
sha256 = "a7cc1e5315a14ea38db1b2b9ce0792e35174161141a6a3e2ef49b7b2788c258c",
95
build_file = "groovy.BUILD",
106
)
11-
127
bind(
13-
name = "groovy",
14-
actual = "@groovy-jar//jar",
15-
)
16-
maven_jar(
17-
name = "groovy-jar",
18-
artifact = "org.codehaus.groovy:groovy-all:2.3.7",
8+
name = "groovy-sdk",
9+
actual = "@groovy-sdk-artifact//:sdk",
1910
)
2011

21-
bind (
22-
name = "junit",
23-
actual = "@junit-jar//jar",
24-
)
2512
maven_jar(
26-
name = "junit-jar",
13+
name = "junit-artifact",
2714
artifact = "junit:junit:4.12",
2815
)
16+
bind(
17+
name = "junit",
18+
actual = "@junit-artifact//jar",
19+
)

tools/build_defs/groovy/groovy.bzl

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,20 @@ def _groovy_jar_impl(ctx):
2727
if hasattr(this_dep, "java"):
2828
all_deps += this_dep.java.transitive_runtime_deps
2929

30+
# Set up the output directory and set JAVA_HOME
31+
cmd = "rm -rf %s\n" % build_output
32+
cmd += "mkdir -p %s\n" % build_output
33+
cmd += "export JAVA_HOME=external/local-jdk\n"
34+
35+
# Set GROOVY_HOME by scanning through the groovy SDK to find the license file,
36+
# which should be at the root of the SDK.
37+
for file in ctx.files._groovysdk:
38+
if file.basename == "CLI-LICENSE.txt":
39+
cmd += "export GROOVY_HOME=%s\n" % file.dirname
40+
break
41+
3042
# Compile all files in srcs with groovyc
31-
cmd = "rm -rf %s; mkdir -p %s\n" % (build_output, build_output)
32-
cmd += "%s %s -d %s %s\n" % (
33-
ctx.file._groovyc.short_path,
43+
cmd += "$GROOVY_HOME/bin/groovyc %s -d %s %s\n" % (
3444
"-cp " + ":".join([dep.path for dep in all_deps]) if len(all_deps) != 0 else "",
3545
build_output,
3646
" ".join([src.path for src in ctx.files.srcs]),
@@ -43,7 +53,7 @@ def _groovy_jar_impl(ctx):
4353
cmd += "root=`pwd`\n"
4454
cmd += "cd %s; $root/%s c ../%s `find . -name '*.class' | cut -c 3-`\n" % (
4555
build_output,
46-
ctx.executable._jar.path,
56+
ctx.executable._zipper.path,
4757
class_jar.basename,
4858
)
4959
cmd += "cd $root\n"
@@ -53,7 +63,12 @@ def _groovy_jar_impl(ctx):
5363

5464
# Execute the command
5565
ctx.action(
56-
inputs = ctx.files.srcs + ctx.files.deps + ctx.files._jar,
66+
inputs = (
67+
ctx.files.srcs
68+
+ list(all_deps)
69+
+ ctx.files._groovysdk
70+
+ ctx.files._jdk
71+
+ ctx.files._zipper),
5772
outputs = [class_jar],
5873
mnemonic = "Groovyc",
5974
command = "set -e;" + cmd,
@@ -69,17 +84,18 @@ _groovy_jar = rule(
6984
"deps": attr.label_list(
7085
mandatory=False,
7186
allow_files=FileType([".jar"])),
72-
"_groovyc": attr.label(
73-
default=Label("//external:groovyc"),
74-
single_file=True),
75-
"_jar": attr.label(
87+
"_groovysdk": attr.label(
88+
default=Label("//external:groovy-sdk")),
89+
"_jdk": attr.label(
90+
default=Label("//tools/defaults:jdk")),
91+
"_zipper": attr.label(
7692
default=Label("//third_party/ijar:zipper"),
7793
executable=True,
7894
single_file=True),
79-
},
95+
},
8096
outputs = {
8197
"class_jar": "lib%{name}.jar",
82-
},
98+
},
8399
)
84100

85101
def groovy_library(name, srcs=[], deps=[], **kwargs):
@@ -135,7 +151,6 @@ def groovy_and_java_library(name, srcs=[], deps=[], **kwargs):
135151
**kwargs
136152
)
137153

138-
139154
def groovy_binary(name, main_class, srcs=[], deps=[], **kwargs):
140155
"""Rule analagous to java_binary that accepts .groovy sources instead of .java
141156
sources.
@@ -165,8 +180,14 @@ def path_to_class(path):
165180
fail("groovy_test sources must be under src/test/java or src/test/groovy")
166181

167182
def groovy_test_impl(ctx):
183+
# Collect jars from the Groovy sdk
184+
groovy_sdk_jars = [file
185+
for file in ctx.files._groovysdk
186+
if file.basename.endswith(".jar")
187+
]
188+
168189
# Extract all transitive dependencies
169-
all_deps = set(ctx.files.deps + ctx.files._implicit_deps)
190+
all_deps = set(ctx.files.deps + ctx.files._implicit_deps + groovy_sdk_jars)
170191
for this_dep in ctx.attr.deps:
171192
if hasattr(this_dep, 'java'):
172193
all_deps += this_dep.java.transitive_runtime_deps
@@ -175,8 +196,7 @@ def groovy_test_impl(ctx):
175196
classes = [path_to_class(src.path) for src in ctx.files.srcs]
176197

177198
# Write a file that executes JUnit on the inferred classes
178-
cmd = "%s %s -cp %s org.junit.runner.JUnitCore %s\n" % (
179-
ctx.executable._java.path,
199+
cmd = "external/local-jdk/bin/java %s -cp %s org.junit.runner.JUnitCore %s\n" % (
180200
" ".join(ctx.attr.jvm_flags),
181201
":".join([dep.short_path for dep in all_deps]),
182202
" ".join(classes),
@@ -188,7 +208,7 @@ def groovy_test_impl(ctx):
188208

189209
# Return all dependencies needed to run the tests
190210
return struct(
191-
runfiles=ctx.runfiles(files=list(all_deps) + ctx.files._java),
211+
runfiles=ctx.runfiles(files=list(all_deps) + ctx.files._jdk),
192212
)
193213

194214
groovy_test = rule(
@@ -197,12 +217,11 @@ groovy_test = rule(
197217
"srcs": attr.label_list(mandatory=True, allow_files=FileType([".groovy"])),
198218
"deps": attr.label_list(allow_files=FileType([".jar"])),
199219
"jvm_flags": attr.string_list(),
200-
"_java": attr.label(
201-
default=Label("@local-jdk//:java"),
202-
executable=True,
203-
single_file=True),
220+
"_groovysdk": attr.label(
221+
default=Label("//external:groovy-sdk")),
222+
"_jdk": attr.label(
223+
default=Label("//tools/defaults:jdk")),
204224
"_implicit_deps": attr.label_list(default=[
205-
Label("//external:groovy"),
206225
Label("//external:junit"),
207226
]),
208227
},

0 commit comments

Comments
 (0)