Skip to content

Commit 16ba1d8

Browse files
authored
final phase is inlined (#947)
all phases are equal DefaultInfo is returned by a default_info phase Other external providers are passed by their respective phases
1 parent 1a6ac1b commit 16ba1d8

12 files changed

+64
-60
lines changed

docs/customizable_phase.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,14 @@ Currently phase architecture is used by 7 rules:
128128
- scala_junit_test
129129
- scala_repl
130130

131-
In each of the rule implementation, it calls `run_phases` and returns the information from `phase_final`, which groups the final returns of the rule. To prevent consumers from accidently removing `phase_final` from the list, we make it a non-customizable phase.
131+
If you need to expose providers to downstream targets you need to return an array of providers from your phase under the `external_providers` attribute.
132+
133+
In each of the rule implementations, it calls `run_phases` and returns an accumulated `external_providers` array declared by the phases.
132134

133135
To make a new phase, you have to define a new `phase_<PHASE_NAME>.bzl` in `scala/private/phases/`. Function definition should have 2 arguments, `ctx` and `p`. You may expose the information for later phases by returning a `struct`. In some phases, there are multiple phase functions since different rules may take slightly different input arguemnts. You may want to re-expose the phase definition in `scala/private/phases/phases.bzl`, so it's more convenient to access in rule files.
134136

135137
In the rule implementations, put your new phase in `builtin_customizable_phases` list. The phases are executed sequentially, the order matters if the new phase depends on previous phases.
136138

137-
If you are making new return fields of the rule, remember to modify `phase_final`.
138-
139139
### Phase naming convention
140140
Files in `scala/private/phases/`
141141
- `phase_<PHASE_NAME>.bzl`: phase definition file

scala/private/phases/api.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def _adjust_phases(phases, adjustments):
4141
return phases
4242

4343
# Execute phases
44-
def run_phases(ctx, builtin_customizable_phases, fixed_phase):
44+
def run_phases(ctx, builtin_customizable_phases):
4545
# Loading custom phases
4646
# Phases must be passed in by provider
4747
phase_providers = [
@@ -64,7 +64,7 @@ def run_phases(ctx, builtin_customizable_phases, fixed_phase):
6464
global_provider = {}
6565
current_provider = struct(**global_provider)
6666
acculmulated_external_providers = []
67-
for (name, function) in adjusted_phases + [fixed_phase]:
67+
for (name, function) in adjusted_phases:
6868
# Run a phase
6969
new_provider = function(ctx, current_provider)
7070

@@ -77,7 +77,7 @@ def run_phases(ctx, builtin_customizable_phases, fixed_phase):
7777
current_provider = struct(**global_provider)
7878

7979
# The final return of rules implementation
80-
return acculmulated_external_providers + current_provider.final
80+
return acculmulated_external_providers
8181

8282
# A method to pass in phase provider
8383
def extras_phases(extras):

scala/private/phases/phase_collect_jars.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def _phase_collect_jars(
108108
transitive_compile_jars = transitive_compile_jars,
109109
transitive_runtime_jars = transitive_rjars,
110110
deps_providers = deps_providers,
111+
external_providers = [jars2labels],
111112
)
112113

113114
def _collect_runtime_jars(dep_targets):

scala/private/phases/phase_compile.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ def _phase_compile(
170170
java_jar = out.java_jar,
171171
source_jars = _pack_source_jars(ctx) + out.source_jars,
172172
merged_provider = out.merged_provider,
173+
external_providers = [out.merged_provider] + out.coverage.providers,
173174
)
174175

175176
def _compile_or_empty(
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#
2+
# PHASE: default_info
3+
#
4+
# DOCUMENT THIS
5+
#
6+
def phase_binary_default_info(ctx, p):
7+
return struct(
8+
external_providers = [
9+
DefaultInfo(
10+
executable = p.declare_executable,
11+
files = depset([p.declare_executable] + p.compile.full_jars),
12+
runfiles = p.runfiles.runfiles,
13+
),
14+
],
15+
)
16+
17+
def phase_library_default_info(ctx, p):
18+
return struct(
19+
external_providers = [
20+
DefaultInfo(
21+
files = depset(p.compile.full_jars),
22+
runfiles = p.runfiles.runfiles,
23+
),
24+
],
25+
)
26+
27+
def phase_scalatest_default_info(ctx, p):
28+
return struct(
29+
external_providers = [
30+
DefaultInfo(
31+
executable = p.declare_executable,
32+
files = depset([p.declare_executable] + p.compile.full_jars),
33+
runfiles = ctx.runfiles(p.coverage_runfiles.coverage_runfiles, transitive_files = p.runfiles.runfiles.files),
34+
),
35+
],
36+
)

scala/private/phases/phase_final.bzl

Lines changed: 0 additions & 27 deletions
This file was deleted.

scala/private/phases/phases.bzl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ load(
4646
_phase_scalatest_runfiles = "phase_scalatest_runfiles",
4747
)
4848
load(
49-
"@io_bazel_rules_scala//scala/private:phases/phase_final.bzl",
50-
_phase_binary_final = "phase_binary_final",
51-
_phase_library_final = "phase_library_final",
52-
_phase_scalatest_final = "phase_scalatest_final",
49+
"@io_bazel_rules_scala//scala/private:phases/phase_default_info.bzl",
50+
_phase_binary_default_info = "phase_binary_default_info",
51+
_phase_library_default_info = "phase_library_default_info",
52+
_phase_scalatest_default_info = "phase_scalatest_default_info",
5353
)
5454
load("@io_bazel_rules_scala//scala/private:phases/phase_scalac_provider.bzl", _phase_scalac_provider = "phase_scalac_provider")
5555
load("@io_bazel_rules_scala//scala/private:phases/phase_write_manifest.bzl", _phase_write_manifest = "phase_write_manifest")
@@ -125,7 +125,7 @@ phase_library_runfiles = _phase_library_runfiles
125125
phase_scalatest_runfiles = _phase_scalatest_runfiles
126126
phase_common_runfiles = _phase_common_runfiles
127127

128-
# final
129-
phase_binary_final = _phase_binary_final
130-
phase_library_final = _phase_library_final
131-
phase_scalatest_final = _phase_scalatest_final
128+
# default_info
129+
phase_binary_default_info = _phase_binary_default_info
130+
phase_library_default_info = _phase_library_default_info
131+
phase_scalatest_default_info = _phase_scalatest_default_info

scala/private/rules/scala_binary.bzl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ load(
1313
"@io_bazel_rules_scala//scala/private:phases/phases.bzl",
1414
"extras_phases",
1515
"phase_binary_compile",
16-
"phase_binary_final",
16+
"phase_binary_default_info",
1717
"phase_common_collect_jars",
1818
"phase_common_java_wrapper",
1919
"phase_common_runfiles",
@@ -42,9 +42,8 @@ def _scala_binary_impl(ctx):
4242
("merge_jars", phase_merge_jars),
4343
("runfiles", phase_common_runfiles),
4444
("write_executable", phase_common_write_executable),
45+
("default_info", phase_binary_default_info),
4546
],
46-
# fixed phase
47-
("final", phase_binary_final),
4847
)
4948

5049
_scala_binary_attrs = {

scala/private/rules/scala_junit_test.bzl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ load("@io_bazel_rules_scala//scala/private:common_outputs.bzl", "common_outputs"
1111
load(
1212
"@io_bazel_rules_scala//scala/private:phases/phases.bzl",
1313
"extras_phases",
14-
"phase_binary_final",
14+
"phase_binary_default_info",
1515
"phase_common_java_wrapper",
1616
"phase_common_runfiles",
1717
"phase_declare_executable",
@@ -47,9 +47,8 @@ def _scala_junit_test_impl(ctx):
4747
("runfiles", phase_common_runfiles),
4848
("jvm_flags", phase_jvm_flags),
4949
("write_executable", phase_junit_test_write_executable),
50+
("default_info", phase_binary_default_info),
5051
],
51-
# fixed phase
52-
("final", phase_binary_final),
5352
)
5453

5554
_scala_junit_test_attrs = {

scala/private/rules/scala_library.bzl

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ load(
2222
"phase_collect_srcjars",
2323
"phase_common_collect_jars",
2424
"phase_library_compile",
25-
"phase_library_final",
25+
"phase_library_default_info",
2626
"phase_library_for_plugin_bootstrapping_collect_jars",
2727
"phase_library_for_plugin_bootstrapping_compile",
2828
"phase_library_runfiles",
@@ -66,9 +66,8 @@ def _scala_library_impl(ctx):
6666
("merge_jars", phase_merge_jars),
6767
("runfiles", phase_library_runfiles),
6868
("collect_exports_jars", phase_collect_exports_jars),
69+
("default_info", phase_library_default_info),
6970
],
70-
# fixed phase
71-
("final", phase_library_final),
7271
)
7372

7473
_scala_library_attrs = {}
@@ -143,9 +142,8 @@ def _scala_library_for_plugin_bootstrapping_impl(ctx):
143142
("merge_jars", phase_merge_jars),
144143
("runfiles", phase_library_runfiles),
145144
("collect_exports_jars", phase_collect_exports_jars),
145+
("default_info", phase_library_default_info),
146146
],
147-
# fixed phase
148-
("final", phase_library_final),
149147
)
150148

151149
# the scala compiler plugin used for dependency analysis is compiled using `scala_library`.
@@ -199,9 +197,8 @@ def _scala_macro_library_impl(ctx):
199197
("merge_jars", phase_merge_jars),
200198
("runfiles", phase_library_runfiles),
201199
("collect_exports_jars", phase_collect_exports_jars),
200+
("default_info", phase_library_default_info),
202201
],
203-
# fixed phase
204-
("final", phase_library_final),
205202
)
206203

207204
_scala_macro_library_attrs = {

scala/private/rules/scala_repl.bzl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ load("@io_bazel_rules_scala//scala/private:common_outputs.bzl", "common_outputs"
1212
load(
1313
"@io_bazel_rules_scala//scala/private:phases/phases.bzl",
1414
"extras_phases",
15-
"phase_binary_final",
15+
"phase_binary_default_info",
1616
"phase_common_runfiles",
1717
"phase_declare_executable",
1818
"phase_merge_jars",
@@ -43,9 +43,8 @@ def _scala_repl_impl(ctx):
4343
("merge_jars", phase_merge_jars),
4444
("runfiles", phase_common_runfiles),
4545
("write_executable", phase_repl_write_executable),
46+
("default_info", phase_binary_default_info),
4647
],
47-
# fixed phase
48-
("final", phase_binary_final),
4948
)
5049

5150
_scala_repl_attrs = {

scala/private/rules/scala_test.bzl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ load(
1919
"phase_scalac_provider",
2020
"phase_scalatest_collect_jars",
2121
"phase_scalatest_compile",
22-
"phase_scalatest_final",
22+
"phase_scalatest_default_info",
2323
"phase_scalatest_runfiles",
2424
"phase_scalatest_write_executable",
2525
"phase_unused_deps_checker",
@@ -44,9 +44,8 @@ def _scala_test_impl(ctx):
4444
("runfiles", phase_scalatest_runfiles),
4545
("coverage_runfiles", phase_coverage_runfiles),
4646
("write_executable", phase_scalatest_write_executable),
47+
("default_info", phase_scalatest_default_info),
4748
],
48-
# fixed phase
49-
("final", phase_scalatest_final),
5049
)
5150

5251
_scala_test_attrs = {

0 commit comments

Comments
 (0)