static BCCScriptRef loadScript() {
  if (!inFile) {
    fprintf(stderr, "input file required\n");
    return NULL;
  }

  struct stat statInFile;
  if (stat(inFile, &statInFile) < 0) {
    fprintf(stderr, "Unable to stat input file: %s\n", strerror(errno));
    return NULL;
  }

  if (!S_ISREG(statInFile.st_mode)) {
    fprintf(stderr, "Input file should be a regular file.\n");
    return NULL;
  }

  FILE *in = fopen(inFile, "r");
  if (!in) {
    fprintf(stderr, "Could not open input file %s\n", inFile);
    return NULL;
  }

  size_t bitcodeSize = statInFile.st_size;

  std::vector<char> bitcode(bitcodeSize + 1, '\0');
  size_t nread = fread(&*bitcode.begin(), 1, bitcodeSize, in);

  if (nread != bitcodeSize)
      fprintf(stderr, "Could not read all of file %s\n", inFile);

  BCCScriptRef script = bccCreateScript();

  if (bccReadBC(script, "file", &*bitcode.begin(), bitcodeSize, 0) != 0) {
    fprintf(stderr, "bcc: FAILS to read bitcode");
    bccDisposeScript(script);
    return NULL;
  }

  bccRegisterSymbolCallback(script, lookupSymbol, NULL);

  if (bccPrepareExecutable(script, ".", "cache", 0) != 0) {
    fprintf(stderr, "bcc: FAILS to prepare executable.\n");
    bccDisposeScript(script);
    return NULL;
  }

  return script;
}
Exemplo n.º 2
0
extern "C" JNIEXPORT jboolean JNICALL Java_com_android_photoeditor_filters_ImageUtils_init(
    JNIEnv *env, jobject obj, jbyteArray scriptRef, jint length)
{
#if !defined(__GDK__) && !defined(__NOGDK__)
    void *new_func_ptr[JNI_max];
    int i, all_func_found = 1;

    BCCScriptRef script_ref = bccCreateScript();
    jbyte* script_ptr = (jbyte *)env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
    LOGI("BCC Script Len: %d", length);
    if (bccReadBC(script_ref, "libjni_photoeditor_portable.bc", (const char*)script_ptr, length, 0)) {
        LOGE("Error! Cannot bccReadBc");
        return JNI_FALSE;
    }
    if (script_ptr) {
        env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr, 0);
    }
#if 0
    if (bccLinkFile(script_ref, "/system/lib/libclcore.bc", 0)) {
        LOGE("Error! Cannot bccLinkBC");
        return JNI_FALSE;
    }
#endif
    bccRegisterSymbolCallback(script_ref, lookupSymbol, NULL);

#ifdef OLD_BCC
    if (bccPrepareExecutable(script_ref, "/data/data/com.android.photoeditor/photoeditorLLVM.oBCC", 0)) {
        LOGE("Error! Cannot bccPrepareExecutable");
        return JNI_FALSE;
    }
#else
    if (bccPrepareExecutable(script_ref, "/data/data/com.android.photoeditor/", "photoeditorLLVM", 0)) {
        LOGE("Error! Cannot bccPrepareExecutable");
        return JNI_FALSE;
    }
#endif // OLD_BCC

    for(i=0; i<JNI_max; i++) {
        new_func_ptr[i] = bccGetFuncAddr(script_ref, JNIFunc[i].func_name);
        if (new_func_ptr[i] == NULL) {
            LOGE("Error! Cannot find %s()\n", JNIFunc[i].func_name);
            all_func_found = 0;
            //return JNI_FALSE;
        } else
            LOGI("Found %s() @ 0x%x", JNIFunc[i].func_name, (unsigned)new_func_ptr[i]);
    }

    //bccDisposeScript(script_ref);

    if (all_func_found)
    {
        LOGI("Use LLVM version");
        for(i=0; i<JNI_max; i++)
            JNIFunc[i].func_ptr = new_func_ptr[i];
    }

    return JNI_TRUE;
#else

    return JNI_FALSE;

#endif // !__GDK__ && !__NOGDK__
}