Skip to content

Commit fa784c1

Browse files
authored
Only generate files automatically when binding source is managed (#164)
1 parent d3508ca commit fa784c1

File tree

3 files changed

+69
-35
lines changed

3 files changed

+69
-35
lines changed

sbt-scala-native-bindgen/src/main/scala/org/scalanative/bindgen/sbt/ScalaNativeBindgenPlugin.scala

+54-31
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ object ScalaNativeBindgenPlugin extends AutoPlugin {
5353
val NativeBinding = BindingOptions
5454
val ScalaNativeBindgen = config("scala-native-bindgen").hide
5555
val nativeBindgenPath =
56-
taskKey[File]("Path to the scala-native-bindgen executable")
56+
settingKey[Option[File]]("Path to the scala-native-bindgen executable")
57+
val nativeBindgenResolvedPath =
58+
taskKey[File]("Resolved path to the scala-native-bindgen executable")
5759
val nativeBindings =
5860
settingKey[Seq[NativeBinding]]("Configuration for each bindings")
5961
val nativeBindgen = taskKey[Seq[File]]("Generate Scala Native bindings")
@@ -69,40 +71,52 @@ object ScalaNativeBindgenPlugin extends AutoPlugin {
6971
Def.settings(
7072
ivyConfigurations += ScalaNativeBindgen,
7173
version in nativeBindgen := BuildInfo.version,
74+
nativeBindgenPath := None,
7275
libraryDependencies ++= {
73-
artifactName.map { name =>
74-
val bindgenVersion = (version in nativeBindgen).value
75-
val url =
76-
s"${BuildInfo.projectUrl}/releases/download/v$bindgenVersion/$name"
76+
(nativeBindgenPath.value, artifactName) match {
77+
case (None, Some(name)) =>
78+
val bindgenVersion = (version in nativeBindgen).value
79+
val url =
80+
s"${BuildInfo.projectUrl}/releases/download/v$bindgenVersion/$name"
7781

78-
BuildInfo.organization % name % bindgenVersion % ScalaNativeBindgen from (url)
79-
}.toSeq
82+
Seq(
83+
BuildInfo.organization % name % bindgenVersion % ScalaNativeBindgen from (url))
84+
case _ =>
85+
Seq.empty
86+
}
8087
},
81-
nativeBindgenPath := {
82-
val scalaNativeBindgenUpdate = (update in ScalaNativeBindgen).value
83-
84-
val artifactFile = artifactName match {
88+
nativeBindgenResolvedPath := Def.taskDyn {
89+
nativeBindgenPath.value match {
90+
case Some(path) => Def.task { path }
8591
case None =>
86-
sys.error(
87-
"No downloadable binaries available for your OS, " +
88-
"please provide path via `nativeBindgenPath`")
89-
case Some(name) =>
90-
scalaNativeBindgenUpdate
91-
.select(artifact = artifactFilter(name = name))
92-
.head
93-
}
92+
Def.task {
93+
val scalaNativeBindgenUpdate =
94+
(update in ScalaNativeBindgen).value
9495

95-
// Set the executable bit on the expected path to fail if it doesn't exist
96-
for (view <- Option(
97-
Files.getFileAttributeView(artifactFile.toPath,
98-
classOf[PosixFileAttributeView]))) {
99-
val permissions = view.readAttributes.permissions
100-
if (permissions.add(PosixFilePermission.OWNER_EXECUTE))
101-
view.setPermissions(permissions)
102-
}
96+
val artifactFile = artifactName match {
97+
case None =>
98+
sys.error(
99+
"No downloadable binaries available for your OS, " +
100+
"please provide path via `nativeBindgenPath`")
101+
case Some(name) =>
102+
scalaNativeBindgenUpdate
103+
.select(artifact = artifactFilter(name = name))
104+
.head
105+
}
103106

104-
artifactFile
105-
}
107+
// Set the executable bit on the expected path to fail if it doesn't exist
108+
for (view <- Option(Files.getFileAttributeView(
109+
artifactFile.toPath,
110+
classOf[PosixFileAttributeView]))) {
111+
val permissions = view.readAttributes.permissions
112+
if (permissions.add(PosixFilePermission.OWNER_EXECUTE))
113+
view.setPermissions(permissions)
114+
}
115+
116+
artifactFile
117+
}
118+
}
119+
}.value
106120
)
107121

108122
private val artifactName =
@@ -115,10 +129,19 @@ object ScalaNativeBindgenPlugin extends AutoPlugin {
115129
inConfig(conf)(
116130
Def.settings(
117131
nativeBindings := Seq.empty,
118-
sourceGenerators += Def.task { nativeBindgen.value },
119132
target in nativeBindgen := sourceManaged.value / "sbt-scala-native-bindgen",
133+
sourceGenerators += Def.taskDyn {
134+
val nativeBindgenTarget = (target in nativeBindgen).value.toPath
135+
val managedSource = sourceManaged.value.toPath
136+
137+
if (nativeBindgenTarget.startsWith(managedSource)) {
138+
Def.task { nativeBindgen.value }
139+
} else {
140+
Def.task { Seq.empty[File] }
141+
}
142+
},
120143
nativeBindgen := {
121-
val bindgen = Bindgen(nativeBindgenPath.value)
144+
val bindgen = Bindgen(nativeBindgenResolvedPath.value)
122145
val optionsList = nativeBindings.value
123146
val outputDirectory = (target in nativeBindgen).value
124147
val logger = streams.value.log

sbt-scala-native-bindgen/src/sbt-test/bindgen/generate/build.sbt

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ enablePlugins(ScalaNativeBindgenPlugin)
55

66
//#example
77
scalaVersion := "2.11.12"
8+
nativeBindgenPath := Some(file(System.getProperty("bindgen.path")))
89

910
//#example
1011
inConfig(Compile)(
1112
Def.settings(
12-
//#example
13-
nativeBindgenPath := file(System.getProperty("bindgen.path")),
14-
//#example
1513
nativeBindings += {
16-
NativeBinding((resourceDirectory in Compile).value / "stdlib.h")
14+
NativeBinding(resourceDirectory.value / "stdlib.h")
1715
.name("stdlib")
1816
.packageName("org.example.app.stdlib")
1917
.excludePrefix("__")

sbt-scala-native-bindgen/src/sbt-test/bindgen/generate/test

+13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1+
# Generate bindings in managed source mode
2+
> sources
3+
$ must-mirror target/scala-2.11/src_managed/main/sbt-scala-native-bindgen/stdlib.scala expected/stdlib.scala
4+
5+
# Generate bindings when nativeBindgen is explicitly run
6+
> clean
17
> nativeBindgen
28
$ must-mirror target/scala-2.11/src_managed/main/sbt-scala-native-bindgen/stdlib.scala expected/stdlib.scala
39

10+
# Not generate bindings when not in managed source mode
411
> clean
512
> set target in (Compile, nativeBindgen) := nativeBindgenCustomTarget.value
13+
> sources
14+
$ absent src/main/scala/org/example/stdlib.scala
15+
16+
# Support custom output path
17+
> clean
618
> nativeBindgen
719
$ must-mirror src/main/scala/org/example/stdlib.scala expected/stdlib.scala
820

21+
# Support multiple bindings
922
> clean
1023
> set nativeBindings in Compile += nativeBindgenCoreBinding.value
1124
> nativeBindgen

0 commit comments

Comments
 (0)