1+ val crossScalaNative212 = Seq (" 2.12.13" , " 2.12.14" , " 2.12.15" )
2+
13val scalaNativeVersion =
24 settingKey[String ](" Version of Scala Native for which to build to CLI" )
35
@@ -6,11 +8,14 @@ val cliAssemblyJarName = settingKey[String]("Name of created assembly jar")
68inThisBuild(
79 Def .settings(
810 organization := " org.scala-native" ,
9- scalaVersion := " 2.12.15 " ,
11+ scalaVersion := crossScalaNative212.last ,
1012 scalaNativeVersion := " 0.4.0" ,
1113 version := scalaNativeVersion.value
1214 )
1315)
16+ val cliPackLibJars =
17+ taskKey[Seq [File ]](" All libraries packed with packed in cliPack" )
18+ val cliPack = taskKey[File ](" Pack the CLI for the current configuration" )
1419
1520lazy val cli = project
1621 .in(file(" cli" ))
@@ -36,17 +41,124 @@ lazy val cli = project
3641 Seq (
3742 " -Xmx1024M" ,
3843 " -Dplugin.version=" + scalaNativeVersion.value,
39- " -Dscala-native-cli=" + cliPath
44+ " -Dscala-native-cli=" + cliPath,
45+ " -Dscala-native-cli-pack=" + (cliPack / crossTarget).value
4046 )
4147 },
4248 scriptedBufferLog := false ,
4349 scriptedDependencies := {
4450 scriptedDependencies
45- .dependsOn(assembly)
51+ .dependsOn(assembly, cliPack )
4652 .value
47- }
53+ },
54+ cliPackSettings
4855 )
4956
57+ lazy val cliPackSettings = Def .settings(
58+ cliPackLibJars := {
59+ val s = streams.value
60+ val log = s.log
61+
62+ val scalaNativeOrg = " org.scala-native"
63+ val scalaBinVer = scalaBinaryVersion.value
64+ val snVer = scalaNativeVersion.value
65+
66+ val scalaFullVers = crossScalaNative212
67+ val cliAssemblyJar = assembly.value
68+
69+ // Standard modules needed for linking of Scala Native
70+ val optLib = snVer match {
71+ case " 0.4.0" => Nil
72+ case v => " windowslib" :: Nil
73+ }
74+ val stdLibModuleIDs = Seq (
75+ " nativelib" ,
76+ " clib" ,
77+ " posixlib" ,
78+ " javalib" ,
79+ " auxlib" ,
80+ " scalalib"
81+ ).++ (optLib).map { lib =>
82+ val nativeBinVersion = ScalaNativeCrossVersion .binaryVersion(snVer)
83+ scalaNativeOrg % s " ${lib}_native ${nativeBinVersion}_ ${scalaBinVer}" % snVer
84+ }
85+ val compilerPluginModuleIDs =
86+ scalaFullVers.map(v => scalaNativeOrg % s " nscplugin_ $v" % snVer)
87+ val allModuleIDs = (stdLibModuleIDs ++ compilerPluginModuleIDs).toVector
88+ val allModuleIDsIntransitive = allModuleIDs.map(_.intransitive())
89+
90+ val resolvedLibJars = {
91+ val retrieveDir = s.cacheDirectory / " cli-lib-jars"
92+ val lm = {
93+ import sbt .librarymanagement .ivy ._
94+ val ivyConfig = InlineIvyConfiguration ().withLog(log)
95+ IvyDependencyResolution (ivyConfig)
96+ }
97+ val dummyModuleName =
98+ s " clilibjars- $snVer- $scalaBinVer- " + scalaFullVers.mkString(" -" )
99+ val dummyModuleID = scalaNativeOrg % dummyModuleName % version.value
100+ val descriptor =
101+ lm.moduleDescriptor(
102+ dummyModuleID,
103+ allModuleIDsIntransitive,
104+ scalaModuleInfo = None
105+ )
106+ lm
107+ .retrieve(descriptor, retrieveDir, log)
108+ .fold(
109+ { unresolvedWarn => throw unresolvedWarn.resolveException },
110+ identity
111+ )
112+ .distinct
113+ }
114+
115+ cliAssemblyJar +: resolvedLibJars
116+ },
117+ cliPack / target := crossTarget.value / " pack" ,
118+ cliPack / moduleName :=
119+ s " scala-native-cli_ ${scalaBinaryVersion.value}- ${scalaNativeVersion.value}" ,
120+ cliPack / crossTarget := (cliPack / target).value / (cliPack / moduleName).value,
121+ cliPack := {
122+ val scalaBinVer = scalaBinaryVersion.value
123+ val snVer = scalaNativeVersion.value
124+
125+ val trg = (cliPack / crossTarget).value
126+ val trgLib = trg / " lib"
127+ val trgBin = trg / " bin"
128+
129+ if (trg.exists)
130+ IO .delete(trg)
131+
132+ IO .createDirectory(trgLib)
133+ val libJars = cliPackLibJars.value
134+ for (libJar <- libJars) {
135+ IO .copyFile(libJar, trgLib / libJar.getName)
136+ }
137+
138+ IO .createDirectory(trgBin)
139+ val scriptDir = (Compile / sourceDirectory).value.getParentFile / " script"
140+ for {
141+ scriptFile <- IO .listFiles(scriptDir)
142+ if ! scriptFile.getPath.endsWith(" ~" )
143+ } {
144+ val content = IO .read(scriptFile)
145+ val processedContent = content
146+ .replaceAllLiterally(" @SCALA_BIN_VER@" , scalaBinVer)
147+ .replaceAllLiterally(" @SCALANATIVE_VER@" , snVer)
148+ .replaceAllLiterally(
149+ " @SCALANATIVE_BIN_VER@" ,
150+ ScalaNativeCrossVersion .binaryVersion(snVer)
151+ )
152+ val dest = trgBin / scriptFile.getName
153+ IO .write(dest, processedContent)
154+ if (scriptFile.canExecute)
155+ dest.setExecutable( /* executable = */ true , /* ownerOnly = */ false )
156+ }
157+
158+ trg
159+ }
160+ )
161+
50162// To be removed since 0.4.2
51163lazy val patchSourcesSettings = {
52164 def patchSources (base : File , version : String , subdir : String ) = {
0 commit comments