Example #1
0
File: Lib.cpp Project: Gama11/hxcpp
void *__hxcpp_get_proc_address(String inLib, String full_name,bool inNdllProc,bool inQuietFail)
{
   String bin = __hxcpp_get_bin_dir();
   String deviceExt = __hxcpp_get_dll_extension();


   #ifdef HX_ANDROID
   String module_name = HX_CSTRING("lib") + inLib;
   #else
   String module_name = inLib;
   #endif

   #ifdef IPHONE
   gLoadDebug = true;
   setenv("DYLD_PRINT_APIS","1",true);

   #elif !defined(HX_WINRT)
   gLoadDebug = gLoadDebug || getenv("HXCPP_LOAD_DEBUG");
   #endif

   if (!sgLibPathIsInit)
   {
      sgLibPathIsInit = true;

      sgLibPath.push_back("./");
      #ifdef HX_MACOS
      sgLibPath.push_back("@executable_path/");
      #endif
      sgLibPath.push_back("");

      #ifdef HXCPP_TRY_HAXELIB
      String hxcpp = GetEnv("HXCPP");
      if (hxcpp.length==0)
         hxcpp = FindHaxelib( HX_CSTRING("hxcpp") );
      if (hxcpp.length!=0)
         __hxcpp_push_dll_path(hxcpp+HX_CSTRING("/bin/") + bin + HX_CSTRING("/"));
      #endif
   }


   Module module = sgLoadedModule[module_name.__s];

   bool new_module = module==0;

   if (!module && sgRegisteredPrims)
   {
      void *registered = (*sgRegisteredPrims)[full_name.__CStr()];
      // Try with lib name ...
      if (!registered)
      {
         String libString = inLib + HX_CSTRING("_") + full_name;
         registered = (*sgRegisteredPrims)[libString.__CStr()];
      }

      if (registered)
         return registered;
   }

   if (!module && gLoadDebug)
   {
      #ifdef ANDROID
       __android_log_print(ANDROID_LOG_INFO, "loader", "Searching for %s...", module_name.__s);
      #else
      printf("Searching for %s...\n", inLib.__s);
      #endif
   }

   String haxelibPath;

   for(int e=0; module==0 && e<3; e++)
   {
      String extension = e==0 ? deviceExt : e==1 ? HX_CSTRING(".ndll") : HX_CSTRING("");

      for(int path=0;path<sgLibPath.size();path++)
      {
         String testPath = String( sgLibPath[path].c_str() ) +  module_name + extension;
         if (gLoadDebug)
         {
            #ifndef ANDROID
            printf(" try %s...\n", testPath.__s);
            #else
            __android_log_print(ANDROID_LOG_INFO, "loader", "Try %s", testPath.__s);
            #endif
         }
         module = hxLoadLibrary(testPath);
         if (module)
         {
            if (gLoadDebug)
            {
               #ifndef ANDROID
               printf("Found %s\n", testPath.__s);
               #else
               __android_log_print(ANDROID_LOG_INFO, "loader", "Found %s", testPath.__s);
               #endif
            }
            break;
         }
      }

      #ifdef HXCPP_TRY_HAXELIB
      if (!module)
      {
         if (e==0)
            haxelibPath = FindHaxelib(inLib);

         if (haxelibPath.length!=0)
         {
            String testPath  = haxelibPath + HX_CSTRING("/ndll/") + bin + HX_CSTRING("/") + inLib + extension;
            if (gLoadDebug)
               printf(" try %s...\n", testPath.__s);
            module = hxLoadLibrary(testPath);
            if (module && gLoadDebug)
            {
               printf("Found %s\n", testPath.__s);
            }
         }
      }
      #endif
   }

   if (!module)
   {
      hx::Throw(HX_CSTRING("Could not load module ") + inLib + HX_CSTRING("@") + full_name);
   }


   if (new_module)
   {
      sgLoadedModule[module_name.__s] = module;

      sgOrderedModules.push_back(module);

      SetLoaderProcFunc set_loader = (SetLoaderProcFunc)hxFindSymbol(module,"hx_set_loader");
      if (set_loader)
         set_loader(hx_cffi);

      GetNekoEntryFunc func = (GetNekoEntryFunc)hxFindSymbol(module,"__neko_entry_point");
      if (func)
      {
         NekoEntryFunc entry = (NekoEntryFunc)func();
         if (entry)
            entry();
      }
   }

   FundFunc proc_query = (FundFunc)hxFindSymbol(module,full_name.__CStr());
   if (!proc_query)
       proc_query = (FundFunc)hxFindSymbol(module, (inLib + HX_CSTRING("_") + full_name).__CStr());

   if (!proc_query && !inQuietFail)
   {
      #ifdef ANDROID
       __android_log_print(ANDROID_LOG_ERROR, "loader", "Could not find primitive %s in %p",
        full_name.__CStr(), module);
      #else
      fprintf(stderr,"Could not find primitive %s.\n", full_name.__CStr());
      #endif
      return 0;
   }

   if (!inNdllProc)
      return (void *)proc_query;

   void *proc = proc_query();
   if (!proc && !inQuietFail)
   {
      #ifdef ANDROID
      __android_log_print(ANDROID_LOG_ERROR, "loader", "Could not identify primitive %s in %s",
        full_name.__CStr(), inLib.__CStr() );
      #else
      fprintf(stderr,"Could not identify primitive %s in %s\n", full_name.__CStr(),inLib.__CStr());
      #endif
   }

   return proc;
}
Example #2
0
void *__hxcpp_get_proc_address(String inLib, String full_name,bool inNdllProc)
{
#ifdef ANDROID
   inLib = HX_CSTRING("lib") + inLib;

   //__android_log_print(ANDROID_LOG_INFO, "loader", "%s: %s", inLib.__CStr(), inPrim.__CStr() );
#endif

   #ifdef IPHONE
   gLoadDebug = true;
   setenv("DYLD_PRINT_APIS","1",true);

   #elif !defined(HX_WINRT)
   gLoadDebug = gLoadDebug || getenv("HXCPP_LOAD_DEBUG");
   #endif

   String ext =
#if defined(_WIN32)
    HX_CSTRING(".dll");
#elif defined(IPHONEOS)
    HX_CSTRING(".ios.dylib");
#elif defined(IPHONESIM)
    HX_CSTRING(".sim.dylib");
#elif defined(__APPLE__)
    HX_CSTRING(".dylib");
#elif defined(ANDROID) || defined(GPH) || defined(WEBOS)  || defined(BLACKBERRY) || defined(EMSCRIPTEN) || defined(TIZEN)
    HX_CSTRING(".so");
#else
    HX_CSTRING(".dso");
#endif



   String bin =
#ifdef _WIN32
    HX_CSTRING("Windows");
// Unix...
#elif defined(__APPLE__)
  #ifdef HXCPP_M64
    HX_CSTRING("Mac64");
  #else
    HX_CSTRING("Mac");
  #endif
#elif defined (ANDROID)
    HX_CSTRING("Android");
#elif defined(WEBOS)
    HX_CSTRING("webOS");
#elif defined(BLACKBERRY)
    HX_CSTRING("BlackBerry");
#elif defined(RASPBERRYPI)
    HX_CSTRING("RPi");
#elif defined(EMSCRIPTEN)
	HX_CSTRING("Emscripten");
#elif defined(TIZEN)
    HX_CSTRING("Tizen");
#elif defined(IPHONESIM)
    HX_CSTRING("IPhoneSim");
#elif defined(IPHONEOS)
    HX_CSTRING("IPhoneOs");
#else
  #ifdef HXCPP_M64
    HX_CSTRING("Linux64");
  #else
    HX_CSTRING("Linux");
  #endif
#endif

    int passes = 4;

   #ifdef ANDROID
   std::string module_name = inLib.__CStr();
   Module module = sgLoadedModule[module_name];
   #else
   Module module = sgLoadedModule[inLib.__s];
   #endif
   bool new_module = module==0;


   if (!module && sgRegisteredPrims)
   {
      void *registered = (*sgRegisteredPrims)[full_name.__CStr()];
      if (registered)
         return registered;
   }

   if (!module && gLoadDebug)
   {
      #ifdef ANDROID
       __android_log_print(ANDROID_LOG_INFO, "loader", "Searching for %s...", module_name.c_str());
      #else
      printf("Searching for %s...\n", inLib.__CStr());
      #endif
   }


   for(int pass=0;module==0 && pass<2;pass++)
   {
      String dll_ext = HX_CSTRING("./") + inLib + ( (pass&1) ? HX_CSTRING(".ndll") : ext );

      // Try Current directory first ...
      if (gLoadDebug)
      {
         #ifndef ANDROID
         printf(" try %s...\n", dll_ext.__CStr());
         #else
         __android_log_print(ANDROID_LOG_INFO, "loader", "Try %s", dll_ext.__CStr());
         #endif
      }
      module = hxLoadLibrary(dll_ext);
      if (module)
         break;
      
      dll_ext = inLib + ( (pass&1) ? HX_CSTRING(".ndll") : ext );
      if (gLoadDebug)
      {
         #ifndef ANDROID
         printf(" try %s...\n", dll_ext.__CStr());
         #else
         __android_log_print(ANDROID_LOG_INFO, "loader", "Try %s", dll_ext.__CStr());
         #endif
      }
      module = hxLoadLibrary(dll_ext);

      // Try exactly as specified...
      if (!module)
      {
         String dll_ext = pass==0 ? inLib : HX_CSTRING("./") + inLib;
         if (gLoadDebug)
         {
            #ifndef ANDROID
            printf(" try %s...\n", dll_ext.__CStr());
            #else
            __android_log_print(ANDROID_LOG_INFO, "loader", "Try %s", dll_ext.__CStr());
            #endif
         }
         module = hxLoadLibrary(dll_ext);
      }
     
      #ifdef HX_MACOS
      if (!module)
      {
         String exe_path = HX_CSTRING("@executable_path/") + inLib + ( (pass&1) ? HX_CSTRING(".ndll") : ext );
         if (gLoadDebug)
         {
            printf(" try %s...\n", exe_path.__CStr());
         }
         module = hxLoadLibrary(exe_path);
      }
      #endif

      #if !defined(ANDROID) && !defined(HX_WINRT) && !defined(IPHONE) && !defined(EMSCRIPTEN)
      if (!module)
      {
         String hxcpp = GetEnv("HXCPP");
         if (hxcpp.length!=0)
         {
             String name = hxcpp + HX_CSTRING("/bin/") + bin + HX_CSTRING("/") + dll_ext;
             if (gLoadDebug)
                printf(" try %s...\n", name.__CStr());
             module = hxLoadLibrary(name);
         }
      }
   
      if (!module)
      {
         String hxcpp = FindHaxelib( HX_CSTRING("hxcpp") );
         if (hxcpp.length!=0)
         {
             String name = hxcpp + HX_CSTRING("/bin/") + bin + HX_CSTRING("/") + dll_ext;
             if (gLoadDebug)
                printf(" try %s...\n", name.__CStr());
             module = hxLoadLibrary(name);
         }
      }

      if (!module)
      {
         String path = FindHaxelib(inLib);
         if (path.length!=0)
         {
            String full_path  = path + HX_CSTRING("/ndll/") + bin + HX_CSTRING("/") + dll_ext;
            if (gLoadDebug)
               printf(" try %s...\n", full_path.__CStr());
            module = hxLoadLibrary(full_path);
         }
      }
      #endif
   }

   if (!module)
   {
     throw Dynamic(HX_CSTRING("Could not load module ") + inLib + HX_CSTRING("@") + full_name);
   }


   if (new_module)
   {
      #ifdef ANDROID
      sgLoadedModule[module_name] = module;
      #else
      sgLoadedModule[inLib.__s] = module;
      #endif

      SetLoaderProcFunc set_loader = (SetLoaderProcFunc)hxFindSymbol(module,"hx_set_loader");
      if (set_loader)
         set_loader(hx_cffi);

      GetNekoEntryFunc func = (GetNekoEntryFunc)hxFindSymbol(module,"__neko_entry_point");
      if (func)
      {
         NekoEntryFunc entry = (NekoEntryFunc)func();
         if (entry)
            entry();
      }
   }

   FundFunc proc_query = (FundFunc)hxFindSymbol(module,full_name.__CStr());
   if (!proc_query)
   {
      #ifdef ANDROID
       __android_log_print(ANDROID_LOG_ERROR, "loader", "Could not find primitive %s in %p",
        full_name.__CStr(), module);
      #else
      fprintf(stderr,"Could not find primitive %s.\n", full_name.__CStr());
      #endif
      return 0;
   }

   if (!inNdllProc)
      return (void *)proc_query;

   void *proc = proc_query();
   if (!proc)
   {
#ifdef ANDROID
      __android_log_print(ANDROID_LOG_ERROR, "loader", "Could not identify primitive %s in %s\n", full_name.__CStr(),inLib.__CStr());
      fprintf(stderr,"Could not identify primitive %s in %s\n", full_name.__CStr(),inLib.__CStr());
#elif defined(ANDROID)
   __android_log_print(ANDROID_LOG_ERROR, "loader", "Could not identify primitive %s in %s",
        inPrim.__CStr(), inLib.__CStr() );
#else
   fprintf(stderr,"Could not identify primitive %s in %s\n", full_name.__CStr(),inLib.__CStr());
#endif
   }

   return proc;
}