Skip to content

Commit

Permalink
Introduce SN 0.5 flavour (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
keynmol authored Mar 1, 2024
1 parent a3cec00 commit 7b2de32
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 9 deletions.
18 changes: 16 additions & 2 deletions modules/bindgen/src/main/scala/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ enum OutputMode:
case SingleFile(out: OutputFile)
case MultiFile(out: OutputDirectory)

enum Flavour(val tag: String):
case ScalaNative04 extends Flavour("scala-native04")
case ScalaNative05 extends Flavour("scala-native05")

object Flavour:
def fromString(raw: String) =
if raw == ScalaNative04.tag then Some(ScalaNative04)
else if raw == ScalaNative05.tag then Some(ScalaNative05)
else None

val all = values.toList

case class Config(
linkName: Option[LinkName],
indentSize: IndentationSize,
Expand All @@ -26,7 +38,8 @@ case class Config(
exportMode: ExportMode,
outputChannel: OutputChannel,
tempDir: TempPath,
excludeSystemPaths: List[SystemPath]
excludeSystemPaths: List[SystemPath],
flavour: Flavour
)

case class Context(
Expand All @@ -53,7 +66,8 @@ object Config:
exportMode = ExportMode.No,
outputChannel = OutputChannel.cli,
tempDir = TempPath(sys.props("java.io.tmpdir")),
excludeSystemPaths = Nil
excludeSystemPaths = Nil,
flavour = Flavour.ScalaNative04
)
object defaults:
val indentSize = IndentationSize(3)
Expand Down
22 changes: 21 additions & 1 deletion modules/bindgen/src/main/scala/cli/arguments.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.monovore.decline.Opts
import java.io.File
import bindgen.RenderingConfig.NameFilter
import bindgen.OutputChannel
import cats.data.Validated

object CLI:
import com.monovore.decline.*
Expand Down Expand Up @@ -91,6 +92,24 @@ object CLI:
if scala then Lang.Scala else Lang.C
}

private val flavour =
val allFlavours = Flavour.all.map(_.tag)
Opts
.option[String](
"flavour",
s"Flavour of bindings to generate, all values: ${allFlavours.mkString(", ")}"
)
.mapValidated: raw =>
Flavour.fromString(raw) match
case None =>
Validated.invalidNel(
s"Flavour not recognised, possible values are: [${allFlavours.mkString(", ")}]"
)
case Some(v) =>
Validated.validNel(v)
.withDefault(Flavour.ScalaNative04)
end flavour

private val printFiles =
Opts
.flag(
Expand Down Expand Up @@ -359,7 +378,8 @@ object CLI:
exportMode,
Opts(OutputChannel.cli),
tempDir,
excludeSystemPaths
excludeSystemPaths,
flavour
).mapN(Config.apply)

val command = Command(
Expand Down
10 changes: 8 additions & 2 deletions modules/bindgen/src/main/scala/render/alias.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def alias(model: Def.Alias, line: Appender)(using
case _ => false

val modifier = if isOpaque then "opaque " else ""

val voidPtr = summon[Config].flavour match
case Flavour.ScalaNative04 => "Ptr[?]"
case Flavour.ScalaNative05 => "CVoidPtr"

renderComment(line, model.meta)
line(s"${modifier}type ${model.name} = ${scalaType(underlyingType)}")
line(s"object ${sanitiseBeforeColon(model.name)}: ")
Expand All @@ -38,19 +43,20 @@ def alias(model: Def.Alias, line: Appender)(using

if isFunctionPointer then
line(
s"inline def fromPtr(ptr: Ptr[Byte]): ${model.name} = CFuncPtr.fromPtr(ptr)"
s"inline def fromPtr(ptr: Ptr[Byte] | $voidPtr): ${model.name} = CFuncPtr.fromPtr(ptr.asInstanceOf[Ptr[Byte]])"
)
end if

if enableConstructor then

line(
s"inline def apply(inline o: ${scalaType(underlyingType)}): ${model.name} = o"
)
line(s"extension (v: ${model.name})")
nest {
line(s"inline def value: ${scalaType(underlyingType)} = v")
if isFunctionPointer then
line(s"inline def toPtr: Ptr[Byte] = CFuncPtr.toPtr(v)")
line(s"inline def toPtr: $voidPtr = CFuncPtr.toPtr(v)")
end if
}
end if
Expand Down
12 changes: 12 additions & 0 deletions modules/interface/src/main/scala/Binding.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import scala.util.control.NonFatal
import BindingLang.*
import Utils.*
import Binding.Defaults
import bindgen.interface.Flavour.ScalaNative04
import bindgen.interface.Flavour.ScalaNative05

class Binding private (
impl: Binding.BindingArgs
Expand All @@ -34,6 +36,7 @@ class Binding private (
def excludeSystemPaths: List[Path] = impl.excludeSystemPaths
def scalaFile: String = impl.scalaFile
def cFile: String = impl.cFile
def flavour: Option[Flavour] = impl.flavour

def withLinkName(name: String) = copy(_.copy(linkName = Some(name)))

Expand Down Expand Up @@ -96,6 +99,9 @@ class Binding private (
_.copy(externalNames = externals)
)

def withFlavour(flavour: Flavour): Binding =
copy(b => b.copy(flavour = Some(flavour)))

def addExternalName(nameFilter: String, packageName: String): Binding =
copy(b =>
b.copy(externalNames = b.externalNames.updated(nameFilter, packageName))
Expand Down Expand Up @@ -177,6 +183,10 @@ class Binding private (
arg("exclude-system-path", path.toString())
}

flavour.foreach { flavour =>
arg("flavour", flavour.tag)
}

sb ++= bindgenArguments

sb.result()
Expand Down Expand Up @@ -271,6 +281,7 @@ object Binding {
externalNames: Map[String, String] = Defaults.externalNames,
bindgenArguments: List[String] = Defaults.bindgenArguments,
excludeSystemPaths: List[Path] = Defaults.excludeSystemPaths,
flavour: Option[Flavour] = None,
scalaFile: String,
cFile: String
)
Expand All @@ -292,6 +303,7 @@ object Binding {
val bindgenArguments = List.empty[String]
val excludeSystemPaths = List.empty[Path]
val exportMode = false
val flavour = Flavour.ScalaNative04
}

}
6 changes: 6 additions & 0 deletions modules/interface/src/main/scala/Interface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ object BindingLang {
case object C extends BindingLang
}

sealed abstract class Flavour(val tag: String) extends Product with Serializable
object Flavour {
case object ScalaNative04 extends Flavour("scala-native04")
case object ScalaNative05 extends Flavour("scala-native05")
}

sealed abstract class LogLevel(val str: String)
extends Product
with Serializable
Expand Down
28 changes: 24 additions & 4 deletions modules/sbt-plugin/src/main/scala/BindgenPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ object BindgenMode {
}

object BindgenPlugin extends AutoPlugin {
import ScalaNativePlugin.autoImport.nativeConfig
import ScalaNativePlugin.autoImport.nativeVersion

object autoImport {
val bindgenVersion = settingKey[String](
s"Version of bindgen to download. Default: ${BuildInfo.version} (matches the plugin version)"
Expand All @@ -49,10 +52,16 @@ object BindgenPlugin extends AutoPlugin {
val bindgenMode = settingKey[BindgenMode](
s"Source generation mode. Default: ${BindgenMode.ResourceGenerator}"
)

val bindgenFlavour = settingKey[Flavour](
s"Bindgen flavour. Default: ${getBindgenFlavour(nativeVersion)}"
)
}

private def getBindgenFlavour(ver: String) =
if (ver.startsWith("0.5")) Flavour.ScalaNative05 else Flavour.ScalaNative04

override def requires: Plugins = ScalaNativePlugin
import ScalaNativePlugin.autoImport.nativeConfig

import autoImport.*

Expand Down Expand Up @@ -173,7 +182,8 @@ object BindgenPlugin extends AutoPlugin {
bindgenBindings := Seq.empty,
bindgenMode := BindgenMode.ResourceGenerator,
bindgenClangPath := nativeConfig.value.clang,
bindgenBinary := resolveBinaryTask.value.get
bindgenBinary := resolveBinaryTask.value.get,
bindgenFlavour := getBindgenFlavour(nativeVersion)
) ++
Seq(Compile, Test).flatMap(conf => inConfig(conf)(definedSettings(conf)))

Expand All @@ -182,7 +192,12 @@ object BindgenPlugin extends AutoPlugin {
bindgenGenerateScalaSources.value ++ bindgenGenerateCSources.value
},
bindgenGenerateScalaSources := {
val selected = (addConf / bindgenBindings).value
val selected = (addConf / bindgenBindings).value.map { b =>
b.flavour match {
case None => b.withFlavour(bindgenFlavour.value)
case Some(_) => b
}
}

val managedDestination = sourceManaged.value

Expand All @@ -201,7 +216,12 @@ object BindgenPlugin extends AutoPlugin {
)
},
bindgenGenerateCSources := {
val selected = (addConf / bindgenBindings).value
val selected = (addConf / bindgenBindings).value.map { b =>
b.flavour match {
case None => b.withFlavour(bindgenFlavour.value)
case Some(_) => b
}
}

val managedDestination = (resourceManaged).value / "scala-native"

Expand Down

0 comments on commit 7b2de32

Please sign in to comment.