+static void crossgen(const char* dllPath, const char* appPath)
+{
+ //pid_t parent = getpid();
+ pid_t pid = fork();
+ if (pid == -1)
+ return;
+
+ if (pid > 0) {
+ int status;
+ waitpid(pid, &status, 0);
+ if (WIFEXITED(status))
+ return;
+ } else {
+ // search dlls in the application directory first, to use application dlls
+ // instead of system dlls when proceeding NI
+ std::vector<std::string> tpaDir;
+ if (appPath != NULL) {
+ std::string path(appPath);
+ std::string::size_type prevPos = 0, pos = 0;
+ while ((pos = path.find(':', pos)) != std::string::npos) {
+ std::string substring(path.substr(prevPos, pos - prevPos));
+ tpaDir.push_back(substring);
+ prevPos = ++pos;
+ }
+ std::string substring(path.substr(prevPos, pos - prevPos));
+ tpaDir.push_back(substring);
+ }
+ tpaDir.push_back(__RUNTIME_DIR);
+ tpaDir.push_back(__DEVICE_API_DIR);
+
+ // get reference API directory ([DEVICE_API_DIR]/ref)
+ int len = strlen(__DEVICE_API_DIR);
+ char* refAPIDir = (char*)calloc(len + 5, 1);
+ if (!refAPIDir) {
+ printf("fail to allocate memory for reference API directory\n");
+ return;
+ }
+ snprintf(refAPIDir, len + 5, "%s%s", __DEVICE_API_DIR, "/ref");
+ tpaDir.push_back(refAPIDir);
+
+ std::string tpa;
+ assembliesInDirectory(tpaDir, tpa);
+
+ std::vector<const char*> argv = {
+ __CROSSGEN_PATH,
+ "/Trusted_Platform_Assemblies", tpa.c_str(),
+ "/JITPath", __JIT_PATH,
+ "/FragileNonVersionable"
+ };
+ if (appPath != nullptr) {
+ argv.push_back("/App_Paths");
+ argv.push_back(appPath);
+ }
+ argv.push_back(dllPath);
+ argv.push_back(nullptr);
+
+ printf("+ %s\n", dllPath);
+
+ execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
+ exit(0);
+ }
+}