-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add runtime compilation of specific resources
- Loading branch information
1 parent
7a743da
commit 1396cba
Showing
7 changed files
with
132 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
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) = | ||
println(name) | ||
Files.createDirectories(appDataStore) | ||
val cacheLocation = appDataStore.resolve(s"$name.c") | ||
if !Files.exists(appDataStore.resolve(s"$name.c")) then | ||
Files.copy(getClass().getResourceAsStream(s"/native/$name.c"), cacheLocation) | ||
|
||
def compileCachedResourceIfNeeded(name: String): Path = | ||
val cacheLocation = appDataStore.resolve(s"$name$sharedLibSuffix") | ||
val headerLocation = appDataStore.resolve(s"$name.c") | ||
|
||
if !Files.exists(cacheLocation) then | ||
val cmd = Seq("clang", "-shared", "-Os", "-o", cacheLocation.nn.toAbsolutePath().nn.toString(), headerLocation.nn.toAbsolutePath().nn.toString()) | ||
if cmd.! != 0 then throw Error(s"failed to compile $headerLocation") | ||
|
||
cacheLocation.nn | ||
|
||
def loadCachedLibrary(name: String) = | ||
val cacheLocation = appDataStore.resolve(s"$name.$sharedLibSuffix") | ||
System.loadLibrary(cacheLocation.nn.toAbsolutePath().nn.toString()) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
int identity_int(int i) { | ||
return i; | ||
} |