Skip to content

Commit

Permalink
Prepare the build for cross compilation
Browse files Browse the repository at this point in the history
Moves `tests`, and `cli` source files to shared directory,
`dynamic` source files predominantly to the jvm directory and
also moves interfaces files to jvm folder (as they
are to be rewritten for Scala Native).
  • Loading branch information
jchyb committed Sep 20, 2024
1 parent 817453f commit d76ce10
Show file tree
Hide file tree
Showing 346 changed files with 84 additions and 75 deletions.
157 changes: 83 additions & 74 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.nio.file.Paths

import Dependencies._
import sbtcrossproject.CrossPlugin.autoImport.crossProject

Expand Down Expand Up @@ -50,37 +52,39 @@ commands += Command.command("ci-test") { s =>
docsTest :: s
}

lazy val dynamic = project.in(file("scalafmt-dynamic")).settings(
moduleName := "scalafmt-dynamic",
description := "Implementation of scalafmt-interfaces",
buildInfoSettings,
buildInfoPackage := "org.scalafmt.dynamic",
buildInfoObject := "BuildInfo",
libraryDependencies ++= List(
"io.get-coursier" % "interface" % "0.0.17",
"com.typesafe" % "config" % "1.4.3",
munit.value % Test,
scalametaTestkit % Test,
),
scalacOptions ++= scalacJvmOptions.value,
).dependsOn(interfaces, sysops.jvm).dependsOn(core.jvm % "test")
lazy val dynamic = crossProject(JVMPlatform).withoutSuffixFor(JVMPlatform)
.in(file("scalafmt-dynamic")).settings(
moduleName := "scalafmt-dynamic",
description := "Implementation of scalafmt-interfaces",
buildInfoSettings,
buildInfoPackage := "org.scalafmt.dynamic",
buildInfoObject := "BuildInfo",
libraryDependencies ++= List(
"io.get-coursier" % "interface" % "0.0.17",
"com.typesafe" % "config" % "1.4.3",
munit.value % Test,
scalametaTestkit % Test,
),
scalacOptions ++= scalacJvmOptions.value,
).dependsOn(interfaces, sysops).dependsOn(core % "test")
.enablePlugins(BuildInfoPlugin)

lazy val interfaces = project.in(file("scalafmt-interfaces")).settings(
moduleName := "scalafmt-interfaces",
description :=
"Dependency-free, pure Java public interfaces to integrate with Scalafmt through a build tool or editor plugin.",
crossVersion := CrossVersion.disabled,
autoScalaLibrary := false,
Compile / resourceGenerators += Def.task {
val out = (Compile / managedResourceDirectories).value.head /
"scalafmt.properties"
val props = new java.util.Properties()
props.put("version", version.value)
IO.write(props, "scalafmt properties", out)
List(out)
},
)
lazy val interfaces = crossProject(JVMPlatform).withoutSuffixFor(JVMPlatform)
.in(file("scalafmt-interfaces")).settings(
moduleName := "scalafmt-interfaces",
description :=
"Dependency-free, pure Java public interfaces to integrate with Scalafmt through a build tool or editor plugin.",
crossVersion := CrossVersion.disabled,
autoScalaLibrary := false,
Compile / resourceGenerators += Def.task {
val out = (Compile / managedResourceDirectories).value.head /
"scalafmt.properties"
val props = new java.util.Properties()
props.put("version", version.value)
IO.write(props, "scalafmt properties", out)
List(out)
},
)

lazy val sysops = crossProject(JVMPlatform).withoutSuffixFor(JVMPlatform)
.in(file("scalafmt-sysops")).settings(
Expand Down Expand Up @@ -161,51 +165,56 @@ val scalacJvmOptions = Def.setting {
cross ++ unused
}

lazy val cli = project.in(file("scalafmt-cli")).settings(
moduleName := "scalafmt-cli",
assembly / mainClass := Some("org.scalafmt.cli.Cli"),
assembly / assemblyOption := (assembly / assemblyOption).value
.withPrependShellScript(Some(defaultUniversalScript(shebang = false))),
assembly / assemblyJarName := "scalafmt.jar",
assembly / assemblyMergeStrategy := {
case "reflect.properties" => MergeStrategy.first
case x =>
val oldStrategy = (assembly / assemblyMergeStrategy).value
oldStrategy(x)
},
libraryDependencies ++= Seq(
"org.scalameta" %% "munit-diff" % "1.0.2",
"com.martiansoftware" % "nailgun-server" % "0.9.1",
"com.github.scopt" %% "scopt" % "4.1.0",
),
scalacOptions ++= scalacJvmOptions.value,
Compile / mainClass := Some("org.scalafmt.cli.Cli"),
nativeImageVersion := "22.3.0",
nativeImageInstalled := isCI,
nativeImageOptions ++= {
val isMusl = sys.env.get("NATIVE_IMAGE_MUSL").exists(_.toBoolean)
if (isMusl) Seq("--libc=musl") else Nil
},
nativeImageOptions ++= {
val isStatic = sys.env.get("NATIVE_IMAGE_STATIC").exists(_.toBoolean)
if (isStatic) Seq("--static") else Nil
},
).dependsOn(coreJVM, dynamic).enablePlugins(NativeImagePlugin)
lazy val cli = crossProject(JVMPlatform).withoutSuffixFor(JVMPlatform)
.in(file("scalafmt-cli")).settings(
moduleName := "scalafmt-cli",
assembly / mainClass := Some("org.scalafmt.cli.Cli"),
assembly / assemblyOption := (assembly / assemblyOption).value
.withPrependShellScript(Some(defaultUniversalScript(shebang = false))),
assembly / assemblyJarName := "scalafmt.jar",
assembly / assemblyMergeStrategy := {
case "reflect.properties" => MergeStrategy.first
case x =>
val oldStrategy = (assembly / assemblyMergeStrategy).value
oldStrategy(x)
},
libraryDependencies ++= Seq(
"org.scalameta" %% "munit-diff" % "1.0.2",
"com.martiansoftware" % "nailgun-server" % "0.9.1",
"com.github.scopt" %% "scopt" % "4.1.0",
),
scalacOptions ++= scalacJvmOptions.value,
Compile / mainClass := Some("org.scalafmt.cli.Cli"),
nativeImageVersion := "22.3.0",
nativeImageInstalled := isCI,
nativeImageOptions ++= {
val isMusl = sys.env.get("NATIVE_IMAGE_MUSL").exists(_.toBoolean)
if (isMusl) Seq("--libc=musl") else Nil
},
nativeImageOptions ++= {
val isStatic = sys.env.get("NATIVE_IMAGE_STATIC").exists(_.toBoolean)
if (isStatic) Seq("--static") else Nil
},
).dependsOn(core, dynamic).enablePlugins(NativeImagePlugin)

lazy val tests = project.in(file("scalafmt-tests")).settings(
publish / skip := true,
libraryDependencies ++= Seq(
// Test dependencies
"com.lihaoyi" %% "scalatags" % "0.13.1",
scalametaTestkit,
munit.value,
),
scalacOptions ++= scalacJvmOptions.value,
javaOptions += "-Dfile.encoding=UTF8",
buildInfoPackage := "org.scalafmt.tests",
buildInfoKeys :=
Seq[BuildInfoKey]("resourceDirectory" -> (Test / resourceDirectory).value),
).enablePlugins(BuildInfoPlugin).dependsOn(coreJVM, dynamic, cli)
lazy val tests = crossProject(JVMPlatform).withoutSuffixFor(JVMPlatform)
.in(file("scalafmt-tests")).settings(
publish / skip := true,
libraryDependencies ++= Seq(
// Test dependencies
"com.lihaoyi" %% "scalatags" % "0.13.1",
scalametaTestkit,
munit.value,
),
scalacOptions ++= scalacJvmOptions.value,
javaOptions += "-Dfile.encoding=UTF8",
buildInfoPackage := "org.scalafmt.tests",
buildInfoKeys := Seq[BuildInfoKey]("resourceDirectory" -> {
val sharedTests = (baseDirectory.value.getParentFile / "shared").toPath
(Test / resourceDirectories).value.find(_.toPath.startsWith(sharedTests))
.get
}),
).enablePlugins(BuildInfoPlugin).dependsOn(core, dynamic, cli)

lazy val communityTests = project.in(file("scalafmt-tests-community")).settings(
publish / skip := true,
Expand Down Expand Up @@ -246,7 +255,7 @@ lazy val docs = project.in(file("scalafmt-docs")).settings(
crossScalaVersions := List(scala212),
publish / skip := true,
mdoc := (Compile / run).evaluated,
).dependsOn(cli, dynamic).enablePlugins(DocusaurusPlugin)
).dependsOn(cli.jvm, dynamic.jvm).enablePlugins(DocusaurusPlugin)

val V = "\\d+\\.\\d+\\.\\d+"
val ReleaseCandidate = s"($V-RC\\d+).*".r
Expand Down
2 changes: 1 addition & 1 deletion docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ handle parse and config errors.

Here is an example how to extend `ScalafmtReporter`.

```scala mdoc:file:scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/ConsoleScalafmtReporter.scala
```scala mdoc:file:scalafmt-dynamic/jvm/src/main/scala/org/scalafmt/dynamic/ConsoleScalafmtReporter.scala

```

Expand Down
File renamed without changes.
Loading

0 comments on commit d76ce10

Please sign in to comment.