forked from mc2-project/opaque-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sbt
271 lines (221 loc) · 9.15 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
name := "opaque"
version := "0.1"
organization := "edu.berkeley.cs.amplab"
scalaVersion := "2.11.8"
spName := "amplab/opaque"
sparkVersion := "2.0.2"
sparkComponents ++= Seq("core", "sql", "catalyst")
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.6" % "test"
val flatbuffersVersion = "1.6.0"
parallelExecution := false
fork in Test := true
javaOptions in Test ++= Seq("-Xmx2048m", "-XX:ReservedCodeCacheSize=384m")
scalacOptions ++= Seq(
"-deprecation",
"-encoding", "UTF-8",
"-feature",
"-unchecked",
"-Xfuture",
"-Xlint:_",
"-Ywarn-adapted-args",
"-Ywarn-dead-code",
"-Ywarn-inaccessible",
"-Ywarn-infer-any",
"-Ywarn-nullary-override",
"-Ywarn-nullary-unit",
"-Ywarn-unused-import"
)
scalacOptions in (Compile, console) := Seq.empty
val flatbuffersGenCppDir = SettingKey[File]("flatbuffersGenCppDir",
"Location of Flatbuffers generated C++ files.")
flatbuffersGenCppDir := sourceManaged.value / "flatbuffers" / "gen-cpp"
val buildType = SettingKey[BuildType]("buildType",
"Release, Debug, or Profile.")
buildType := Release
scalacOptions ++= { if (buildType.value == Debug) Seq("-g:vars") else Nil }
javaOptions ++= { if (buildType.value == Debug) Seq("-Xdebug", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000") else Nil }
val fetchFlatbuffersLibTask = TaskKey[File]("fetchFlatbuffersLib",
"Fetches and builds the Flatbuffers library, returning its location.")
unmanagedSources in Compile ++= ((fetchFlatbuffersLibTask.value / "java") ** "*.java").get
val buildFlatbuffersTask = TaskKey[Seq[File]]("buildFlatbuffers",
"Generates Java and C++ sources from Flatbuffers interface files, returning the Java sources.")
sourceGenerators in Compile += buildFlatbuffersTask.taskValue
val enclaveBuildTask = TaskKey[File]("enclaveBuild",
"Builds the C++ enclave code, returning the directory containing the resulting shared libraries.")
baseDirectory in enclaveBuildTask := (baseDirectory in ThisBuild).value
compile in Compile := { (compile in Compile).dependsOn(enclaveBuildTask).value }
val copyEnclaveLibrariesToResourcesTask = TaskKey[Seq[File]]("copyEnclaveLibrariesToResources",
"Copies the enclave libraries to the managed resources directory, returning the copied files.")
resourceGenerators in Compile += copyEnclaveLibrariesToResourcesTask.taskValue
// Add the managed resource directory to the resource classpath so we can find libraries at runtime
managedResourceDirectories in Compile += resourceManaged.value
// Watch the enclave C++ files
watchSources ++=
((sourceDirectory.value / "enclave") ** (
("*.cpp" || "*.h" || "*.tcc" || "*.edl" || "CMakeLists.txt") -- ".*")).get
// Watch the Flatbuffer schemas
watchSources ++=
((sourceDirectory.value / "flatbuffers") ** "*.fbs").get
val synthTestDataTask = TaskKey[Unit]("synthTestData",
"Synthesizes test data.")
test in Test := { (test in Test).dependsOn(synthTestDataTask).value }
val sgxGdbTask = TaskKey[Unit]("sgx-gdb-task",
"Runs OpaqueSinglePartitionSuite under the sgx-gdb debugger.")
def sgxGdbCommand = Command.command("sgx-gdb") { state =>
val extracted = Project extract state
val newState = extracted.append(Seq(buildType := Debug), state)
Project.extract(newState).runTask(sgxGdbTask, newState)
state
}
commands += sgxGdbCommand
initialCommands in console :=
"""
|import org.apache.spark.SparkContext
|import org.apache.spark.sql.SQLContext
|import org.apache.spark.sql.catalyst.analysis._
|import org.apache.spark.sql.catalyst.dsl._
|import org.apache.spark.sql.catalyst.errors._
|import org.apache.spark.sql.catalyst.expressions._
|import org.apache.spark.sql.catalyst.plans.logical._
|import org.apache.spark.sql.catalyst.rules._
|import org.apache.spark.sql.catalyst.util._
|import org.apache.spark.sql.execution
|import org.apache.spark.sql.functions._
|import org.apache.spark.sql.types._
|
|val spark = (org.apache.spark.sql.SparkSession.builder()
| .master("local")
| .appName("Opaque shell")
| .getOrCreate())
|val sc = spark.sparkContext
|val sqlContext = spark.sqlContext
|
|import spark.implicits._
|
|import edu.berkeley.cs.rise.opaque.implicits._
|edu.berkeley.cs.rise.opaque.Utils.initSQLContext(sqlContext)
""".stripMargin
cleanupCommands in console := "spark.stop()"
sgxGdbTask := {
(compile in Test).value
Process(Seq(
"sgx-gdb", "java",
"-x",
((baseDirectory in ThisBuild).value / "project" / "resources" / "run-tests.gdb").getPath),
None,
"CLASSPATH" -> (fullClasspath in Test).value.map(_.data.getPath).mkString(":")).!
}
fetchFlatbuffersLibTask := {
val flatbuffersSource = target.value / "flatbuffers" / s"flatbuffers-$flatbuffersVersion"
if (!flatbuffersSource.exists) {
// Fetch flatbuffers from Github
streams.value.log.info(s"Fetching Flatbuffers")
val flatbuffersUrl = new java.net.URL(
s"https://github.com/google/flatbuffers/archive/v$flatbuffersVersion.zip")
IO.unzipURL(flatbuffersUrl, flatbuffersSource.getParentFile)
}
val flatc = flatbuffersSource / "flatc"
if (!flatc.exists) {
// Build flatbuffers with cmake
import sys.process._
streams.value.log.info(s"Building Flatbuffers")
if (Process(Seq(
"cmake", "-G", "Unix Makefiles",
"-DFLATBUFFERS_BUILD_TESTS=OFF",
"-DFLATBUFFERS_BUILD_FLATLIB=OFF",
"-DFLATBUFFERS_BUILD_FLATHASH=OFF",
"-DFLATBUFFERS_BUILD_FLATC=ON"), flatbuffersSource).! != 0
|| Process(Seq("make"), flatbuffersSource).! != 0) {
sys.error("Flatbuffers library build failed.")
}
}
flatbuffersSource
}
// Flatbuffers header file generation
buildFlatbuffersTask := {
import sys.process._
val flatc = fetchFlatbuffersLibTask.value / "flatc"
val javaOutDir = sourceManaged.value / "flatbuffers" / "gen-java"
val flatbuffers = ((sourceDirectory.value / "flatbuffers") ** "*.fbs").get
// Only regenerate Flatbuffers headers if any .fbs file changed, indicated by their latest
// modification time being newer than all generated headers. We do this because regenerating
// Flatbuffers headers causes a full enclave rebuild, which is slow.
val fbsLastMod = flatbuffers.map(_.lastModified).max
val gen = (flatbuffersGenCppDir.value ** "*.h" +++ javaOutDir ** "*.java").get
if (gen.isEmpty || fbsLastMod > gen.map(_.lastModified).max) {
for (fbs <- flatbuffers) {
streams.value.log.info(s"Generating flatbuffers for ${fbs}")
if (Seq(flatc.getPath, "--cpp", "-o", flatbuffersGenCppDir.value.getPath, fbs.getPath).! != 0
|| Seq(flatc.getPath, "--java", "-o", javaOutDir.getPath, fbs.getPath).! != 0) {
sys.error("Flatbuffers build failed.")
}
}
}
(javaOutDir ** "*.java").get
}
nativePlatform := {
try {
val lines = Process("uname -sm").lines
if (lines.length == 0) {
sys.error("Error occured trying to run `uname`")
}
// uname -sm returns "<kernel> <hardware name>"
val parts = lines.head.split(" ")
if (parts.length != 2) {
sys.error("'uname -sm' returned unexpected string: " + lines.head)
} else {
val arch = parts(1).toLowerCase.replaceAll("\\s", "")
val kernel = parts(0).toLowerCase.replaceAll("\\s", "")
arch + "-" + kernel
}
} catch {
case ex: Exception =>
sLog.value.error("Error trying to determine platform.")
sLog.value.warn("Cannot determine platform! It will be set to 'unknown'.")
"unknown-unknown"
}
}
enclaveBuildTask := {
buildFlatbuffersTask.value // Enclave build depends on the generated C++ headers
import sys.process._
val enclaveSourceDir = baseDirectory.value / "src" / "enclave"
val enclaveBuildDir = target.value / "enclave"
enclaveBuildDir.mkdirs()
val cmakeResult =
Process(Seq(
"cmake",
s"-DCMAKE_INSTALL_PREFIX:PATH=${enclaveBuildDir.getPath}",
s"-DCMAKE_BUILD_TYPE=${buildType.value}",
s"-DFLATBUFFERS_LIB_DIR=${(fetchFlatbuffersLibTask.value / "include").getPath}",
s"-DFLATBUFFERS_GEN_CPP_DIR=${flatbuffersGenCppDir.value.getPath}",
enclaveSourceDir.getPath), enclaveBuildDir).!
if (cmakeResult != 0) sys.error("C++ build failed.")
val buildResult = Process(Seq("make"), enclaveBuildDir).!
if (buildResult != 0) sys.error("C++ build failed.")
val installResult = Process(Seq("make", "install"), enclaveBuildDir).!
if (installResult != 0) sys.error("C++ build failed.")
enclaveBuildDir / "lib"
}
copyEnclaveLibrariesToResourcesTask := {
val libraries = (enclaveBuildTask.value ** "*.so").get
val mappings: Seq[(File, String)] =
libraries pair rebase(enclaveBuildTask.value, s"/native/${nativePlatform.value}")
val resources: Seq[File] = for ((file, path) <- mappings) yield {
val resource = resourceManaged.value / path
IO.copyFile(file, resource)
resource
}
resources
}
synthTestDataTask := {
val diseaseDataFiles =
for {
diseaseDir <- (baseDirectory.value / "data" / "disease").get
name <- Seq("disease.csv", "gene.csv", "treatment.csv", "patient-125.csv")
} yield new File(diseaseDir, name)
if (!diseaseDataFiles.forall(_.exists)) {
import sys.process._
val ret = Seq("data/disease/synth-disease-data").!
if (ret != 0) sys.error("Failed to synthesize test data.")
}
}