Skip to content

Commit f452324

Browse files
expose build_ijar for scala_library (#1753)
Expose build_ijar to enable compiling scala code, that is not for macro definitions, but would be used in inlining (so ijar support should be disabled for it). You may need strict dependency checks (transitive dependencies not on the classpath) for code that is to be used in inlining. Till now one would need to use scala_macro_library for that, but it was recently switched to include all transitive dependencies on the compiler's classpath, hence strict dependencies are not enforced there. The new attribute enables having both: a) code compiled for inlining b) strict dependency checks
1 parent 5ecfe95 commit f452324

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

docs/scala_library.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,5 +172,21 @@ In order to have a Java rule use this jar file, use the `java_import` rule.
172172
</p>
173173
</td>
174174
</tr>
175+
<tr>
176+
<td><code>build_ijar</code></td>
177+
<td>
178+
<p><code>Boolean; optional (default True)</code></p>
179+
<p>
180+
Enables building an interface jar.
181+
You can also disable the interface jar (`ijar`) functionality, it may be useful in some specific cases.
182+
183+
Example: if you want to enable inlining the compiled code, when it is given as a dependency for another Scala target.
184+
`ijar` does not contain an implementation, so it cannot be used for inlining, disable `ijar` in this usecase.
185+
186+
Only change this parameter if you have an explicit usecase like the above.
187+
For macro code, use a specific rule: scala_macro_library.
188+
</p>
189+
</td>
190+
</tr>
175191
</tbody>
176192
</table>

scala/private/phases/phase_compile.bzl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,19 @@ def phase_compile_binary(ctx, p):
3333
return _phase_compile_default(ctx, p, args)
3434

3535
def phase_compile_library(ctx, p):
36-
args = struct(
37-
srcjars = p.collect_srcjars,
38-
unused_dependency_checker_ignored_targets = [
36+
args = {
37+
"srcjars": p.collect_srcjars,
38+
"unused_dependency_checker_ignored_targets": [
3939
target.label
4040
for target in p.scalac_provider.default_classpath + ctx.attr.exports +
4141
ctx.attr.unused_dependency_checker_ignored_targets
4242
],
43-
)
44-
return _phase_compile_default(ctx, p, args)
43+
}
44+
45+
if hasattr(ctx.attr, "build_ijar"):
46+
args["buildijar"] = ctx.attr.build_ijar
47+
48+
return _phase_compile_default(ctx, p, struct(**args))
4549

4650
def phase_compile_library_for_plugin_bootstrapping(ctx, p):
4751
args = struct(

scala/private/rules/scala_library.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ _scala_library_attrs = {}
8585

8686
_scala_library_attrs.update(implicit_deps)
8787

88+
_scala_library_attrs.update({
89+
"build_ijar": attr.bool(default = True),
90+
})
91+
8892
_scala_library_attrs.update(common_attrs)
8993

9094
_scala_library_attrs.update(_library_attrs)

test/BUILD

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,3 +729,25 @@ scala_junit_test(
729729
},
730730
suffixes = ["Test"],
731731
)
732+
733+
# Targets with inlining support
734+
735+
# If this target doesn't have build_ijar disabled, then InlinedExported will fail to compile.
736+
scala_library(
737+
name = "InlinableExported",
738+
srcs = ["Exported.scala"],
739+
build_ijar = False,
740+
runtime_deps = ["Runtime"],
741+
)
742+
743+
scala_library(
744+
name = "InlinedExported",
745+
srcs = ["OtherLib.scala"],
746+
scalacopts = [
747+
"-opt:l:inline",
748+
"-opt-inline-from:scalarules.test.**",
749+
# We need fatal warnings to ensure that the inlining actually worked.
750+
"-Xfatal-warnings",
751+
],
752+
deps = [":InlinableExported"],
753+
)

0 commit comments

Comments
 (0)