Skip to content

Commit

Permalink
Dual model?
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold856 committed Nov 13, 2024
1 parent 86882ad commit 1e72a45
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,73 @@

package org.photonvision.jni;

import edu.wpi.first.util.RuntimeDetector;
import edu.wpi.first.util.RuntimeLoader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import org.photonvision.common.hardware.Platform;

public class PhotonTargetingJniLoader {
public static boolean isWorking = false;

public static boolean load() throws IOException, UnsatisfiedLinkError {
if (isWorking) return true;
if (isWorking)
return true;
isWorking = load_();
return isWorking;
}

public static boolean load_() throws IOException, UnsatisfiedLinkError {
// We always extract the shared object (we could hash each so, but that's a lot
// of work)
String arch_name = Platform.getNativeLibraryFolderName();
var clazz = PhotonTargetingJniLoader.class;

for (var libraryName : List.of("photontargeting", "photontargetingJNI")) {
RuntimeLoader.loadLibrary(libraryName);
try {
RuntimeLoader.loadLibrary(libraryName);
} catch (Exception e) {
System.out.println("Direct library load failed; falling back to extraction");
}

var nativeLibName = System.mapLibraryName(libraryName);
var path = "/nativelibraries/" + arch_name + "/" + nativeLibName;
var in = clazz.getResourceAsStream(path);

if (in == null) {
System.err.println("Could not get resource at path " + path);
return false;
}

// It's important that we don't mangle the names of these files on Windows at
// least
var tempfolder = Files.createTempDirectory("nativeextract");
File temp = new File(tempfolder.toAbsolutePath().toString(), nativeLibName);
System.out.println(temp.getAbsolutePath().toString());
FileOutputStream fos = new FileOutputStream(temp);

int read = -1;
byte[] buffer = new byte[1024];
while ((read = in.read(buffer)) != -1) {
fos.write(buffer, 0, read);
}
fos.close();
in.close();

try {
System.load(temp.getAbsolutePath());
} catch (Throwable t) {
System.err.println("Unable to System.load " + temp.getName() + " : " + t.getMessage());
t.printStackTrace();
return false;
}

System.out.println("Successfully loaded shared object " + temp.getName());
}
isWorking = true;
return isWorking;

return true;
}
}
26 changes: 24 additions & 2 deletions shared/javacpp/publish.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,30 @@ addTaskToCopyAllOutputs(cppHeadersZip)

model {
publishing {
def cppTaskList = createComponentZipTasks($.components, [nativeName], zipBaseName, Zip, project, includeStandardZipFormat)
def jniTaskList = createComponentZipTasks($.components, ["${nativeName}JNI"], jniBaseName, Zip, project, includeStandardZipFormat)
def cppTaskList = createComponentZipTasks($.components, [nativeName, "${nativeName}JNI"], zipBaseName, Zip, project, includeStandardZipFormat)

// From https://github.com/wpilibsuite/allwpilib/blob/1c220ebc607daa8da7d983b8f17bc40323633cb2/shared/jni/publish.gradle#L80C9-L100C11
def jniTaskList = createComponentZipTasks($.components, ["${nativeName}JNI"], jniBaseName, Jar, project, { task, value ->
value.each { binary ->
if (binary.buildable) {
if (binary instanceof SharedLibraryBinarySpec) {
task.dependsOn binary.tasks.link
def hashFile = new File(binary.sharedLibraryFile.parentFile.absolutePath, "${binary.component.baseName}.hash")
task.outputs.file(hashFile)
task.inputs.file(binary.sharedLibraryFile)
task.from(hashFile) {
into nativeUtils.getPlatformPath(binary)
}
task.doFirst {
hashFile.text = MessageDigest.getInstance("MD5").digest(binary.sharedLibraryFile.bytes).encodeHex().toString()
}
task.from(binary.sharedLibraryFile) {
into nativeUtils.getPlatformPath(binary)
}
}
}
}
})

publications {
cpp(MavenPublication) {
Expand Down

0 comments on commit 1e72a45

Please sign in to comment.