Skip to content

Commit

Permalink
Merge pull request #8 from multi-os-engine/simulator-framework
Browse files Browse the repository at this point in the history
Try finding out the correct framework dir on simulator
  • Loading branch information
Noisyfox authored Jul 23, 2022
2 parents 5286fae + b96b5d6 commit 2f9a96e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
32 changes: 31 additions & 1 deletion src/main/java/org/moe/natj/general/NatJ.java
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,29 @@ private static NativeRuntime getRuntime(Class<?> type, boolean def) {
*/
private static Map<String, String> resolvedLibraries = new HashMap<String, String>();


private static String darwinSystemFrameworkRootDir = null;

private static String getDarwinSystemFrameworkRootDir() {
if (darwinSystemFrameworkRootDir == null) {
String simulatorFrameworkPath = getSimulatorFrameworkPath();
if (simulatorFrameworkPath == null || simulatorFrameworkPath.isEmpty()) {
darwinSystemFrameworkRootDir = "/System/Library/Frameworks/";
} else {
int index = simulatorFrameworkPath.indexOf("CoreFoundation.framework");
if (index < 0) {
// Not good
System.err.println("Error: Unexpected framework path: " + simulatorFrameworkPath);
darwinSystemFrameworkRootDir = "/System/Library/Frameworks/";
} else {
darwinSystemFrameworkRootDir = simulatorFrameworkPath.substring(0, index);
}
}
}

return darwinSystemFrameworkRootDir;
}

/**
* Looks up a library by its name in the file system.
*
Expand Down Expand Up @@ -478,7 +501,7 @@ protected static String lookUpLibrary(String name, boolean load) {

if (isDarwin()) {
for (String path : new String[] {
"/System/Library/Frameworks/" + name + ".framework"
getDarwinSystemFrameworkRootDir() + name + ".framework"
}) {
String exec_path = path + "/" + name;
File file = new File(path);
Expand Down Expand Up @@ -1182,4 +1205,11 @@ public static long toNative(Object instance, NativeObjectConstructionInfo info)
* all other cases
*/
public static native boolean loadFramework(String path);

/**
* Get the runtime system framework path on simulator.
*
* @return The path to the CoreFoundation.framework if running on simulator, or NULL on read device.
*/
private static native String getSimulatorFrameworkPath();
}
22 changes: 21 additions & 1 deletion src/main/native/natj/NatJ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,27 @@ jstring JNICALL Java_org_moe_natj_general_NatJ_getPlatformName(JNIEnv* env,
return env->NewStringUTF(NATJ_PLATFORM);
}

jstring JNICALL Java_org_moe_natj_general_NatJ_getSimulatorFrameworkPath(JNIEnv* env,
jclass clazz) {

#ifdef __APPLE__
#if TARGET_OS_SIMULATOR
Dl_info di;
int ret = dladdr((const void *)CFArrayContainsValue, &di);
if (!ret) {
LOGF << "Failed to get framework path!";
}
return env->NewStringUTF(di.dli_fname);
#else
// Only for simulator builds
return NULL;
#endif
#else
// Only for Darwin systems
return NULL;
#endif
}

jboolean JNICALL Java_org_moe_natj_general_NatJ_loadFramework(JNIEnv* env,
jclass clazz,
jstring path) {
Expand All @@ -594,7 +615,6 @@ jboolean JNICALL Java_org_moe_natj_general_NatJ_loadFramework(JNIEnv* env,
#endif
}


void handleShutdown(JNIEnv* env) { gJVMIsRunning = false; }

void* getJNICallFunction(JNIEnv* env, ffi_type* type, bool isStatic) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/native/natj/NatJ.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ limitations under the License.
/** @} */

#ifdef __APPLE__
#import <CoreFoundation/CoreFoundation.h>

#import <TargetConditionals.h>
#if TARGET_OS_IOS
#define NATJ_PLATFORM "ios"
Expand Down Expand Up @@ -378,6 +380,17 @@ JNIEXPORT jstring JNICALL
Java_org_moe_natj_general_NatJ_getPlatformName(JNIEnv* env,
jclass clazz);

/**
* Get the runtime system framework path on simulator.
*
* @param env JNIEnv pointer for the current thread
* @param clazz Java class of NatJ, used for nothing
* @return The path to the CoreFoundation.framework if running on simulator, or NULL on read device.
*/
JNIEXPORT jstring JNICALL
Java_org_moe_natj_general_NatJ_getSimulatorFrameworkPath(JNIEnv* env,
jclass clazz);

/**
* Try to load framework.
* Should be applied only for darwin OS.
Expand Down

0 comments on commit 2f9a96e

Please sign in to comment.