Skip to content

Commit

Permalink
Merge pull request #239 from ekrich/topic/native
Browse files Browse the repository at this point in the history
Add support for Scala Native
  • Loading branch information
SethTisue authored Aug 1, 2019
2 parents f03c313 + 486374a commit eff9f2b
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 54 deletions.
19 changes: 16 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,28 @@ matrix:

# run migration test
- scala: 2.12.8
env: TEST_SCALAFIX=true ADOPTOPENJDK=11
env: TEST_SCALAFIX=true ADOPTOPENJDK=11

# run binary compatibility test
- scala: 2.12.8
env: TEST_BINARY_COMPAT=true ADOPTOPENJDK=11
env: TEST_BINARY_COMPAT=true ADOPTOPENJDK=11

# run scalafmt
- scala: 2.12.8
env: TEST_SCALAFMT=true ADOPTOPENJDK=11
env: TEST_SCALAFMT=true ADOPTOPENJDK=11

# Scala Native includes
- scala: 2.11.12
env: SCALANATIVE_VERSION=0.3.9 ADOPTOPENJDK=8

- scala: 2.11.12
env: SCALANATIVE_VERSION=0.4.0-M2 ADOPTOPENJDK=8

- scala: 2.11.12
env: SCALANATIVE_VERSION=0.3.9 ADOPTOPENJDK=11

- scala: 2.11.12
env: SCALANATIVE_VERSION=0.4.0-M2 ADOPTOPENJDK=11

before_install:
# adding $HOME/.sdkman to cache would create an empty directory, which interferes with the initial installation
Expand Down
37 changes: 24 additions & 13 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ lazy val root = project
.aggregate(
compat211JVM,
compat211JS,
compat211Native,
compat212JVM,
compat212JS,
compat213JVM,
Expand Down Expand Up @@ -60,17 +61,17 @@ scalaVersionsByJvm in ThisBuild := {
}

/** Create an OSGi version range for standard Scala versioning
* schemes that describes binary compatible versions. */
* schemes that describes binary compatible versions. */
def osgiVersionRange(version: String, requireMicro: Boolean = false): String =
if(version contains '-') "${@}" // M, RC or SNAPSHOT -> exact version
else if(requireMicro) "${range;[===,=+)}" // At least the same micro version
if (version contains '-') "${@}" // M, RC or SNAPSHOT -> exact version
else if (requireMicro) "${range;[===,=+)}" // At least the same micro version
else "${range;[==,=+)}" // Any binary compatible version

/** Create an OSGi Import-Package version specification. */
def osgiImport(pattern: String, version: String, requireMicro: Boolean = false): String =
pattern + ";version=\"" + osgiVersionRange(version, requireMicro) + "\""

lazy val compat = MultiScalaCrossProject(JSPlatform, JVMPlatform)(
lazy val compat = MultiScalaCrossProject(JSPlatform, JVMPlatform, NativePlatform)(
"compat",
_.settings(scalaModuleSettings)
.settings(commonSettings)
Expand All @@ -90,7 +91,8 @@ lazy val compat = MultiScalaCrossProject(JSPlatform, JVMPlatform)(
if (scalaVersion.value.startsWith("2.13."))
Seq(s"scala.collection.compat.*;version=${version.value}")
else
Seq(s"scala.collection.compat.*;version=${version.value},scala.jdk.*;version=${version.value}")
Seq(
s"scala.collection.compat.*;version=${version.value},scala.jdk.*;version=${version.value}")
},
OsgiKeys.importPackage := Seq(osgiImport("*", scalaBinaryVersion.value)),
OsgiKeys.privatePackage := Nil,
Expand All @@ -109,18 +111,25 @@ lazy val compat = MultiScalaCrossProject(JSPlatform, JVMPlatform)(
)
.jsConfigure(_.enablePlugins(ScalaJSJUnitPlugin))
.disablePlugins(ScalafixPlugin)
.nativeSettings(
crossScalaVersions := List(scala211),
scalaVersion := scala211, // allows to compile if scalaVersion set not 2.11
nativeLinkStubs := true,
Test / test := {}
)
)

val compat211 = compat(scala211)
val compat212 = compat(scala212)
val compat213 = compat(scala213)

lazy val compat211JVM = compat211.jvm
lazy val compat211JS = compat211.js
lazy val compat212JVM = compat212.jvm
lazy val compat212JS = compat212.js
lazy val compat213JVM = compat213.jvm
lazy val compat213JS = compat213.js
lazy val compat211JVM = compat211.jvm
lazy val compat211JS = compat211.js
lazy val compat211Native = compat211.native
lazy val compat212JVM = compat212.jvm
lazy val compat212JS = compat212.js
lazy val compat213JVM = compat213.jvm
lazy val compat213JS = compat213.js

lazy val `binary-compat-old` = project
.in(file("binary-compat/old"))
Expand Down Expand Up @@ -285,6 +294,7 @@ val preRelease = "preRelease"
val travisScalaVersion = sys.env.get("TRAVIS_SCALA_VERSION").flatMap(Version.parse)
val releaseVersion = sys.env.get("TRAVIS_TAG").flatMap(Version.parse)
val isScalaJs = sys.env.get("SCALAJS_VERSION").map(_.nonEmpty).getOrElse(false)
val isScalaNative = sys.env.get("SCALANATIVE_VERSION").map(_.nonEmpty).getOrElse(false)
val isScalafix = sys.env.get("TEST_SCALAFIX").nonEmpty
val isScalafmt = sys.env.get("TEST_SCALAFMT").nonEmpty
val isBinaryCompat = sys.env.get("TEST_BINARY_COMPAT").nonEmpty
Expand Down Expand Up @@ -342,13 +352,14 @@ inThisBuild(
"TRAVIS_SCALA_VERSION",
"TRAVIS_TAG",
"SCALAJS_VERSION",
"SCALANATIVE_VERSION",
"TEST_SCALAFIX",
"TEST_SCALAFMT",
"TEST_BINARY_COMPAT"
).foreach(k =>
println(k.padTo(20, " ").mkString("") + " -> " + sys.env.get(k).getOrElse("None")))

val platformSuffix = if (isScalaJs) "JS" else ""
val platformSuffix = if (isScalaJs) "JS" else if (isScalaNative) "Native" else ""

val compatProject = "compat" + travisScalaVersion.get.binary + platformSuffix
val binaryCompatProject = "binary-compat"
Expand Down Expand Up @@ -387,7 +398,7 @@ inThisBuild(
Seq(
setPublishVersion,
List(s"$projectPrefix/clean"),
List(s"$testProjectPrefix/test"),
if (isScalaNative) List() else List(s"$testProjectPrefix/test"),
List(s"$projectPrefix/publishLocal"),
publishTask
).flatten
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ trait BuildFrom[-From, -A, +C] extends Any {
def fromSpecific(from: From)(it: IterableOnce[A]): C

/** Get a Builder for the collection. For non-strict collection types this will use an intermediate buffer.
* Building collections with `fromSpecific` is preferred because it can be lazy for lazy collections. */
* Building collections with `fromSpecific` is preferred because it can be lazy for lazy collections. */
def newBuilder(from: From): mutable.Builder[A, C]

@deprecated("Use newBuilder() instead of apply()", "2.13.0")
Expand All @@ -40,7 +40,7 @@ object BuildFrom {
implicit cbf: CanBuildFrom[From, A, C]): BuildFrom[From, A, C] =
new BuildFrom[From, A, C] {
def fromSpecific(from: From)(it: IterableOnce[A]): C = (cbf(from) ++= it).result()
def newBuilder(from: From): mutable.Builder[A, C] = cbf(from)
def newBuilder(from: From): mutable.Builder[A, C] = cbf(from)
}

// Implicit conversion derived from an implicit conversion to CanBuildFrom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ private[compat] trait PackageShared {
type IterableOnce[+X] = c.TraversableOnce[X]
val IterableOnce = c.TraversableOnce

implicit def toMapViewExtensionMethods[K, V, C <: scala.collection.Map[K, V]](self: IterableView[(K, V), C]): MapViewExtensionMethods[K, V, C] =
implicit def toMapViewExtensionMethods[K, V, C <: scala.collection.Map[K, V]](
self: IterableView[(K, V), C]): MapViewExtensionMethods[K, V, C] =
new MapViewExtensionMethods[K, V, C](self)
}

Expand Down Expand Up @@ -242,7 +243,10 @@ class TraversableExtensionMethods[A](private val self: c.Traversable[A]) extends
def iterableFactory: GenericCompanion[Traversable] = self.companion
}

class MapViewExtensionMethods[K, V, C <: scala.collection.Map[K, V]](private val self: IterableView[(K, V), C]) extends AnyVal {
def mapValues[W, That](f: V => W)(implicit bf: CanBuildFrom[IterableView[(K, V), C], (K, W), That]): That =
class MapViewExtensionMethods[K, V, C <: scala.collection.Map[K, V]](
private val self: IterableView[(K, V), C])
extends AnyVal {
def mapValues[W, That](f: V => W)(
implicit bf: CanBuildFrom[IterableView[(K, V), C], (K, W), That]): That =
self.map[(K, W), That] { case (k, v) => (k, f(v)) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ object ArraySeq {
return false

val len = xs.length
var i = 0
var i = 0
while (i < len) {
if (xs(i) != ys(i))
return false
Expand Down
40 changes: 20 additions & 20 deletions compat/src/test/scala/scala/jdk/CollectionConvertersTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,73 +15,73 @@ class CollectionConvertersTest {
val it = "a b c".split(" ").iterator

{
val j = it.asJava
val j = it.asJava
val je = it.asJavaEnumeration
val s = (j: ju.Iterator[String]).asScala
val s = (j: ju.Iterator[String]).asScala
assert((s: Iterator[String]) eq it)
val es = (je: ju.Enumeration[String]).asScala
assert((es: Iterator[String]) eq it)
}

{
val i: c.Iterable[String] = it.to(Iterable)
val j = i.asJava
val jc = i.asJavaCollection
val s = (j: jl.Iterable[String]).asScala
val j = i.asJava
val jc = i.asJavaCollection
val s = (j: jl.Iterable[String]).asScala
assert((s: c.Iterable[String]) eq i)
val cs = (jc: ju.Collection[String]).asScala
assert((cs: c.Iterable[String]) eq i)
}

{
val b: m.Buffer[String] = it.to(m.Buffer)
val j = b.asJava
val s = (j: ju.List[String]).asScala
val j = b.asJava
val s = (j: ju.List[String]).asScala
assert((s: m.Buffer[String]) eq b)
}

{
val q: m.Seq[String] = it.to(m.Seq)
val j = q.asJava
val s = (j: ju.List[String]).asScala
val j = q.asJava
val s = (j: ju.List[String]).asScala
assert((s: m.Buffer[String]) == q) // not eq
}

{
val q: c.Seq[String] = it.to(c.Seq)
val j = q.asJava
val s = (j: ju.List[String]).asScala
val j = q.asJava
val s = (j: ju.List[String]).asScala
assert((s: m.Buffer[String]) == q) // not eq
}

{
val t: m.Set[String] = it.to(m.Set)
val j = t.asJava
val s = (j: ju.Set[String]).asScala
val j = t.asJava
val s = (j: ju.Set[String]).asScala
assert((s: m.Set[String]) eq t)
}

{
val t: c.Set[String] = it.to(c.Set)
val j = t.asJava
val s = (j: ju.Set[String]).asScala
val j = t.asJava
val s = (j: ju.Set[String]).asScala
assert((s: m.Set[String]) == t) // not eq
}

{
val p: m.Map[String, String] = m.Map(it.map(a => (a, a)).toSeq: _*)
val j = p.asJava
val jd = p.asJavaDictionary
val s = (j: ju.Map[String, String]).asScala
val j = p.asJava
val jd = p.asJavaDictionary
val s = (j: ju.Map[String, String]).asScala
assert((s: m.Map[String, String]) eq p)
val ds = (jd: ju.Dictionary[String, String]).asScala
assert((ds: m.Map[String, String]) eq p)
}

{
val p: c.Map[String, String] = c.Map(it.map(a => (a, a)).toSeq: _*)
val j = p.asJava
val s = (j: ju.Map[String, String]).asScala
val j = p.asJava
val s = (j: ju.Map[String, String]).asScala
assert((s: m.Map[String, String]) == p) // not eq
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,6 @@ class BuildFromTest {
// Implement BuildFrom
class MyBuildFrom[From, A, C] extends BuildFrom[From, A, C] {
def fromSpecific(from: From)(it: IterableOnce[A]): C = ???
def newBuilder(from: From): Builder[A, C] = ???
def newBuilder(from: From): Builder[A, C] = ???
}
}
6 changes: 3 additions & 3 deletions compat/src/test/scala/test/scala/collection/ViewTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class ViewTest {

@Test
def mapValues: Unit = {
val m = Map("a" -> 1, "b" -> 2, "c" -> 3)
val oldStyle = m.mapValues(x => x*x)
val newStyle = m.view.mapValues(x => x*x)
val m = Map("a" -> 1, "b" -> 2, "c" -> 3)
val oldStyle = m.mapValues(x => x * x)
val newStyle = m.view.mapValues(x => x * x)
assertEquals(oldStyle.toMap, newStyle.toMap)
}

Expand Down
22 changes: 14 additions & 8 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
val scalaJSVersion = Option(System.getenv("SCALAJS_VERSION")).filter(_.nonEmpty).getOrElse("0.6.28")
val crossVer = "0.6.1"
val scalaJSVersion =
Option(System.getenv("SCALAJS_VERSION")).filter(_.nonEmpty).getOrElse("0.6.28")
val scalaNativeVersion =
Option(System.getenv("SCALANATIVE_VERSION")).filter(_.nonEmpty).getOrElse("0.3.9")

addSbtPlugin("org.scala-js" % "sbt-scalajs" % scalaJSVersion)
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "0.6.1")
addSbtPlugin("org.scala-lang.modules" % "sbt-scala-module" % "2.0.0")
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.5")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.9.0")
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.2-1")
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.2.0")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % scalaJSVersion)
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % crossVer)
addSbtPlugin("org.scala-native" % "sbt-scala-native" % scalaNativeVersion)
addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % crossVer)
addSbtPlugin("org.scala-lang.modules" % "sbt-scala-module" % "2.0.0")
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.5")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.9.0")
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.2-1")
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.2.0")

0 comments on commit eff9f2b

Please sign in to comment.