Skip to content

Commit

Permalink
Merge pull request #2 from alexarchambault/version-in-dll
Browse files Browse the repository at this point in the history
Put version in dll name
  • Loading branch information
alexarchambault committed Apr 13, 2021
2 parents 3a44b86 + 0b97d5b commit 4ce8c97
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
9 changes: 6 additions & 3 deletions build.sc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import $file.deps, deps.{Deps, MingwCommands, Scala, WindowsJvm}
import $file.settings, settings.{GenerateHeaders, HasCSources, JniUtilsPublishModule, downloadWindowsJvmArchive, isWindows, unpackWindowsJvmArchive}
import $file.settings, settings.{GenerateHeaders, HasCSources, JniUtilsPublishModule, JniUtilsPublishVersion, WithDllNameJava, downloadWindowsJvmArchive, isWindows, unpackWindowsJvmArchive}

import mill._, scalalib._

Expand All @@ -8,7 +8,6 @@ import scala.concurrent.duration._

object `windows-jni-utils` extends WindowsUtils with HasCSources with JniUtilsPublishModule {
def linkingLibs = Seq("ole32")
def dllName = "csjniutils"

def compile = T{
headers.`windows-jni-utils`.compile()
Expand Down Expand Up @@ -69,8 +68,12 @@ object headers extends Module {
define.BasePath(super.millModuleBasePath.value / os.up)
}

trait WindowsUtils extends MavenModule {
trait WindowsUtils extends MavenModule with JniUtilsPublishVersion with WithDllNameJava {
def compileIvyDeps = Agg(Deps.svm)
def dllName = T{
val ver = publishVersion()
s"csjniutils-$ver"
}
}

def windowsJvmArchive = T.persistent {
Expand Down
44 changes: 32 additions & 12 deletions settings.sc
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,7 @@ def unpackWindowsJvmArchive(windowsJvmArchive: os.Path, windowsJvmArchiveName: S
os.list(destDir).head
}

trait JniUtilsPublishModule extends PublishModule {
import mill.scalalib.publish._
def pomSettings = PomSettings(
description = artifactName(),
organization = "io.get-coursier.jniutils",
url = "https://github.com/coursier/jni-utils",
licenses = Seq(License.`Apache-2.0`),
versionControl = VersionControl.github("coursier", "jni-utils"),
developers = Seq(
Developer("alexarchambault", "Alex Archambault","https://github.com/alexarchambault")
)
)
trait JniUtilsPublishVersion extends Module {
def publishVersion = T{
val state = VcsVersion.vcsState()
if (state.commitsSinceLastTag > 0) {
Expand All @@ -243,6 +232,20 @@ trait JniUtilsPublishModule extends PublishModule {
}
}

trait JniUtilsPublishModule extends PublishModule with JniUtilsPublishVersion {
import mill.scalalib.publish._
def pomSettings = PomSettings(
description = artifactName(),
organization = "io.get-coursier.jniutils",
url = "https://github.com/coursier/jni-utils",
licenses = Seq(License.`Apache-2.0`),
versionControl = VersionControl.github("coursier", "jni-utils"),
developers = Seq(
Developer("alexarchambault", "Alex Archambault","https://github.com/alexarchambault")
)
)
}

def publishSonatype(
credentials: String,
pgpPassword: String,
Expand Down Expand Up @@ -277,3 +280,20 @@ def publishSonatype(

publisher.publishAll(isRelease, artifacts: _*)
}

trait WithDllNameJava extends JavaModule {
def dllName: T[String]
def generatedSources = T{
val f = T.ctx().dest / "coursier" / "jniutils" / "DllName.java"
val dllName0 = dllName()
val content =
s"""package coursier.jniutils;
|
|final class DllName {
| static String name = "$dllName0";
|}
|""".stripMargin
os.write(f, content, createFolders = true)
Seq(PathRef(f))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ static File fromClassPath(ClassLoader cl) {
} catch (URISyntaxException ex) {
throw new RuntimeException(ex);
}
if (f.getName().equals(dllName + ".dll") && f.isFile())
if (f.getName().equals(dllName() + ".dll") && f.isFile())
return f;
}
}
} else if (cl.getClass().getName().startsWith("jdk.internal.loader.ClassLoaders$AppClassLoader")) {
String strCp = System.getProperty("java.class.path");
for (String elem: strCp.split(File.pathSeparator)) {
File f = new File(elem).getAbsoluteFile();
if (f.getName().equals(dllName + ".dll") && f.isFile())
if (f.getName().equals(dllName() + ".dll") && f.isFile())
return f;
}
}
Expand All @@ -40,13 +40,13 @@ static File fromClassPath(ClassLoader cl) {
}

static File fromResources(ClassLoader cl) throws IOException, URISyntaxException {
URL url = cl.getResource(dllResourcePath);
URL url = cl.getResource(dllResourcePath());
if (url == null)
return null;
if (url.getProtocol().equals("file"))
return new File(url.toURI());
try (InputStream is = url.openStream(); ReadableByteChannel ic = Channels.newChannel(is)) {
File tmpFile = File.createTempFile(dllName, ".dll");
File tmpFile = File.createTempFile(dllName(), ".dll");
tmpFile.deleteOnExit();
try (FileOutputStream os = new FileOutputStream(tmpFile)) {
os.getChannel().transferFrom(ic, 0, Long.MAX_VALUE);
Expand All @@ -70,15 +70,19 @@ public static void ensureInitialized() {
throw new RuntimeException(ex);
}
if (dll == null)
System.loadLibrary(dllName);
System.loadLibrary(dllName());
else
System.load(dll.getAbsolutePath());
}
}
}
}

final static String dllName = "csjniutils";
final static String dllResourcePath = "META-INF/native/windows64/" + dllName + ".dll";
final static String dllName() {
return DllName.name;
}
final static String dllResourcePath() {
return "META-INF/native/windows64/" + dllName() + ".dll";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void beforeAnalysis(BeforeAnalysisAccess access) {
private void registerDllResource(Class<?> cls) {
if (!registered.getAndSet(true)) {
JNIRuntimeAccess.register(cls);
String resource = LoadWindowsLibrary.dllResourcePath;
String resource = LoadWindowsLibrary.dllResourcePath();
InputStream is = cls.getClassLoader().getResourceAsStream(resource);
if (is == null)
throw new RuntimeException("Could not find resource " + resource);
Expand Down

0 comments on commit 4ce8c97

Please sign in to comment.