1111 "SCALA_VERSION" ,
1212)
1313
14- def dt_patched_compiler_setup ():
14+ def _dt_patched_compiler_impl (rctx ):
15+ # Need to give the file a .zip extension so rctx.extract knows what type of archive it is
16+ rctx .symlink (rctx .attr .srcjar , "file.zip" )
17+ rctx .extract (archive = "file.zip" )
18+ rctx .patch (rctx .attr .patch )
19+ rctx .file ("BUILD" , content = rctx .attr .build_file_content )
20+
21+ dt_patched_compiler = repository_rule (
22+ attrs = {
23+ "patch" : attr .label (),
24+ "srcjar" : attr .label (),
25+ "build_file_content" : attr .string (),
26+ },
27+ implementation = _dt_patched_compiler_impl ,
28+ )
29+
30+ def _validate_scalac_srcjar (srcjar ):
31+ if type (srcjar ) != "dict" :
32+ return False
33+ oneof = ["url" , "urls" , "label" ]
34+ count = 0
35+ for key in oneof :
36+ if key in srcjar :
37+ count += 1
38+ return count == 1
39+
40+ def dt_patched_compiler_setup (scala_compiler_srcjar = None ):
1541 patch = "@io_bazel_rules_scala//dt_patches:dt_compiler_%s.patch" % SCALA_MAJOR_VERSION
1642
1743 minor_version = int (SCALA_MINOR_VERSION )
@@ -22,20 +48,40 @@ def dt_patched_compiler_setup():
2248 elif minor_version <= 11 :
2349 patch = "@io_bazel_rules_scala//dt_patches:dt_compiler_%s.8.patch" % SCALA_MAJOR_VERSION
2450
25- http_archive (
26- name = "scala_compiler_source" ,
27- build_file_content = "\n " .join ([
28- "package(default_visibility = [\" //visibility:public\" ])" ,
29- "filegroup(" ,
30- " name = \" src\" ," ,
31- " srcs=[\" scala/tools/nsc/symtab/SymbolLoaders.scala\" ]," ,
32- ")" ,
33- ]),
34- patches = [patch ],
35- url = "https://repo1.maven.org/maven2/org/scala-lang/scala-compiler/%s/scala-compiler-%s-sources.jar" % (SCALA_VERSION , SCALA_VERSION ),
51+ build_file_content = "\n " .join ([
52+ "package(default_visibility = [\" //visibility:public\" ])" ,
53+ "filegroup(" ,
54+ " name = \" src\" ," ,
55+ " srcs=[\" scala/tools/nsc/symtab/SymbolLoaders.scala\" ]," ,
56+ ")" ,
57+ ])
58+ default_scalac_srcjar = {
59+ "url" : "https://repo1.maven.org/maven2/org/scala-lang/scala-compiler/%s/scala-compiler-%s-sources.jar" % (SCALA_VERSION , SCALA_VERSION ),
60+ }
61+ srcjar = scala_compiler_srcjar if scala_compiler_srcjar != None else default_scalac_srcjar
62+ _validate_scalac_srcjar (srcjar ) or fail (
63+ ("scala_compiler_srcjar invalid, must be a dict with exactly one of \" label\" , \" url\" " +
64+ " or \" urls\" keys, got: " ) + repr (srcjar ),
3665 )
66+ if "label" in srcjar :
67+ dt_patched_compiler (
68+ name = "scala_compiler_source" ,
69+ build_file_content = build_file_content ,
70+ patch = patch ,
71+ srcjar = srcjar ["label" ],
72+ )
73+ else :
74+ http_archive (
75+ name = "scala_compiler_source" ,
76+ build_file_content = build_file_content ,
77+ patches = [patch ],
78+ url = srcjar .get ("url" ),
79+ urls = srcjar .get ("urls" ),
80+ sha256 = srcjar .get ("sha256" ),
81+ integrity = srcjar .get ("integrity" ),
82+ )
3783
38- def rules_scala_setup ():
84+ def rules_scala_setup (scala_compiler_srcjar = None ):
3985 if not native .existing_rule ("bazel_skylib" ):
4086 http_archive (
4187 name = "bazel_skylib" ,
@@ -74,7 +120,7 @@ def rules_scala_setup():
74120 ],
75121 )
76122
77- dt_patched_compiler_setup ()
123+ dt_patched_compiler_setup (scala_compiler_srcjar )
78124
79125ARTIFACT_IDS = [
80126 "io_bazel_rules_scala_scala_library" ,
0 commit comments