@@ -5,10 +5,17 @@ object ScalaZ3build extends Build {
5
5
6
6
val natives = List (" z3.Z3Wrapper" )
7
7
8
+ lazy val PS = java.io.File .pathSeparator
9
+ lazy val DS = java.io.File .separator
10
+
8
11
lazy val cPath = file(" src" ) / " c"
9
12
lazy val cFiles = file(" src" ) / " c" * " *.c"
10
13
lazy val soName = System .mapLibraryName(" scalaz3" )
14
+
15
+ lazy val z3Name = if (isMac) " libz3.dylib" else if (isWindows) " libz3.dll" else System .mapLibraryName(" z3" )
16
+
11
17
lazy val libBinPath = file(" lib-bin" )
18
+ lazy val z3BinFilePath = z3LibPath / z3Name
12
19
lazy val libBinFilePath = libBinPath / soName
13
20
lazy val jdkIncludePath = file(System .getProperty(" java.home" )) / " .." / " include"
14
21
lazy val jdkUnixIncludePath = jdkIncludePath / " linux"
@@ -54,7 +61,7 @@ object ScalaZ3build extends Build {
54
61
55
62
val javahKey = TaskKey [Unit ](" javah" , " Prepares the JNI headers" )
56
63
val gccKey = TaskKey [Unit ](" gcc" , " Compiles the C sources" )
57
- val checksumKey = TaskKey [Unit ](" checksum" , " Generates checksum file." )
64
+ val checksumKey = TaskKey [String ](" checksum" , " Generates checksum file." )
58
65
59
66
val checksumTask = (streams, sourceDirectory in Compile ) map {
60
67
case (s, sd) =>
@@ -97,25 +104,31 @@ object ScalaZ3build extends Build {
97
104
fw.close
98
105
99
106
s.log.info(" Wrote checksum " + md5String + " as part of " + checksumFilePath.asFile + " ." )
107
+
108
+ md5String
100
109
}
101
110
102
111
103
112
val javahTask = (streams, dependencyClasspath in Compile , classDirectory in Compile ) map {
104
113
case (s, deps, cd) =>
105
114
106
- deps.map(_.data.absolutePath).find(_.endsWith(" lib/ scala-library.jar" )) match {
115
+ deps.map(_.data.absolutePath).find(_.endsWith(" lib" + DS + " scala-library.jar" )) match {
107
116
case Some (lib) =>
108
117
s.log.info(" Preparing JNI headers..." )
109
- exec(" javah -classpath " + cd.absolutePath + " : " + lib+ " -d " + cPath.absolutePath + " " + natives.mkString(" " ), s)
118
+ exec(" javah -classpath " + cd.absolutePath + PS + lib + " -d " + cPath.absolutePath + " " + natives.mkString(" " ), s)
110
119
111
120
case None =>
112
121
s.log.error(" Scala library not found in dependencies ?!?" )
113
122
114
123
}
115
124
} dependsOn(compile.in(Compile ))
116
125
126
+ def extractDir (checksum : String ): String = {
127
+ System .getProperty(" java.io.tmpdir" ) + DS + " SCALAZ3_" + checksum + DS + " lib-bin" + DS
128
+ }
129
+
117
130
118
- val gccTask = (streams) map { case (s) =>
131
+ val gccTask = (streams, checksumKey ) map { case (s, cs ) =>
119
132
s.log.info(" Compiling C sources ..." )
120
133
121
134
// First, we look for z3
@@ -131,17 +144,20 @@ object ScalaZ3build extends Build {
131
144
" -I" + jdkUnixIncludePath.absolutePath + " " +
132
145
" -I" + z3IncludePath.absolutePath + " " +
133
146
" -L" + z3LibPath.absolutePath + " " +
134
- " -g -lc -Wl,--no-as-needed -Wl,--copy-dt-needed -lz3 -fPIC -O2 -fopenmp " +
147
+ " -g -lc " +
148
+ " -Wl,-rpath," + extractDir(cs)+ " -Wl,--no-as-needed -Wl,--copy-dt-needed " +
149
+ " -lz3 -fPIC -O2 -fopenmp " +
135
150
cFiles.getPaths.mkString(" " ), s)
136
151
137
152
} else if (isWindows) {
138
153
exec(" gcc -shared -o " + libBinFilePath.absolutePath + " " +
139
154
" -D_JNI_IMPLEMENTATION_ -Wl,--kill-at " +
140
- " -I " + " \" " + jdkIncludePath.absolutePath + " \" " + " " +
141
- " -I " + " \" " + jdkWinIncludePath.absolutePath + " \" " + " " +
142
- " -I " + z3IncludePath.absolutePath + " " +
143
- cFiles.getPaths.mkString(" " ) + " " +
144
- z3LibPath.absolutePath, s)
155
+ " -D__int64=\" long long\" " +
156
+ " -I " + " \" " + jdkIncludePath.absolutePath + " \" " +
157
+ " -I " + " \" " + jdkWinIncludePath.absolutePath + " \" " +
158
+ " -I " + " \" " + z3IncludePath.absolutePath + " \" " +
159
+ cFiles.getPaths.mkString(" " ) +
160
+ " " + z3BinFilePath.absolutePath + " \" " , s)
145
161
} else if (isMac) {
146
162
val frameworkPath = " /System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers"
147
163
@@ -151,7 +167,9 @@ object ScalaZ3build extends Build {
151
167
" -I" + frameworkPath + " " +
152
168
" -I" + z3IncludePath.absolutePath + " " +
153
169
" -L" + z3LibPath.absolutePath + " " +
154
- " -g -lc -lz3 -fPIC -O2 -fopenmp " +
170
+ " -g -lc " +
171
+ " -Wl,-rpath," + extractDir(cs)+ " " +
172
+ " -lz3 -fPIC -O2 -fopenmp " +
155
173
cFiles.getPaths.mkString(" " ), s)
156
174
} else {
157
175
s.log.error(" Unknown arch: " + osInf+ " - " + osArch)
@@ -161,9 +179,25 @@ object ScalaZ3build extends Build {
161
179
162
180
val packageTask = (Keys .`package` in Compile ).dependsOn(javahKey, gccKey)
163
181
164
- val newMappingsTask = mappings in (Compile , packageBin) <<= (mappings in (Compile , packageBin)) map {
165
- case maps =>
166
- (libBinFilePath.getAbsoluteFile -> (" lib-bin/" + soName)) +: maps
182
+ val newMappingsTask = mappings in (Compile , packageBin) <<= (mappings in (Compile , packageBin), streams) map {
183
+ case (normalFiles, s) =>
184
+ val newFiles =
185
+ (libBinFilePath.getAbsoluteFile -> (" lib-bin/" + libBinFilePath.getName)) ::
186
+ (z3LibPath.listFiles.toList.map { f =>
187
+ f.getAbsoluteFile -> (" lib-bin/" + f.getName)
188
+ })
189
+
190
+ s.log.info(" Bundling files:" )
191
+ for ((from, to) <- newFiles) {
192
+ s.log.info(" - " + from+ " -> " + to)
193
+ }
194
+
195
+ newFiles ++ normalFiles
196
+ }
197
+
198
+ val newTestClassPath = internalDependencyClasspath in (Test ) <<= (artifactPath in (Compile , packageBin)) map {
199
+ case jar =>
200
+ List (Attributed .blank(jar))
167
201
}
168
202
169
203
lazy val root = Project (id = " ScalaZ3" ,
@@ -174,6 +208,7 @@ object ScalaZ3build extends Build {
174
208
javahKey <<= javahTask,
175
209
compile.in(Compile ) <<= compile.in(Compile ).dependsOn(checksumTask),
176
210
Keys .`package`.in(Compile ) <<= packageTask,
211
+ newTestClassPath,
177
212
newMappingsTask
178
213
)
179
214
)
0 commit comments