Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set different scalacOptions per Scala version when cross-compiling using Build.scala?

When building with Scala 2.10 and SBT 0.13.2, I want to have -language:_, but this isn't recognized by Scala 2.9. There's a question about settings different scalacOptions for cross-compilation (Conditional scalacOptions with SBT), but it is about build.sbt. I'm using Build.scala because I'm doing a multi-project build.

I have tried this:

  def scalacOptionsVersion(v: String) = {
    Seq(
      "-unchecked",
      "-deprecation",
      "-Xlint",
      "-Xfatal-warnings",
      "-Ywarn-dead-code",
      "-target:jvm-1.7",
      "-encoding", "UTF-8") ++ (
    if (v.startsWith("2.9")) Seq() else Seq("-language:_"))
  }

  override val settings = super.settings ++ Seq(
    ...,
    scalaVersion := "2.10.4",
    scalacOptions <++= scalaVersion(scalacOptionsVersion),
    crossScalaVersions := Seq("2.9.2", "2.10.4", "2.11.4"),
    ...
  )

but I get an error:

[error] /Users/benwing/devel/lemkit/scala/project/build.scala:29: type mismatch;
[error]  found   : sbt.Def.Initialize[Equals]
[error]  required: sbt.Def.Initialize[sbt.Task[?]]
[error] Note: Equals >: sbt.Task[?], but trait Initialize is invariant in type T.
[error] You may wish to define T as -T instead. (SLS 4.5)
[error]     scalacOptions <++= scalaVersion(scalacOptionsVersion),
[error]                                    ^
[error] one error found

Help?

like image 435
Urban Vagabond Avatar asked Dec 30 '14 01:12

Urban Vagabond


People also ask

What is crossScalaVersions?

SBT's crossScalaVersions allows projects to compile with multiple versions of Scala. However, crossScalaVersions is a hack that reuses projects by mutates settings.

What is scalacOptions?

As shown, scalacOptions lets you set Scala compiler options in your SBT project build. I got that example from my earlier very large Scala/SBT build. sbt example. Also, I just saw that tpolecat has put together a nice list of scalaOptions flags at this URL.


1 Answers

In SBT 0.13+ this will work:

def scalacOptionsVersion(scalaVersion: String) = {
  Seq(
    "-unchecked",
    "-deprecation",
    "-Xlint",
    "-Xfatal-warnings",
    "-Ywarn-dead-code",
    "-target:jvm-1.7",
    "-encoding", "UTF-8"
  ) ++ CrossVersion.partialVersion(scalaVersion) match {
         case Some((2, scalaMajor)) if scalaMajor == 9 => Nil
         case _ => Seq("-language:_")
       }
}


val appSettings = Seq(
  scalacOptions := scalacOptionsVersion(scalaVersion.value)

  // other settings...
)
like image 199
sksamuel Avatar answered Oct 23 '22 14:10

sksamuel