@@ -149,8 +149,7 @@ def _collect_plugin_paths(plugins):
149
149
return paths
150
150
151
151
152
- def _compile (ctx , _jars , dep_srcjars , buildijar ):
153
- jars = _jars
152
+ def _compile (ctx , cjars , dep_srcjars , buildijar ):
154
153
ijar_output_path = ""
155
154
ijar_cmd_path = ""
156
155
if buildijar :
@@ -165,7 +164,7 @@ def _compile(ctx, _jars, dep_srcjars, buildijar):
165
164
plugins = _collect_plugin_paths (ctx .attr .plugins )
166
165
plugin_arg = "," .join (list (plugins ))
167
166
168
- compiler_classpath = ":" .join ([j .path for j in jars ])
167
+ compiler_classpath = ":" .join ([j .path for j in cjars ])
169
168
170
169
scalac_args = """
171
170
Classpath: {cp}
@@ -222,7 +221,7 @@ SourceJars: {srcjars}
222
221
# _jdk added manually since _java doesn't currently setup runfiles
223
222
# _scalac, as a java_binary, should already have it in its runfiles; however,
224
223
# adding does ensure _java not orphaned if _scalac ever was not a java_binary
225
- ins = (list (jars ) +
224
+ ins = (list (cjars ) +
226
225
list (dep_srcjars ) +
227
226
list (srcjars ) +
228
227
list (sources ) +
@@ -355,32 +354,20 @@ def collect_srcjars(targets):
355
354
356
355
def _collect_jars (targets ):
357
356
"""Compute the runtime and compile-time dependencies from the given targets""" # noqa
358
- compile_jars = set ()
359
- runtime_jars = set ()
357
+ compile_jars = depset ()
358
+ runtime_jars = depset ()
360
359
for target in targets :
361
- found = False
362
- if hasattr (target , "scala" ):
363
- if hasattr (target .scala .outputs , "ijar" ):
364
- compile_jars += [target .scala .outputs .ijar ]
365
- compile_jars += target .scala .transitive_compile_exports
366
- runtime_jars += target .scala .transitive_runtime_deps
367
- runtime_jars += target .scala .transitive_runtime_exports
368
- found = True
369
- if hasattr (target , "java" ):
370
- # see JavaSkylarkApiProvider.java,
371
- # this is just the compile-time deps
372
- # this should be improved in bazel 0.1.5 to get outputs.ijar
373
- # compile_jars += [output.ijar for output in target.java.outputs.jars]
374
- compile_jars += target .java .transitive_deps
375
- runtime_jars += target .java .transitive_runtime_deps
376
- found = True
377
- if not found :
360
+ if java_common .provider in target :
361
+ java_provider = target [java_common .provider ]
362
+ compile_jars += java_provider .compile_jars
363
+ runtime_jars += java_provider .transitive_runtime_jars
364
+ else :
378
365
# support http_file pointed at a jar. http_jar uses ijar,
379
366
# which breaks scala macros
380
- runtime_jars += target .files
381
367
compile_jars += target .files
368
+ runtime_jars += target .files
382
369
383
- return struct (compiletime = compile_jars , runtime = runtime_jars )
370
+ return struct (compile_jars = compile_jars , transitive_runtime_jars = runtime_jars )
384
371
385
372
# Extract very common code out from dependency analysis into single place
386
373
# automatically adds dependency on scala-library and scala-reflect
@@ -389,42 +376,67 @@ def _collect_jars_from_common_ctx(ctx, extra_deps = [], extra_runtime_deps = [])
389
376
# Get jars from deps
390
377
auto_deps = [ctx .attr ._scalalib , ctx .attr ._scalareflect ]
391
378
deps_jars = _collect_jars (ctx .attr .deps + auto_deps + extra_deps )
392
- (cjars , rjars ) = (deps_jars .compiletime , deps_jars .runtime )
393
- rjars += _collect_jars (ctx .attr .runtime_deps + extra_runtime_deps ).runtime
394
- return struct (compiletime = cjars , runtime = rjars )
379
+ (cjars , transitive_rjars ) = (deps_jars .compile_jars , deps_jars .transitive_runtime_jars )
380
+ transitive_rjars += _collect_jars (
381
+ ctx .attr .runtime_deps + extra_runtime_deps ).transitive_runtime_jars
382
+ return struct (compile_jars = cjars , transitive_runtime_jars = transitive_rjars )
395
383
396
384
def _lib (ctx , non_macro_lib ):
385
+ # Build up information from dependency-like attributes
386
+
397
387
# This will be used to pick up srcjars from non-scala library
398
388
# targets (like thrift code generation)
399
389
srcjars = collect_srcjars (ctx .attr .deps )
400
390
jars = _collect_jars_from_common_ctx (ctx )
401
- (cjars , rjars ) = (jars .compiletime , jars .runtime )
391
+ (cjars , transitive_rjars ) = (jars .compile_jars , jars .transitive_runtime_jars )
402
392
403
393
write_manifest (ctx )
404
394
outputs = _compile_or_empty (ctx , cjars , srcjars , non_macro_lib )
405
395
406
- rjars += [ctx .outputs .jar ]
396
+ transitive_rjars += [ctx .outputs .jar ]
407
397
408
- _build_deployable (ctx , rjars )
409
- rule_outputs = struct (ijar = outputs .ijar , class_jar = outputs .class_jar , deploy_jar = ctx .outputs .deploy_jar )
398
+ _build_deployable (ctx , transitive_rjars )
410
399
411
- texp = _collect_jars (ctx .attr .exports )
412
- scalaattr = struct (outputs = rule_outputs ,
413
- transitive_runtime_deps = rjars ,
414
- transitive_compile_exports = texp .compiletime ,
415
- transitive_runtime_exports = texp .runtime
416
- )
400
+ # Now, need to setup providers for dependents
401
+ # Notice that transitive_rjars just carries over from dependency analysis
402
+ # but cjars 'resets' between cjars and next_cjars
403
+ next_cjars = depset ([outputs .ijar ]) # use ijar, if available, for future compiles
417
404
418
- # Note that rjars already transitive so don't really
419
- # need to use transitive_files with _get_all_runfiles
405
+ # Using transitive_files since transitive_rjars a depset and avoiding linearization
420
406
runfiles = ctx .runfiles (
421
- files = list (rjars ),
422
- collect_data = True )
407
+ transitive_files = transitive_rjars ,
408
+ collect_data = True ,
409
+ )
410
+
411
+ # Add information from exports (is key that AFTER all build actions/runfiles analysis)
412
+ # Since after, will not show up in deploy_jar or old jars runfiles
413
+ # Notice that compile_jars is intentionally transitive for exports
414
+ exports_jars = _collect_jars (ctx .attr .exports )
415
+ next_cjars += exports_jars .compile_jars
416
+ transitive_rjars += exports_jars .transitive_runtime_jars
417
+
418
+ rule_outputs = struct (
419
+ ijar = outputs .ijar ,
420
+ class_jar = outputs .class_jar ,
421
+ deploy_jar = ctx .outputs .deploy_jar ,
422
+ )
423
+ # Note that, internally, rules only care about compile_jars and transitive_runtime_jars
424
+ # in a similar manner as the java_library and JavaProvider
425
+ scalaattr = struct (
426
+ outputs = rule_outputs ,
427
+ compile_jars = next_cjars ,
428
+ transitive_runtime_jars = transitive_rjars ,
429
+ )
430
+ java_provider = java_common .create_provider (
431
+ compile_time_jars = scalaattr .compile_jars ,
432
+ runtime_jars = scalaattr .transitive_runtime_jars ,
433
+ )
423
434
424
435
return struct (
425
- files = set ([ctx .outputs .jar ]), # Here is the default output
426
- scala = scalaattr ,
427
- runfiles = runfiles ,
436
+ files = depset ([ctx .outputs .jar ]), # Here is the default output
437
+ scala = scalaattr ,
438
+ providers = [java_provider ],
439
+ runfiles = runfiles ,
428
440
# This is a free monoid given to the graph for the purpose of
429
441
# extensibility. This is necessary when one wants to create
430
442
# new targets which want to leverage a scala_library. For example,
@@ -470,39 +482,39 @@ def _scala_binary_common(ctx, cjars, rjars):
470
482
class_jar = outputs .class_jar ,
471
483
deploy_jar = ctx .outputs .deploy_jar ,
472
484
)
473
- scalaattr = struct (outputs = rule_outputs ,
474
- transitive_runtime_deps = rjars ,
475
- transitive_compile_exports = set ( ),
476
- transitive_runtime_exports = set ()
477
- )
485
+ scalaattr = struct (
486
+ outputs = rule_outputs ,
487
+ compile_jars = depset ([ outputs . class_jar ] ),
488
+ transitive_runtime_jars = rjars ,
489
+ )
478
490
return struct (
479
491
files = set ([ctx .outputs .executable ]),
480
492
scala = scalaattr ,
481
493
runfiles = runfiles )
482
494
483
495
def _scala_binary_impl (ctx ):
484
496
jars = _collect_jars_from_common_ctx (ctx )
485
- (cjars , rjars ) = (jars .compiletime , jars .runtime )
486
- rjars += [ctx .outputs .jar ]
497
+ (cjars , transitive_rjars ) = (jars .compile_jars , jars .transitive_runtime_jars )
498
+ transitive_rjars += [ctx .outputs .jar ]
487
499
488
500
_write_launcher (
489
501
ctx = ctx ,
490
- rjars = rjars ,
502
+ rjars = transitive_rjars ,
491
503
main_class = ctx .attr .main_class ,
492
504
jvm_flags = ctx .attr .jvm_flags ,
493
505
)
494
- return _scala_binary_common (ctx , cjars , rjars )
506
+ return _scala_binary_common (ctx , cjars , transitive_rjars )
495
507
496
508
def _scala_repl_impl (ctx ):
497
509
# need scala-compiler for MainGenericRunner below
498
510
jars = _collect_jars_from_common_ctx (ctx , extra_runtime_deps = [ctx .attr ._scalacompiler ])
499
- (cjars , rjars ) = (jars .compiletime , jars .runtime )
500
- rjars += [ctx .outputs .jar ]
511
+ (cjars , transitive_rjars ) = (jars .compile_jars , jars .transitive_runtime_jars )
512
+ transitive_rjars += [ctx .outputs .jar ]
501
513
502
514
args = " " .join (ctx .attr .scalacopts )
503
515
_write_launcher (
504
516
ctx = ctx ,
505
- rjars = rjars ,
517
+ rjars = transitive_rjars ,
506
518
main_class = "scala.tools.nsc.MainGenericRunner" ,
507
519
jvm_flags = ["-Dscala.usejavacp=true" ] + ctx .attr .jvm_flags ,
508
520
args = args ,
@@ -522,7 +534,7 @@ trap finish EXIT
522
534
""" ,
523
535
)
524
536
525
- return _scala_binary_common (ctx , cjars , rjars )
537
+ return _scala_binary_common (ctx , cjars , transitive_rjars )
526
538
527
539
def _scala_test_impl (ctx ):
528
540
if len (ctx .attr .suites ) != 0 :
@@ -532,14 +544,14 @@ def _scala_test_impl(ctx):
532
544
jars = _collect_jars_from_common_ctx (ctx ,
533
545
extra_runtime_deps = [ctx .attr ._scalatest_reporter , ctx .attr ._scalatest_runner ],
534
546
)
535
- (cjars , rjars ) = (jars .compiletime , jars .runtime )
547
+ (cjars , transitive_rjars ) = (jars .compile_jars , jars .transitive_runtime_jars )
536
548
# _scalatest is an http_jar, so its compile jar is run through ijar
537
549
# however, contains macros, so need to handle separately
538
- scalatest_jars = _collect_jars ([ctx .attr ._scalatest ]).runtime
550
+ scalatest_jars = _collect_jars ([ctx .attr ._scalatest ]).transitive_runtime_jars
539
551
cjars += scalatest_jars
540
- rjars += scalatest_jars
552
+ transitive_rjars += scalatest_jars
541
553
542
- rjars += [ctx .outputs .jar ]
554
+ transitive_rjars += [ctx .outputs .jar ]
543
555
544
556
args = " " .join ([
545
557
"-R \" {path}\" " .format (path = ctx .outputs .jar .short_path ),
@@ -549,12 +561,12 @@ def _scala_test_impl(ctx):
549
561
# main_class almost has to be "org.scalatest.tools.Runner" due to args....
550
562
_write_launcher (
551
563
ctx = ctx ,
552
- rjars = rjars ,
564
+ rjars = transitive_rjars ,
553
565
main_class = ctx .attr .main_class ,
554
566
jvm_flags = ctx .attr .jvm_flags ,
555
567
args = args ,
556
568
)
557
- return _scala_binary_common (ctx , cjars , rjars )
569
+ return _scala_binary_common (ctx , cjars , transitive_rjars )
558
570
559
571
def _gen_test_suite_flags_based_on_prefixes_and_suffixes (ctx , archive ):
560
572
return struct (testSuiteFlag = "-Dbazel.test_suite=io.bazel.rulesscala.test_discovery.DiscoveredTestSuite" ,
@@ -569,20 +581,20 @@ def _scala_junit_test_impl(ctx):
569
581
jars = _collect_jars_from_common_ctx (ctx ,
570
582
extra_deps = [ctx .attr ._junit , ctx .attr ._hamcrest , ctx .attr ._suite , ctx .attr ._bazel_test_runner ],
571
583
)
572
- (cjars , rjars ) = (jars .compiletime , jars .runtime )
584
+ (cjars , transitive_rjars ) = (jars .compile_jars , jars .transitive_runtime_jars )
573
585
574
- rjars += [ctx .outputs .jar ]
586
+ transitive_rjars += [ctx .outputs .jar ]
575
587
576
588
test_suite = _gen_test_suite_flags_based_on_prefixes_and_suffixes (ctx , ctx .outputs .jar )
577
589
launcherJvmFlags = ["-ea" , test_suite .archiveFlag , test_suite .prefixesFlag , test_suite .suffixesFlag , test_suite .printFlag , test_suite .testSuiteFlag ]
578
590
_write_launcher (
579
591
ctx = ctx ,
580
- rjars = rjars ,
592
+ rjars = transitive_rjars ,
581
593
main_class = "com.google.testing.junit.runner.BazelTestRunner" ,
582
594
jvm_flags = launcherJvmFlags + ctx .attr .jvm_flags ,
583
595
)
584
596
585
- return _scala_binary_common (ctx , cjars , rjars )
597
+ return _scala_binary_common (ctx , cjars , transitive_rjars )
586
598
587
599
_launcher_template = {
588
600
"_java_stub_template" : attr .label (default = Label ("@java_stub_template//file" )),
@@ -786,18 +798,6 @@ def scala_repositories():
786
798
787
799
native .bind (name = "io_bazel_rules_scala/dependency/scalatest/scalatest" , actual = "@scalatest//jar" )
788
800
789
- def scala_export_to_java (name , exports , runtime_deps ):
790
- jars = []
791
- for target in exports :
792
- jars .append ("{}_deploy.jar" .format (target ))
793
-
794
- native .java_import (
795
- name = name ,
796
- # these are the outputs of the scala_library targets
797
- jars = jars ,
798
- runtime_deps = ["//external:io_bazel_rules_scala/dependency/scala/scala_library" ] + runtime_deps
799
- )
800
-
801
801
def _sanitize_string_for_usage (s ):
802
802
res_array = []
803
803
for c in s :
0 commit comments