@@ -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
85101def groovy_library (name , srcs = [], deps = [], ** kwargs ):
@@ -135,7 +151,6 @@ def groovy_and_java_library(name, srcs=[], deps=[], **kwargs):
135151 ** kwargs
136152 )
137153
138-
139154def 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
167182def 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
194214groovy_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