-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.sbt
349 lines (320 loc) · 11.3 KB
/
build.sbt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
val ScalaNativeVersion = "0.5.7-SNAPSHOT"
// Update during release procedure to provide access to staged, but not published artifacts
val StagingRepoIds = Nil
val StagingRepoNames = StagingRepoIds.map(id => s"orgscala-native-$id").toSeq
val crossScalaVersions212 = (14 to 20).map("2.12." + _)
val crossScalaVersions213 = (8 to 15).map("2.13." + _)
val crossScalaVersions3 =
(2 to 3).map("3.1." + _) ++
(0 to 2).map("3.2." + _) ++
(0 to 4).map("3.3." + _) ++
(0 to 3).map("3.4." + _) ++
(0 to 2).map("3.5." + _) ++
Seq("3.6.1")
val scala2_12 = crossScalaVersions212.last
val scala2_13 = crossScalaVersions213.last
val scala3 = crossScalaVersions3.last
val scala3PublishVersion = "3.1.3"
val publishScalaVersions = Seq(scala2_12, scala2_13, scala3PublishVersion)
def scalaReleasesForBinaryVersion(v: String): Seq[String] = v match {
case "2.12" => crossScalaVersions212
case "2.13" => crossScalaVersions213
case "3" => crossScalaVersions3
case ver =>
throw new IllegalArgumentException(
s"Unsupported binary scala version `${ver}`"
)
}
def scalaStdlibForBinaryVersion(
organization: String,
nativeVersion: String,
nativeBinaryVersion: String,
scalaBinaryVersion: String
): Seq[ModuleID] = {
def artifact(module: String, binV: String, version: String = nativeVersion) =
organization % s"${module}_native${nativeBinaryVersion}_$binV" % version
def crossScalaLibVersion(scalaVersion: String) =
s"$scalaVersion+$nativeVersion"
def scalalibVersion(scalaBinVersion: String): String = {
val scalaVersion = scalaReleasesForBinaryVersion(scalaBinVersion).last
crossScalaLibVersion(scalaVersion)
}
def scalalib(binV: String) = artifact("scalalib", binV, scalalibVersion(binV))
val scala3lib =
artifact("scala3lib", "3", crossScalaLibVersion(scala3PublishVersion))
val crossRuntimeLibraries = List(
"nativelib",
"clib",
"posixlib",
"windowslib",
"javalib",
"auxlib"
).map(artifact(_, scalaBinaryVersion))
val nonCrossRuntimeLibraries = List("javalib-intf")
.map(organization % _ % nativeVersion)
val runtimeLibraries = crossRuntimeLibraries ++ nonCrossRuntimeLibraries
scalaBinaryVersion match {
case "2.12" | "2.13" => scalalib(scalaBinaryVersion) :: runtimeLibraries
case "3" => scala3lib :: scalalib("2.13") :: runtimeLibraries
case ver =>
throw new IllegalArgumentException(
s"Unsupported binary scala version `${ver}`"
)
}
}
val scalaNativeVersion =
settingKey[String]("Version of Scala Native for which to build to CLI")
val cliAssemblyJarName = settingKey[String]("Name of created assembly jar")
inThisBuild(
Def.settings(
organization := "org.scala-native",
scalaNativeVersion := ScalaNativeVersion,
version := scalaNativeVersion.value,
scalaVersion := scala3PublishVersion,
crossScalaVersions := publishScalaVersions,
homepage := Some(url("http://www.scala-native.org")),
startYear := Some(2021),
licenses := Seq(
"BSD-like" -> url(
"https://github.com/scala-native/scala-native-cli/blob/main/LICENSE"
)
),
scmInfo := Some(
ScmInfo(
browseUrl = url("https://github.com/scala-native/scala-native-cli"),
connection = "scm:git:[email protected]:scala-native/scala-native-cli.git",
devConnection =
Some("scm:git:[email protected]:scala-native/scala-native-cli.git")
)
),
// Used during the releases
resolvers ++= StagingRepoNames.flatMap(Resolver.sonatypeOssRepos(_)),
resolvers ++= Resolver.sonatypeOssRepos("snapshots"),
resolvers += Resolver.mavenCentral,
resolvers += Resolver.defaultLocal
)
)
val cliPackLibJars =
taskKey[Seq[File]]("All libraries packed with packed in cliPack")
val cliPack = taskKey[File]("Pack the CLI for the current configuration")
lazy val cli = project
.in(file("cli"))
.enablePlugins(BuildInfoPlugin)
.settings(
name := "scala-native-cli",
crossScalaVersions := publishScalaVersions,
Compile / run / mainClass :=
Some("scala.scalanative.cli.ScalaNativeLd"),
scalacOptions += "-Ywarn-unused:imports",
scalacOptions ++= CrossVersion.partialVersion(scalaVersion.value).collect {
case (2, _) => "-target:jvm-1.8"
case (3, _) => "-Xtarget:8"
},
libraryDependencies ++= Seq(
"org.scala-native" %% "tools" % scalaNativeVersion.value,
"com.github.scopt" %% "scopt" % "4.0.1",
"org.scalatest" %% "scalatest" % "3.2.10" % Test
),
buildInfoKeys := Seq[BuildInfoKey](
"nativeVersion" -> scalaNativeVersion.value
),
buildInfoPackage := "scala.scalanative.cli.options",
cliAssemblyJarName := s"${normalizedName.value}-assembly_${scalaBinaryVersion.value}-${scalaNativeVersion.value}.jar",
assembly / assemblyJarName := cliAssemblyJarName.value,
assembly / mainClass := (Compile / run / mainClass).value,
cliPackSettings,
publishSettings
)
lazy val cliScriptedTests = project
.in(file("cliScriptedTests"))
.enablePlugins(ScriptedPlugin)
.settings(
sbtTestDirectory := (cli / sourceDirectory).value / "sbt-test",
scalaVersion := crossScalaVersions212.last,
scriptedLaunchOpts ++= {
val jarName = (cli / cliAssemblyJarName).value
val cliPath = (cli / Compile / crossTarget).value / jarName
val packDir = (cli / cliPack / crossTarget).value
Seq(
"-Xmx1024M",
"-Dplugin.version=" + (cli / scalaNativeVersion).value,
"-Dscala.version=" + (cli / scalaVersion).value,
"-Dscala-native-cli=" + cliPath,
"-Dscala-native-cli-pack=" + packDir,
"-Dscalanative.build.staging.resolvers=" + StagingRepoNames
.mkString(",")
)
},
scriptedBufferLog := false,
scriptedDependencies := {
scriptedDependencies
.dependsOn(
cli / assembly,
cli / cliPack
)
.value
}
)
def nativeBinaryVersion(version: String): String = {
val VersionPattern = raw"(\d+)\.(\d+)\.(\d+)(\-.*)?".r
val VersionPattern(major, minor, patch, milestone) = version
if (patch != null && milestone != null) version
else s"$major.$minor"
}
val nativeSourceExtensions = Set(".c", ".cpp", ".cxx", ".h", ".hpp", ".S")
val DeduplicateOrRename = new sbtassembly.MergeStrategy {
def name: String = "deduplicate-or-rename"
def apply(
tempDir: java.io.File,
path: String,
files: Seq[java.io.File]
): Either[String, Seq[(java.io.File, String)]] =
MergeStrategy.deduplicate(tempDir, path, files) match {
case v @ Right(_) => v
case _ => MergeStrategy.rename(tempDir, path, files)
}
}
lazy val cliPackSettings = Def.settings(
assemblyMergeStrategy := {
val default = assemblyMergeStrategy.value
file =>
if (nativeSourceExtensions.exists(file.endsWith)) DeduplicateOrRename
else if (file.endsWith("scala-native.properties")) MergeStrategy.concat
else default(file)
},
cliPackLibJars := {
val s = streams.value
val log = s.log
val scalaNativeOrg = organization.value
val scalaBinVer = scalaBinaryVersion.value
val snVer = scalaNativeVersion.value
val nativeBinVer = nativeBinaryVersion(snVer)
val scalaFullVers = scalaReleasesForBinaryVersion(scalaBinVer)
val cliAssemblyJar = assembly.value
// Standard modules needed for linking of Scala Native
val stdLibModuleIDs =
scalaStdlibForBinaryVersion(
organization = scalaNativeOrg,
nativeVersion = snVer,
nativeBinaryVersion = nativeBinVer,
scalaBinaryVersion = scalaBinVer
)
val compilerPluginModuleIDs =
scalaFullVers.map(v => scalaNativeOrg % s"nscplugin_$v" % snVer)
val allModuleIDs = (stdLibModuleIDs ++ compilerPluginModuleIDs).toVector
val allModuleIDsIntransitive = allModuleIDs.map(_.intransitive())
val resolvedLibJars = {
val retrieveDir = s.cacheDirectory / "cli-lib-jars"
val lm = {
import sbt.librarymanagement.ivy._
val ivyConfig = InlineIvyConfiguration()
.withResolvers(resolvers.value.toVector)
.withLog(log)
IvyDependencyResolution(ivyConfig)
}
val dummyModuleName =
s"clilibjars-$snVer-$scalaBinVer-" + scalaFullVers.mkString("-")
val dummyModuleID = scalaNativeOrg % dummyModuleName % version.value
val descriptor =
lm.moduleDescriptor(
dummyModuleID,
allModuleIDsIntransitive,
scalaModuleInfo = None
)
lm
.retrieve(descriptor, retrieveDir, log)
.fold(
{ unresolvedWarn => throw unresolvedWarn.resolveException },
identity
)
.distinct
}
cliAssemblyJar +: resolvedLibJars
},
cliPack / target := crossTarget.value / "pack",
cliPack / moduleName :=
s"scala-native-cli_${scalaBinaryVersion.value}-${scalaNativeVersion.value}",
cliPack / crossTarget := (cliPack / target).value / (cliPack / moduleName).value,
cliPack := {
val scalaBinVer = scalaBinaryVersion.value
val snVer = scalaNativeVersion.value
val trg = (cliPack / crossTarget).value
val trgLib = trg / "lib"
val trgBin = trg / "bin"
if (trg.exists)
IO.delete(trg)
IO.createDirectory(trgLib)
val libJars = cliPackLibJars.value
for (libJar <- libJars) {
IO.copyFile(libJar, trgLib / libJar.getName)
}
IO.createDirectory(trgBin)
val scriptDir = (Compile / sourceDirectory).value.getParentFile / "script"
for {
scriptFile <- IO.listFiles(scriptDir)
if !scriptFile.getPath.endsWith("~")
} {
val content = IO.read(scriptFile)
val processedContent = content
.replaceAllLiterally("@SCALA_BIN_VER@", scalaBinVer)
.replaceAllLiterally("@SCALANATIVE_VER@", snVer)
.replaceAllLiterally(
"@SCALANATIVE_BIN_VER@",
nativeBinaryVersion(snVer)
)
.replaceAllLiterally(
"@SCALALIB_2_13_FOR_3_VER@",
crossScalaVersions213.last
)
val dest = trgBin / scriptFile.getName
IO.write(dest, processedContent)
if (scriptFile.canExecute)
dest.setExecutable( /* executable = */ true, /* ownerOnly = */ false)
}
trg
}
)
lazy val publishSettings = Def.settings(
Compile / publishArtifact := true,
Test / publishArtifact := false,
publishMavenStyle := true,
pomIncludeRepository := (_ => false),
publishTo := {
val nexus = "https://oss.sonatype.org/"
if (isSnapshot.value)
Some("snapshots" at nexus + "content/repositories/snapshots")
else
Some("releases" at nexus + "service/local/staging/deploy/maven2")
},
credentials ++= {
for {
user <- sys.env.get("MAVEN_USER")
password <- sys.env.get("MAVEN_PASSWORD")
} yield Credentials(
realm = "Sonatype Nexus Repository Manager",
host = "oss.sonatype.org",
userName = user,
passwd = password
)
}.toSeq,
developers ++= List(
Developer(
email = "[email protected]",
id = "wmazur",
name = "Wojciech Mazur",
url = url("https://github.com/WojciechMazur")
),
Developer(
email = "[email protected]",
id = "jchyb",
name = "Jan Chyb",
url = url("https://github.com/jchyb")
)
),
pomExtra := (
<issueManagement>
<system>GitHub Issues</system>
<url>https://github.com/scala-native/scala-native-cli/issues</url>
</issueManagement>
)
)