15
15
"""Rules for supporting the Scala language."""
16
16
17
17
load ("//specs2:specs2_junit.bzl" , "specs2_junit_dependencies" )
18
- load (":scala_cross_version.bzl" , "scala_version" , "scala_mvn_artifact" )
19
- load ("@io_bazel_rules_scala//scala:scala_toolchain.bzl" , "scala_toolchain" )
20
-
21
18
_jar_filetype = FileType ([".jar" ])
22
19
_java_filetype = FileType ([".java" ])
23
20
_scala_filetype = FileType ([".scala" ])
@@ -69,7 +66,7 @@ def _adjust_resources_path(path, resource_strip_prefix):
69
66
def _add_resources_cmd (ctx ):
70
67
res_cmd = []
71
68
for f in ctx .files .resources :
72
- c_dir , res_path = _adjust_resources_path (f .short_path , ctx .attr .resource_strip_prefix )
69
+ c_dir , res_path = _adjust_resources_path (f .path , ctx .attr .resource_strip_prefix )
73
70
target_path = res_path
74
71
if target_path [0 ] == "/" :
75
72
target_path = target_path [1 :]
@@ -210,15 +207,17 @@ PrintCompileTime: {print_compile_time}
210
207
ResourceDests: {resource_dest}
211
208
ResourceJars: {resource_jars}
212
209
ResourceSrcs: {resource_src}
213
- ResourceShortPaths: {resource_short_paths}
214
210
ResourceStripPrefix: {resource_strip_prefix}
215
211
ScalacOpts: {scala_opts}
216
212
SourceJars: {srcjars}
217
213
DependencyAnalyzerMode: {dependency_analyzer_mode}
218
214
""" .format (
219
215
out = ctx .outputs .jar .path ,
220
216
manifest = ctx .outputs .manifest .path ,
221
- scala_opts = "," .join (scalacopts ),
217
+ # always append -YdisableFlatCpCaching, workaround for
218
+ # https://github.com/bazelbuild/rules_scala/issues/305
219
+ # remove once we upgrade to Scala 2.12.4
220
+ scala_opts = "," .join (ctx .attr .scalacopts + ["-YdisableFlatCpCaching" ]),
222
221
print_compile_time = ctx .attr .print_compile_time ,
223
222
plugin_arg = plugin_arg ,
224
223
cp = compiler_classpath ,
@@ -229,9 +228,8 @@ DependencyAnalyzerMode: {dependency_analyzer_mode}
229
228
srcjars = "," .join ([f .path for f in all_srcjars ]),
230
229
java_files = "," .join ([f .path for f in java_srcs ]),
231
230
resource_src = "," .join ([f .path for f in ctx .files .resources ]),
232
- resource_short_paths = "," .join ([f .short_path for f in ctx .files .resources ]),
233
231
resource_dest = "," .join (
234
- [_adjust_resources_path_by_default_prefixes (f .short_path )[1 ] for f in ctx .files .resources ]
232
+ [_adjust_resources_path_by_default_prefixes (f .path )[1 ] for f in ctx .files .resources ]
235
233
),
236
234
resource_strip_prefix = ctx .attr .resource_strip_prefix ,
237
235
resource_jars = "," .join ([f .path for f in ctx .files .resource_jars ]),
@@ -295,24 +293,6 @@ DependencyAnalyzerMode: {dependency_analyzer_mode}
295
293
return java_jar
296
294
297
295
298
- def _interim_java_provider_for_java_compilation (scala_output ):
299
- # This is needed because Bazel >=0.7.0 requires ctx.actions and a Java
300
- # toolchain. Fortunately, the same change that added this requirement also
301
- # added this field to the Java provider so we can use it to test which
302
- # Bazel version we are running under.
303
- test_provider = java_common .create_provider ()
304
- if hasattr (test_provider , "full_compile_jars" ):
305
- return java_common .create_provider (
306
- use_ijar = False ,
307
- compile_time_jars = [scala_output ],
308
- runtime_jars = [],
309
- )
310
- else :
311
- return java_common .create_provider (
312
- compile_time_jars = [scala_output ],
313
- runtime_jars = [],
314
- )
315
-
316
296
def try_to_compile_java_jar (ctx ,
317
297
scala_output ,
318
298
all_srcjars ,
@@ -323,7 +303,10 @@ def try_to_compile_java_jar(ctx,
323
303
324
304
providers_of_dependencies = collect_java_providers_of (ctx .attr .deps )
325
305
providers_of_dependencies += collect_java_providers_of (implicit_junit_deps_needed_for_java_compilation )
326
- scala_sources_java_provider = _interim_java_provider_for_java_compilation (scala_output )
306
+ scala_sources_java_provider = java_common .create_provider (
307
+ compile_time_jars = [scala_output ],
308
+ runtime_jars = [],
309
+ )
327
310
providers_of_dependencies += [scala_sources_java_provider ]
328
311
329
312
full_java_jar = ctx .actions .declare_file (ctx .label .name + "_java.jar" )
@@ -341,7 +324,6 @@ def try_to_compile_java_jar(ctx,
341
324
exports = [],
342
325
java_toolchain = ctx .attr ._java_toolchain ,
343
326
host_javabase = ctx .attr ._host_javabase ,
344
- strict_deps = ctx .fragments .java .strict_java_deps ,
345
327
)
346
328
return struct (jar = full_java_jar , ijar = provider .compile_jars .to_list ().pop ())
347
329
@@ -414,35 +396,21 @@ def _write_launcher(ctx, rjars, main_class, jvm_flags, args="", wrapper_preamble
414
396
# RUNPATH is defined here:
415
397
# https://github.com/bazelbuild/bazel/blob/0.4.5/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt#L227
416
398
classpath = ":" .join (["${RUNPATH}%s" % (j .short_path ) for j in rjars ])
417
-
418
- if ctx .attr .testonly :
419
- classpath = ":" .join (["%s" % (j .short_path ) for j in rjars ])
420
- classpath = "target/test-classes:%s" % classpath
421
-
422
399
jvm_flags = " " .join ([ctx .expand_location (f , ctx .attr .data ) for f in jvm_flags ])
423
400
javabin = "%s/%s" % (runfiles_root , ctx .executable ._java .short_path )
424
401
template = ctx .attr ._java_stub_template .files .to_list ()[0 ]
425
402
426
- exec_str = ""
427
- if wrapper_preamble == "" :
428
- exec_str = "exec "
429
-
430
403
wrapper = ctx .new_file (ctx .label .name + "_wrapper.sh" )
431
404
ctx .file_action (
432
405
output = wrapper ,
433
406
content = """#!/bin/bash
434
407
{preamble}
435
408
436
- rm -rf {runfiles_root}/target/test-classes
437
- mkdir -p {runfiles_root}/target/test-classes
438
-
439
- {exec_str}{javabin} "$@" {args}
409
+ {javabin} "$@" {args}
440
410
""" .format (
441
411
preamble = wrapper_preamble ,
442
- exec_str = exec_str ,
443
412
javabin = javabin ,
444
413
args = args ,
445
- runfiles_root = runfiles_root
446
414
),
447
415
)
448
416
@@ -469,17 +437,12 @@ def collect_srcjars(targets):
469
437
srcjars += [target .srcjars .srcjar ]
470
438
return srcjars
471
439
472
- def add_labels_of_jars_to (jars2labels , dependency , all_jars , direct_jars ):
473
- for jar in direct_jars :
474
- add_label_of_direct_jar_to (jars2labels , dependency , jar )
440
+ def add_labels_of_jars_to (jars2labels , dependency , all_jars ):
475
441
for jar in all_jars :
476
- add_label_of_indirect_jar_to (jars2labels , dependency , jar )
442
+ add_label_of_jar_to (jars2labels , dependency , jar )
477
443
478
444
479
- def add_label_of_direct_jar_to (jars2labels , dependency , jar ):
480
- jars2labels [jar .path ] = dependency .label
481
-
482
- def add_label_of_indirect_jar_to (jars2labels , dependency , jar ):
445
+ def add_label_of_jar_to (jars2labels , dependency , jar ):
483
446
if label_already_exists (jars2labels , jar ):
484
447
return
485
448
@@ -488,10 +451,8 @@ def add_label_of_indirect_jar_to(jars2labels, dependency, jar):
488
451
if provider_of_dependency_contains_label_of (dependency , jar ):
489
452
jars2labels [jar .path ] = dependency .jars_to_labels [jar .path ]
490
453
else :
491
- jars2labels [jar .path ] = "Unknown label of file {jar_path} which came from {dependency_label}" .format (
492
- jar_path = jar .path ,
493
- dependency_label = dependency .label
494
- )
454
+ jars2labels [jar .path ] = dependency .label
455
+
495
456
496
457
def label_already_exists (jars2labels , jar ):
497
458
return jar .path in jars2labels
@@ -517,19 +478,6 @@ def not_sources_jar(name):
517
478
def filter_not_sources (deps ):
518
479
return depset ([dep for dep in deps .to_list () if not_sources_jar (dep .basename ) ])
519
480
520
- def _collect_runtime_jars (dep_targets ):
521
- runtime_jars = depset ()
522
-
523
- for dep_target in dep_targets :
524
- if java_common .provider in dep_target :
525
- runtime_jars += dep_target [java_common .provider ].transitive_runtime_jars
526
- else :
527
- # support http_file pointed at a jar. http_jar uses ijar,
528
- # which breaks scala macros
529
- runtime_jars += filter_not_sources (dep_target .files )
530
-
531
- return runtime_jars
532
-
533
481
def _collect_jars_when_dependency_analyzer_is_off (dep_targets ):
534
482
compile_jars = depset ()
535
483
runtime_jars = depset ()
@@ -557,31 +505,26 @@ def _collect_jars_when_dependency_analyzer_is_on(dep_targets):
557
505
runtime_jars = depset ()
558
506
559
507
for dep_target in dep_targets :
560
- current_dep_compile_jars = depset ()
561
- current_dep_transitive_compile_jars = depset ()
562
-
563
508
if java_common .provider in dep_target :
564
509
java_provider = dep_target [java_common .provider ]
565
- current_dep_compile_jars = java_provider .compile_jars
566
- current_dep_transitive_compile_jars = java_provider .transitive_compile_time_jars
510
+ compile_jars + = java_provider .compile_jars
511
+ transitive_compile_jars + = java_provider .transitive_compile_time_jars + java_provider . compile_jars
567
512
runtime_jars += java_provider .transitive_runtime_jars
568
513
else :
569
514
# support http_file pointed at a jar. http_jar uses ijar,
570
515
# which breaks scala macros
571
- current_dep_compile_jars = filter_not_sources (dep_target .files )
516
+ compile_jars + = filter_not_sources (dep_target .files )
572
517
runtime_jars += filter_not_sources (dep_target .files )
573
- current_dep_transitive_compile_jars = filter_not_sources (dep_target .files )
518
+ transitive_compile_jars + = filter_not_sources (dep_target .files )
574
519
575
- compile_jars += current_dep_compile_jars
576
- transitive_compile_jars += current_dep_transitive_compile_jars
577
- add_labels_of_jars_to (jars2labels , dep_target , current_dep_transitive_compile_jars , current_dep_compile_jars )
520
+ add_labels_of_jars_to (jars2labels , dep_target , transitive_compile_jars )
578
521
579
522
return struct (compile_jars = compile_jars ,
580
523
transitive_runtime_jars = runtime_jars ,
581
524
jars2labels = jars2labels ,
582
525
transitive_compile_jars = transitive_compile_jars )
583
526
584
- def collect_jars (dep_targets , dependency_analyzer_is_off = True ):
527
+ def _collect_jars (dep_targets , dependency_analyzer_is_off = True ):
585
528
"""Compute the runtime and compile-time dependencies from the given targets""" # noqa
586
529
587
530
if dependency_analyzer_is_off :
@@ -609,26 +552,31 @@ def _collect_jars_from_common_ctx(ctx, extra_deps = [], extra_runtime_deps = [])
609
552
610
553
# Get jars from deps
611
554
auto_deps = [ctx .attr ._scalalib , ctx .attr ._scalareflect ]
612
- deps_jars = collect_jars (ctx .attr .deps + auto_deps + extra_deps , dependency_analyzer_is_off )
555
+ deps_jars = _collect_jars (ctx .attr .deps + auto_deps + extra_deps , dependency_analyzer_is_off )
613
556
(cjars , transitive_rjars , jars2labels , transitive_compile_jars ) = (deps_jars .compile_jars , deps_jars .transitive_runtime_jars , deps_jars .jars2labels , deps_jars .transitive_compile_jars )
614
557
615
- transitive_rjars += _collect_runtime_jars (ctx .attr .runtime_deps + extra_runtime_deps )
558
+ runtime_dep_jars = _collect_jars (ctx .attr .runtime_deps + extra_runtime_deps , dependency_analyzer_is_off )
559
+ transitive_rjars += runtime_dep_jars .transitive_runtime_jars
560
+
561
+ if not dependency_analyzer_is_off :
562
+ jars2labels .update (runtime_dep_jars .jars2labels )
616
563
617
564
return struct (compile_jars = cjars , transitive_runtime_jars = transitive_rjars , jars2labels = jars2labels , transitive_compile_jars = transitive_compile_jars )
618
565
619
566
def _format_full_jars_for_intellij_plugin (full_jars ):
620
567
return [struct (class_jar = jar , ijar = None ) for jar in full_jars ]
621
568
622
- def create_java_provider ( scalaattr , transitive_compile_time_jars ):
623
- # This is needed because Bazel >=0.7 .0 requires ctx.actions and a Java
569
+ def _create_java_provider ( ctx , scalaattr , transitive_compile_time_jars ):
570
+ # This is needed because Bazel >=0.6 .0 requires ctx.actions and a Java
624
571
# toolchain. Fortunately, the same change that added this requirement also
625
572
# added this field to the Java provider so we can use it to test which
626
573
# Bazel version we are running under.
627
574
test_provider = java_common .create_provider ()
628
575
629
576
if hasattr (test_provider , "full_compile_jars" ):
630
577
return java_common .create_provider (
631
- use_ijar = False ,
578
+ ctx .actions ,
579
+ java_toolchain = ctx .attr ._java_toolchain ,
632
580
compile_time_jars = scalaattr .compile_jars ,
633
581
runtime_jars = scalaattr .transitive_runtime_jars ,
634
582
transitive_compile_time_jars = transitive_compile_time_jars + scalaattr .compile_jars ,
@@ -672,7 +620,7 @@ def _lib(ctx, non_macro_lib):
672
620
# Add information from exports (is key that AFTER all build actions/runfiles analysis)
673
621
# Since after, will not show up in deploy_jar or old jars runfiles
674
622
# Notice that compile_jars is intentionally transitive for exports
675
- exports_jars = collect_jars (ctx .attr .exports )
623
+ exports_jars = _collect_jars (ctx .attr .exports )
676
624
next_cjars += exports_jars .compile_jars
677
625
transitive_rjars += exports_jars .transitive_runtime_jars
678
626
@@ -693,7 +641,7 @@ def _lib(ctx, non_macro_lib):
693
641
transitive_exports = [] #needed by intellij plugin
694
642
)
695
643
696
- java_provider = create_java_provider ( scalaattr , jars .transitive_compile_jars )
644
+ java_provider = _create_java_provider ( ctx , scalaattr , jars .transitive_compile_jars )
697
645
698
646
return struct (
699
647
files = depset ([ctx .outputs .jar ]), # Here is the default output
@@ -757,7 +705,7 @@ def _scala_binary_common(ctx, cjars, rjars, transitive_compile_time_jars, jars2l
757
705
transitive_exports = [] #needed by intellij plugin
758
706
)
759
707
760
- java_provider = create_java_provider ( scalaattr , transitive_compile_time_jars )
708
+ java_provider = _create_java_provider ( ctx , scalaattr , transitive_compile_time_jars )
761
709
762
710
return struct (
763
711
files = depset ([ctx .outputs .executable ]),
@@ -834,13 +782,13 @@ def _scala_test_impl(ctx):
834
782
jars .transitive_compile_jars , jars .jars2labels )
835
783
# _scalatest is an http_jar, so its compile jar is run through ijar
836
784
# however, contains macros, so need to handle separately
837
- scalatest_jars = collect_jars ([ctx .attr ._scalatest ]).transitive_runtime_jars
785
+ scalatest_jars = _collect_jars ([ctx .attr ._scalatest ]).transitive_runtime_jars
838
786
cjars += scalatest_jars
839
787
transitive_rjars += scalatest_jars
840
788
841
789
if is_dependency_analyzer_on (ctx ):
842
790
transitive_compile_jars += scalatest_jars
843
- add_labels_of_jars_to (jars_to_labels , ctx .attr ._scalatest , scalatest_jars , scalatest_jars )
791
+ add_labels_of_jars_to (jars_to_labels , ctx .attr ._scalatest , scalatest_jars )
844
792
845
793
args = " " .join ([
846
794
"-R \" {path}\" " .format (path = ctx .outputs .jar .short_path ),
@@ -1074,6 +1022,17 @@ scala_repl = rule(
1074
1022
toolchains = ['@io_bazel_rules_scala//scala:toolchain_type' ],
1075
1023
)
1076
1024
1025
+ def scala_version ():
1026
+ """return the scala version for use in maven coordinates"""
1027
+ return "2.12"
1028
+
1029
+ def scala_mvn_artifact (artifact ):
1030
+ gav = artifact .split (":" )
1031
+ groupid = gav [0 ]
1032
+ artifactid = gav [1 ]
1033
+ version = gav [2 ]
1034
+ return "%s:%s_%s:%s" % (groupid , artifactid , scala_version (), version )
1035
+
1077
1036
SCALA_BUILD_FILE = """
1078
1037
# scala.BUILD
1079
1038
java_import(
@@ -1116,9 +1075,9 @@ java_library(
1116
1075
def scala_repositories ():
1117
1076
native .new_http_archive (
1118
1077
name = "scala" ,
1119
- strip_prefix = "scala-2.12.4 " ,
1120
- sha256 = "9554a0ca31aa8701863e881281b1772370a87e993ce785bb24505f2431292a21 " ,
1121
- url = "https://downloads.lightbend.com/scala/2.12.4 /scala-2.12.4 .tgz" ,
1078
+ strip_prefix = "scala-2.12.3 " ,
1079
+ sha256 = "2b796ab773fbedcc734ba881a6486d54180b699ade8ba7493e91912044267c8c " ,
1080
+ url = "https://downloads.lightbend.com/scala/2.12.3 /scala-2.12.3 .tgz" ,
1122
1081
build_file_content = SCALA_BUILD_FILE ,
1123
1082
)
1124
1083
@@ -1138,7 +1097,7 @@ def scala_repositories():
1138
1097
1139
1098
native .maven_server (
1140
1099
name = "scalac_deps_maven_server" ,
1141
- url = "https ://mirror.bazel.build/repo1.maven.org/maven2/" ,
1100
+ url = "http ://mirror.bazel.build/repo1.maven.org/maven2/" ,
1142
1101
)
1143
1102
1144
1103
native .maven_jar (
0 commit comments