-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsettings.sc
314 lines (287 loc) · 9.34 KB
/
settings.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
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
import $ivy.`de.tototec::de.tobiasroeser.mill.vcs.version::0.4.1`
import $ivy.`org.codehaus.plexus:plexus-archiver:4.2.2`
import de.tobiasroeser.mill.vcs.version.VcsVersion
import mill._, scalalib._
import mill.scalalib.publish.PublishInfo
import org.codehaus.plexus.archiver.zip.ZipUnArchiver
import scala.concurrent.duration.Duration
trait GenerateHeaders extends JavaModule {
def cDirectory = T{
millSourcePath / "src" / "main" / "c"
}
def javacOptions = T{
val dest = cDirectory()
Seq("-h", dest.toNIO.toAbsolutePath.toString)
}
def compile = T{
val res = super.compile()
val dir = cDirectory()
val headerFiles = Seq(dir).flatMap { dir =>
if (os.isDir(dir))
os.walk(dir)
.filter(p => os.isFile(p) && p.last.endsWith(".h"))
else
Nil
}
for (f <- headerFiles) {
val content = os.read.bytes(f)
for (updatedContent <- toCrLfOpt(content))
os.write.over(f, updatedContent)
}
res
}
}
def toCrLfOpt(content: Array[Byte]): Option[Array[Byte]] = {
val cr = '\r'.toByte
val lf = '\n'.toByte
val indices = content
.iterator
.zipWithIndex
.collect {
case (`lf`, idx) if idx > 0 && content(idx - 1) != cr => idx
}
.toVector
if (indices.isEmpty)
None
else {
val updatedContent = Array.ofDim[Byte](content.length + indices.length)
var i = 0 // content index
var j = 0 // updatedContent index
var n = 0 // indices index
while (n < indices.length) {
val idx = indices(n)
System.arraycopy(content, i, updatedContent, j, idx - i)
j += idx - i
i = idx
updatedContent(j) = cr
updatedContent(j + 1) = lf
i += 1
j += 2
n += 1
}
val idx = content.length
System.arraycopy(content, i, updatedContent, j, content.length - i)
j += idx - i
i = idx
assert(i == content.length)
assert(j == updatedContent.length)
Some(updatedContent)
}
}
private def vcVersions = Seq("2022", "2019", "2017")
private def vcEditions = Seq("Enterprise", "Community", "BuildTools")
private def progFiles = Seq(
"""C:\Program Files""",
"""C:\Program Files (x86)"""
)
private lazy val vcvarsCandidates = Option(System.getenv("VCVARSALL")) ++ {
for {
progFiles0 <- progFiles
version <- vcVersions
edition <- vcEditions
} yield progFiles0 + """\Microsoft Visual Studio\""" + version + "\\" + edition + """\VC\Auxiliary\Build\vcvars64.bat"""
}
private def vcvarsOpt: Option[os.Path] =
vcvarsCandidates
.iterator
.map(os.Path(_, os.pwd))
.find(os.exists)
private lazy val vcvars = vcvarsOpt.getOrElse {
sys.error("vcvars64.bat not found. Ensure Visual Studio is installed, or put the vcvars64.bat path in VCVARSALL.")
}
private def q = "\""
trait HasCSources extends JavaModule with PublishModule {
def windowsJavaHome: T[String]
def dllName: T[String]
def linkingLibs = T{ Seq.empty[String] }
def cSources = T.sources {
Seq(PathRef(millSourcePath / "src" / "main" / "c"))
}
def cCompile = T.persistent {
val destDir = T.ctx().dest / "obj"
val cFiles = cSources().flatMap { dir =>
if (os.isDir(dir.path))
os.walk(dir.path)
.filter(p => os.isFile(p) && p.last.endsWith(".c"))
else
Nil
}
val javaHome0 = os.Path(windowsJavaHome())
for (f <- cFiles) yield {
if (!os.exists(destDir))
os.makeDir.all(destDir)
val path = f.relativeTo(os.pwd).toString
val output = destDir / s"${f.last.stripSuffix(".c")}.obj"
val needsUpdate = !os.isFile(output) || os.mtime(output) < os.mtime(f)
if (needsUpdate) {
val script =
s"""@call "$vcvars"
|if %errorlevel% neq 0 exit /b %errorlevel%
|cl /I $q${javaHome0 / "include"}$q /I $q${javaHome0 / "include/win32"}$q /utf-8 /c $q$f$q
|""".stripMargin
val scriptPath = T.dest / "run-cl.bat"
os.write.over(scriptPath, script.getBytes, createFolders = true)
os.proc(scriptPath).call(cwd = destDir)
}
PathRef(output.resolveFrom(os.pwd))
}
}
def cLib = T.persistent {
val allObjFiles = cCompile().map(_.path)
val fileName = "csjniutils.lib"
val output = T.dest / fileName
val libNeedsUpdate = !os.isFile(output) || allObjFiles.exists(f => os.mtime(output) < os.mtime(f))
if (libNeedsUpdate) {
val script =
s"""@call "$vcvars"
|if %errorlevel% neq 0 exit /b %errorlevel%
|lib "/out:$fileName" ${allObjFiles.map(f => "\"" + f.toString + "\"").mkString(" ")}
|""".stripMargin
val scriptPath = T.dest / "run-lib.bat"
os.write.over(scriptPath, script.getBytes, createFolders = true)
os.proc(scriptPath).call(cwd = T.dest)
if (!os.isFile(output))
sys.error(s"Error: $output not created")
}
PathRef(output)
}
def dll = T.persistent {
val dllName0 = dllName()
val destDir = T.ctx().dest / "dlls"
if (!os.exists(destDir))
os.makeDir.all(destDir)
val dest = destDir / s"$dllName0.dll"
val relDest = dest.relativeTo(os.pwd)
val objs = cCompile()
val objsArgs = objs.map(o => o.path.relativeTo(os.pwd).toString).distinct
val libsArgs = linkingLibs().map(l => l + ".lib")
val needsUpdate = !os.isFile(dest) || {
val destMtime = os.mtime(dest)
objs.exists(o => os.mtime(o.path) > destMtime)
}
if (needsUpdate) {
val script =
s"""@call "$vcvars"
|if %errorlevel% neq 0 exit /b %errorlevel%
|link /DLL "/OUT:$dest" ${libsArgs.mkString(" ")} ${objs.map(f => "\"" + f.path.toString + "\"").mkString(" ")}
|""".stripMargin
val scriptPath = T.dest / "run-cl.bat"
os.write.over(scriptPath, script.getBytes, createFolders = true)
os.proc(scriptPath).call(cwd = T.dest)
}
PathRef(dest)
}
def resources = T.sources {
val dll0 = dll().path
val dir = T.ctx().dest / "dll-resources"
val dllDir = dir / "META-INF" / "native" / "windows64"
os.copy(dll0, dllDir / dll0.last, replaceExisting = true, createFolders = true)
super.resources() ++ Seq(PathRef(dir))
}
def extraPublish = super.extraPublish() ++ Seq(
PublishInfo(
file = dll(),
ivyConfig = "compile",
classifier = Some("x86_64-pc-win32"),
ext = "dll",
ivyType = "dll"
),
PublishInfo(
file = cLib(),
ivyConfig = "compile",
classifier = Some("x86_64-pc-win32"),
ext = "lib",
ivyType = "lib"
)
)
}
trait JniUtilsPublishVersion extends Module {
def publishVersion = T{
val state = VcsVersion.vcsState()
if (state.commitsSinceLastTag > 0) {
val versionOrEmpty = state.lastTag
.map(_.stripPrefix("v"))
.map { tag =>
val idx = tag.lastIndexOf(".")
if (idx >= 0) tag.take(idx + 1) + (tag.drop(idx + 1).toInt + 1).toString + "-SNAPSHOT"
else ""
}
.getOrElse("0.0.1-SNAPSHOT")
Some(versionOrEmpty)
.filter(_.nonEmpty)
.getOrElse(state.format())
} else
state
.lastTag
.getOrElse(state.format())
.stripPrefix("v")
}
}
trait JniUtilsPublishModule extends PublishModule with JniUtilsPublishVersion {
import mill.scalalib.publish._
def pomSettings = PomSettings(
description = artifactName(),
organization = "io.get-coursier.jniutils",
url = "https://github.com/coursier/jni-utils",
licenses = Seq(License.`Apache-2.0`),
versionControl = VersionControl.github("coursier", "jni-utils"),
developers = Seq(
Developer("alexarchambault", "Alex Archambault","https://github.com/alexarchambault")
)
)
}
def publishSonatype(
credentials: String,
pgpPassword: String,
data: Seq[PublishModule.PublishData],
timeout: Duration,
log: mill.api.Logger,
workspace: os.Path,
env: Map[String, String]
): Unit = {
val artifacts = data.map {
case PublishModule.PublishData(a, s) =>
(s.map { case (p, f) => (p.path, f) }, a)
}
val isRelease = {
val versions = artifacts.map(_._2.version).toSet
val set = versions.map(!_.endsWith("-SNAPSHOT"))
assert(set.size == 1, s"Found both snapshot and non-snapshot versions: ${versions.toVector.sorted.mkString(", ")}")
set.head
}
val publisher = new publish.SonatypePublisher(
uri = "https://oss.sonatype.org/service/local",
snapshotUri = "https://oss.sonatype.org/content/repositories/snapshots",
credentials = credentials,
signed = true,
gpgArgs = Seq("--detach-sign", "--batch=true", "--yes", "--pinentry-mode", "loopback", "--passphrase", pgpPassword, "--armor", "--use-agent"),
readTimeout = timeout.toMillis.toInt,
connectTimeout = timeout.toMillis.toInt,
log = log,
workspace = workspace,
env = env,
awaitTimeout = timeout.toMillis.toInt,
stagingRelease = isRelease
)
publisher.publishAll(isRelease, artifacts: _*)
}
trait WithDllNameJava extends JavaModule {
def generatedSources = T{
val f = T.ctx().dest / "coursier" / "jniutils" / "DllName.java"
val dllName0 = dllName()
val content =
s"""package coursier.jniutils;
|
|final class DllName {
| static String name = "$dllName0";
|}
|""".stripMargin
os.write(f, content, createFolders = true)
Seq(PathRef(f))
}
def publishVersion: T[String]
def dllName = T{
val ver = publishVersion()
s"csjniutils-$ver"
}
}