-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sbt
executable file
·147 lines (106 loc) · 4.05 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
name := "beanpiece"
organization := "com.dongjinlee"
version := "0.2"
description := "Java bindings for Google SentencePiece."
enablePlugins(JniPlugin)
autoScalaLibrary := false
crossPaths := false
logBuffered in Test := false
parallelExecution in Test := false
libraryDependencies ++= Seq(
"junit" % "junit" % "4.12" % "test",
"org.hamcrest" % "hamcrest-all" % "1.3" % "test"
)
javacOptions ++= Seq("-source", "1.7")
// sbt-jni configuration
jniLibraryName := "beanpiece"
jniNativeCompiler := "g++"
import java.util.Locale
val os = System.getProperty("os.name").toLowerCase(Locale.ENGLISH) match {
case os if os contains "win" => "windows"
case os if os contains "mac" => "osx"
case os if (os contains "nix") || (os contains "nux") => "linux"
case os => os
}
val arch = System.getProperty("os.arch")
lazy val soFileSrc = settingKey[File]("Where the shared object will be copied from")
soFileSrc := {
baseDirectory.value / "library" / os / arch / ("libsentencepiece." + jniLibSuffix.value)
}
lazy val soFileDst = settingKey[File]("Where the shared object will be copied to")
soFileDst := {
jniBinPath.value / ("libsentencepiece." + jniLibSuffix.value)
}
jniBinPath := {
(target in Compile).value / "classes" / os / arch
}
jniIncludes ++= Seq(
"-I" + jniNativeSources.value.toString
)
jniGccFlags ++= Seq(
// "-L" + , "-lsentencepiece"
)
jniUseCpp11 := true
jniLibSuffix := (System.getProperty("os.name").toLowerCase(Locale.ENGLISH) match {
case os if os startsWith "mac" => "dylib"
case os if os startsWith "darwin" => "dylib"
case os if os startsWith "win" => "dll"
case _ => "so"
})
// Temp: skip jniJavah task.
jniJavah := Def.task {
val log = streams.value.log
log.info("skipping header generation.")
}
// Temp: as of June 2018, sbt-jni 2.0 does not support compile flags ordering. So, override the implementation.
import scala.sys.process.Process
def checkExitCode(name: String, exitCode: Int): Unit = {
if (exitCode != 0) {
throw new MessageOnlyException(
s"$name exited with non-zero status ($exitCode)"
)
}
}
jniCompile := Def.task {
val log = streams.value.log
jniBinPath.value.getAbsoluteFile.mkdirs()
val sources = jniSourceFiles.value.mkString(" ")
val flags = jniGccFlags.value.mkString(" ")
val command = s"${jniNativeCompiler.value} $flags -o ${jniBinPath.value}/lib${jniLibraryName.value}.${jniLibSuffix.value} $sources -L${(baseDirectory.value / "library" / os / arch).toString} -lsentencepiece"
log.info(command)
checkExitCode(jniNativeCompiler.value, Process(command, jniBinPath.value) ! log)
}.dependsOn(jniJavah)
.tag(Tags.Compile, Tags.CPU)
.value
// copy sentencepiece shared object into target directory.
lazy val copySharedObject = taskKey[Unit](s"Copy sentencepiece shared object into target directory")
copySharedObject := {
println(s"Copying ${soFileSrc.value} into ${soFileDst.value}")
IO.copyFile(soFileSrc.value, soFileDst.value)
println("Copying completed.")
}
// dependency: jniCompile -> copySharedObject
copySharedObject := (copySharedObject dependsOn jniCompile).value
// dependency: copySharedObject -> compile
(compile in Compile) := ((compile in Compile) dependsOn copySharedObject).value
// sbt-sonatype configuration
homepage := Some(url("https://github.com/dongjinleekr/beanpiece"))
scmInfo := Some(ScmInfo(url("https://github.com/dongjinleekr/beanpiece"),
"git@github.com:dongjinleekr/beanpiece.git"))
developers := List(Developer("username",
"Lee Dongjin",
"dongjin@apache.org",
url("https://github.com/dongjinleekr")))
licenses += ("Apache-2.0", url("http://www.apache.org/licenses/LICENSE-2.0"))
publishTo := {
val nexus = "https://oss.sonatype.org/"
if (isSnapshot.value)
Some("snapshots" at nexus + "content/repositories/snapshots")
else
Some("releases" at nexus + "service/local/staging/deploy/maven2")
}
publishMavenStyle := true
publishArtifact in Test := false
pomIncludeRepository := { _ => false }
packageOptions in(Compile, packageBin) +=
Package.ManifestAttributes(new java.util.jar.Attributes.Name("Automatic-Module-Name") -> "com.dongjinlee.beanpiece")