forked from scalapy/scalapy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sbt
249 lines (226 loc) · 9.17 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
import sbtcrossproject.CrossPlugin.autoImport.{crossProject, CrossType}
import scala.sys.process._
ThisBuild / organization := "dev.scalapy"
lazy val scala212Version = "2.12.16"
lazy val scala213Version = "2.13.8"
lazy val scala3Version = "3.1.3"
lazy val supportedScalaVersions = List(scala212Version, scala213Version, scala3Version)
ThisBuild / scalaVersion := scala213Version
lazy val scalapy = project.in(file(".")).aggregate(
macrosJVM, macrosNative,
coreJVM, coreNative,
).settings(
publish := {},
publishLocal := {}
)
addCommandAlias(
"publishSignedAll",
(scalapy: ProjectDefinition[ProjectReference])
.aggregate
.map(p => s"+ ${p.asInstanceOf[LocalProject].project}/publishSigned")
.mkString(";", ";", "")
)
lazy val macros = crossProject(JVMPlatform, NativePlatform)
.crossType(CrossType.Pure)
.in(file("coreMacros"))
.settings(
name := "scalapy-macros",
libraryDependencies ++= {
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, _)) => Seq(
"org.scala-lang" % "scala-reflect" % scalaVersion.value
)
case _ => Seq.empty
}
},
Compile / unmanagedSourceDirectories += {
val sharedSourceDir = (ThisBuild / baseDirectory).value / "coreMacros/src/main"
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, _)) => sharedSourceDir / "scala-2"
case _ => sharedSourceDir / "scala-3"
}
},
crossScalaVersions := supportedScalaVersions
)
lazy val macrosJVM = macros.jvm
lazy val macrosNative = macros.native
lazy val pythonLdFlags = {
val withoutEmbed = "python3-config --ldflags".!!
if (withoutEmbed.contains("-lpython")) {
withoutEmbed.split(' ').map(_.trim).filter(_.nonEmpty).toSeq
} else {
val withEmbed = "python3-config --ldflags --embed".!!
withEmbed.split(' ').map(_.trim).filter(_.nonEmpty).toSeq
}
}
lazy val pythonLibsDir = {
pythonLdFlags.find(_.startsWith("-L")).get.drop("-L".length)
}
lazy val core = crossProject(JVMPlatform, NativePlatform)
.in(file("core"))
.dependsOn(macros)
.settings(
name := "scalapy-core",
Compile / sourceGenerators += Def.task {
val fileToWrite = (Compile / sourceManaged).value / "TupleReaders.scala"
val methods = (2 to 22).map { n =>
val tupleElements = (1 to n).map(t => s"r$t.read(orArr(${t - 1}))")
.mkString(", ")
s"""implicit def tuple${n}Reader[${(1 to n).map(t => s"T$t").mkString(", ")}](implicit ${(1 to n).map(t => s"r$t: Reader[T$t]").mkString(", ")}): Reader[(${(1 to n).map(t => s"T$t").mkString(", ")})] = {
| new Reader[(${(1 to n).map(t => s"T$t").mkString(", ")})] {
| override def read(or: PyValue) = {
| val orArr = or.getTuple
| ($tupleElements)
| }
| }
|}"""
}
val toWrite =
s"""package me.shadaj.scalapy.readwrite
|import me.shadaj.scalapy.interpreter.PyValue
|trait TupleReaders {
|${methods.mkString("\n")}
|}""".stripMargin
IO.write(fileToWrite, toWrite)
Seq(fileToWrite)
},
Compile / sourceGenerators += Def.task {
val fileToWrite = (Compile / sourceManaged).value / "TupleWriters.scala"
val methods = (2 to 22).map { n =>
val seqArgs = (1 to n).map(t => s"r$t.write(v._" + t + ")").mkString(", ")
s"""implicit def tuple${n}Writer[${(1 to n).map(t => s"T$t").mkString(", ")}](implicit ${(1 to n).map(t => s"r$t: Writer[T$t]").mkString(", ")}): Writer[(${(1 to n).map(t => s"T$t").mkString(", ")})] = {
| new Writer[(${(1 to n).map(t => s"T$t").mkString(", ")})] {
| override def write(v: (${(1 to n).map(t => s"T$t").mkString(", ")})): PyValue = {
| CPythonInterpreter.createTuple(Seq(${seqArgs}))
| }
| }
|}"""
}
val toWrite =
s"""package me.shadaj.scalapy.readwrite
|import me.shadaj.scalapy.interpreter.{CPythonInterpreter, PyValue}
|trait TupleWriters {
|${methods.mkString("\n")}
|}""".stripMargin
IO.write(fileToWrite, toWrite)
Seq(fileToWrite)
},
Compile / sourceGenerators += Def.task {
val fileToWrite = (Compile / sourceManaged).value / "FunctionReaders.scala"
val methods = (0 to 22).map { n =>
val functionArgs = (1 to n).map(t => s"w$t.write(i$t)")
.mkString(", ")
s"""implicit def function${n}Reader[${((1 to n).map(t => s"T$t") :+ "O").mkString(", ")}](implicit ${((1 to n).map(t => s"w$t: Writer[T$t]") :+ "oReader: Reader[O]").mkString(", ")}): Reader[(${(1 to n).map(t => s"T$t").mkString(", ")}) => O] = {
| new Reader[(${(1 to n).map(t => s"T$t").mkString(", ")}) => O] {
| override def read(orig: PyValue) = {
| (${(1 to n).map(t => s"i$t: T$t").mkString(", ")}) => {
| oReader.read(CPythonInterpreter.call(orig, Seq($functionArgs), Seq()))
| }
| }
| }
|}"""
}
val toWrite =
s"""package me.shadaj.scalapy.readwrite
|import me.shadaj.scalapy.interpreter.{CPythonInterpreter, PyValue}
|trait FunctionReaders {
|${methods.mkString("\n")}
|}""".stripMargin
IO.write(fileToWrite, toWrite)
Seq(fileToWrite)
},
Compile / sourceGenerators += Def.task {
val fileToWrite = (Compile / sourceManaged).value / "FunctionWriters.scala"
val methods = (0 to 22).map { n =>
val seqArgs = (1 to n).map(t => s"r$t.read(args(${t - 1}))").mkString(", ")
s"""implicit def function${n}Writer[${((1 to n).map(t => s"T$t") :+ "O").mkString(", ")}](implicit ${((1 to n).map(t => s"r$t: Reader[T$t]") :+ "oWriter: Writer[O]").mkString(", ")}): Writer[(${(1 to n).map(t => s"T$t").mkString(", ")}) => O] = {
| new Writer[(${(1 to n).map(t => s"T$t").mkString(", ")}) => O] {
| override def write(v: (${(1 to n).map(t => s"T$t").mkString(", ")}) => O): PyValue = {
| CPythonInterpreter.createLambda(args => oWriter.write(v($seqArgs)))
| }
| }
|}"""
}
val toWrite =
s"""package me.shadaj.scalapy.readwrite
|import me.shadaj.scalapy.interpreter.{CPythonInterpreter, PyValue}
|trait FunctionWriters {
|${methods.mkString("\n")}
|}""".stripMargin
IO.write(fileToWrite, toWrite)
Seq(fileToWrite)
},
libraryDependencies += "org.scala-lang.modules" %%% "scala-collection-compat" % "2.8.1",
libraryDependencies ++= {
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, _)) => Seq(
"org.scala-lang" % "scala-reflect" % scalaVersion.value
)
case _ => Seq.empty
}
},
libraryDependencies += "org.scalatest" %%% "scalatest" % "3.2.13" % Test,
Compile / unmanagedSourceDirectories += {
val sharedSourceDir = (ThisBuild / baseDirectory).value / "core/shared/src/main"
if (scalaVersion.value.startsWith("2.13.") || scalaVersion.value.startsWith("3")) sharedSourceDir / "scala-2.13"
else sharedSourceDir / "scala-2.11_2.12"
},
Compile / unmanagedSourceDirectories += {
val sharedSourceDir = (ThisBuild / baseDirectory).value / "core/shared/src/main"
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, _)) => sharedSourceDir / "scala-2"
case _ => sharedSourceDir / "scala-3"
}
},
crossScalaVersions := supportedScalaVersions
).jvmSettings(
libraryDependencies += "net.java.dev.jna" % "jna" % "5.11.0",
Test / fork := true,
Test / javaOptions += s"-Djna.library.path=$pythonLibsDir",
unmanagedSources / excludeFilter := HiddenFileFilter || "*Native*"
).nativeSettings(
nativeLinkStubs := true,
nativeLinkingOptions ++= pythonLdFlags
)
lazy val coreJVM = core.jvm
lazy val coreNative = core.native
lazy val facadeGen = project.in(file("facadeGen"))
.dependsOn(coreJVM)
.settings(
run / fork := true,
run / javaOptions += s"-Djna.library.path=${"python3-config --prefix".!!.trim}/lib"
)
lazy val docs = project
.in(file("built-docs"))
.settings(
moduleName := "built-docs",
)
.enablePlugins(MdocPlugin, DocusaurusPlugin)
.dependsOn(coreJVM)
.settings(
fork := true,
connectInput := true,
javaOptions += s"-Djna.library.path=$pythonLibsDir",
docusaurusCreateSite := {
(Compile / mdoc).toTask(" ").value
Process(List("yarn", "install"), cwd = DocusaurusPlugin.website.value).!
Process(List("yarn", "run", "build"), cwd = DocusaurusPlugin.website.value).!
val out = DocusaurusPlugin.website.value / "build"
out
}
)
lazy val bench = crossProject(JVMPlatform, NativePlatform)
.crossType(CrossType.Pure)
.in(file("bench"))
.settings(
name := "scalapy-bench",
version := "0.1.0-SNAPSHOT",
crossScalaVersions := supportedScalaVersions
).jvmSettings(
javaOptions += s"-Djna.library.path=$pythonLibsDir"
).nativeSettings(
nativeLinkingOptions ++= pythonLdFlags,
nativeMode := "release-fast"
).dependsOn(core)
lazy val benchJVM = bench.jvm
lazy val benchNative = bench.native