diff --git a/src/main/java/org/moe/natj/general/NatJ.java b/src/main/java/org/moe/natj/general/NatJ.java index 438cafe..8952f2e 100644 --- a/src/main/java/org/moe/natj/general/NatJ.java +++ b/src/main/java/org/moe/natj/general/NatJ.java @@ -402,6 +402,29 @@ private static NativeRuntime getRuntime(Class type, boolean def) { */ private static Map resolvedLibraries = new HashMap(); + + 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. * @@ -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); @@ -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(); } diff --git a/src/main/native/natj/NatJ.cpp b/src/main/native/natj/NatJ.cpp index 0ba9d44..f777079 100644 --- a/src/main/native/natj/NatJ.cpp +++ b/src/main/native/natj/NatJ.cpp @@ -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) { @@ -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) { diff --git a/src/main/native/natj/NatJ.h b/src/main/native/natj/NatJ.h index 0adb508..3ccf1b5 100644 --- a/src/main/native/natj/NatJ.h +++ b/src/main/native/natj/NatJ.h @@ -94,6 +94,8 @@ limitations under the License. /** @} */ #ifdef __APPLE__ +#import + #import #if TARGET_OS_IOS #define NATJ_PLATFORM "ios" @@ -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.