Skip to content

Commit

Permalink
Factor dll name in whole codebase
Browse files Browse the repository at this point in the history
It's now only defined in build.sc.
  • Loading branch information
alexarchambault committed Apr 13, 2021
1 parent 3a44b86 commit d7f1128
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
6 changes: 3 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, 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,9 @@ object headers extends Module {
define.BasePath(super.millModuleBasePath.value / os.up)
}

trait WindowsUtils extends MavenModule {
trait WindowsUtils extends MavenModule with WithDllNameJava {
def compileIvyDeps = Agg(Deps.svm)
def dllName = "csjniutils"
}

def windowsJvmArchive = T.persistent {
Expand Down
17 changes: 17 additions & 0 deletions settings.sc
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,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 d7f1128

Please sign in to comment.