-
Notifications
You must be signed in to change notification settings - Fork 281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate from java_common.create_provider to JavaInfo() #781
Changes from all commits
597e84d
31f9abd
0cd63ef
819fba3
e782e99
f412410
56b7294
c5b3d88
ac7887d
483928f
d4a6f45
0edf49b
408f51d
86a229e
549f2e0
c27ca9b
99a3f7e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ hash1 | |
hash2 | ||
.DS_store | ||
.bazel_cache | ||
.ijwb |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
public class PlaceHolderClassToCreateEmptyJarForScalaImport { } |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,6 @@ load( | |
"collect_jars", | ||
"collect_plugin_paths", | ||
"collect_srcjars", | ||
"create_java_provider", | ||
"not_sources_jar", | ||
"write_manifest", | ||
) | ||
|
@@ -318,10 +317,10 @@ StatsfileOutput: {statsfile_output} | |
) | ||
|
||
def _interim_java_provider_for_java_compilation(scala_output): | ||
return java_common.create_provider( | ||
use_ijar = False, | ||
compile_time_jars = [scala_output], | ||
runtime_jars = [], | ||
return JavaInfo( | ||
output_jar = scala_output, | ||
compile_jar = scala_output, | ||
neverlink = True | ||
) | ||
|
||
def _scalac_provider(ctx): | ||
|
@@ -371,10 +370,12 @@ def try_to_compile_java_jar( | |
host_javabase = find_java_runtime_toolchain(ctx, ctx.attr._host_javabase), | ||
strict_deps = ctx.fragments.java.strict_java_deps, | ||
) | ||
|
||
return struct( | ||
ijar = provider.compile_jars.to_list().pop(), | ||
jar = full_java_jar, | ||
source_jars = provider.source_jars, | ||
java_compilation_provider = provider | ||
) | ||
|
||
def collect_java_providers_of(deps): | ||
|
@@ -394,11 +395,14 @@ def _compile_or_empty( | |
jars2labels, | ||
implicit_junit_deps_needed_for_java_compilation, | ||
unused_dependency_checker_mode, | ||
unused_dependency_checker_ignored_targets): | ||
unused_dependency_checker_ignored_targets, | ||
deps_providers): | ||
# We assume that if a srcjar is present, it is not empty | ||
if len(ctx.files.srcs) + len(srcjars.to_list()) == 0: | ||
_build_nosrc_jar(ctx) | ||
|
||
scala_compilation_provider = _create_scala_compilation_provider(ctx, ctx.outputs.jar, deps_providers) | ||
|
||
# no need to build ijar when empty | ||
return struct( | ||
class_jar = ctx.outputs.jar, | ||
|
@@ -408,6 +412,7 @@ def _compile_or_empty( | |
ijars = [ctx.outputs.jar], | ||
java_jar = False, | ||
source_jars = [], | ||
merged_provider = scala_compilation_provider | ||
) | ||
else: | ||
in_srcjars = [ | ||
|
@@ -471,6 +476,8 @@ def _compile_or_empty( | |
# so set ijar == jar | ||
ijar = ctx.outputs.jar | ||
|
||
scala_compilation_provider = _create_scala_compilation_provider(ctx, ijar, deps_providers) | ||
|
||
# compile the java now | ||
java_jar = try_to_compile_java_jar( | ||
ctx, | ||
|
@@ -490,6 +497,11 @@ def _compile_or_empty( | |
|
||
coverage = _jacoco_offline_instrument(ctx, ctx.outputs.jar) | ||
|
||
if java_jar: | ||
merged_provider = java_common.merge([scala_compilation_provider, java_jar.java_compilation_provider]) | ||
else: | ||
merged_provider = scala_compilation_provider | ||
|
||
return struct( | ||
class_jar = ctx.outputs.jar, | ||
coverage = coverage, | ||
|
@@ -498,8 +510,24 @@ def _compile_or_empty( | |
ijars = ijars, | ||
java_jar = java_jar, | ||
source_jars = source_jars, | ||
merged_provider = merged_provider | ||
) | ||
|
||
def _create_scala_compilation_provider(ctx, ijar, deps_providers): | ||
exports = [] | ||
if hasattr(ctx.attr, "exports"): | ||
exports = [dep[JavaInfo] for dep in ctx.attr.exports] | ||
runtime_deps = [] | ||
if hasattr(ctx.attr, "runtime_deps"): | ||
runtime_deps = [dep[JavaInfo] for dep in ctx.attr.runtime_deps] | ||
return JavaInfo( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just realized that this doesn’t pass the source_jar. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure which is the source jar here. Is it |
||
output_jar = ctx.outputs.jar, | ||
compile_jar = ijar, | ||
deps = deps_providers, | ||
exports = exports, | ||
runtime_deps = runtime_deps, | ||
) | ||
|
||
def _build_deployable(ctx, jars_list): | ||
# This calls bazels singlejar utility. | ||
# For a full list of available command line options see: | ||
|
@@ -725,11 +753,13 @@ def _collect_jars_from_common_ctx( | |
transitive_rjars, | ||
jars2labels, | ||
transitive_compile_jars, | ||
deps_providers | ||
) = ( | ||
deps_jars.compile_jars, | ||
deps_jars.transitive_runtime_jars, | ||
deps_jars.jars2labels, | ||
deps_jars.transitive_compile_jars, | ||
deps_jars.deps_providers | ||
) | ||
|
||
transitive_rjars = depset( | ||
|
@@ -742,6 +772,7 @@ def _collect_jars_from_common_ctx( | |
jars2labels = jars2labels, | ||
transitive_compile_jars = transitive_compile_jars, | ||
transitive_runtime_jars = transitive_rjars, | ||
deps_providers = deps_providers, | ||
) | ||
|
||
def _lib( | ||
|
@@ -781,6 +812,7 @@ def _lib( | |
unused_dependency_checker_ignored_targets | ||
], | ||
unused_dependency_checker_mode = unused_dependency_checker_mode, | ||
deps_providers = jars.deps_providers, | ||
) | ||
|
||
transitive_rjars = depset(outputs.full_jars, transitive = [transitive_rjars]) | ||
|
@@ -817,13 +849,11 @@ def _lib( | |
transitive_runtime_jars = transitive_rjars, | ||
) | ||
|
||
java_provider = create_java_provider(scalaattr, jars.transitive_compile_jars) | ||
|
||
return struct( | ||
files = depset([ctx.outputs.jar] + outputs.full_jars), # Here is the default output | ||
instrumented_files = outputs.coverage.instrumented_files, | ||
jars_to_labels = jars.jars2labels, | ||
providers = [java_provider, jars.jars2labels] + outputs.coverage.providers, | ||
providers = [outputs.merged_provider, jars.jars2labels] + outputs.coverage.providers, | ||
runfiles = runfiles, | ||
scala = scalaattr, | ||
) | ||
|
@@ -877,6 +907,7 @@ def _scala_binary_common( | |
java_wrapper, | ||
unused_dependency_checker_mode, | ||
unused_dependency_checker_ignored_targets, | ||
deps_providers, | ||
implicit_junit_deps_needed_for_java_compilation = [], | ||
runfiles_ext = []): | ||
write_manifest(ctx) | ||
|
@@ -892,6 +923,7 @@ def _scala_binary_common( | |
unused_dependency_checker_ignored_targets = | ||
unused_dependency_checker_ignored_targets, | ||
unused_dependency_checker_mode = unused_dependency_checker_mode, | ||
deps_providers = deps_providers, | ||
) # no need to build an ijar for an executable | ||
rjars = depset(outputs.full_jars, transitive = [rjars]) | ||
|
||
|
@@ -918,14 +950,12 @@ def _scala_binary_common( | |
transitive_runtime_jars = rjars, | ||
) | ||
|
||
java_provider = create_java_provider(scalaattr, transitive_compile_time_jars) | ||
|
||
return struct( | ||
executable = executable, | ||
coverage = outputs.coverage, | ||
files = depset([executable, ctx.outputs.jar]), | ||
instrumented_files = outputs.coverage.instrumented_files, | ||
providers = [java_provider, jars2labels] + outputs.coverage.providers, | ||
providers = [outputs.merged_provider, jars2labels] + outputs.coverage.providers, | ||
runfiles = runfiles, | ||
scala = scalaattr, | ||
transitive_rjars = | ||
|
@@ -991,6 +1021,7 @@ def scala_binary_impl(ctx): | |
ctx.attr.unused_dependency_checker_ignored_targets | ||
], | ||
unused_dependency_checker_mode = unused_dependency_checker_mode, | ||
deps_providers = jars.deps_providers, | ||
) | ||
_write_executable( | ||
ctx = ctx, | ||
|
@@ -1054,6 +1085,7 @@ trap finish EXIT | |
ctx.attr.unused_dependency_checker_ignored_targets | ||
], | ||
unused_dependency_checker_mode = unused_dependency_checker_mode, | ||
deps_providers = jars.deps_providers, | ||
) | ||
_write_executable( | ||
ctx = ctx, | ||
|
@@ -1140,6 +1172,7 @@ def scala_test_impl(ctx): | |
unused_dependency_checker_ignored_targets, | ||
unused_dependency_checker_mode = unused_dependency_checker_mode, | ||
runfiles_ext = [argsFile], | ||
deps_providers = jars.deps_providers, | ||
) | ||
|
||
rjars = out.transitive_rjars | ||
|
@@ -1265,6 +1298,7 @@ def scala_junit_test_impl(ctx): | |
unused_dependency_checker_ignored_targets = | ||
unused_dependency_checker_ignored_targets, | ||
unused_dependency_checker_mode = unused_dependency_checker_mode, | ||
deps_providers = jars.deps_providers, | ||
) | ||
|
||
if ctx.attr.tests_from: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I need to read the code a bit more in depth but from what I remember this won't work because this way we won't have the java code present at runtime. I think the whole "interim" approach is now wrong and was needed before.
I might be mistaken but my hunch is that a bit more of a refactor is needed to have the scala compilation propagate out the JavaInfo of the java (or a different refactoring)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason why
runtime_jars
was previously empty?IIUC this provider is needed to compile Java sources with scala dependencies. Does scala also need this provider?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Context:
We call java_common.compile with all inputs to the rule (deps/runtime_deps/etc). In addition we need to give it a provider representing the output of scalac we ran a second before (javac compilation depends on scalac compilation). This solution was a hack meant to generate a provider which will be added on top of the other deps
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also now that I think about it a bit more then maybe I'm wrong and your solution does work. This will mean that javac will get an ijar of the scalac code and it won't propagate it onwards since it's neverlink. Sounds good actually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, exactly. This only works because it's neverlink, otherwise we need to pass in the full jar as well (output_jar=full_jar, compile_jar=ijar) to avoid having an ijar at runtime.