Skip to content

Commit 9b02cf9

Browse files
liucijusBorja Lorente
authored andcommitted
Add neverlink support (bazel-contrib#1123)
* Add neverlink support * Fix tests meaning
1 parent 5333ee3 commit 9b02cf9

File tree

7 files changed

+53
-0
lines changed

7 files changed

+53
-0
lines changed

scala/private/common_attributes.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ common_attrs_for_plugin_bootstrapping = {
4040
default = False,
4141
mandatory = False,
4242
),
43+
"neverlink": attr.bool(
44+
default = False,
45+
mandatory = False,
46+
),
4347
}
4448

4549
common_attrs = {}

scala/private/phases/phase_compile.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ def _create_scala_compilation_provider(ctx, ijar, source_jar, deps_providers):
300300
deps = deps_providers,
301301
exports = exports,
302302
runtime_deps = runtime_deps,
303+
neverlink = ctx.attr.neverlink,
303304
)
304305

305306
def _pack_source_jar(ctx, scala_srcs, in_srcjars):

scala/private/rule_impls.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ def compile_java(ctx, source_jars, source_files, output, extra_javac_opts, provi
259259
#needs to be empty since we want the provider.compile_jars to only contain the sources ijar
260260
#workaround until https://github.com/bazelbuild/bazel/issues/3528 is resolved
261261
exports = [],
262+
neverlink = getattr(ctx.attr, "neverlink", False),
262263
java_toolchain = find_java_toolchain(ctx, ctx.attr._java_toolchain),
263264
host_javabase = find_java_runtime_toolchain(ctx, ctx.attr._host_javabase),
264265
strict_deps = ctx.fragments.java.strict_java_deps,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package neverlink
2+
3+
class A
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package neverlink
2+
3+
class B
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
load("//scala:scala.bzl", "scala_library", "scala_test")
2+
3+
scala_library(
4+
name = "LinkableA",
5+
srcs = ["A.scala"],
6+
)
7+
8+
scala_library(
9+
name = "NonLinkableB",
10+
srcs = ["B.scala"],
11+
neverlink = True,
12+
)
13+
14+
scala_library(
15+
name = "intermediate_export",
16+
exports = [
17+
":LinkableA",
18+
":NonLinkableB",
19+
],
20+
)
21+
22+
scala_test(
23+
name = "NeverlinkTest",
24+
srcs = ["NeverlinkTest.scala"],
25+
runtime_deps = [":intermediate_export"],
26+
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package neverlink
2+
3+
import org.scalatest._
4+
5+
class NeverlinkTest extends FlatSpec {
6+
"neverlink=False" should "include jar into classpath" in {
7+
getClass.getClassLoader.loadClass("neverlink.A")
8+
}
9+
10+
"neverlink=True" should "exclude jar from classpath" in {
11+
assertThrows[ClassNotFoundException]{
12+
getClass.getClassLoader.loadClass("neverlink.B")
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)