-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.sc
executable file
·144 lines (127 loc) · 3.46 KB
/
compile.sc
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
#!/usr/local/bin/amm
import java.time.Instant
import ammonite.ops._
import ammonite.ops.ImplicitWd._
import scala.util.Try
val build = pwd/"build.sbt"
val plugins = pwd/"project"/"plugins.sbt"
@main(
doc = "Compile project via Scala Native"
)
def native(): Unit = {
val toBuild =
"""
|
|import scala.scalanative.build._
|
|enablePlugins(ScalaNativePlugin)
|
|nativeConfig ~= {
| _.withMode(Mode.releaseFull)
|}
|""".stripMargin
val toPlugins =
"""
|
|addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.0")
|""".stripMargin
withBackup { backup =>
backup(build)
backup(plugins)
write.append(build, toBuild)
write.append(plugins, toPlugins)
buildModified()
%sbt "clean; nativeLink"
successfullyCompiled("ScalaNative")
}
}
@main(
doc = "Compile project via GraalVM Native Image"
)
def graal(
@arg(
doc = "Enable llvm as Native Image compiler backend."
)
llvm: Boolean = false
): Unit = {
val toPlugins =
"""
|
|addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.15.0")
|""".stripMargin
withBackup { backup =>
backup(plugins)
write.append(plugins, toPlugins)
buildModified()
%sbt "clean; assembly"
println("Successfully created fat jar.")
val compilerArgs = {
val default = Seq[Shellable](
"-H:Optimize=2",
"--no-fallback",
)
val compilerDependent =
if(llvm) {
Seq[Shellable](
"-H:CompilerBackend=llvm",
"-H:Features=org.graalvm.home.HomeFinderFeature",
"-H:+SpawnIsolates",
)
} else {
Seq.empty[Shellable]
}
val files = Seq[Shellable](
"-jar",
"target/scala-2.13/NativeTest-assembly-0.1.jar",
"target/scala-2.13/graal-compiled"
)
default ++ compilerDependent ++ files
}
%.applyDynamic("native-image")(compilerArgs: _*)
successfullyCompiled("GraalVM Native Image")
}
}
@main(
doc = "Print help."
)
def help(): Unit =
println(
"""Usage: "./compile.sc <native|graal|help>"
| - "native" - compile project via Scala Native
| - "graal --llvm=<true|false>" - compile project via GraalVM Native Image
| - "--llvm" - enable llvm as compiler backend, false by default
| - "help" - prints this help
|""".stripMargin)
def withBackup(func: (Path => Path) => Unit): Unit = {
import scala.collection.mutable
val backups = mutable.HashMap.empty[Path, Path]
def makeBackup(path: Path): Path = {
println(s"Create backup for $path")
if(!exists(path)) {
write(path, "")
}
val backup = Path(s"${path.wrapped.getParent}/${path.baseName}-backup.${path.ext}")
cp(path, backup)
backups.addOne(path, backup)
path
}
def applyBackup(backupInfo: (Path, Path)): Unit = {
val (origin, backup) = backupInfo
println(s"Apply backup for $origin")
rm! origin
mv(backup, origin)
rm! backup
}
Try(func(makeBackup)).recover {
case e =>
println("Something goes wrong, make log file with stacktrace.")
write(
pwd/s"err-${Instant.now()}.log",
s"$e at\n${e.getStackTrace.mkString("\n")}"
)
}
backups.foreach(applyBackup)
}
def successfullyCompiled(via: String): Unit =
println(s"Successfully compiled project into binary via $via.")
def buildModified(): Unit = println("Build modified successfully.")