Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markehammons/issue5 #27

Merged
merged 4 commits into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ object v {
}

trait BaseModule extends ScalaModule with ScalafmtModule {
def scalaVersion = "3.2.1"
def scalaVersion = "3.2.0"

val munitVersion = "1.0.0-M6"
val jmhV = "1.33"
Expand Down
18 changes: 14 additions & 4 deletions core/src/fr/hammons/slinc/LibraryI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LibraryI(platformSpecific: LibraryI.PlatformSpecific):
val (handles, varGens) =
MethodHandleTools.calculateMethodHandles[L](platformSpecific, addresses)

inline def binding[R]: R =
inline def binding[R]: R =
${
LibraryI.bindingImpl[R, Library]
}
Expand All @@ -30,7 +30,10 @@ object LibraryI:
descriptor: Descriptor
): MethodHandle

def getLookup(name: Option[String]): Lookup
def getLocalLookup(name: String): Lookup
def getLibraryPathLookup(name: String): Lookup
def getStandardLibLookup: Lookup
def getResourceLibLookup(location: String): Lookup

def checkMethodIsCompatible(using q: Quotes)(s: q.reflect.Symbol): Unit =
import quotes.reflect.*
Expand Down Expand Up @@ -235,8 +238,15 @@ object LibraryI:
platformSpecificExpr: Expr[PlatformSpecific]
)(using Quotes, Type[L]) =
import quotes.reflect.*
val name = LibraryName.libraryName[L]
'{ $platformSpecificExpr.getLookup(${ Expr(name) }) }
val name: LibraryLocation = LibraryName.libraryName[L]
name match
case LibraryLocation.Standardard => '{ $platformSpecificExpr.getStandardLibLookup }
case LibraryLocation.Local(s) =>
'{ $platformSpecificExpr.getLocalLookup(${ Expr(s) }) }
case LibraryLocation.Path(s) =>
'{ $platformSpecificExpr.getLibraryPathLookup(${ Expr(s) }) }
case LibraryLocation.Resource(s) =>
'{ $platformSpecificExpr.getResourceLibLookup(${Expr(s)} ) }

inline def getMethodAddress[L](l: Lookup) = ${
getMethodAddressImpl[L]('l)
Expand Down
12 changes: 11 additions & 1 deletion core/src/fr/hammons/slinc/LibraryName.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import scala.quoted.*

class LibraryName(name: String) extends StaticAnnotation

enum LibraryLocation:
case Local(s: String)
case Resource(s: String)
case Path(s: String)
case Standardard

object LibraryName:
def libraryName[L](using Quotes, Type[L]) =
import quotes.reflect.*
Expand All @@ -15,4 +21,8 @@ object LibraryName:
List(Literal(StringConstant(name)))
) =>
name
}.headOption
}.map{
case s"@$path" => LibraryLocation.Resource(path)
case s"#$path" => LibraryLocation.Path(path)
case s => LibraryLocation.Local(s)
}.headOption.getOrElse(LibraryLocation.Standardard)
15 changes: 14 additions & 1 deletion core/src/fr/hammons/slinc/Lookup.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
package fr.hammons.slinc

private[slinc] trait Lookup:
import java.nio.file.Path
import java.nio.file.Paths

trait Lookup(libraryLocation: LibraryLocation):
def lookup(name: String): Object

def lookupError(name: String): Error = libraryLocation match
case LibraryLocation.Standardard => Error(s"Failed to load symbol $name from the standard library.")
case LibraryLocation.Resource(location) =>
Error(s"Failed to load symbol $name from resource $location. This could be caused by resource collision. Is the resource name unique enough?")
case LibraryLocation.Local(location) =>
val absPath = Paths.get(location).nn.toAbsolutePath.nn
Error(s"Failed to load symbol $name from local path $absPath")
case LibraryLocation.Path(libName) =>
Error(s"Failed to load symbol $name from library $libName")
62 changes: 62 additions & 0 deletions core/src/fr/hammons/slinc/Tools.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package fr.hammons.slinc

import java.nio.file.{Files, Paths, Path}
import types.{os, OS}
import java.security.MessageDigest
import java.util.HexFormat
import fr.hammons.slinc.types.OS
import fr.hammons.slinc.types.OS
import java.nio.channels.Channel
import java.nio.channels.ByteChannel
import java.nio.channels.Channels
import scala.sys.process.*

object Tools:
private val appDataStore =
Paths
.get(os match
case OS.Windows =>
s"${System.getenv("APPDATA").nn}\\local\\slinc\\libstore\\"
case OS.Linux => s"${System.getProperty("user.home").nn}/.cache/slinc/"
case OS.Darwin =>
s"${System.getProperty("user.home").nn}/Library/Application Support/"
)
.nn

private val sharedLibSuffix =
os match
case OS.Linux | OS.Darwin => ".so"
case OS.Windows => ".dll"
case OS.Unknown =>
throw Error("Cannot do lib compilation on this platform, it's unknown.")

def sendResourceToCache(name: String): Unit =
Files.createDirectories(appDataStore)
val cacheLocation = appDataStore.resolve(s"$name.c")

if !Files.exists(appDataStore.resolve(s"$name.c")) then
val stream = getClass().getResourceAsStream(s"/native/$name.c")
if stream != null then Files.copy(stream, cacheLocation)
else throw Error(s"Could not find resource /native/$name.c")

def compileCachedResourceIfNeeded(name: String): Unit =
val cacheLocation = appDataStore.resolve(s"$name$sharedLibSuffix")
val headerLocation = appDataStore.resolve(s"$name.c")

if !Files.exists(cacheLocation) then
val cmd = Seq(
"clang",
"-shared",
"-fvisibility=default",
"-Os",
"-o",
cacheLocation.nn.toAbsolutePath().nn.toString(),
headerLocation.nn.toAbsolutePath().nn.toString()
)
if cmd.! != 0 then throw Error(s"failed to compile $headerLocation: ${cmd.mkString(" ")}")

def loadCachedLibrary(name: String) =
val cacheLocation = appDataStore.resolve(s"$name$sharedLibSuffix")
System.load(cacheLocation.nn.toAbsolutePath().nn.toString())

end Tools
9 changes: 9 additions & 0 deletions core/test/resources/native/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifdef _WIN32
# define EXPORTED __declspec( dllexport )
#else
# define EXPORTED
#endif

EXPORTED int identity_int(int i) {
return i;
}
14 changes: 14 additions & 0 deletions core/test/src/fr/hammons/slinc/BindingSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fr.hammons.slinc

import munit.ScalaCheckSuite

trait BindingSpec(val slinc: Slinc) extends ScalaCheckSuite:
import slinc.{given,*}
@LibraryName("@test")
object Test derives Library:
def identity_int(i: CInt): CInt = Library.binding

test("int_identity") {
assertEquals(Test.identity_int(5), 5)
}

49 changes: 25 additions & 24 deletions j17/src/fr/hammons/slinc/Library17.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,28 @@ class Library17(layoutI: LayoutI, linker: CLinker)

linker.downcallHandle(address.asInstanceOf[Addressable], md, fd).nn

override def getLookup(name: Option[String]): Lookup =
import scala.jdk.OptionConverters.*
name match
case Some(n) =>
new Lookup:
if Files.exists(Paths.get(n)) then
System.load(Paths.get(n).nn.toRealPath().nn.toString())
else System.loadLibrary(n)
val l = SymbolLookup.loaderLookup().nn
def lookup(name: String) = l
.lookup(name)
.nn
.toScala
.getOrElse(throw Error(s"Failed to load $name from $n"))
case None =>
new Lookup:
val l = CLinker.systemLookup().nn
def lookup(name: String) = l
.lookup(name)
.nn
.toScala
.getOrElse(
throw Error(s"Failed to load $name from standard library")
)
class J17Lookup(s: SymbolLookup, libraryLocation: LibraryLocation) extends Lookup(libraryLocation):
def lookup(name: String): Object = s.lookup(name).nn.orElseThrow(() => throw this.lookupError(name)).nn
private val standardLibLookup = J17Lookup(CLinker.systemLookup().nn,LibraryLocation.Standardard)
override def getStandardLibLookup: Lookup =
Tools.hashCode()

standardLibLookup
override def getLibraryPathLookup(libName: String): Lookup =
System.loadLibrary(libName)
J17Lookup(SymbolLookup.loaderLookup().nn, LibraryLocation.Path(libName))


override def getResourceLibLookup(location: String): Lookup =
Tools.sendResourceToCache(location)
Tools.compileCachedResourceIfNeeded(location)
Tools.loadCachedLibrary(location)

J17Lookup(SymbolLookup.loaderLookup().nn, LibraryLocation.Resource(location))

override def getLocalLookup(libPath: String): Lookup =
System.load(libPath)

J17Lookup(SymbolLookup.loaderLookup().nn, LibraryLocation.Local(libPath))

end Library17
3 changes: 3 additions & 0 deletions j17/test/src/fr/hammons/slinc/BindingSpec17.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package fr.hammons.slinc

class BindingSpec17 extends BindingSpec(Slinc17.default)
44 changes: 20 additions & 24 deletions j19/src/fr/hammons/slinc/Library19.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,24 @@ class Library19(layoutI: LayoutI, linker: Linker)

linker.downcallHandle(address.asInstanceOf[Addressable], fd).nn

override def getLookup(name: Option[String]): Lookup =
import scala.jdk.OptionConverters.*
class J19Lookup(s: SymbolLookup, l: LibraryLocation) extends Lookup(l):
def lookup(name: String): Object = s.lookup(name).nn.orElseThrow(() => this.lookupError(name)).nn
override def getLibraryPathLookup(name: String): Lookup =
System.loadLibrary(name)

name match
case Some(n) =>
new Lookup:
if Files.exists(Paths.get(n)) then
System.load(Paths.get(n).nn.toRealPath().nn.toString())
else System.loadLibrary(n)
val l = SymbolLookup.loaderLookup().nn
def lookup(name: String) =
l.lookup(name)
.nn
.toScala
.getOrElse(throw Error(s"Lookup of $name in $n failed"))
case None =>
new Lookup:
val l = linker.defaultLookup().nn
def lookup(name: String) = l
.lookup(name)
.nn
.toScala
.getOrElse(
throw Error(s"Lookup of $name in standard library failed")
)
J19Lookup(SymbolLookup.loaderLookup().nn, LibraryLocation.Path(name))

override def getLocalLookup(name: String): Lookup =
System.load(name)

J19Lookup(SymbolLookup.loaderLookup().nn, LibraryLocation.Local(name))

override def getResourceLibLookup(location: String): Lookup =
Tools.sendResourceToCache(location)
Tools.compileCachedResourceIfNeeded(location)
Tools.loadCachedLibrary(location)

J19Lookup(SymbolLookup.loaderLookup().nn, LibraryLocation.Resource(location))

override def getStandardLibLookup: Lookup = J19Lookup(linker.defaultLookup().nn, LibraryLocation.Standardard)
end Library19
3 changes: 3 additions & 0 deletions j19/test/src/fr/hammons/slinc/BindingSpec19.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package fr.hammons.slinc

class BindingSpec19 extends BindingSpec(Slinc19.default)