forked from scalapy/scalapy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sbt
207 lines (187 loc) · 8.07 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
import sbtcrossproject.CrossPlugin.autoImport.{crossProject, CrossType}
import scala.sys.process._
organization in ThisBuild := "me.shadaj"
lazy val scala211Version = "2.11.12"
lazy val scala212Version = "2.12.9"
lazy val scala213Version = "2.13.1"
lazy val supportedScalaVersions = List(scala211Version, scala212Version, scala213Version)
scalaVersion in ThisBuild := 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 += "org.scala-lang" % "scala-reflect" % scalaVersion.value
).jvmSettings(
crossScalaVersions := supportedScalaVersions,
).nativeSettings(
scalaVersion := scala211Version
)
lazy val macrosJVM = macros.jvm
lazy val macrosNative = macros.native
lazy val core = crossProject(JVMPlatform, NativePlatform)
.in(file("core"))
.dependsOn(macros)
.settings(
name := "scalapy-core",
sourceGenerators in Compile += Def.task {
val fileToWrite = (sourceManaged in Compile).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)
},
sourceGenerators in Compile += Def.task {
val fileToWrite = (sourceManaged in Compile).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)
},
sourceGenerators in Compile += Def.task {
val fileToWrite = (sourceManaged in Compile).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)
},
sourceGenerators in Compile += Def.task {
val fileToWrite = (sourceManaged in Compile).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)
}
).settings(
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value,
unmanagedSourceDirectories in Compile += {
val sharedSourceDir = (baseDirectory in ThisBuild).value / "core/shared/src/main"
if (scalaVersion.value.startsWith("2.13.")) sharedSourceDir / "scala-2.13"
else sharedSourceDir / "scala-2.11_2.12"
}
).jvmSettings(
crossScalaVersions := supportedScalaVersions,
libraryDependencies += "net.java.dev.jna" % "jna" % "5.5.0",
libraryDependencies += "org.scalatest" %%% "scalatest" % "3.2.2" % Test,
libraryDependencies += "org.scalacheck" %% "scalacheck" % "1.14.3" % Test,
fork in Test := true,
javaOptions in Test += s"-Djna.library.path=${"python3-config --prefix".!!.trim}/lib"
).nativeSettings(
scalaVersion := scala211Version,
libraryDependencies += "org.scalatest" %%% "scalatest" % "3.1.0-RC3" % Test,
libraryDependencies += "com.github.lolgab" %%% "scalacheck" % "1.14.1" % Test,
nativeLinkStubs := true,
nativeLinkingOptions ++= "python3-config --ldflags".!!.split(' ').map(_.trim).filter(_.nonEmpty).toSeq
)
lazy val coreJVM = core.jvm
lazy val coreNative = core.native
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=${"python3-config --prefix".!!.trim}/lib",
docusaurusCreateSite := {
mdoc.in(Compile).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"
).jvmSettings(
fork := true,
crossScalaVersions := supportedScalaVersions,
javaOptions += s"-Djna.library.path=${"python3-config --prefix".!!.trim}/lib"
).nativeSettings(
scalaVersion := scala211Version,
nativeLinkingOptions ++= "python3-config --ldflags".!!.split(' ').map(_.trim).filter(_.nonEmpty).toSeq,
nativeMode := "release-fast"
).dependsOn(core)
lazy val benchJVM = bench.jvm
lazy val benchNative = bench.native