11 #include <sys/param.h>
15 #include <CoreFoundation/CoreFoundation.h>
48 map<string, VuoFileUtilities::File *> VuoCompiler::Environment::moduleCacheFileForLocking;
49 dispatch_queue_t VuoCompiler::Environment::moduleCacheBuildingQueue = dispatch_queue_create(
"org.vuo.compiler.cache", NULL);
50 const string VuoCompiler::Environment::pidCacheDirPrefix =
"pid-";
51 dispatch_queue_t VuoCompiler::environmentQueue = dispatch_queue_create(
"org.vuo.compiler.environment", NULL);
52 vector< vector<VuoCompiler::Environment *> > VuoCompiler::sharedEnvironments;
53 map<string, vector<VuoCompiler::Environment *> > VuoCompiler::environmentsForCompositionFamily;
54 dispatch_group_t VuoCompiler::moduleSourceCompilersExist = dispatch_group_create();
55 string VuoCompiler::vuoFrameworkInProgressPath;
65 struct rlimit rl{OPEN_MAX, OPEN_MAX};
66 getrlimit(RLIMIT_NOFILE, &rl);
67 rl.rlim_cur =
MIN(OPEN_MAX, rl.rlim_max);
68 if (setrlimit(RLIMIT_NOFILE, &rl))
69 VUserLog(
"Warning: Couldn't set open-files limit: %s", strerror(errno));
72 llvmQueue = dispatch_queue_create(
"org.vuo.compiler.llvm", NULL);
75 llvm::InitializeNativeTarget();
88 Module module(
"", getGlobalContext());
107 VuoCompiler::ModuleInfo::ModuleInfo(Environment *environment,
const string &searchPath,
const string &relativePath,
108 bool isSourceFile,
bool isSubcomposition)
111 initialize(environment, searchPath, file, isSourceFile, isSubcomposition);
117 VuoCompiler::ModuleInfo::ModuleInfo(Environment *environment,
const string &searchPath,
VuoFileUtilities::File *file,
118 bool isSourceFile,
bool isSubcomposition)
120 initialize(environment, searchPath, file, isSourceFile, isSubcomposition);
126 void VuoCompiler::ModuleInfo::initialize(Environment *environment,
const string &searchPath,
VuoFileUtilities::File *file,
127 bool isSourceFile,
bool isSubcomposition)
129 this->environment = environment;
130 this->searchPath = searchPath;
133 sourceCodeOverridden =
false;
144 loading = dispatch_group_create();
147 if (isSubcomposition)
161 VuoCompiler::ModuleInfo::~ModuleInfo(
void)
166 dispatch_release(loading);
169 VuoCompiler::Environment * VuoCompiler::ModuleInfo::getEnvironment(
void)
const
174 string VuoCompiler::ModuleInfo::getSearchPath(
void)
const
179 string VuoCompiler::ModuleInfo::getModuleKey(
void)
const
189 string VuoCompiler::ModuleInfo::getSourceCode(
void)
const
194 void VuoCompiler::ModuleInfo::setSourceCode(
const string &sourceCode)
196 this->sourceCode = sourceCode;
199 void VuoCompiler::ModuleInfo::revertSourceCode(
void)
204 bool VuoCompiler::ModuleInfo::isSourceCodeOverridden(
void)
const
206 return sourceCodeOverridden;
209 void VuoCompiler::ModuleInfo::setSourceCodeOverridden(
bool overridden)
211 sourceCodeOverridden = overridden;
214 bool VuoCompiler::ModuleInfo::isNewerThan(ModuleInfo *other)
const
216 return lastModified > other->lastModified;
219 bool VuoCompiler::ModuleInfo::isNewerThan(
unsigned long ageInSeconds)
const
221 return lastModified > ageInSeconds;
224 bool VuoCompiler::ModuleInfo::isOlderThan(
unsigned long ageInSeconds)
const
226 return lastModified < ageInSeconds;
229 void VuoCompiler::ModuleInfo::setLastModifiedToNow(
void)
232 gettimeofday(&t, NULL);
233 lastModified = t.tv_sec;
236 set<string> VuoCompiler::ModuleInfo::getContainedNodeClasses(
void)
const
238 return containedNodeClasses;
241 bool VuoCompiler::ModuleInfo::getAttempted(
void)
const
246 void VuoCompiler::ModuleInfo::setAttempted(
bool attempted)
248 this->attempted = attempted;
251 dispatch_group_t VuoCompiler::ModuleInfo::getLoadingGroup(
void)
const
256 void VuoCompiler::ModuleInfo::dump()
const
258 fprintf(stderr,
"module %s:\n"
259 "\tloadingGroup=%p\n"
264 "\tsourceCodeOverridden=%d\n"
266 "\tcontainedNodeClasses:\n",
271 environment->getCompiledModuleCachePath().c_str(),
275 sourceCodeOverridden,
277 for (
auto i: containedNodeClasses)
278 fprintf(stderr,
"\t\t%s\n", i.c_str());
281 VuoCompiler::DependencyGraphVertex * VuoCompiler::DependencyGraphVertex::vertexForDependency(
const string &dependency,
VuoDirectedAcyclicGraph *graph)
285 return dynamic_cast<DependencyGraphVertex *>(v);
287 DependencyGraphVertex *vv =
new DependencyGraphVertex();
288 vv->dependency = dependency;
289 vv->environment = NULL;
290 vv->compatible =
true;
294 string VuoCompiler::DependencyGraphVertex::getDependency(
void)
299 VuoCompiler::Environment * VuoCompiler::DependencyGraphVertex::getEnvironment(
void)
304 void VuoCompiler::DependencyGraphVertex::setEnvironment(Environment *environment)
306 this->environment = environment;
309 bool VuoCompiler::DependencyGraphVertex::isCompatible(
void)
314 void VuoCompiler::DependencyGraphVertex::setCompatible(
bool compatible)
316 this->compatible = compatible;
319 string VuoCompiler::DependencyGraphVertex::key(
void)
329 VuoCompiler::ModuleInfoIterator::ModuleInfoIterator(map<
string, map<string, ModuleInfo *> > *allModuleInfos)
331 this->allModuleInfos = allModuleInfos;
332 hasSearchModuleKeys =
false;
341 VuoCompiler::ModuleInfoIterator::ModuleInfoIterator(map<
string, map<string, ModuleInfo *> > *allModuleInfos,
const set<string> &searchModuleKeys)
343 this->allModuleInfos = allModuleInfos;
344 this->searchModuleKeys = searchModuleKeys;
345 hasSearchModuleKeys =
true;
354 void VuoCompiler::ModuleInfoIterator::initialize(
void)
356 currSearchPath = allModuleInfos->begin();
357 hasCurrModuleKey =
false;
365 VuoCompiler::ModuleInfo * VuoCompiler::ModuleInfoIterator::next(
void)
367 for ( ; currSearchPath != allModuleInfos->end(); ++currSearchPath)
369 if (! hasCurrModuleKey)
371 currModuleKey = currSearchPath->second.begin();
372 hasCurrModuleKey =
true;
375 for ( ; currModuleKey != currSearchPath->second.end(); ++currModuleKey)
377 if (! hasSearchModuleKeys || searchModuleKeys.find(currModuleKey->first) != searchModuleKeys.end())
379 ModuleInfo *moduleInfo = currModuleKey->second;
385 hasCurrModuleKey =
false;
394 VuoCompiler::Environment::Environment(
void)
396 compilersToNotifyQueue = dispatch_queue_create(
"org.vuo.compiler.notify", 0);
397 moduleSearchPathContentsChangedQueue = dispatch_queue_create(
"org.vuo.compiler.watch", 0);
398 isModuleCacheableDataDirty =
false;
399 isModuleCacheInitialized =
false;
400 isModuleCacheAvailable =
false;
409 VuoCompiler::Environment::~Environment(
void)
411 stopWatchingModuleSearchPaths();
412 dispatch_sync(moduleSearchPathContentsChangedQueue, ^{});
414 dispatch_release(moduleSearchPathContentsChangedQueue);
415 dispatch_release(compilersToNotifyQueue);
417 for (map<string, VuoCompilerNodeClass *>::iterator i = nodeClasses.begin(); i != nodeClasses.end(); ++i)
418 destroyModule(i->second);
420 for (map<string, VuoCompilerType *>::iterator i = types.begin(); i != types.end(); ++i)
421 destroyModule(i->second);
423 for (map<string, VuoCompilerModule *>::iterator i = libraryModules.begin(); i != libraryModules.end(); ++i)
424 destroyModule(i->second);
426 for (set<VuoCompilerGenericType *>::iterator i = genericTypes.begin(); i != genericTypes.end(); ++i)
428 VuoType *base = (*i)->getBase();
433 for (map<string, VuoNodeSet *>::iterator i = nodeSetForName.begin(); i != nodeSetForName.end(); ++i)
436 for (map<
string, map<string, ModuleInfo *> >::iterator i = moduleFilesAtSearchPath.begin(); i != moduleFilesAtSearchPath.end(); ++i)
437 for (map<string, ModuleInfo *>::iterator j = i->second.begin(); j != i->second.end(); ++j)
440 for (map<
string, map<string, ModuleInfo *> >::iterator i = sourceFilesAtSearchPath.begin(); i != sourceFilesAtSearchPath.end(); ++i)
441 for (map<string, ModuleInfo *>::iterator j = i->second.begin(); j != i->second.end(); ++j)
444 delete dependencyGraph;
445 delete compositionDependencyGraph;
451 void VuoCompiler::Environment::addCompilerToNotify(
VuoCompiler *compiler)
453 dispatch_sync(compilersToNotifyQueue, ^{
454 compilersToNotify.insert(compiler);
461 void VuoCompiler::Environment::removeCompilerToNotify(
VuoCompiler *compiler)
463 dispatch_sync(compilersToNotifyQueue, ^{
464 compilersToNotify.erase(compiler);
473 map<string, VuoCompilerNodeClass *> VuoCompiler::Environment::getNodeClasses(
void)
485 map<string, VuoCompilerNodeClass *>::iterator nodeClassIter = nodeClasses.find(moduleKey);
486 if (nodeClassIter != nodeClasses.end())
487 return nodeClassIter->second;
500 map<string, VuoCompilerNodeClass *>::iterator nodeClassIter = nodeClasses.find(moduleKey);
501 if (nodeClassIter != nodeClasses.end())
503 nodeClass = nodeClassIter->second;
505 nodeClasses.erase(nodeClassIter);
506 removeFromDependencyGraph(nodeClass);
518 map<string, VuoCompilerType *> VuoCompiler::Environment::getTypes(
void)
528 VuoCompilerType * VuoCompiler::Environment::getType(
const string &moduleKey)
530 map<string, VuoCompilerType *>::iterator typeIter = types.find(moduleKey);
531 if (typeIter != types.end())
532 return typeIter->second;
542 map<string, VuoNodeSet *> VuoCompiler::Environment::getNodeSets(
void)
544 return nodeSetForName;
552 map<string, VuoCompilerModule *> VuoCompiler::Environment::getLibraryModules(
void)
554 return libraryModules;
563 VuoCompilerModule * VuoCompiler::Environment::findModule(
const string &moduleKey)
565 map<string, VuoCompilerNodeClass *>::iterator nodeClassIter = nodeClasses.find(moduleKey);
566 if (nodeClassIter != nodeClasses.end())
567 return nodeClassIter->second;
569 map<string, VuoCompilerType *>::iterator typeIter = types.find(moduleKey);
570 if (typeIter != types.end())
571 return typeIter->second;
573 map<string, VuoCompilerModule *>::iterator libraryIter = libraryModules.find(moduleKey);
574 if (libraryIter != libraryModules.end())
575 return libraryIter->second;
585 VuoNodeSet * VuoCompiler::Environment::findNodeSet(
const string &name)
588 map<string, VuoNodeSet *>::iterator nodeSetIter = nodeSetForName.find(name);
589 if (nodeSetIter != nodeSetForName.end())
590 return nodeSetIter->second;
601 void VuoCompiler::Environment::addModuleSearchPath(
const string &path,
bool shouldWatch)
603 moduleSearchPaths.push_back(path);
605 updateModulesAtSearchPath(path);
606 updateSourceFilesAtSearchPath(path);
609 startWatchingModuleSearchPath(path);
617 vector<string> VuoCompiler::Environment::getModuleSearchPaths(
void)
619 return moduleSearchPaths;
627 void VuoCompiler::Environment::addHeaderSearchPath(
const string &path)
629 headerSearchPaths.push_back(path);
637 vector<string> VuoCompiler::Environment::getHeaderSearchPaths(
void)
639 return headerSearchPaths;
647 void VuoCompiler::Environment::addLibrarySearchPath(
const string &path)
649 librarySearchPaths.push_back(path);
657 vector<string> VuoCompiler::Environment::getLibrarySearchPaths(
void)
659 return librarySearchPaths;
667 void VuoCompiler::Environment::addFrameworkSearchPath(
const string &path)
669 frameworkSearchPaths.push_back(path);
677 vector<string> VuoCompiler::Environment::getFrameworkSearchPaths(
void)
679 return frameworkSearchPaths;
684 return dependencyGraph;
689 return compositionDependencyGraph;
697 void VuoCompiler::Environment::setModuleCachePath(
const string &path)
699 moduleCachePath = path;
707 string VuoCompiler::Environment::getCompiledModuleCachePath(
void)
709 return (moduleCachePath.empty() ?
"" : moduleCachePath +
"/Modules");
717 string VuoCompiler::Environment::getOverriddenCompiledModuleCachePath(
void)
719 if (moduleCachePath.empty())
725 string dir, moduleCacheDirName, ext;
738 void VuoCompiler::Environment::addExpatriateSourceFile(
const string &sourcePath)
740 expatriateSourceFiles.push_back(sourcePath);
742 if (find(moduleSearchPaths.begin(), moduleSearchPaths.end(),
"") == moduleSearchPaths.end())
743 moduleSearchPaths.push_back(
"");
745 auto iter = sourceFilesAtSearchPath.find(
"");
746 if (iter != sourceFilesAtSearchPath.end())
747 sourceFilesAtSearchPath.erase(iter);
755 void VuoCompiler::Environment::removeExpatriateSourceFile(
const string &sourcePath)
757 for (
auto i = expatriateSourceFiles.begin(); i != expatriateSourceFiles.end(); ++i)
761 expatriateSourceFiles.erase(i);
763 auto iter = sourceFilesAtSearchPath.find(
"");
764 if (iter != sourceFilesAtSearchPath.end())
765 sourceFilesAtSearchPath.erase(iter);
784 void VuoCompiler::Environment::updateModulesAtSearchPath(
const string &path,
bool shouldCleanModuleCache)
786 if (moduleFilesAtSearchPath.find(path) != moduleFilesAtSearchPath.end())
789 set<string> moduleExtensions;
790 moduleExtensions.insert(
"vuonode");
791 moduleExtensions.insert(
"vuonode+");
792 moduleExtensions.insert(
"bc");
793 moduleExtensions.insert(
"bc+");
794 set<string> archiveExtensions;
795 archiveExtensions.insert(
"vuonode");
798 map<string, ModuleInfo *> fileForModuleKey;
799 for (set<VuoFileUtilities::File *>::iterator i = moduleFiles.begin(); i != moduleFiles.end(); ++i)
803 ModuleInfo *m =
new ModuleInfo(
this, path, moduleFile,
false,
false);
804 fileForModuleKey[m->getModuleKey()] = m;
807 if (shouldCleanModuleCache && path == getCompiledModuleCachePath())
809 for (map<string, ModuleInfo *>::iterator i = fileForModuleKey.begin(); i != fileForModuleKey.end(); )
811 ModuleInfo *sourceInfo = listSourceFile(i->first);
812 if (! sourceInfo || sourceInfo->isNewerThan(i->second))
814 ModuleInfo *m = i->second;
817 fileForModuleKey.erase(i++);
824 moduleFilesAtSearchPath[path] = fileForModuleKey;
834 void VuoCompiler::Environment::updateSourceFilesAtSearchPath(
const string &path)
836 map<string, map<string, ModuleInfo *> >::iterator sourceFilesIter = sourceFilesAtSearchPath.find(path);
837 if (sourceFilesIter != sourceFilesAtSearchPath.end())
840 set<VuoFileUtilities::File *> sourceFiles;
843 set<string> sourceExtensions;
844 sourceExtensions.insert(
"vuo");
845 sourceExtensions.insert(
"fs");
847 sourceExtensions.insert(cext.begin(), cext.end());
852 for (
const string &sourcePath : expatriateSourceFiles)
854 string dir, file, ext;
857 sourceFiles.insert(sourceFile);
861 map<string, ModuleInfo *> fileForModuleKey;
862 for (set<VuoFileUtilities::File *>::iterator i = sourceFiles.begin(); i != sourceFiles.end(); ++i)
866 string dir, moduleKey, ext;
868 bool isSubcomposition = (ext ==
"vuo");
871 if (path.empty() && !sourceFile->
exists())
874 ModuleInfo *m =
new ModuleInfo(
this, path, sourceFile,
true, isSubcomposition);
875 if (fileForModuleKey.find(m->getModuleKey()) != fileForModuleKey.end())
876 VUserLog(
"Warning: Conflicting source files for module %s are installed at %s", m->getModuleKey().c_str(), path.c_str());
877 fileForModuleKey[m->getModuleKey()] = m;
879 if (isSubcomposition)
881 DependencyGraphVertex *compositionVertex = DependencyGraphVertex::vertexForDependency(moduleKey, compositionDependencyGraph);
882 compositionDependencyGraph->addVertex(compositionVertex);
884 compositionVertex->setEnvironment(
this);
886 set<string> dependencies = m->getContainedNodeClasses();
887 for (set<string>::iterator j = dependencies.begin(); j != dependencies.end(); ++j)
889 DependencyGraphVertex *dependencyVertex = DependencyGraphVertex::vertexForDependency(*j, compositionDependencyGraph);
890 compositionDependencyGraph->addEdge(compositionVertex, dependencyVertex);
895 sourceFilesAtSearchPath[path] = fileForModuleKey;
903 VuoCompiler::ModuleInfo * VuoCompiler::Environment::listModule(
const string &moduleKey)
905 for (
const auto &moduleFiles : moduleFilesAtSearchPath)
907 map<string, ModuleInfo *>::const_iterator foundIter = moduleFiles.second.find(moduleKey);
908 if (foundIter != moduleFiles.second.end())
909 return foundIter->second;
920 VuoCompiler::ModuleInfoIterator VuoCompiler::Environment::listModules(
const set<string> &moduleKeys)
922 for (
const string &path : moduleSearchPaths)
923 updateModulesAtSearchPath(path);
925 return ModuleInfoIterator(&moduleFilesAtSearchPath, moduleKeys);
933 VuoCompiler::ModuleInfoIterator VuoCompiler::Environment::listAllModules(
void)
935 for (
const string &path : moduleSearchPaths)
936 updateModulesAtSearchPath(path);
938 return ModuleInfoIterator(&moduleFilesAtSearchPath);
946 VuoCompiler::ModuleInfo * VuoCompiler::Environment::listSourceFile(
const string &moduleKey)
948 for (
const auto &sourceFiles : sourceFilesAtSearchPath)
950 map<string, ModuleInfo *>::const_iterator foundIter = sourceFiles.second.find(moduleKey);
951 if (foundIter != sourceFiles.second.end())
952 return foundIter->second;
963 VuoCompiler::ModuleInfoIterator VuoCompiler::Environment::listSourceFiles(
const set<string> &moduleKeys)
965 for (
const string &path : moduleSearchPaths)
966 updateSourceFilesAtSearchPath(path);
968 return ModuleInfoIterator(&sourceFilesAtSearchPath, moduleKeys);
976 VuoCompiler::ModuleInfoIterator VuoCompiler::Environment::listAllSourceFiles(
void)
978 for (
const string &path : moduleSearchPaths)
979 updateSourceFilesAtSearchPath(path);
981 return ModuleInfoIterator(&sourceFilesAtSearchPath);
987 vector<string> VuoCompiler::Environment::getBuiltInModuleSearchPaths(
void)
989 vector<string> builtInModuleSearchPaths;
991 string vuoFrameworkPath = getVuoFrameworkPath();
992 if (! vuoFrameworkPath.empty())
994 builtInModuleSearchPaths.push_back(vuoFrameworkPath +
"/Modules");
998 builtInModuleSearchPaths.push_back(VUO_BUILD_DIR
"/library");
999 builtInModuleSearchPaths.push_back(VUO_BUILD_DIR
"/node");
1000 builtInModuleSearchPaths.push_back(VUO_BUILD_DIR
"/type");
1001 builtInModuleSearchPaths.push_back(VUO_BUILD_DIR
"/type/list");
1004 return builtInModuleSearchPaths;
1010 vector<string> VuoCompiler::Environment::getBuiltInHeaderSearchPaths(
void)
1012 vector<string> builtInHeaderSearchPaths;
1014 string vuoFrameworkPath = getVuoFrameworkPath();
1015 if (! vuoFrameworkPath.empty())
1017 builtInHeaderSearchPaths.push_back(vuoFrameworkPath +
"/Headers");
1018 builtInHeaderSearchPaths.push_back(vuoFrameworkPath +
"/Headers/macos");
1019 builtInHeaderSearchPaths.push_back(vuoFrameworkPath +
"/Frameworks/llvm.framework/Versions/A/Headers/lib/c++/v1");
1023 builtInHeaderSearchPaths.push_back(VUO_SOURCE_DIR
"/library");
1024 builtInHeaderSearchPaths.push_back(VUO_SOURCE_DIR
"/node");
1025 builtInHeaderSearchPaths.push_back(VUO_SOURCE_DIR
"/type");
1026 builtInHeaderSearchPaths.push_back(VUO_SOURCE_DIR
"/type/list");
1027 builtInHeaderSearchPaths.push_back(VUO_SOURCE_DIR
"/runtime");
1028 builtInHeaderSearchPaths.push_back(JSONC_ROOT
"/include");
1029 builtInHeaderSearchPaths.push_back(LLVM_ROOT
"/include/c++/v1");
1032 return builtInHeaderSearchPaths;
1038 vector<string> VuoCompiler::Environment::getBuiltInLibrarySearchPaths(
void)
1040 vector<string> builtInLibrarySearchPaths;
1042 string vuoFrameworkPath = getVuoFrameworkPath();
1043 if (! vuoFrameworkPath.empty())
1045 builtInLibrarySearchPaths.push_back(vuoFrameworkPath +
"/Modules");
1048 builtInLibrarySearchPaths.push_back(OPENSSL_ROOT
"/lib");
1052 builtInLibrarySearchPaths.push_back(VUO_BUILD_DIR
"/library");
1053 builtInLibrarySearchPaths.push_back(VUO_BUILD_DIR
"/node");
1054 builtInLibrarySearchPaths.push_back(VUO_BUILD_DIR
"/type");
1055 builtInLibrarySearchPaths.push_back(VUO_BUILD_DIR
"/type/list");
1056 builtInLibrarySearchPaths.push_back(VUO_BUILD_DIR
"/runtime");
1058 builtInLibrarySearchPaths.push_back(LEAP_ROOT);
1061 builtInLibrarySearchPaths.insert(builtInLibrarySearchPaths.end(), conanLibDirs.begin(), conanLibDirs.end());
1064 return builtInLibrarySearchPaths;
1070 vector<string> VuoCompiler::Environment::getBuiltInFrameworkSearchPaths(
void)
1072 vector<string> builtInFrameworkSearchPaths;
1074 string vuoFrameworkPath = getVuoFrameworkPath();
1075 if (! vuoFrameworkPath.empty())
1077 builtInFrameworkSearchPaths.push_back(vuoFrameworkPath +
"/Modules/");
1078 builtInFrameworkSearchPaths.push_back(vuoFrameworkPath +
"/Frameworks/");
1082 builtInFrameworkSearchPaths.push_back(SYPHON_ROOT);
1085 return builtInFrameworkSearchPaths;
1094 void VuoCompiler::Environment::startWatchingModuleSearchPath(
const string &moduleSearchPath)
1097 moduleSearchPathWatchers.insert(watcher);
1105 void VuoCompiler::Environment::stopWatchingModuleSearchPaths(
void)
1107 for (set<VuoFileWatcher *>::iterator i = moduleSearchPathWatchers.begin(); i != moduleSearchPathWatchers.end(); ++i)
1110 moduleSearchPathWatchers.clear();
1117 void VuoCompiler::Environment::fileChanged(
const string &moduleSearchPath)
1119 dispatch_sync(moduleSearchPathContentsChangedQueue, ^{
1120 moduleSearchPathContentsChanged(moduleSearchPath);
1131 void VuoCompiler::Environment::moduleSearchPathContentsChanged(
const string &moduleSearchPath,
const string &moduleAddedOrModifiedPath,
1132 const string &moduleAddedOrModifiedSourceCode,
1133 std::function<
void(
void)> moduleLoadedCallback,
1139 __block set<string> modulesAdded;
1140 __block set<string> modulesModifed;
1141 __block set<string> modulesRemoved;
1142 __block set<string> compositionsAdded;
1143 __block set<string> compositionsModifed;
1144 __block set<string> compositionsRemoved;
1146 dispatch_sync(environmentQueue, ^{
1150 map<string, ModuleInfo *> oldModules;
1151 map<string, ModuleInfo *> oldCompositions;
1153 map<string, map<string, ModuleInfo *> >::iterator mf = moduleFilesAtSearchPath.find(moduleSearchPath);
1154 if (mf != moduleFilesAtSearchPath.end())
1156 for (map<string, ModuleInfo *>::iterator j = mf->second.begin(); j != mf->second.end(); ++j)
1158 oldModules[j->first] = j->second;
1161 moduleFilesAtSearchPath.erase(mf);
1164 map<string, map<string, ModuleInfo *> >::iterator cf = sourceFilesAtSearchPath.find(moduleSearchPath);
1165 if (cf != sourceFilesAtSearchPath.end())
1167 for (map<string, ModuleInfo *>::iterator j = cf->second.begin(); j != cf->second.end(); ++j)
1169 oldCompositions[j->first] = j->second;
1172 compositionDependencyGraph->removeVertex(vertex);
1175 sourceFilesAtSearchPath.erase(cf);
1180 updateModulesAtSearchPath(moduleSearchPath, moduleAddedOrModifiedPath.empty());
1181 updateSourceFilesAtSearchPath(moduleSearchPath);
1183 mf = moduleFilesAtSearchPath.find(moduleSearchPath);
1184 if (mf != moduleFilesAtSearchPath.end())
1186 for (map<string, ModuleInfo *>::iterator n = mf->second.begin(); n != mf->second.end(); ++n)
1188 string moduleKey = n->first;
1190 map<string, ModuleInfo *>::iterator o = oldModules.find(moduleKey);
1191 if (o != oldModules.end())
1193 if (n->second->isNewerThan(o->second) ||
1194 (n->second->getFile() && (n->second->getFile()->isInArchive() ||
VuoFileUtilities::arePathsEqual(n->second->getFile()->path(), moduleAddedOrModifiedPath))))
1196 modulesModifed.insert(moduleKey);
1200 n->second->setAttempted(o->second->getAttempted());
1204 oldModules.erase(o);
1208 modulesModifed.insert(moduleKey);
1212 modulesAdded.insert(moduleKey);
1217 cf = sourceFilesAtSearchPath.find(moduleSearchPath);
1218 if (cf != sourceFilesAtSearchPath.end())
1220 for (map<string, ModuleInfo *>::iterator n = cf->second.begin(); n != cf->second.end(); ++n)
1222 string moduleKey = n->first;
1224 map<string, ModuleInfo *>::iterator o = oldCompositions.find(moduleKey);
1225 if (o != oldCompositions.end())
1227 if (n->second->isNewerThan(o->second))
1229 compositionsModifed.insert(moduleKey);
1233 n->second->setAttempted(o->second->getAttempted());
1234 n->second->setSourceCode(o->second->getSourceCode());
1238 oldCompositions.erase(o);
1242 compositionsAdded.insert(moduleKey);
1247 for (map<string, ModuleInfo *>::iterator o = oldModules.begin(); o != oldModules.end(); ++o)
1250 modulesRemoved.insert(o->first);
1253 for (map<string, ModuleInfo *>::iterator o = oldCompositions.begin(); o != oldCompositions.end(); ++o)
1256 compositionsRemoved.insert(o->first);
1259 compilerForLoading->loadModulesAndSources(modulesAdded, modulesModifed, modulesRemoved,
1260 compositionsAdded, compositionsModifed, compositionsRemoved,
1261 false,
false,
this, issues, moduleLoadedCallback, moduleAddedOrModifiedSourceCode);
1265 delete compilerForLoading;
1271 void VuoCompiler::Environment::notifyCompilers(
const set<VuoCompilerModule *> &modulesAdded,
1272 const set< pair<VuoCompilerModule *, VuoCompilerModule *> > &modulesModified,
1274 bool oldModulesInvalidated)
1276 if (! (modulesAdded.empty() && modulesModified.empty() && modulesRemoved.empty() && issues->
isEmpty()) )
1278 set< pair<VuoCompilerModule *, VuoCompilerModule *> > invalidatedModulesModified;
1279 set<VuoCompilerModule *> invalidatedModulesRemoved;
1280 if (oldModulesInvalidated)
1282 invalidatedModulesModified = modulesModified;
1283 invalidatedModulesRemoved = modulesRemoved;
1286 VuoCompilerDelegate::LoadedModulesData *delegateData =
new VuoCompilerDelegate::LoadedModulesData(invalidatedModulesModified, invalidatedModulesRemoved, issues);
1288 map<string, VuoCompilerModule *> modulesAddedMap;
1289 map<string, pair<VuoCompilerModule *, VuoCompilerModule *> > modulesModifiedMap;
1290 map<string, VuoCompilerModule *> modulesRemovedMap;
1292 modulesAddedMap[m->getPseudoBase()->getModuleKey()] = m;
1293 for (pair<VuoCompilerModule *, VuoCompilerModule *> m : modulesModified)
1294 modulesModifiedMap[m.first->getPseudoBase()->getModuleKey()] = m;
1296 modulesRemovedMap[m->getPseudoBase()->getModuleKey()] = m;
1298 dispatch_sync(compilersToNotifyQueue, ^{
1299 for (set<VuoCompiler *>::iterator i = compilersToNotify.begin(); i != compilersToNotify.end(); ++i) {
1300 delegateData->retain();
1302 for (set<VuoCompiler *>::iterator i = compilersToNotify.begin(); i != compilersToNotify.end(); ++i) {
1303 (*i)->loadedModules(modulesAddedMap, modulesModifiedMap, modulesRemovedMap, issues, delegateData,
this);
1323 set<VuoCompilerModule *> VuoCompiler::Environment::loadCompiledModules(
const set<string> &moduleKeys,
const map<string, string> &sourceCodeForModule)
1325 ModuleInfoIterator modulesToLoadIter = listModules(moduleKeys);
1326 set<VuoCompilerModule *> modulesLoaded;
1328 ModuleInfo *moduleInfo;
1329 while ((moduleInfo = modulesToLoadIter.next()))
1331 string moduleKey = moduleInfo->getModuleKey();
1335 if (moduleInfo->getEnvironment() !=
this || findModule(moduleKey) || moduleInfo->getAttempted())
1338 moduleInfo->setAttempted(
true);
1345 modulesLoaded.insert(module);
1346 addToDependencyGraph(module);
1350 string searchPath = moduleInfo->getSearchPath();
1357 ModuleInfo *sourceInfo = listSourceFile(moduleKey);
1361 auto sourceCodeIter = sourceCodeForModule.find(moduleKey);
1362 if (sourceCodeIter != sourceCodeForModule.end())
1369 return modulesLoaded;
1383 set<dispatch_group_t> VuoCompiler::Environment::loadSpecializedModules(
const set<string> &moduleKeys,
1386 set<dispatch_group_t > loadingGroups;
1388 for (
string moduleKey : moduleKeys)
1397 if (findModule(moduleKey))
1402 map<string, dispatch_group_t>::iterator iter = specializedModulesLoading.find(moduleKey);
1403 if (iter != specializedModulesLoading.end())
1405 loadingGroups.insert(iter->second);
1406 dispatch_retain(iter->second);
1410 dispatch_group_t loadingGroup = dispatch_group_create();
1411 specializedModulesLoading[moduleKey] = loadingGroup;
1412 loadingGroups.insert(loadingGroup);
1418 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
1419 dispatch_group_async(loadingGroup, queue, ^{
1433 compiler->loadNodeClassGeneratedAtRuntime(specNodeClass,
this);
1436 compiler->loadModulesIfNeeded(dependencies);
1439 dispatch_sync(environmentQueue, ^{
1440 specializedModulesLoading.erase(moduleKey);
1445 return loadingGroups;
1460 set<dispatch_group_t> VuoCompiler::Environment::compileModulesFromSourceCode(
const set<string> &moduleKeys,
bool shouldRecompileIfUnchanged)
1462 ModuleInfoIterator modulesToLoadIter = listSourceFiles(moduleKeys);
1464 set<dispatch_group_t> sourcesLoading;
1465 int sourcesEnqueued = 0;
1466 ModuleInfo *sourceInfo;
1467 while ((sourceInfo = modulesToLoadIter.next()))
1469 string moduleKey = sourceInfo->getModuleKey();
1471 dispatch_group_t loadingGroup = sourceInfo->getLoadingGroup();
1472 sourcesLoading.insert(loadingGroup);
1477 if (sourceInfo->getAttempted())
1482 string sourceCode = sourceInfo->getSourceCode();
1484 if (! shouldRecompileIfUnchanged)
1487 if (existingNodeClass && existingNodeClass->
getSourceCode() == sourceCode)
1493 sourceInfo->setAttempted(
true);
1495 dispatch_group_enter(loadingGroup);
1496 dispatch_group_enter(moduleSourceCompilersExist);
1500 queueItem->
sourcePath = sourceInfo->getFile()->path();
1502 queueItem->
sourceFile = sourceInfo->getFile();
1503 queueItem->
cachedModulesPath = sourceInfo->isSourceCodeOverridden() ? getOverriddenCompiledModuleCachePath() : getCompiledModuleCachePath();
1506 moduleCompilationQueue->enqueue(queueItem);
1515 for (
int i = 0; i < sourcesEnqueued; ++i)
1517 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
1518 dispatch_async(queue, ^{
1530 notifyCompilers(set<VuoCompilerModule *>(), set< pair<VuoCompilerModule *, VuoCompilerModule *> >(), set<VuoCompilerModule *>(), issues,
false);
1535 auto moduleLoadedCallback = [=](){
1537 moduleCompilationQueue->completed(queueItem);
1551 notifyCompilers(set<VuoCompilerModule *>(), set< pair<VuoCompilerModule *, VuoCompilerModule *> >(), set<VuoCompilerModule *>(), issues,
false);
1558 auto getType = [&compiler] (
const string &moduleKey) {
return compiler->
getType(moduleKey); };
1577 moduleLoadedCallback();
1591 notifyCompilers(set<VuoCompilerModule *>(), set< pair<VuoCompilerModule *, VuoCompilerModule *> >(), set<VuoCompilerModule *>(), issues,
false);
1592 moduleLoadedCallback();
1602 dispatch_group_leave(moduleSourceCompilersExist);
1606 return sourcesLoading;
1617 set<VuoCompilerModule *> VuoCompiler::Environment::unloadCompiledModules(
const set<string> &moduleKeys)
1619 set<VuoCompilerModule *> modulesUnloaded;
1621 for (set<string>::const_iterator i = moduleKeys.begin(); i != moduleKeys.end(); ++i)
1623 string moduleKey = *i;
1626 map<string, VuoCompilerNodeClass *>::iterator nodeClassIter = nodeClasses.find(moduleKey);
1627 if (nodeClassIter != nodeClasses.end())
1629 module = nodeClassIter->second;
1630 nodeClasses.erase(nodeClassIter);
1634 map<string, VuoCompilerType *>::iterator typeIter = types.find(moduleKey);
1635 if (typeIter != types.end())
1637 module = typeIter->second;
1638 types.erase(typeIter);
1642 map<string, VuoCompilerModule *>::iterator libraryModuleIter = libraryModules.find(moduleKey);
1643 if (libraryModuleIter != libraryModules.end())
1645 module = libraryModuleIter->second;
1646 libraryModules.erase(libraryModuleIter);
1653 modulesUnloaded.insert(module);
1654 removeFromDependencyGraph(module);
1659 return modulesUnloaded;
1667 void VuoCompiler::Environment::deleteModulesCompiledFromSourceCode(
const set<string> &moduleKeys)
1669 for (set<string>::const_iterator i = moduleKeys.begin(); i != moduleKeys.end(); ++i)
1671 string moduleKey = *i;
1673 map<string, ModuleInfo *>::iterator iter = moduleFilesAtSearchPath[getCompiledModuleCachePath()].find(moduleKey);
1674 if (iter != moduleFilesAtSearchPath[getCompiledModuleCachePath()].end())
1686 VuoCompilerModule * VuoCompiler::Environment::loadModule(ModuleInfo *moduleInfo)
1688 string moduleKey = moduleInfo->getModuleKey();
1695 __block
size_t inputDataBytes;
1696 __block
char *rawInputData;
1700 rawInputData = moduleInfo->getFile()->getContentsAsRawData(inputDataBytes);
1704 rawInputData = NULL;
1705 VUserLog(
"Warning: Couldn't load module '%s'. Its file may have been deleted. (%s)", moduleKey.c_str(), e.
what());
1711 char *processedInputData;
1713 processedInputData = loadModule_Pro0(moduleInfo, moduleKey, inputDataBytes, rawInputData);
1715 processedInputData = rawInputData;
1718 Module *module = NULL;
1719 bool moduleParseError = !processedInputData;
1720 if (!moduleParseError)
1722 string moduleReadError;
1724 module = readModuleFromBitcodeData(processedInputData, inputDataBytes, moduleReadError);
1726 free(processedInputData);
1730 moduleParseError =
true;
1732 string dir, file, ext;
1733 VuoFileUtilities::splitPath(moduleInfo->getFile()->isInArchive() ? moduleInfo->getFile()->getRelativePath() : moduleInfo->getFile()->path(), dir, file, ext);
1735 if (dir == getCompiledModuleCachePath())
1738 VUserLog(
"Error: Couldn't parse module '%s': %s.", moduleInfo->getFile()->getRelativePath().c_str(), moduleReadError.c_str());
1742 if (!moduleParseError)
1745 if (! moduleInfo->getFile()->isInArchive())
1746 modulePath = moduleInfo->getFile()->path();
1755 if (dynamic_cast<VuoCompilerNodeClass *>(compilerModule))
1756 nodeClasses[moduleKey] = static_cast<VuoCompilerNodeClass *>(compilerModule);
1757 else if (dynamic_cast<VuoCompilerType *>(compilerModule))
1758 types[moduleKey] = static_cast<VuoCompilerType *>(compilerModule);
1760 libraryModules[moduleKey] = compilerModule;
1765 map<string, VuoNodeSet *>::iterator nodeSetIter = nodeSetForName.find(nodeSet->
getName());
1766 if (nodeSetIter == nodeSetForName.end())
1768 nodeSetForName[nodeSet->
getName()] = nodeSet;
1773 nodeSet = nodeSetIter->second;
1775 compilerModule->getPseudoBase()->setNodeSet(nodeSet);
1779 loadModule_Pro1(rawInputData, processedInputData, compilerModule);
1782 vector<string> builtInModuleSearchPaths = getBuiltInModuleSearchPaths();
1783 bool isBuiltIn = (find(builtInModuleSearchPaths.begin(), builtInModuleSearchPaths.end(), moduleInfo->getSearchPath()) != builtInModuleSearchPaths.end());
1784 compilerModule->setBuiltIn(isBuiltIn);
1786 return compilerModule;
1790 destroyLlvmModule(module);
1809 removeFromDependencyGraph(oldNodeClass);
1810 destroyModule(oldNodeClass);
1813 nodeClasses[moduleKey] = newNodeClass;
1814 addToDependencyGraph(newNodeClass);
1820 DependencyGraphVertex *vertex = DependencyGraphVertex::vertexForDependency(module->
getPseudoBase()->
getModuleKey(), dependencyGraph);
1821 dependencyGraph->addVertex(vertex);
1823 vertex->setEnvironment(
this);
1830 for (set<string>::iterator i = dependencies.begin(); i != dependencies.end(); ++i)
1832 DependencyGraphVertex *depVertex = DependencyGraphVertex::vertexForDependency(*i, dependencyGraph);
1833 dependencyGraph->addEdge(vertex, depVertex);
1837 void VuoCompiler::Environment::removeFromDependencyGraph(
VuoCompilerModule *module)
1839 DependencyGraphVertex *vertex = DependencyGraphVertex::vertexForDependency(module->
getPseudoBase()->
getModuleKey(), dependencyGraph);
1840 dependencyGraph->removeVertex(vertex);
1853 void VuoCompiler::Environment::reifyPortTypes(
const map<string, VuoCompilerType *> &inheritedTypes)
1855 map<string, VuoCompilerType *> availableTypes = inheritedTypes;
1856 availableTypes.insert(types.begin(), types.end());
1858 for (map<string, VuoCompilerNodeClass *>::const_iterator i = nodeClasses.begin(); i != nodeClasses.end(); ++i)
1864 vector<VuoPortClass *> portClasses;
1865 portClasses.insert(portClasses.end(), inputPortClasses.begin(), inputPortClasses.end());
1866 portClasses.insert(portClasses.end(), outputPortClasses.begin(), outputPortClasses.end());
1868 for (vector<VuoPortClass *>::iterator j = portClasses.begin(); j != portClasses.end(); ++j)
1873 if (baseType && ! baseType->hasCompiler())
1878 VuoGenericType *genericType = dynamic_cast<VuoGenericType *>(baseType);
1883 genericTypes.insert( static_cast<VuoCompilerGenericType *>(reifiedType) );
1888 map<string, VuoCompilerType *>::iterator reifiedTypeIter = availableTypes.find(typeName);
1889 if (reifiedTypeIter != availableTypes.end())
1892 reifiedType = reifiedTypeIter->second;
1903 for (vector<VuoCompilerTriggerDescription *>::iterator j = triggers.begin(); j != triggers.end(); ++j)
1911 map<string, VuoCompilerType *>::iterator reifiedTypeIter = availableTypes.find(typeName);
1912 if (reifiedTypeIter != availableTypes.end())
1935 void VuoCompiler::Environment::getCacheableModulesAndDependencies(
bool builtIn,
bool installed, set<string> &cacheableModulesAndDependencies,
1936 set<string> &dylibsNeededToLinkToThisCache,
1937 set<string> &frameworksNeededToLinkToThisCache)
1939 if (isModuleCacheInitialized && ! isModuleCacheableDataDirty)
1941 cacheableModulesAndDependencies = moduleCacheContents;
1942 dylibsNeededToLinkToThisCache = moduleCacheDylibs;
1943 frameworksNeededToLinkToThisCache = moduleCacheFrameworks;
1951 map<string, VuoCompilerModule *> allModules;
1952 allModules.insert(nodeClasses.begin(), nodeClasses.end());
1953 allModules.insert(types.begin(), types.end());
1954 allModules.insert(libraryModules.begin(), libraryModules.end());
1956 set<string> dependencies;
1957 for (map<string, VuoCompilerModule *>::iterator i = allModules.begin(); i != allModules.end(); ++i)
1959 string moduleKey = i->first;
1964 if (module->requiresPro())
1972 cacheableModulesAndDependencies.insert(moduleKey);
1975 dependencies.insert(moduleDependencies.begin(), moduleDependencies.end());
1979 if (builtIn && installed)
1981 vector<string> coreDependencies = getCoreVuoDependencies();
1982 dependencies.insert(coreDependencies.begin(), coreDependencies.end());
1987 for (set<string>::iterator i = dependencies.begin(); i != dependencies.end(); ++i)
1989 string dependency = *i;
1990 if (allModules.find(dependency) == allModules.end())
1993 frameworksNeededToLinkToThisCache.insert(dependency);
1996 string dependencyPath = VuoCompiler::getLibraryPath(dependency, librarySearchPaths);
1997 if (! dependencyPath.empty())
2000 dylibsNeededToLinkToThisCache.insert(dependencyPath);
2002 cacheableModulesAndDependencies.insert(dependency);
2008 moduleCacheSuffix = (installed ?
"installed" :
"generated");
2009 dylibsNeededToLinkToThisCache.insert(moduleCachePath +
"/libVuoModuleCache-" + moduleCacheSuffix +
".dylib");
2011 moduleCacheDylibs = dylibsNeededToLinkToThisCache;
2012 moduleCacheFrameworks = frameworksNeededToLinkToThisCache;
2034 void VuoCompiler::Environment::useModuleCache(
bool shouldUseExistingCache,
VuoCompiler *compiler, set<string> cacheableModulesAndDependencies,
2035 set<string> dylibsNeededToLinkToCaches, set<string> frameworksNeededToLinkToCaches)
2039 static dispatch_once_t checked = 0;
2040 static bool prelinkCache =
true;
2041 dispatch_once(&checked, ^{
2043 bool result = CFPreferencesGetAppBooleanValue(CFSTR(
"prelinkCache"), CFSTR(
"org.vuo.Editor"), &valid);
2045 prelinkCache = result;
2049 VDebugLog(
"Ignoring the module cache since the 'prelinkCache' preference is false.");
2055 if (moduleCachePath.empty())
2060 string cacheDescription = string() +
"the cache of " + moduleCacheSuffix +
" modules at '" + moduleCachePath +
"'";
2061 if (isModuleCacheInitialized && ! isModuleCacheableDataDirty)
2063 VDebugLog(
"No need to recheck %s.", cacheDescription.c_str());
2069 VDebugLog(
"Checking if %s is up-to-date…", cacheDescription.c_str());
2071 bool isCacheUpToDate =
true;
2073 if (isModuleCacheInitialized && isModuleCacheableDataDirty)
2074 isCacheUpToDate =
false;
2076 isModuleCacheInitialized =
true;
2077 isModuleCacheableDataDirty =
false;
2079 const string dylibFileName =
"libVuoModuleCache-" + moduleCacheSuffix +
".dylib";
2080 const string indexFileName =
"moduleCache-" + moduleCacheSuffix +
".txt";
2081 string dylibPath = moduleCachePath +
"/" + dylibFileName;
2082 string indexPath = moduleCachePath +
"/" + indexFileName;
2086 bool dylibFileExists =
false;
2087 bool indexFileExists =
false;
2096 if (! (dirExists && dylibFileExists && indexFileExists))
2098 if (shouldUseExistingCache)
2099 throw VuoException(
"Trying to use the existing cache, but the cache doesn't exist.");
2104 if (! indexFileExists)
2106 if (! dylibFileExists)
2109 isCacheUpToDate =
false;
2117 fileForLocking = moduleCacheFileForLocking[dylibPath];
2118 if (! fileForLocking)
2121 moduleCacheFileForLocking[dylibPath] = fileForLocking;
2125 VDebugLog(
"\tWarning: Couldn't lock for reading.");
2130 if (isCacheUpToDate)
2135 if (shouldUseExistingCache)
2136 throw VuoException(
"Trying to use the existing cache, but the cache doesn't contain readable data.");
2138 isCacheUpToDate =
false;
2144 const char separator =
'\n';
2145 if (isCacheUpToDate || shouldUseExistingCache)
2148 string index = indexFile.getContentsAsString();
2151 moduleCacheContents.clear();
2152 moduleCacheContents.insert(actualIndex.begin(), actualIndex.end());
2154 if (shouldUseExistingCache)
2156 isModuleCacheAvailable =
true;
2163 if (isCacheUpToDate)
2165 if (moduleCacheContents.size() != cacheableModulesAndDependencies.size())
2166 isCacheUpToDate =
false;
2169 set<string> contentsInBoth;
2170 std::set_intersection(moduleCacheContents.begin(), moduleCacheContents.end(),
2171 cacheableModulesAndDependencies.begin(), cacheableModulesAndDependencies.end(),
2172 std::inserter(contentsInBoth, contentsInBoth.begin()));
2174 if (contentsInBoth.size() != cacheableModulesAndDependencies.size())
2175 isCacheUpToDate =
false;
2181 if (isCacheUpToDate)
2185 for (map<
string, map<string, ModuleInfo *> >::iterator i = moduleFilesAtSearchPath.begin(); i != moduleFilesAtSearchPath.end(); ++i)
2187 for (map<string, ModuleInfo *>::iterator j = i->second.begin(); j != i->second.end(); ++j)
2189 if (! j->second->isOlderThan(cacheLastModified))
2191 isCacheUpToDate =
false;
2200 if (isCacheUpToDate)
2204 isModuleCacheAvailable =
true;
2210 dispatch_async(moduleCacheBuildingQueue, ^{
2211 VDebugLog(
"Rebuilding %s…", cacheDescription.c_str());
2213 set<Module *> modulesToLink;
2214 set<string> librariesToLink;
2215 set<string> frameworksToLink;
2217 compiler->getLinkerInputs(cacheableModulesAndDependencies, Optimization_SmallBinary, modulesToLink, librariesToLink, frameworksToLink);
2219 librariesToLink.insert(dylibsNeededToLinkToCaches.begin(), dylibsNeededToLinkToCaches.end());
2220 set<string>::iterator iter = librariesToLink.find(dylibPath);
2221 if (iter != librariesToLink.end())
2222 librariesToLink.erase(iter);
2224 frameworksToLink.insert(frameworksNeededToLinkToCaches.begin(), frameworksNeededToLinkToCaches.end());
2227 bool gotLockForWriting =
false;
2232 if (! gotLockForWriting)
2233 throw VuoException(
"The cache file is being used by another process. "
2234 "If any composition windows are open from previous Vuo sessions, quit them. "
2235 "If any processes whose names start with \"VuoComposition\" or one of your composition file names appear in Activity Monitor, force-quit them.");
2238 string dir, file, ext;
2241 compiler->link(tmpPath, modulesToLink, librariesToLink, frameworksToLink,
true);
2249 getVuoFrameworkPath() +
"/Helpers/install_name_tool",
2256 vector<string> expectedContents(cacheableModulesAndDependencies.begin(), cacheableModulesAndDependencies.end());
2262 VDebugLog(
"\tWarning: Couldn't downgrade the lock back to reading.");
2264 dispatch_sync(environmentQueue, ^{
2265 moduleCacheContents = cacheableModulesAndDependencies;
2266 isModuleCacheAvailable =
true;
2272 if (gotLockForWriting)
2274 VDebugLog(
"\tWarning: Couldn't downgrade the lock back to reading.");
2276 VUserLog(
"Warning: Couldn't rebuild %s for the \"faster build\" optimization: %s", cacheDescription.c_str(), e.
what());
2284 VUserLog(
"Warning: Couldn't use %s for the \"faster build\" optimization: %s", cacheDescription.c_str(), e.
what());
2291 void VuoCompiler::Environment::waitForModuleCachesToBuild(
void)
2293 dispatch_sync(moduleCacheBuildingQueue, ^{});
2305 bool VuoCompiler::Environment::findInModuleCache(
const string &moduleOrDependency,
string &cachePath)
2307 if (isModuleCacheAvailable && moduleCacheContents.find(moduleOrDependency) != moduleCacheContents.end())
2309 cachePath = moduleCachePath +
"/libVuoModuleCache-" + moduleCacheSuffix +
".dylib";
2322 void VuoCompiler::Environment::modulesChanged(
void)
2324 isModuleCacheableDataDirty =
true;
2325 isModuleCacheAvailable =
false;
2331 bool VuoCompiler::Environment::isBuiltIn()
2333 return this == sharedEnvironments[0][0];
2339 string VuoCompiler::Environment::getName()
2343 else if (
this == sharedEnvironments[1][0])
2345 else if (
this == sharedEnvironments[2][0])
2347 return "composition-local";
2353 void VuoCompiler::applyToInstalledEnvironments(
void (^doForEnvironment)(Environment *))
2355 dispatch_sync(environmentQueue, ^{
2356 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i) {
2357 doForEnvironment((*i)[0]);
2365 void VuoCompiler::applyToInstalledEnvironments(
void (^doForEnvironment)(Environment *,
bool *,
string),
bool *result,
string arg)
2367 dispatch_sync(environmentQueue, ^{
2368 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i) {
2369 doForEnvironment((*i)[0], result, arg);
2377 void VuoCompiler::applyToAllEnvironments(
void (^doForEnvironment)(Environment *environment))
2379 dispatch_sync(environmentQueue, ^{
2380 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i) {
2381 for (vector<Environment *>::iterator j = i->begin(); j != i->end(); ++j) {
2382 doForEnvironment(*j);
2402 shouldLoadAllModules =
true;
2403 hasLoadedAllModules =
false;
2404 modulesToLoadQueue = dispatch_queue_create(
"org.vuo.compiler.modules", NULL);
2405 moduleCacheBuilding = dispatch_group_create();
2406 dependencyGraph = NULL;
2407 compositionDependencyGraph = NULL;
2409 _shouldShowSplashWindow =
false;
2411 delegateQueue = dispatch_queue_create(
"org.vuo.compiler.delegate", NULL);
2413 string vuoFrameworkPath = getVuoFrameworkPath();
2414 if (! vuoFrameworkPath.empty())
2415 clangPath = vuoFrameworkPath +
"/Helpers/clang";
2417 clangPath = llvm::sys::Path(StringRef(LLVM_ROOT
"/bin/clang"));
2419 dispatch_sync(environmentQueue, ^{
2420 if (sharedEnvironments.empty())
2422 sharedEnvironments = vector< vector<Environment *> >(3, vector<Environment *>(2, NULL));
2423 for (vector< vector<Environment *> >::iterator i = sharedEnvironments.begin(); i != sharedEnvironments.end(); ++i) {
2424 for (vector<Environment *>::iterator j = i->begin(); j != i->end(); ++j) {
2425 *j =
new Environment();
2429 vector<string> builtInModuleSearchPaths = Environment::getBuiltInModuleSearchPaths();
2430 for (vector<string>::iterator i = builtInModuleSearchPaths.begin(); i != builtInModuleSearchPaths.end(); ++i) {
2431 sharedEnvironments[0][0]->addModuleSearchPath(*i,
false);
2434 vector<string> builtInHeaderSearchPaths = Environment::getBuiltInHeaderSearchPaths();
2435 for (vector<string>::iterator i = builtInHeaderSearchPaths.begin(); i != builtInHeaderSearchPaths.end(); ++i) {
2436 sharedEnvironments[0][0]->addHeaderSearchPath(*i);
2439 vector<string> builtInLibrarySearchPaths = Environment::getBuiltInLibrarySearchPaths();
2440 for (vector<string>::iterator i = builtInLibrarySearchPaths.begin(); i != builtInLibrarySearchPaths.end(); ++i) {
2441 sharedEnvironments[0][0]->addLibrarySearchPath(*i);
2444 vector<string> builtInFrameworkSearchPaths = Environment::getBuiltInFrameworkSearchPaths();
2445 for (vector<string>::iterator i = builtInFrameworkSearchPaths.begin(); i != builtInFrameworkSearchPaths.end(); ++i) {
2446 sharedEnvironments[0][0]->addFrameworkSearchPath(*i);
2460 string vuoFrameworkPath = getVuoFrameworkPath();
2461 if (! vuoFrameworkPath.empty())
2463 vector<string> moduleCachePaths(3);
2464 moduleCachePaths[0] = vuoFrameworkPath +
"/Modules/Builtin";
2468 for (
size_t i = 0; i < moduleCachePaths.size(); ++i)
2470 string moduleCachePath = moduleCachePaths[i];
2472 sharedEnvironments[i][0]->setModuleCachePath(moduleCachePath);
2473 sharedEnvironments[i][0]->addModuleSearchPath(moduleCachePath +
"/Modules",
false);
2475 sharedEnvironments[i][1]->setModuleCachePath(moduleCachePath);
2489 dispatch_group_wait(moduleCacheBuilding, DISPATCH_TIME_FOREVER);
2490 dispatch_release(moduleCacheBuilding);
2492 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i)
2493 (*i)[0]->removeCompilerToNotify(
this);
2495 dispatch_sync(delegateQueue, ^{});
2497 dispatch_release(modulesToLoadQueue);
2498 dispatch_release(delegateQueue);
2500 delete dependencyGraph;
2501 delete compositionDependencyGraph;
2507 void VuoCompiler::reset(
void)
2509 dispatch_group_wait(moduleSourceCompilersExist, DISPATCH_TIME_FOREVER);
2511 for (vector< vector<Environment *> >::iterator i = sharedEnvironments.begin(); i != sharedEnvironments.end(); ++i)
2513 (*i)[0]->stopWatchingModuleSearchPaths();
2514 dispatch_sync((*i)[0]->moduleSearchPathContentsChangedQueue, ^{});
2517 for (map<
string, vector<Environment *> >::iterator i = environmentsForCompositionFamily.begin(); i != environmentsForCompositionFamily.end(); ++i)
2519 (i->second)[0]->stopWatchingModuleSearchPaths();
2520 dispatch_sync((i->second)[0]->moduleSearchPathContentsChangedQueue, ^{});
2523 for (vector< vector<Environment *> >::iterator i = sharedEnvironments.begin(); i != sharedEnvironments.end(); ++i)
2524 for (vector<Environment *>::iterator j = i->begin(); j != i->end(); ++j)
2527 for (map<
string, vector<Environment *> >::iterator i = environmentsForCompositionFamily.begin(); i != environmentsForCompositionFamily.end(); ++i)
2528 for (vector<Environment *>::iterator j = i->second.begin(); j != i->second.end(); ++j)
2531 sharedEnvironments.clear();
2532 environmentsForCompositionFamily.clear();
2542 dispatch_async(delegateQueue, ^{
2543 this->delegate = delegate;
2558 string compositionModulesDir;
2559 string compositionBaseDir;
2560 lastCompositionIsSubcomposition =
false;
2561 if (! compositionPath.empty())
2569 string compositionDir;
2572 lastCompositionIsSubcomposition = (compositionDir == compositionModulesDir);
2577 dispatch_sync(environmentQueue, ^{
2578 if (! environments.empty() && compositionBaseDir == lastCompositionBaseDir) {
2581 lastCompositionBaseDir = compositionBaseDir;
2585 Environment *oldCompositionFamilyInstalledEnvironment =
nullptr;
2586 vector<Environment *> compositionEnvironments;
2587 if (! environments.empty())
2589 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i) {
2590 (*i)[0]->removeCompilerToNotify(
this);
2593 if (environments.size() >= 5) {
2594 oldCompositionFamilyInstalledEnvironment = environments[3][0];
2597 compositionEnvironments = environments.back();
2599 environments.clear();
2606 bool isCompositionInSharedEnvironment =
false;
2607 for (vector< vector<Environment *> >::iterator i = sharedEnvironments.begin(); i != sharedEnvironments.end(); ++i)
2609 environments.push_back(*i);
2611 vector<string> moduleSearchPaths = (*i)[0]->getModuleSearchPaths();
2612 for (vector<string>::iterator j = moduleSearchPaths.begin(); j != moduleSearchPaths.end(); ++j)
2614 string moduleSearchPath = *j;
2616 if (moduleSearchPath == compositionModulesDir)
2618 isCompositionInSharedEnvironment =
true;
2623 if (isCompositionInSharedEnvironment) {
2630 Environment *newCompositionFamilyInstalledEnvironment =
nullptr;
2631 if (! isCompositionInSharedEnvironment && ! compositionPath.empty())
2633 vector<Environment *> compositionFamilyEnvironments = environmentsForCompositionFamily[compositionBaseDir];
2634 if (compositionFamilyEnvironments.empty())
2636 compositionFamilyEnvironments = vector<Environment *>(2, NULL);
2637 compositionFamilyEnvironments[0] =
new Environment();
2638 compositionFamilyEnvironments[1] =
new Environment();
2639 environmentsForCompositionFamily[compositionBaseDir] = compositionFamilyEnvironments;
2643 compositionFamilyEnvironments[0]->addModuleSearchPath(compositionModulesDir);
2647 string moduleCachePath = getCachePathForComposition(compositionBaseDir);
2649 compositionFamilyEnvironments[0]->setModuleCachePath(moduleCachePath);
2650 compositionFamilyEnvironments[0]->addModuleSearchPath(moduleCachePath +
"/Modules",
false);
2652 compositionFamilyEnvironments[1]->setModuleCachePath(moduleCachePath);
2654 environments.push_back(compositionFamilyEnvironments);
2656 newCompositionFamilyInstalledEnvironment = compositionFamilyEnvironments[0];
2661 if (compositionEnvironments.empty())
2663 compositionEnvironments = vector<Environment *>(2, NULL);
2664 compositionEnvironments[0] =
new Environment();
2665 compositionEnvironments[1] =
new Environment();
2667 environments.push_back(compositionEnvironments);
2669 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i) {
2670 (*i)[0]->addCompilerToNotify(
this);
2673 delete dependencyGraph;
2674 delete compositionDependencyGraph;
2675 dependencyGraph = makeDependencyNetwork(environments, ^
VuoDirectedAcyclicGraph * (Environment *env) {
return env->getDependencyGraph(); });
2676 compositionDependencyGraph = makeDependencyNetwork(environments, ^
VuoDirectedAcyclicGraph * (Environment *env) {
return env->getCompositionDependencyGraph(); });
2681 if (oldCompositionFamilyInstalledEnvironment != newCompositionFamilyInstalledEnvironment)
2683 auto getModules = [] (Environment *env)
2685 map<string, VuoCompilerModule *> modules;
2688 for (
auto i : env->getNodeClasses()) {
2691 for (
auto i : env->getTypes()) {
2694 for (
auto i : env->getLibraryModules()) {
2701 map<string, VuoCompilerModule *> modulesAdded = getModules(newCompositionFamilyInstalledEnvironment);
2702 map<string, VuoCompilerModule *> modulesRemoved = getModules(oldCompositionFamilyInstalledEnvironment);
2704 map<string, pair<VuoCompilerModule *, VuoCompilerModule *> > modulesModified;
2705 for (map<string, VuoCompilerModule *>::iterator add = modulesAdded.begin(); add != modulesAdded.end(); )
2707 map<string, VuoCompilerModule *>::iterator rem = modulesRemoved.find(add->first);
2708 if (rem != modulesRemoved.end())
2710 modulesModified[add->first] = make_pair(rem->second, add->second);
2711 modulesAdded.erase(add++);
2712 modulesRemoved.erase(rem);
2720 if (! (modulesAdded.empty() && modulesModified.empty() && modulesRemoved.empty()) )
2723 VuoCompilerDelegate::LoadedModulesData *delegateData =
new VuoCompilerDelegate::LoadedModulesData(set< pair<VuoCompilerModule *, VuoCompilerModule *> >(), set<VuoCompilerModule *>(), issues);
2724 delegateData->retain();
2726 Environment *scopeEnvironment = newCompositionFamilyInstalledEnvironment;
2727 if (! scopeEnvironment) {
2728 scopeEnvironment = compositionEnvironments.at(0);
2731 loadedModules(modulesAdded, modulesModified, modulesRemoved, issues, delegateData, scopeEnvironment);
2741 if (!graphForEnvironment)
2746 for (vector< vector<Environment *> >::const_iterator i = environments.begin(); i != environments.end(); ++i)
2750 network->
addEdge(graphForEnvironment(i->at(0)), graphForEnvironment(i->at(1)));
2754 for (vector< vector<Environment *> >::const_iterator ii = environments.begin(); ii != i; ++ii)
2756 network->
addEdge(graphForEnvironment(i->at(0)), graphForEnvironment(ii->at(0)));
2757 network->
addEdge(graphForEnvironment(i->at(1)), graphForEnvironment(ii->at(0)));
2784 void VuoCompiler::loadModulesIfNeeded(
const set<string> &moduleKeys)
2786 __block
bool willLoadAllModules =
false;
2787 if (moduleKeys.empty())
2789 dispatch_sync(modulesToLoadQueue, ^{
2790 if (shouldLoadAllModules && ! hasLoadedAllModules) {
2791 willLoadAllModules =
true;
2792 hasLoadedAllModules =
true;
2797 if (! willLoadAllModules && moduleKeys.empty())
2802 __block set<dispatch_group_t> sourcesLoading;
2803 dispatch_sync(environmentQueue, ^{
2804 sourcesLoading = loadModulesAndSources(moduleKeys, set<string>(), set<string>(),
2805 moduleKeys, set<string>(), set<string>(),
2806 willLoadAllModules,
false,
nullptr,
nullptr,
nullptr,
"");
2812 for (set<dispatch_group_t>::iterator i = sourcesLoading.begin(); i != sourcesLoading.end(); ++i)
2814 dispatch_group_wait(*i, DISPATCH_TIME_FOREVER);
2815 dispatch_release(*i);
2827 set<dispatch_group_t> VuoCompiler::loadModulesAndSources(
const set<string> &modulesAddedKeys,
const set<string> &modulesModifiedKeys,
const set<string> &modulesRemovedKeys,
2828 const set<string> &sourcesAddedKeys,
const set<string> &sourcesModifiedKeys,
const set<string> &sourcesRemovedKeys,
2829 bool willLoadAllModules,
bool shouldRecompileSourcesIfUnchanged,
2830 Environment *currentEnvironment,
VuoCompilerIssues *issuesForCurrentEnvironment,
2831 std::function<
void(
void)> moduleLoadedCallback,
const string &moduleAddedOrModifiedSourceCode)
2843 map<Environment *, set<string> > modulesAdded;
2844 map<Environment *, set<string> > modulesModified;
2845 map<Environment *, set<string> > modulesRemoved;
2846 map<Environment *, set<string> > sourcesAdded;
2847 map<Environment *, set<string> > sourcesModified;
2848 map<Environment *, set<string> > sourcesRemoved;
2849 map<Environment *, set<string> > potentialSpecializedModules;
2851 if (currentEnvironment)
2853 modulesAdded[currentEnvironment] = modulesAddedKeys;
2854 modulesModified[currentEnvironment] = modulesModifiedKeys;
2855 modulesRemoved[currentEnvironment] = modulesRemovedKeys;
2856 sourcesAdded[currentEnvironment] = sourcesAddedKeys;
2857 sourcesModified[currentEnvironment] = sourcesModifiedKeys;
2858 sourcesRemoved[currentEnvironment] = sourcesRemovedKeys;
2862 Environment *genEnv =
nullptr;
2863 if (! willLoadAllModules)
2869 int scope = environments.size() - (lastCompositionIsSubcomposition ? 2 : 1);
2870 genEnv = environments.at(scope).at(1);
2871 potentialSpecializedModules[genEnv] = modulesAddedKeys;
2874 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i)
2876 Environment *env = (*i).at(0);
2878 ModuleInfoIterator modulesAddedIter = (willLoadAllModules ? env->listAllModules() : env->listModules(modulesAddedKeys));
2879 ModuleInfoIterator sourcesAddedIter = (willLoadAllModules ? env->listAllSourceFiles() : env->listSourceFiles(sourcesAddedKeys));
2880 ModuleInfoIterator sourcesModifiedIter = (willLoadAllModules ? env->listAllSourceFiles() : env->listSourceFiles(sourcesModifiedKeys));
2882 ModuleInfo *moduleInfo;
2883 while ((moduleInfo = modulesAddedIter.next()))
2885 string moduleKey = moduleInfo->getModuleKey();
2887 modulesAdded[env].insert(moduleKey);
2889 if (! willLoadAllModules)
2891 auto foundIter = potentialSpecializedModules[genEnv].find(moduleKey);
2892 if (foundIter != potentialSpecializedModules[genEnv].end())
2893 potentialSpecializedModules[genEnv].erase(foundIter);
2899 auto isCompiledModuleAtSameSearchPath = [&env] (ModuleInfo *sourceInfo)
2901 ModuleInfo *compiledModuleInfo = env->listModule(sourceInfo->getModuleKey());
2902 return (compiledModuleInfo && compiledModuleInfo->getSearchPath() == sourceInfo->getSearchPath());
2905 while ((moduleInfo = sourcesAddedIter.next()))
2907 if (isCompiledModuleAtSameSearchPath(moduleInfo))
2910 sourcesAdded[env].insert( moduleInfo->getModuleKey() );
2913 while ((moduleInfo = sourcesModifiedIter.next()))
2915 if (isCompiledModuleAtSameSearchPath(moduleInfo))
2918 sourcesModified[env].insert( moduleInfo->getModuleKey() );
2923 map<Environment *, VuoCompilerIssues *> issues;
2924 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i)
2926 Environment *env = (*i).at(0);
2927 issues[env] = (env == currentEnvironment && issuesForCurrentEnvironment ? issuesForCurrentEnvironment :
new VuoCompilerIssues());
2932 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i)
2934 Environment *env = (*i).at(0);
2939 set<VuoDirectedAcyclicGraph::Vertex *> circularDependencies = env->getCompositionDependencyGraph()->getCycleVertices();
2941 set<string> sourcesAddedModified;
2942 sourcesAddedModified.insert(sourcesAdded[env].begin(), sourcesAdded[env].end());
2943 sourcesAddedModified.insert(sourcesModified[env].begin(), sourcesModified[env].end());
2945 for (set<string>::iterator j = sourcesAddedModified.begin(); j != sourcesAddedModified.end(); ++j)
2947 string moduleKey = *j;
2950 for (set<VuoDirectedAcyclicGraph::Vertex *>::iterator k = circularDependencies.begin(); k != circularDependencies.end(); ++k)
2952 DependencyGraphVertex *vertex = static_cast<DependencyGraphVertex *>(*k);
2953 if (vertex->getDependency() == moduleKey)
2962 sourcesAdded[env].erase(moduleKey);
2963 sourcesModified[env].erase(moduleKey);
2966 "Subcomposition contains itself",
2967 "%moduleKey contains an instance of itself, "
2968 "or contains another subcomposition that contains an instance of %moduleKey.");
2969 issue.setModuleKey(moduleKey);
2971 ModuleInfo *sourceInfo = env->listSourceFile(moduleKey);
2973 issue.setFilePath(sourceInfo->getFile()->path());
2975 issues[env]->append(issue);
2984 map<Environment *, set<string> > modulesDepOnModulesModified;
2985 map<Environment *, set<string> > sourcesDepOnModulesModified;
2986 map<Environment *, set<string> > modulesDepOnModulesRemoved;
2987 map<Environment *, set<string> > sourcesDepOnModulesRemoved;
2990 __block map<Environment *, set<string> > modulesDepOnModulesModified_otherCompiler;
2991 __block map<Environment *, set<string> > sourcesDepOnModulesModified_otherCompiler;
2992 __block map<Environment *, set<string> > modulesDepOnModulesRemoved_otherCompiler;
2993 __block map<Environment *, set<string> > sourcesDepOnModulesRemoved_otherCompiler;
2995 vector<VuoDirectedAcyclicNetwork *> searchDependencyGraphs;
2996 searchDependencyGraphs.push_back(dependencyGraph);
2997 for (map<
string, vector<Environment *> >::iterator ii = environmentsForCompositionFamily.begin(); ii != environmentsForCompositionFamily.end(); ++ii)
2999 vector< vector<Environment *> > otherEnvs = sharedEnvironments;
3000 otherEnvs.push_back(ii->second);
3002 searchDependencyGraphs.push_back(other);
3005 VuoDirectedAcyclicGraph *currentEnvironmentDependencyGraph = (currentEnvironment ? currentEnvironment->getDependencyGraph() :
nullptr);
3007 findDependentModulesAndSources(modulesModified, searchDependencyGraphs, currentEnvironmentDependencyGraph,
3008 modulesDepOnModulesModified, modulesDepOnModulesModified_otherCompiler,
3009 sourcesDepOnModulesModified, sourcesDepOnModulesModified_otherCompiler);
3011 findDependentModulesAndSources(modulesRemoved, searchDependencyGraphs, currentEnvironmentDependencyGraph,
3012 modulesDepOnModulesRemoved, modulesDepOnModulesRemoved_otherCompiler,
3013 sourcesDepOnModulesRemoved, sourcesDepOnModulesRemoved_otherCompiler);
3015 set<Environment *> otherEnvironments;
3016 for (map<Environment *, set<string> >::iterator i = modulesDepOnModulesModified_otherCompiler.begin(); i != modulesDepOnModulesModified_otherCompiler.end(); ++i)
3017 otherEnvironments.insert(i->first);
3018 for (map<Environment *, set<string> >::iterator i = sourcesDepOnModulesModified_otherCompiler.begin(); i != sourcesDepOnModulesModified_otherCompiler.end(); ++i)
3019 otherEnvironments.insert(i->first);
3020 for (map<Environment *, set<string> >::iterator i = modulesDepOnModulesRemoved_otherCompiler.begin(); i != modulesDepOnModulesRemoved_otherCompiler.end(); ++i)
3021 otherEnvironments.insert(i->first);
3022 for (map<Environment *, set<string> >::iterator i = sourcesDepOnModulesRemoved_otherCompiler.begin(); i != sourcesDepOnModulesRemoved_otherCompiler.end(); ++i)
3023 otherEnvironments.insert(i->first);
3025 for (Environment *env : otherEnvironments)
3027 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
3028 string moduleSearchPath = env->getModuleSearchPaths().front();
3031 dispatch_sync(environmentQueue, ^{
3032 otherCompiler->loadModulesAndSources(set<string>(), modulesDepOnModulesModified_otherCompiler[env], modulesDepOnModulesRemoved_otherCompiler[env],
3033 set<string>(), sourcesDepOnModulesModified_otherCompiler[env], sourcesDepOnModulesRemoved_otherCompiler[env],
3034 false,
true, env,
nullptr,
nullptr,
"");
3037 delete otherCompiler;
3046 map<Environment *, set<VuoCompilerModule *> > actualModulesRemoved;
3047 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i)
3049 Environment *env = (*i).at(0);
3051 set<string> modulesToUnload;
3052 modulesToUnload.insert(modulesRemoved[env].begin(), modulesRemoved[env].end());
3053 modulesToUnload.insert(modulesModified[env].begin(), modulesModified[env].end());
3054 modulesToUnload.insert(modulesDepOnModulesRemoved[env].begin(), modulesDepOnModulesRemoved[env].end());
3055 modulesToUnload.insert(modulesDepOnModulesModified[env].begin(), modulesDepOnModulesModified[env].end());
3057 actualModulesRemoved[env] = env->unloadCompiledModules(modulesToUnload);
3059 if (!env->isBuiltIn() && !actualModulesRemoved[env].empty())
3061 set<string> actualModulesRemovedKeys;
3062 for (
auto m : actualModulesRemoved[env])
3063 actualModulesRemovedKeys.insert(m->getPseudoBase()->getModuleKey());
3075 map<Environment *, set<string> > modulesToLoad;
3076 map<Environment *, map<string, string> > modulesToLoadSourceCode;
3077 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i)
3079 Environment *env = (*i).at(0);
3081 if (! modulesAdded[env].empty())
3082 modulesToLoad[env].insert(modulesAdded[env].begin(), modulesAdded[env].end());
3083 if (! modulesModified[env].empty())
3084 modulesToLoad[env].insert(modulesModified[env].begin(), modulesModified[env].end());
3085 if (! modulesDepOnModulesModified[env].empty())
3086 modulesToLoad[env].insert(modulesDepOnModulesModified[env].begin(), modulesDepOnModulesModified[env].end());
3088 if (env == currentEnvironment && moduleLoadedCallback)
3090 if (modulesAdded[env].size() == 1)
3091 modulesToLoadSourceCode[env][*modulesAdded[env].begin()] = moduleAddedOrModifiedSourceCode;
3092 else if (modulesModified[env].size() == 1)
3093 modulesToLoadSourceCode[env][*modulesModified[env].begin()] = moduleAddedOrModifiedSourceCode;
3097 map<Environment *, set<VuoCompilerModule *> > actualModulesAdded;
3098 while (! modulesToLoad.empty())
3100 set<string> dependenciesToLoad;
3101 map<Environment *, set<string> > potentialSpecializedDependencies;
3102 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i)
3104 Environment *env = (*i).at(0);
3105 Environment *genEnv = (*i).at(1);
3107 set<VuoCompilerModule *> actualModulesLoaded = env->loadCompiledModules(modulesToLoad[env], modulesToLoadSourceCode[env]);
3109 actualModulesAdded[env].insert(actualModulesLoaded.begin(), actualModulesLoaded.end());
3110 modulesToLoad.erase(env);
3112 set<string> actualModulesLoadedKeys;
3113 for (set<VuoCompilerModule *>::iterator j = actualModulesLoaded.begin(); j != actualModulesLoaded.end(); ++j)
3115 set<string> dependencies = (*j)->getDependencies();
3116 dependenciesToLoad.insert(dependencies.begin(), dependencies.end());
3117 potentialSpecializedDependencies[genEnv].insert(dependencies.begin(), dependencies.end());
3118 actualModulesLoadedKeys.insert((*j)->getPseudoBase()->getModuleKey());
3121 if (!env->isBuiltIn() && !actualModulesLoadedKeys.empty())
3125 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i)
3127 Environment *env = (*i).at(0);
3129 ModuleInfoIterator dependenciesInEnv = env->listModules(dependenciesToLoad);
3130 ModuleInfo *moduleInfo;
3131 while ((moduleInfo = dependenciesInEnv.next()))
3133 modulesToLoad[env].insert( moduleInfo->getModuleKey() );
3135 for (map<Environment *, set<string> >::iterator j = potentialSpecializedDependencies.begin(); j != potentialSpecializedDependencies.end(); ++j)
3137 auto foundIter = j->second.find( moduleInfo->getModuleKey() );
3138 if (foundIter != j->second.end())
3139 j->second.erase(foundIter);
3144 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i)
3146 Environment *genEnv = (*i).at(1);
3147 potentialSpecializedModules[genEnv].insert(potentialSpecializedDependencies[genEnv].begin(), potentialSpecializedDependencies[genEnv].end());
3154 set<dispatch_group_t> specializedModulesLoading;
3155 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i)
3157 Environment *genEnv = (*i).at(1);
3158 set<dispatch_group_t> s = genEnv->loadSpecializedModules(potentialSpecializedModules[genEnv],
this,
llvmQueue);
3159 specializedModulesLoading.insert(s.begin(), s.end());
3164 if (moduleLoadedCallback)
3165 moduleLoadedCallback();
3169 map<Environment *, set< pair<VuoCompilerModule *, VuoCompilerModule *> > > actualModulesModified;
3170 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i)
3172 Environment *env = (*i).at(0);
3174 for (set<VuoCompilerModule *>::iterator add = actualModulesAdded[env].begin(); add != actualModulesAdded[env].end(); )
3176 set<VuoCompilerModule *>::iterator rem;
3177 for (rem = actualModulesRemoved[env].begin(); rem != actualModulesRemoved[env].end(); ++rem)
3178 if ((*rem)->getPseudoBase()->getModuleKey() == (*add)->getPseudoBase()->getModuleKey())
3181 if (rem != actualModulesRemoved[env].end())
3183 actualModulesModified[env].insert( make_pair(*rem, *add) );
3184 actualModulesRemoved[env].erase(rem);
3185 actualModulesAdded[env].erase(add++);
3194 bool wereModulesAddedOrModified =
false;
3195 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i)
3197 Environment *env = (*i).at(0);
3198 if (! (actualModulesAdded[env].empty() && actualModulesModified[env].empty()) )
3200 wereModulesAddedOrModified =
true;
3205 if (wereModulesAddedOrModified)
3207 map<string, VuoCompilerType *> inheritedTypes;
3208 for (
const vector<Environment *> &envs : environments)
3210 for (Environment *env : envs)
3212 env->reifyPortTypes(inheritedTypes);
3213 map<string, VuoCompilerType *> envTypes = env->getTypes();
3214 inheritedTypes.insert(envTypes.begin(), envTypes.end());
3223 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i)
3225 Environment *env = (*i).at(0);
3227 set<string> sourcesToUnload;
3228 sourcesToUnload.insert(sourcesRemoved[env].begin(), sourcesRemoved[env].end());
3229 sourcesToUnload.insert(sourcesDepOnModulesRemoved[env].begin(), sourcesDepOnModulesRemoved[env].end());
3230 if (! sourcesToUnload.empty())
3232 string moduleSearchPath = env->getModuleSearchPaths().front();
3234 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
3237 dispatch_sync(environmentQueue, ^{
3238 otherCompiler->loadModulesAndSources(set<string>(), set<string>(), sourcesToUnload,
3239 set<string>(), set<string>(), set<string>(),
3240 false,
false, env,
nullptr,
nullptr,
"");
3243 delete otherCompiler;
3247 if (!env->isBuiltIn() && !sourcesToUnload.empty())
3250 env->deleteModulesCompiledFromSourceCode(sourcesToUnload);
3258 map<Environment *, set<string> > sourcesDepOnModulesAdded;
3260 map<Environment *, set<string> > modulesDepOnModulesAdded;
3261 __block map<Environment *, set<string> > modulesDepOnModulesAdded_otherCompiler;
3262 __block map<Environment *, set<string> > sourcesDepOnModulesAdded_otherCompiler;
3264 map<Environment *, set<string> > actualModuleKeysAdded;
3265 for (
const vector<Environment *> &envs : environments)
3267 Environment *env = envs.at(0);
3272 vector<VuoDirectedAcyclicNetwork *> searchDependencyGraphs;
3273 searchDependencyGraphs.push_back(compositionDependencyGraph);
3274 for (map<
string, vector<Environment *> >::iterator ii = environmentsForCompositionFamily.begin(); ii != environmentsForCompositionFamily.end(); ++ii)
3276 vector< vector<Environment *> > otherEnvs = sharedEnvironments;
3277 otherEnvs.push_back(ii->second);
3279 searchDependencyGraphs.push_back(other);
3282 VuoDirectedAcyclicGraph *currentEnvironmentDependencyGraph = (currentEnvironment ? currentEnvironment->getCompositionDependencyGraph() :
nullptr);
3284 findDependentModulesAndSources(actualModuleKeysAdded, searchDependencyGraphs, currentEnvironmentDependencyGraph,
3285 modulesDepOnModulesAdded, modulesDepOnModulesAdded_otherCompiler,
3286 sourcesDepOnModulesAdded, sourcesDepOnModulesAdded_otherCompiler);
3288 set<Environment *> otherEnvironments;
3289 for (map<Environment *, set<string> >::iterator i = sourcesDepOnModulesAdded_otherCompiler.begin(); i != sourcesDepOnModulesAdded_otherCompiler.end(); ++i)
3290 otherEnvironments.insert(i->first);
3292 for (Environment *env : otherEnvironments)
3294 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
3295 string moduleSearchPath = env->getModuleSearchPaths().front();
3298 dispatch_sync(environmentQueue, ^{
3299 otherCompiler->loadModulesAndSources(set<string>(), set<string>(), set<string>(),
3300 sourcesDepOnModulesAdded_otherCompiler[env], set<string>(), set<string>(),
3301 false,
true, env,
nullptr,
nullptr,
"");
3304 delete otherCompiler;
3309 set<dispatch_group_t> sourcesLoading;
3310 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i)
3312 Environment *env = (*i).at(0);
3314 set<string> sourcesToCompile;
3315 sourcesToCompile.insert(sourcesAdded[env].begin(), sourcesAdded[env].end());
3316 sourcesToCompile.insert(sourcesModified[env].begin(), sourcesModified[env].end());
3318 if (sourcesToCompile.size() == 0)
3321 set<dispatch_group_t> s = env->compileModulesFromSourceCode(sourcesToCompile, shouldRecompileSourcesIfUnchanged);
3322 sourcesLoading.insert(s.begin(), s.end());
3325 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i)
3327 Environment *env = (*i).at(0);
3329 set<string> sourcesToCompile;
3330 sourcesToCompile.insert(sourcesDepOnModulesAdded[env].begin(), sourcesDepOnModulesAdded[env].end());
3331 sourcesToCompile.insert(sourcesDepOnModulesModified[env].begin(), sourcesDepOnModulesModified[env].end());
3333 if (sourcesToCompile.size() == 0)
3336 env->compileModulesFromSourceCode(sourcesToCompile,
true);
3341 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i)
3343 Environment *env = (*i).at(0);
3344 env->notifyCompilers(actualModulesAdded[env], actualModulesModified[env], actualModulesRemoved[env], issues[env]);
3351 for (
const dispatch_group_t &group : sourcesLoading)
3352 dispatch_retain(group);
3354 set<dispatch_group_t> loadingGroups;
3355 loadingGroups.insert(specializedModulesLoading.begin(), specializedModulesLoading.end());
3356 loadingGroups.insert(sourcesLoading.begin(), sourcesLoading.end());
3357 return loadingGroups;
3371 void VuoCompiler::findDependentModulesAndSources(map<Environment *, set<string> > &changedModules,
3372 const vector<VuoDirectedAcyclicNetwork *> &searchDependencyGraphs,
3374 map<Environment *, set<string> > &modulesDepOnChangedModules_this,
3375 map<Environment *, set<string> > &modulesDepOnChangedModules_other,
3376 map<Environment *, set<string> > &sourcesDepOnChangedModules_this,
3377 map<Environment *, set<string> > &sourcesDepOnChangedModules_other)
3379 for (
const vector<Environment *> &envs : environments)
3381 Environment *env = envs.at(0);
3383 for (
const string &module : changedModules[env])
3385 set<VuoDirectedAcyclicGraph::Vertex *> dependents;
3388 vector<VuoDirectedAcyclicGraph::Vertex *> moduleVertices;
3389 if (currentEnvironmentDependencyGraph)
3395 moduleVertices.push_back(mv);
3398 moduleVertices = searchDependencyGraph->findVertex(module);
3402 DependencyGraphVertex *moduleVertex = static_cast<DependencyGraphVertex *>(moduleVertexRaw);
3403 if (moduleVertex->getEnvironment())
3405 vector<VuoDirectedAcyclicGraph::Vertex *> upstream = searchDependencyGraph->getUpstreamVertices(moduleVertex);
3406 dependents.insert(upstream.begin(), upstream.end());
3413 DependencyGraphVertex *v = static_cast<DependencyGraphVertex *>(dependentVertexRaw);
3414 Environment *dependentEnv = v->getEnvironment();
3418 string dependent = v->getDependency();
3422 if (changedModules[dependentEnv].find(dependent) != changedModules[dependentEnv].end())
3425 ModuleInfo *foundSourceInfo = dependentEnv->listSourceFile(dependent);
3426 ModuleInfo *foundModuleInfo = dependentEnv->listModule(dependent);
3428 bool belongsToCurrentCompiler =
false;
3429 for (
const vector<Environment *> &envs2 : environments)
3431 if (find(envs2.begin(), envs2.end(), dependentEnv) != envs2.end())
3433 belongsToCurrentCompiler =
true;
3438 map<Environment *, set<string> > *whicheverDependents =
nullptr;
3439 ModuleInfo *moduleInfo =
nullptr;
3440 if (foundSourceInfo)
3442 moduleInfo = foundSourceInfo;
3443 whicheverDependents = (belongsToCurrentCompiler ? &sourcesDepOnChangedModules_this : &sourcesDepOnChangedModules_other);
3445 else if (foundModuleInfo)
3447 moduleInfo = foundModuleInfo;
3448 whicheverDependents = (belongsToCurrentCompiler ? &modulesDepOnChangedModules_this : &modulesDepOnChangedModules_other);
3451 (*whicheverDependents)[dependentEnv].insert(dependent);
3452 moduleInfo->setAttempted(
false);
3461 void VuoCompiler::loadedModules(map<string, VuoCompilerModule *> modulesAdded,
3462 map<
string, pair<VuoCompilerModule *, VuoCompilerModule *> > modulesModified,
3463 map<string, VuoCompilerModule *> modulesRemoved,
3464 VuoCompilerIssues *issues,
void *delegateDataV, Environment *currentEnvironment)
3476 auto findVersionsOfModule = [
this, currentEnvironment] (
const string &moduleKey)
3478 vector< pair<Environment *, VuoCompilerModule *> > moduleVersions;
3479 for (
const vector<Environment *> &envs : environments)
3481 Environment *env = envs.at(0);
3483 if (module || env == currentEnvironment)
3484 moduleVersions.push_back( make_pair(env, module) );
3486 return moduleVersions;
3489 for (map<string, VuoCompilerModule *>::iterator i = modulesAdded.begin(); i != modulesAdded.end(); )
3491 string moduleKey = i->first;
3494 vector< pair<Environment *, VuoCompilerModule *> > moduleVersions = findVersionsOfModule(moduleKey);
3496 if (moduleVersions.size() > 1)
3498 modulesAdded.erase(i++);
3500 if (moduleVersions.back().second == moduleAdded)
3502 VuoCompilerModule *moduleSuperseded = moduleVersions.at(moduleVersions.size()-2).second;
3503 modulesModified[moduleKey] = make_pair(moduleSuperseded, moduleAdded);
3510 for (map<
string, pair<VuoCompilerModule *, VuoCompilerModule *> >::iterator i = modulesModified.begin(); i != modulesModified.end(); )
3512 string moduleKey = i->first;
3515 vector< pair<Environment *, VuoCompilerModule *> > moduleVersions = findVersionsOfModule(moduleKey);
3517 if (moduleVersions.size() > 1 && moduleVersions.back().second != moduleModified)
3518 modulesModified.erase(i++);
3523 for (map<string, VuoCompilerModule *>::iterator i = modulesRemoved.begin(); i != modulesRemoved.end(); )
3525 string moduleKey = i->first;
3528 vector< pair<Environment *, VuoCompilerModule *> > moduleVersions = findVersionsOfModule(moduleKey);
3530 if (moduleVersions.size() > 1)
3532 modulesRemoved.erase(i++);
3534 if (moduleVersions.back().first == currentEnvironment)
3536 VuoCompilerModule *moduleUnsuperseded = moduleVersions.at(moduleVersions.size()-2).second;
3537 modulesModified[moduleKey] = make_pair(moduleRemoved, moduleUnsuperseded);
3544 dispatch_async(delegateQueue, ^{
3545 VuoCompilerDelegate::LoadedModulesData *delegateData = static_cast<VuoCompilerDelegate::LoadedModulesData *>(delegateDataV);
3547 if (delegate && ! (modulesAdded.empty() && modulesModified.empty() && modulesRemoved.empty() && issues->
isEmpty()))
3549 delegate->enqueueData(delegateData);
3550 delegate->
loadedModules(modulesAdded, modulesModified, modulesRemoved, issues);
3554 delegateData->release();
3564 void VuoCompiler::loadNodeClassGeneratedAtRuntime(
VuoCompilerNodeClass *nodeClass, Environment *env)
3566 Module *module = nodeClass->
getModule();
3570 setTargetForModule(nodeClass->
getModule());
3574 dispatch_sync(environmentQueue, ^{
3575 env->replaceNodeClass(nodeClass);
3578 __block map<string, VuoCompilerType *> inheritedTypes;
3579 void (^envReifyPortTypes)(Environment *) = ^
void (Environment *env) {
3580 env->reifyPortTypes(inheritedTypes);
3581 map<string, VuoCompilerType *> currentTypes = env->getTypes();
3582 inheritedTypes.insert(currentTypes.begin(), currentTypes.end());
3584 applyToAllEnvironments(envReifyPortTypes);
3593 reifyGenericPortTypes(node->getBase());
3601 void VuoCompiler::reifyGenericPortTypes(
VuoNode *node)
3611 vector<VuoPort *> ports;
3612 ports.insert(ports.end(), inputPorts.begin(), inputPorts.end());
3613 ports.insert(ports.end(), outputPorts.begin(), outputPorts.end());
3615 for (vector<VuoPort *>::iterator j = ports.begin(); j != ports.end(); ++j)
3617 VuoCompilerPort *port = static_cast<VuoCompilerPort *>((*j)->getCompiler());
3622 if (! genericType->hasCompiler())
3662 vector<string> allIncludePaths = includePaths;
3663 string preprocessedInputPath = inputPath;
3665 string tmpPreprocessedInputDir;
3666 string dir, file, ext;
3671 string preprocessedInputContents = inputContents;
3673 if (inputContents != preprocessedInputContents)
3676 allIncludePaths.push_back(dir.empty() ?
"." : dir);
3678 preprocessedInputPath = tmpPreprocessedInputDir +
"/" + file +
"." + ext;
3683 else if (ext ==
"fs")
3689 auto getType = [
this] (
const string &moduleKey) {
return this->
getType(moduleKey); };
3694 writeModuleToBitcode(module, outputPath);
3704 vector<string> extraArgs;
3705 for (vector<string>::iterator i = allIncludePaths.begin(); i != allIncludePaths.end(); ++i)
3707 extraArgs.push_back(
"-I");
3708 extraArgs.push_back(*i);
3711 string macosxSdkFolder =
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/";
3714 extraArgs.push_back(
"-isysroot");
3715 extraArgs.push_back(macosxSdkFolder +
"MacOSX10.10.sdk");
3718 __block vector<string> headerSearchPaths;
3719 void (^envGetHeaderSearchPaths)(Environment *) = ^
void (Environment *env) {
3720 vector<string> result = env->getHeaderSearchPaths();
3721 headerSearchPaths.insert(headerSearchPaths.end(), result.begin(), result.end());
3723 applyToInstalledEnvironments(envGetHeaderSearchPaths);
3725 __block Module *module;
3727 module = readModuleFromC(preprocessedInputPath, headerSearchPaths, extraArgs);
3730 if (! tmpPreprocessedInputDir.empty())
3731 remove(tmpPreprocessedInputDir.c_str());
3735 "",
"%moduleKey couldn't be compiled as a node class, type, or library. Check the macOS Console for details.");
3742 if (! compilerModule)
3744 VUserLog(
"Error: Didn't recognize '%s' as a node class, type, or library.", inputPath.c_str());
3748 setTargetForModule(module, target);
3749 writeModuleToBitcode(module, outputPath);
3758 Module * VuoCompiler::compileCompositionToModule(
VuoCompilerComposition *composition,
const string &moduleKey,
bool isTopLevelComposition,
3761 composition->
check(issues);
3763 reifyGenericPortTypes(composition);
3766 isTopLevelComposition,
3768 if (telemetry ==
"console")
3771 __block Module *module;
3774 setTargetForModule(module, target);
3796 Module *module = compileCompositionToModule(composition, moduleKey, isTopLevelComposition, issues);
3799 writeModuleToBitcode(module, outputPath);
3816 VDebugLog(
"Compiling '%s'…", inputPath.c_str());
3823 "",
"The composition file couldn't be read or was empty.");
3859 delete baseComposition;
3865 void VuoCompiler::compileSubcompositionString(
const string &compositionString,
const string &outputPath,
3866 std::function<
void(
void)> moduleLoadedCallback, Environment *environment,
3872 bool compilationSucceeded =
false;
3876 compilationSucceeded =
true;
3884 if (! compilationSucceeded)
3890 string outputDir, file, ext;
3894 environment->moduleSearchPathContentsChanged(outputDir, outputPath, compositionString, moduleLoadedCallback,
this, issues);
3912 linkCompositionToCreateExecutableOrDynamicLibrary(inputPath, outputPath, optimization,
false, rPath);
3933 linkCompositionToCreateExecutableOrDynamicLibrary(inputPath, outputPath, optimization,
true);
3950 void VuoCompiler::linkCompositionToCreateExecutableOrDynamicLibrary(
string compiledCompositionPath,
string linkedCompositionPath,
3951 Optimization optimization,
bool isDylib,
string rPath)
3957 shouldLoadAllModules =
false;
3959 set<string> dependencies = getDependenciesForComposition(compiledCompositionPath);
3960 dependencies.insert(getRuntimeDependency());
3962 dependencies.insert(getRuntimeMainDependency());
3964 set<Module *> modules;
3965 set<string> libraries;
3966 set<string> frameworks;
3967 getLinkerInputs(dependencies, optimization, modules, libraries, frameworks);
3969 libraries.insert(compiledCompositionPath);
3971 link(linkedCompositionPath, modules, libraries, frameworks, isDylib, rPath);
3997 set<string> allDependencies = getDependenciesForComposition(compiledCompositionPath);
3998 set<string> addedDependencies;
3999 std::set_difference(allDependencies.begin(), allDependencies.end(),
4000 carriedOverDependencies.begin(), carriedOverDependencies.end(),
4001 std::inserter(addedDependencies, addedDependencies.end()));
4007 set<string> carriedOverExternalLibraries = runningCompositionLibraries->
getExternalLibraries();
4012 string nonUnloadableResourcePath;
4013 string unloadableResourcePath;
4014 set<string> nonUnloadableDependencies;
4015 set<string> unloadableDependencies;
4016 map<string, set<string> > builtInCacheDependencies;
4017 map<string, set<string> > userCacheDependencies;
4018 set<string> builtInLibraries;
4019 set<string> userLibraries;
4020 set<string> addedExternalLibraries;
4021 set<string> addedFrameworks;
4022 set<string> allFrameworks;
4023 if (! addedDependencies.empty())
4025 set<string> builtInModuleAndLibraryDependencies;
4026 set<string> userModuleAndLibraryDependencies;
4027 set<Module *> builtInModules;
4028 set<Module *> userModules;
4031 builtInModuleAndLibraryDependencies, userModuleAndLibraryDependencies, builtInCacheDependencies, userCacheDependencies,
4032 builtInModules, userModules, builtInLibraries, userLibraries, addedExternalLibraries, addedFrameworks);
4034 allFrameworks.insert(carriedOverFrameworks.begin(), carriedOverFrameworks.end());
4035 allFrameworks.insert(addedFrameworks.begin(), addedFrameworks.end());
4037 string dir, linkedCompositionFile, ext;
4040 if (! builtInModules.empty() || builtInLibraries.size() > builtInCacheDependencies.size())
4043 nonUnloadableDependencies = builtInModuleAndLibraryDependencies;
4045 set<string> librariesForNonUnloadableResource = builtInLibraries;
4046 librariesForNonUnloadableResource.insert(carriedOverNonUnloadableLibraries.begin(), carriedOverNonUnloadableLibraries.end());
4047 librariesForNonUnloadableResource.insert(carriedOverExternalLibraries.begin(), carriedOverExternalLibraries.end());
4048 librariesForNonUnloadableResource.insert(addedExternalLibraries.begin(), addedExternalLibraries.end());
4050 link(nonUnloadableResourcePath, builtInModules, librariesForNonUnloadableResource, allFrameworks,
true);\
4052 for (set<string>::iterator i = builtInLibraries.begin(); i != builtInLibraries.end(); )
4055 builtInLibraries.erase(i++);
4060 for (set<string>::iterator i = addedExternalLibraries.begin(); i != addedExternalLibraries.end(); )
4063 addedExternalLibraries.erase(i++);
4069 if (! userModules.empty() || userLibraries.size() > userCacheDependencies.size())
4072 unloadableDependencies = userModuleAndLibraryDependencies;
4074 set<string> librariesForUnloadableResource = userLibraries;
4075 librariesForUnloadableResource.insert(builtInLibraries.begin(), builtInLibraries.end());
4076 librariesForUnloadableResource.insert(carriedOverUnloadableLibraries.begin(), carriedOverUnloadableLibraries.end());
4077 librariesForUnloadableResource.insert(carriedOverNonUnloadableLibraries.begin(), carriedOverNonUnloadableLibraries.end());
4078 librariesForUnloadableResource.insert(carriedOverExternalLibraries.begin(), carriedOverExternalLibraries.end());
4079 librariesForUnloadableResource.insert(addedExternalLibraries.begin(), addedExternalLibraries.end());
4080 if (! nonUnloadableResourcePath.empty())
4081 librariesForUnloadableResource.insert(nonUnloadableResourcePath);
4083 link(unloadableResourcePath, userModules, librariesForUnloadableResource, allFrameworks,
true);
4085 for (set<string>::iterator i = userLibraries.begin(); i != userLibraries.end(); )
4088 userLibraries.erase(i++);
4097 set<string> vuoRuntimePaths;
4099 set<Module *> modules;
4100 set<string> libraries;
4101 set<string> frameworks;
4103 set<string> dependencies;
4104 dependencies.insert(getRuntimeDependency());
4106 vuoRuntimePaths = libraries;
4112 set<Module *> modules;
4113 set<string> libraries;
4115 libraries.insert(compiledCompositionPath);
4116 libraries.insert(carriedOverExternalLibraries.begin(), carriedOverExternalLibraries.end());
4117 libraries.insert(addedExternalLibraries.begin(), addedExternalLibraries.end());
4118 libraries.insert(carriedOverNonUnloadableLibraries.begin(), carriedOverNonUnloadableLibraries.end());
4119 libraries.insert(carriedOverUnloadableLibraries.begin(), carriedOverUnloadableLibraries.end());
4120 libraries.insert(builtInLibraries.begin(), builtInLibraries.end());
4121 libraries.insert(userLibraries.begin(), userLibraries.end());
4122 if (! nonUnloadableResourcePath.empty())
4123 libraries.insert(nonUnloadableResourcePath);
4124 if (! unloadableResourcePath.empty())
4125 libraries.insert(unloadableResourcePath);
4126 libraries.insert(vuoRuntimePaths.begin(), vuoRuntimePaths.end());
4127 link(linkedCompositionPath, modules, libraries, allFrameworks,
true);
4132 if (! nonUnloadableResourcePath.empty())
4135 if (! unloadableResourcePath.empty())
4138 for (map<
string, set<string> >::iterator i = builtInCacheDependencies.begin(); i != builtInCacheDependencies.end(); ++i)
4141 for (map<
string, set<string> >::iterator i = userCacheDependencies.begin(); i != userCacheDependencies.end(); ++i)
4154 set<string> VuoCompiler::getDependenciesForComposition(
const string &compiledCompositionPath)
4156 VDebugLog(
"Gathering dependencies for '%s'…", compiledCompositionPath.c_str());
4159 __block set<string> directDependencies;
4161 Module *module = readModuleFromBitcode(compiledCompositionPath);
4165 delete compilerModule;
4171 auto deps = getDependenciesForComposition(directDependencies,
true);
4190 set<string> directDependencies;
4199 vector<VuoPublishedPort *> publishedPorts;
4200 publishedPorts.insert(publishedPorts.end(), publishedInputPorts.begin(), publishedInputPorts.end());
4201 publishedPorts.insert(publishedPorts.end(), publishedOutputPorts.begin(), publishedOutputPorts.end());
4204 if (publishedPort->getClass()->hasCompiler())
4206 VuoType *portType = static_cast<VuoCompilerPortClass *>( publishedPort->getClass()->getCompiler() )->getDataVuoType();
4210 VuoGenericType *genericType = dynamic_cast<VuoGenericType *>(portType);
4220 directDependencies.insert(dependency);
4225 return directDependencies;
4237 return getDependenciesForComposition(directDependencies,
false);
4247 __block vector<string> librarySearchPaths;
4248 applyToInstalledEnvironments(^
void (Environment *env) {
4249 vector<string> result = env->getLibrarySearchPaths();
4250 librarySearchPaths.insert(librarySearchPaths.end(), result.begin(), result.end());
4253 set<string> dylibDeps;
4254 for (
string dep : getDependenciesForComposition(composition))
4256 string path = getLibraryPath(dep, librarySearchPaths);
4258 dylibDeps.insert(path);
4277 set<string> VuoCompiler::getDependenciesForComposition(
const set<string> &directDependencies,
bool checkCompatibility)
4280 for (set<string>::const_iterator i = directDependencies.begin(); i != directDependencies.end(); ++i)
4283 set<string> dependencies;
4284 for (set<string>::iterator i = directDependencies.begin(); i != directDependencies.end(); ++i)
4286 string moduleKey = *i;
4289 vector<VuoDirectedAcyclicGraph::Vertex *> firstPassVertices = dependencyGraph->
findVertex(moduleKey);
4290 set<VuoDirectedAcyclicGraph::Vertex *> firstPassDependencies(firstPassVertices.begin(), firstPassVertices.end());
4291 for (vector<VuoDirectedAcyclicGraph::Vertex *>::iterator j = firstPassVertices.begin(); j != firstPassVertices.end(); ++j)
4294 firstPassDependencies.insert(downstream.begin(), downstream.end());
4298 for (set<VuoDirectedAcyclicGraph::Vertex *>::iterator j = firstPassDependencies.begin(); j != firstPassDependencies.end(); ++j)
4300 DependencyGraphVertex *v = static_cast<DependencyGraphVertex *>(*j);
4305 vector<VuoDirectedAcyclicGraph::Vertex *> moduleVertices = dependencyGraph->
findVertex(moduleKey);
4306 set<VuoDirectedAcyclicGraph::Vertex *> moduleDependencies(moduleVertices.begin(), moduleVertices.end());
4307 for (vector<VuoDirectedAcyclicGraph::Vertex *>::iterator j = moduleVertices.begin(); j != moduleVertices.end(); ++j)
4310 moduleDependencies.insert(downstream.begin(), downstream.end());
4314 set<string> dependenciesToAdd;
4315 set<string> incompatibleDependencies;
4316 for (set<VuoDirectedAcyclicGraph::Vertex *>::iterator j = moduleDependencies.begin(); j != moduleDependencies.end(); ++j)
4318 DependencyGraphVertex *v = static_cast<DependencyGraphVertex *>(*j);
4319 if (! checkCompatibility || ! v->getEnvironment() || v->isCompatible())
4320 dependenciesToAdd.insert(v->getDependency());
4322 incompatibleDependencies.insert(v->getDependency());
4325 if (! checkCompatibility || incompatibleDependencies.empty())
4327 dependencies.insert(dependenciesToAdd.begin(), dependenciesToAdd.end());
4334 string dependencyTargetString;
4339 for (set<string>::iterator i = incompatibleDependencies.begin(); i != incompatibleDependencies.end(); ++i)
4348 dependencyTargetString = dependencyTargets.
toString();
4351 dependencyTargetString =
"(unknown operating systems)";
4353 string modulePlaceholder = (module ?
"%module" :
"%moduleKey");
4355 "Node incompatible with operating system",
4356 modulePlaceholder +
" is only compatible with " + dependencyTargetString +
4357 ", so this composition can't run on your macOS version (" + compositionTargets.
toString() +
").");
4359 issue.setModuleKey(moduleKey);
4365 vector<string> coreDependencies = getCoreVuoDependencies();
4366 dependencies.insert(coreDependencies.begin(), coreDependencies.end());
4368 return dependencies;
4375 void VuoCompiler::getLinkerInputs(
const set<string> &dependencies, Optimization optimization,
4376 set<Module *> &modules, set<string> &libraries, set<string> &frameworks)
4378 set<string> builtInModuleAndLibraryDependencies;
4379 set<string> userModuleAndLibraryDependencies;
4380 map<string, set<string> > builtInCacheDependencies;
4381 map<string, set<string> > userCacheDependencies;
4382 set<Module *> builtInModules;
4383 set<Module *> userModules;
4384 set<string> builtInLibraries;
4385 set<string> userLibraries;
4386 set<string> externalLibraries;
4388 getLinkerInputs(dependencies, optimization,
4389 builtInModuleAndLibraryDependencies, userModuleAndLibraryDependencies, builtInCacheDependencies, userCacheDependencies,
4390 builtInModules, userModules, builtInLibraries, userLibraries, externalLibraries, frameworks);
4392 modules.insert(builtInModules.begin(), builtInModules.end());
4393 modules.insert(userModules.begin(), userModules.end());
4394 libraries.insert(builtInLibraries.begin(), builtInLibraries.end());
4395 libraries.insert(userLibraries.begin(), userLibraries.end());
4396 libraries.insert(externalLibraries.begin(), externalLibraries.end());
4412 void VuoCompiler::getLinkerInputs(
const set<string> &dependencies, Optimization optimization,
4413 set<string> &builtInModuleAndLibraryDependencies, set<string> &userModuleAndLibraryDependencies,
4414 map<
string, set<string> > &builtInCacheDependencies, map<
string, set<string> > &userCacheDependencies,
4415 set<Module *> &builtInModules, set<Module *> &userModules,
4416 set<string> &builtInLibraries, set<string> &userLibraries,
4417 set<string> &externalLibraries, set<string> &externalFrameworks)
4420 if (shouldUseModuleCache)
4423 __block vector<string> librarySearchPaths;
4424 void (^envGetLibrarySearchPaths)(Environment *) = ^
void (Environment *env) {
4425 vector<string> result = env->getLibrarySearchPaths();
4426 librarySearchPaths.insert(librarySearchPaths.end(), result.begin(), result.end());
4428 applyToInstalledEnvironments(envGetLibrarySearchPaths);
4430 for (set<string>::iterator i = dependencies.begin(); i != dependencies.end(); ++i)
4432 string dependency = *i;
4434 bool foundInCache =
false;
4435 string moduleCachePath;
4436 bool isInBuiltInModuleCache =
false;
4437 if (shouldUseModuleCache)
4438 foundInCache = findInModuleCache(dependency, moduleCachePath, isInBuiltInModuleCache);
4442 if (isInBuiltInModuleCache)
4444 builtInLibraries.insert(moduleCachePath);
4445 builtInCacheDependencies[moduleCachePath].insert(dependency);
4449 userLibraries.insert(moduleCachePath);
4450 userCacheDependencies[moduleCachePath].insert(dependency);
4456 void (^envFindModule)(Environment *) = ^
void (Environment *env) {
4461 applyToAllEnvironments(envFindModule);
4465 if (module->getModule())
4467 string modulePath = module->getModulePath();
4468 if (! modulePath.empty() && dynamic_cast<VuoCompilerType *>(module))
4470 if (module->isBuiltIn())
4471 builtInLibraries.insert(modulePath);
4473 userLibraries.insert(modulePath);
4477 if (module->isBuiltIn())
4478 builtInModules.insert(module->getModule());
4480 userModules.insert(module->getModule());
4483 if (module->isBuiltIn())
4484 builtInModuleAndLibraryDependencies.insert(dependency);
4486 userModuleAndLibraryDependencies.insert(dependency);
4492 externalFrameworks.insert(dependency);
4495 string dependencyPath = getLibraryPath(dependency, librarySearchPaths);
4496 if (! dependencyPath.empty())
4497 externalLibraries.insert(dependencyPath);
4499 VUserLog(
"Warning: Could not locate dependency '%s'.", dependency.c_str());
4511 string VuoCompiler::getLibraryPath(
const string &dependency, vector<string> librarySearchPaths)
4518 if (dependency !=
"crypto"
4519 && dependency !=
"ssl")
4520 librarySearchPaths.push_back(
"/usr/lib");
4522 for (
auto &path : librarySearchPaths)
4524 vector<string> variations;
4525 variations.push_back(path +
"/" + dependency);
4526 variations.push_back(path +
"/lib" + dependency);
4527 variations.push_back(path +
"/lib" + dependency +
".dylib");
4528 variations.push_back(path +
"/lib" + dependency +
".a");
4529 for (
auto &variation : variations)
4544 void VuoCompiler::useModuleCache(
bool shouldUseExistingBuiltInCaches,
bool shouldUseExistingOtherCaches)
4546 loadModulesIfNeeded();
4550 dispatch_sync(environmentQueue, ^{
4551 set<string> dylibsForCachesOfInstalledModules;
4552 set<string> frameworksForCachesOfInstalledModules;
4553 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i)
4555 bool builtin = (i == environments.begin());
4556 set<string> dylibsForCacheOfGeneratedModules;
4557 set<string> frameworksForCacheOfGeneratedModules;
4559 for (
int j = i->size() - 1; j >= 0; --j)
4561 Environment *env = i->at(j);
4562 bool installed = (j == 0);
4564 set<string> cacheableModulesAndDependencies;
4565 set<string> dylibsNeededToLinkToThisCache;
4566 set<string> frameworksNeededToLinkToThisCache;
4567 env->getCacheableModulesAndDependencies(builtin, installed, cacheableModulesAndDependencies,
4568 dylibsNeededToLinkToThisCache, frameworksNeededToLinkToThisCache);
4570 set<string> accumulatedDylibs;
4571 accumulatedDylibs.insert(dylibsNeededToLinkToThisCache.begin(), dylibsNeededToLinkToThisCache.end());
4572 accumulatedDylibs.insert(dylibsForCachesOfInstalledModules.begin(), dylibsForCachesOfInstalledModules.end());
4573 accumulatedDylibs.insert(dylibsForCacheOfGeneratedModules.begin(), dylibsForCacheOfGeneratedModules.end());
4575 set<string> accumulatedFrameworks;
4576 accumulatedFrameworks.insert(frameworksNeededToLinkToThisCache.begin(), frameworksNeededToLinkToThisCache.end());
4577 accumulatedFrameworks.insert(frameworksForCachesOfInstalledModules.begin(), frameworksForCachesOfInstalledModules.end());
4578 accumulatedFrameworks.insert(frameworksForCacheOfGeneratedModules.begin(), frameworksForCacheOfGeneratedModules.end());
4580 bool shouldUseExistingCache = (builtin ? shouldUseExistingBuiltInCaches : shouldUseExistingOtherCaches);
4581 env->useModuleCache(shouldUseExistingCache,
this, cacheableModulesAndDependencies,
4582 accumulatedDylibs, accumulatedFrameworks);
4586 dylibsForCachesOfInstalledModules.insert(dylibsNeededToLinkToThisCache.begin(), dylibsNeededToLinkToThisCache.end());
4587 frameworksForCachesOfInstalledModules.insert(frameworksNeededToLinkToThisCache.begin(), frameworksNeededToLinkToThisCache.end());
4591 dylibsForCacheOfGeneratedModules.insert(dylibsNeededToLinkToThisCache.begin(), dylibsNeededToLinkToThisCache.end());
4592 frameworksForCacheOfGeneratedModules.insert(frameworksNeededToLinkToThisCache.begin(), frameworksNeededToLinkToThisCache.end());
4598 Environment::waitForModuleCachesToBuild();
4609 bool VuoCompiler::findInModuleCache(
const string &moduleOrDependency,
string &cachePath,
bool &isBuiltinCache)
4611 __block
bool found =
false;
4612 __block
string outPath;
4613 __block
bool outBuiltin;
4614 dispatch_sync(environmentQueue, ^{
4615 for (vector< vector<Environment *> >::iterator i = environments.begin(); i != environments.end(); ++i)
4617 bool builtin = (i == environments.begin());
4619 for (
int j = i->size() - 1; j >= 0; --j)
4621 Environment *env = i->at(j);
4624 bool resultFound = env->findInModuleCache(moduleOrDependency, resultPath);
4628 outPath = resultPath;
4629 outBuiltin = builtin;
4635 cachePath = outPath;
4636 isBuiltinCache = outBuiltin;
4649 dispatch_group_async(moduleCacheBuilding, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
4650 useModuleCache(
true,
false);
4668 vuoFrameworkInProgressPath = vuoFrameworkPath;
4671 compiler.useModuleCache(
false,
true);
4683 unsigned long maxSeconds = 30 * 24 * 60 * 60;
4686 for (set<VuoFileUtilities::File *>::iterator i = cacheDirs.begin(); i != cacheDirs.end(); ++i)
4688 string path = (*i)->path();
4690 string file = (*i)->basename();
4691 if (file !=
"Builtin" && file !=
"System" && file !=
"User")
4694 if (fileSeconds > maxSeconds)
4700 string pidAsString = file.substr(Environment::pidCacheDirPrefix.length());
4701 int pid = atoi(pidAsString.c_str());
4702 if (kill(pid, 0) != 0)
4716 dispatch_sync(modulesToLoadQueue, ^{
4717 this->shouldLoadAllModules = shouldLoadAllModules;
4732 void VuoCompiler::link(
string outputPath,
const set<Module *> &modules,
const set<string> &libraries,
const set<string> &frameworks,
bool isDylib,
string rPath)
4734 VDebugLog(
"Linking '%s'…", outputPath.c_str());
4742 Module *compositeModule =
new Module(
"composite", getGlobalContext());
4743 setTargetForModule(compositeModule);
4744 for (set<Module *>::const_iterator i = modules.begin(); i != modules.end(); ++i)
4747 if (Linker::LinkModules(compositeModule, *i, Linker::PreserveSource, &error))
4748 VUserLog(
"Error: Failed to link compositeModule: %s", error.c_str());
4750 writeModuleToBitcode(compositeModule, compositeModulePath);
4751 delete compositeModule;
4758 llvm::sys::Path clangPath = getClangPath();
4760 vector<const char *> args;
4761 vector<char *> argsToFree;
4762 args.push_back(clangPath.c_str());
4764 args.push_back(compositeModulePath.c_str());
4766 vector<string> coreDependencies = getCoreVuoDependencies();
4767 for (set<string>::const_iterator i = libraries.begin(); i != libraries.end(); ++i)
4769 string library = *i;
4771 for (vector<string>::iterator j = coreDependencies.begin(); j != coreDependencies.end(); ++j)
4773 string coreDependency = *j;
4775 args.push_back(
"-force_load");
4783 library = libraryObject;
4786 char *libraryZ = strdup(library.c_str());
4787 args.push_back(libraryZ);
4788 argsToFree.push_back(libraryZ);
4792 vector<string> frameworkArguments;
4794 __block vector<string> frameworkSearchPaths;
4795 void (^envGetFrameworkSearchPaths)(Environment *) = ^
void (Environment *env) {
4796 vector<string> result = env->getFrameworkSearchPaths();
4797 frameworkSearchPaths.insert(frameworkSearchPaths.end(), result.begin(), result.end());
4799 applyToInstalledEnvironments(envGetFrameworkSearchPaths);
4801 for (vector<string>::const_iterator i = frameworkSearchPaths.begin(); i != frameworkSearchPaths.end(); ++i)
4805 frameworkArguments.push_back(a);
4806 char *frameworkArgument = strdup(a.c_str());
4807 args.push_back(frameworkArgument);
4808 argsToFree.push_back(frameworkArgument);
4811 for (set<string>::const_iterator i = frameworks.begin(); i != frameworks.end(); ++i)
4813 args.push_back(
"-framework");
4815 string frameworkName = *i;
4816 frameworkName = frameworkName.substr(0, frameworkName.length() - string(
".framework").length());
4817 char *frameworkNameZ = strdup(frameworkName.c_str());
4818 args.push_back(frameworkNameZ);
4819 argsToFree.push_back(frameworkNameZ);
4823 llvm::sys::Path cRuntimePath;
4824 llvm::sys::Path crt1Path;
4825 string vuoFrameworkPath = getVuoFrameworkPath();
4826 string vuoFrameworkContainingFolder = vuoFrameworkPath +
"/..";
4827 if (! vuoFrameworkPath.empty())
4829 cRuntimePath = vuoFrameworkPath +
"/Modules/";
4830 crt1Path = cRuntimePath;
4831 crt1Path.appendComponent(
"crt1.o");
4836 if (!isDylib && crt1Path.canRead())
4838 args.push_back(
"-nostartfiles");
4839 args.push_back(crt1Path.c_str());
4843 args.push_back(
"-Xlinker");
4844 args.push_back(
"--no-demangle");
4847 args.push_back(
"-v");
4850 args.push_back(
"-dynamiclib");
4852 args.push_back(
"-Xlinker");
4853 args.push_back(
"-headerpad_max_install_names");
4857 args.push_back(
"-rpath");
4858 string rPathArg = (rPath.empty() ? vuoFrameworkContainingFolder : rPath);
4859 args.push_back(rPathArg.c_str());
4862 args.push_back(
"-rpath");
4863 args.push_back(LLVM_ROOT
"/lib");
4866 args.push_back(
"-std=c++11");
4867 args.push_back(
"-stdlib=libc++");
4870 clang::DiagnosticOptions *diagOptions =
new clang::DiagnosticOptions();
4871 clang::TextDiagnosticPrinter *diagClient =
new clang::TextDiagnosticPrinter(llvm::errs(), diagOptions);
4872 diagClient->setPrefix(clangPath.str());
4873 IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID(
new clang::DiagnosticIDs());
4874 clang::DiagnosticsEngine Diags(DiagID, diagOptions, diagClient);
4876 clang::driver::Driver TheDriver(args[0],
"x86_64-apple-macosx10.10.0", outputPath, Diags);
4878 TheDriver.CCCIsCXX =
true;
4883 for (vector<const char *>::iterator i = args.begin(); i != args.end(); ++i)
4888 OwningPtr<clang::driver::Compilation> C(TheDriver.BuildCompilation(args));
4893 SmallVector<std::pair<int, const clang::driver::Command *>, 4> FailingCommands;
4895 Res = TheDriver.ExecuteCompilation(*C, FailingCommands);
4898 for (
auto i : argsToFree)
4902 remove(compositeModulePath.c_str());
4908 chmod(outputPath.c_str(), 0755);
4912 __block vector<string> thirdPartyNodeClasses;
4913 dispatch_sync(environmentQueue, ^{
4914 for (
size_t i = 1; i < environments.size(); ++i)
4916 map<string, VuoCompilerNodeClass *> envNodeClasses = environments[i].at(0)->getNodeClasses();
4917 for (map<string, VuoCompilerNodeClass *>::iterator j = envNodeClasses.begin(); j != envNodeClasses.end(); ++j)
4918 thirdPartyNodeClasses.push_back(j->first);
4922 string details =
"One or more nodes in this composition can't be used by this version of Vuo. ";
4923 if (! thirdPartyNodeClasses.empty())
4925 details +=
"Make sure you're using the latest version of all the extra Vuo nodes you've installed:\n";
4926 sort(thirdPartyNodeClasses.begin(), thirdPartyNodeClasses.end());
4927 for (vector<string>::iterator i = thirdPartyNodeClasses.begin(); i != thirdPartyNodeClasses.end(); ++i)
4928 details +=
" • " + *i +
"\n";
4930 details +=
"Check the macOS Console for more information about the problem.";
4933 "Node broken or outdated", details);
4944 Module * VuoCompiler::readModuleFromC(
string inputPath,
const vector<string> &headerSearchPaths,
const vector<string> &extraArgs)
4948 vector<const char *> args;
4949 args.push_back(inputPath.c_str());
4950 args.push_back(
"-DVUO_COMPILER");
4951 args.push_back(
"-fblocks");
4954 args.push_back(
"-Wall");
4955 args.push_back(
"-Wextra");
4956 args.push_back(
"-Wimplicit-fallthrough");
4957 args.push_back(
"-Wno-unused-parameter");
4958 args.push_back(
"-Wno-c++11-extensions");
4959 args.push_back(
"-Wno-sign-compare");
4963 args.push_back(
"-std=c++11");
4964 args.push_back(
"-stdlib=libc++");
4967 for (vector<string>::const_iterator i = headerSearchPaths.begin(); i != headerSearchPaths.end(); ++i)
4969 args.push_back(
"-I");
4970 args.push_back(i->c_str());
4974 args.push_back(
"-v");
4976 for (vector<string>::const_iterator i = extraArgs.begin(); i != extraArgs.end(); ++i)
4977 args.push_back(i->c_str());
4979 clang::DiagnosticOptions * diagOptions =
new clang::DiagnosticOptions();
4980 IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID(
new clang::DiagnosticIDs());
4981 clang::DiagnosticsEngine Diags(DiagID, diagOptions);
4983 OwningPtr<clang::CompilerInvocation> CI(
new clang::CompilerInvocation);
4984 clang::CompilerInvocation::CreateFromArgs(*CI, &args[0], &args[0] + args.size(), Diags);
4986 clang::CompilerInstance Clang;
4987 Clang.setInvocation(CI.take());
4989 Clang.createDiagnostics();
4990 if (!Clang.hasDiagnostics())
4994 llvm::sys::Path builtinHeaderSearchPath;
4995 string vuoFrameworkPath = getVuoFrameworkPath();
4996 if (vuoFrameworkPath.empty())
4998 llvm::sys::Path clangPath = getClangPath();
4999 builtinHeaderSearchPath = clangPath;
5000 builtinHeaderSearchPath.eraseComponent();
5001 builtinHeaderSearchPath.eraseComponent();
5002 builtinHeaderSearchPath.appendComponent(
"lib");
5003 builtinHeaderSearchPath.appendComponent(
"clang");
5004 builtinHeaderSearchPath.appendComponent(CLANG_VERSION_STRING);
5008 builtinHeaderSearchPath = vuoFrameworkPath;
5009 builtinHeaderSearchPath.appendComponent(
"Frameworks");
5010 builtinHeaderSearchPath.appendComponent(
"llvm.framework");
5011 builtinHeaderSearchPath.appendComponent(
"Versions");
5012 builtinHeaderSearchPath.appendComponent(
"A");
5013 builtinHeaderSearchPath.appendComponent(
"lib");
5014 builtinHeaderSearchPath.appendComponent(
"clang");
5015 builtinHeaderSearchPath.appendComponent(CLANG_VERSION_STRING);
5017 Clang.getHeaderSearchOpts().ResourceDir = builtinHeaderSearchPath.str();
5020 clang::CodeGenAction *Act =
new clang::EmitLLVMOnlyAction();
5021 if (!Clang.ExecuteAction(*Act))
5024 return Act->takeModule();
5032 Module * VuoCompiler::readModuleFromBitcode(
string inputPath)
5034 string dir, file, ext;
5037 return readModuleFromBitcode(&inputFile);
5049 size_t inputDataBytes;
5054 Module *module = readModuleFromBitcodeData(inputData, inputDataBytes, error);
5069 Module * VuoCompiler::readModuleFromBitcodeData(
char *inputData,
size_t inputDataBytes,
string &error)
5071 __block Module *module;
5073 StringRef inputDataAsStringRef(inputData, inputDataBytes);
5074 MemoryBuffer *mb = MemoryBuffer::getMemBuffer(inputDataAsStringRef,
"",
false);
5075 module = ParseBitcodeFile(&(*mb), getGlobalContext(), &error);
5088 bool VuoCompiler::writeModuleToBitcode(Module *module,
string outputPath)
5090 if (verifyModule(*module, PrintMessageAction))
5092 VUserLog(
"Error: Module verification failed.");
5097 raw_fd_ostream out(outputPath.c_str(), err);
5100 VUserLog(
"Error: Couldn't open file '%s' for writing: %s", outputPath.c_str(), err.c_str());
5103 WriteBitcodeToFile(module, out);
5113 void VuoCompiler::setTargetForModule(Module *module,
string target)
5140 module->setTargetTriple(
"x86_64-apple-macosx10.10.0");
5151 Module *llvmModule = module->
getModule();
5164 baseNodeClass = dynamic_cast<VuoNodeClass *>(nodeClass->
getBase());
5167 if (dynamic_cast<VuoCompilerSpecializedNodeClass *>(module))
5174 baseType = dynamic_cast<VuoType *>(type->
getBase());
5180 delete baseNodeClass;
5213 return nodeClassForNode->
newNode(title, x, y);
5222 return createPublishedNode(nodeClassName, publishedInputPorts);
5231 return createPublishedNode(nodeClassName, publishedOutputPorts);
5237 VuoNode * VuoCompiler::createPublishedNode(
const string &nodeClassName,
const vector<VuoPublishedPort *> &publishedPorts)
5245 for (
size_t i = 0; i < publishedPorts.size(); ++i)
5247 VuoType *publishedPortType = static_cast<VuoCompilerPort *>(publishedPorts[i]->getCompiler())->getDataVuoType();
5248 if (! dynamic_cast<VuoGenericType *>(publishedPortType))
5251 set<VuoPort *> nodePorts;
5255 nodePorts.insert(inputPort);
5258 nodePorts.insert(outputPort);
5263 nodePorts.insert(inputPort);
5266 for (
VuoPort *port : nodePorts)
5268 VuoCompilerPort *compilerPort = static_cast<VuoCompilerPort *>(port->getCompiler());
5273 reifyGenericPortTypes(node);
5284 dispatch_sync(environmentQueue, ^{
5285 if (environments.size() >= 5)
5286 environments.at(3).at(0)->addExpatriateSourceFile(sourcePath);
5296 dispatch_sync(environmentQueue, ^{
5297 if (environments.size() >= 5)
5299 environments.at(3).at(0)->removeExpatriateSourceFile(sourcePath);
5301 set<string> sourcesRemoved;
5303 loadModulesAndSources(set<string>(), set<string>(), set<string>(), set<string>(), set<string>(), sourcesRemoved,
5304 false,
false, environments.at(3).at(0),
nullptr,
nullptr,
"");
5327 string sourcePathCopy = sourcePath;
5328 string sourceCodeCopy = sourceCode;
5330 dispatch_async(environmentQueue, ^{
5332 ModuleInfo *sourceInfo = NULL;
5334 for (
const vector<Environment *> &envs : environments)
5336 for (Environment *env : envs)
5338 ModuleInfo *potentialSourceInfo = env->listSourceFile(nodeClassName);
5341 sourceInfo = potentialSourceInfo;
5350 bool shouldRecompileSourcesIfUnchanged;
5351 if (! sourceCodeCopy.empty())
5353 sourceInfo->setSourceCode(sourceCodeCopy);
5354 sourceInfo->setSourceCodeOverridden(
true);
5356 shouldRecompileSourcesIfUnchanged =
false;
5360 sourceInfo->revertSourceCode();
5361 sourceInfo->setSourceCodeOverridden(
false);
5363 shouldRecompileSourcesIfUnchanged =
true;
5365 sourceInfo->setAttempted(
false);
5366 sourceInfo->setLastModifiedToNow();
5368 set<string> sourcesModified;
5369 sourcesModified.insert(nodeClassName);
5371 loadModulesAndSources(set<string>(), set<string>(), set<string>(), set<string>(), sourcesModified, set<string>(),
5372 false, shouldRecompileSourcesIfUnchanged,
nullptr,
nullptr,
nullptr,
"");
5411 set<string> nodeClassNameSet;
5412 nodeClassNameSet.insert(nodeClassName);
5413 loadModulesIfNeeded(nodeClassNameSet);
5418 void (^envGetNodeClass)(Environment *) = ^
void (Environment *env) {
5423 applyToAllEnvironments(envGetNodeClass);
5434 loadModulesIfNeeded();
5436 __block map<string, VuoCompilerNodeClass *> nodeClasses;
5437 void (^envGetNodeClasses)(Environment *) = ^
void (Environment *env) {
5438 map<string, VuoCompilerNodeClass *> result = env->getNodeClasses();
5439 nodeClasses.insert(result.begin(), result.end());
5441 applyToInstalledEnvironments(envGetNodeClasses);
5452 set<string> typeNameSet;
5453 typeNameSet.insert(typeName);
5454 loadModulesIfNeeded(typeNameSet);
5457 void (^envGetType)(Environment *) = ^
void (Environment *env) {
5462 applyToInstalledEnvironments(envGetType);
5484 loadModulesIfNeeded();
5486 __block map<string, VuoCompilerType *> types;
5487 void (^envGetTypes)(Environment *) = ^
void (Environment *env) {
5488 map<string, VuoCompilerType *> result = env->getTypes();
5489 types.insert(result.begin(), result.end());
5491 applyToInstalledEnvironments(envGetTypes);
5502 loadModulesIfNeeded();
5504 __block map<string, VuoCompilerModule *> libraryModules;
5505 void (^envGetLibraryModules)(Environment *) = ^
void (Environment *env) {
5506 map<string, VuoCompilerModule *> result = env->getLibraryModules();
5507 libraryModules.insert(result.begin(), result.end());
5509 applyToInstalledEnvironments(envGetLibraryModules);
5510 return libraryModules;
5520 loadModulesIfNeeded();
5522 __block map<string, VuoNodeSet *> nodeSets;
5523 void (^envGetNodeSets)(Environment *) = ^
void (Environment *env) {
5524 map<string, VuoNodeSet *> result = env->getNodeSets();
5525 nodeSets.insert(result.begin(), result.end());
5527 applyToInstalledEnvironments(envGetNodeSets);
5538 loadModulesIfNeeded();
5541 void (^envFindNodeSet)(Environment *) = ^
void (Environment *env) {
5546 applyToInstalledEnvironments(envFindNodeSet);
5556 void (^envFindModule)(Environment *) = ^
void (Environment *env) {
5561 applyToAllEnvironments(envFindModule);
5578 map<string, VuoCompilerNodeClass *> nodeClasses =
getNodeClasses();
5579 for (map<string, VuoCompilerNodeClass *>::const_iterator i = nodeClasses.begin(); i != nodeClasses.end(); ++i)
5586 else if (format ==
"path")
5590 else if (format ==
"dot")
5608 vector<string> VuoCompiler::getCoreVuoDependencies(
void)
5610 vector<string> dependencies;
5611 dependencies.push_back(
"VuoCompositionDiff.o");
5612 dependencies.push_back(
"VuoHeap");
5613 dependencies.push_back(
"VuoException.o");
5614 dependencies.push_back(
"VuoNodeRegistry.o");
5615 dependencies.push_back(
"VuoRuntimeCommunicator.o");
5616 dependencies.push_back(
"VuoRuntimeContext.o");
5617 dependencies.push_back(
"VuoRuntimePersistentState.o");
5618 dependencies.push_back(
"VuoRuntimeState.o");
5619 dependencies.push_back(
"VuoTelemetry.o");
5620 dependencies.push_back(
"VuoThreadManager.o");
5621 dependencies.push_back(
"VuoApp");
5622 dependencies.push_back(
"zmq");
5623 dependencies.push_back(
"json-c");
5624 dependencies.push_back(
"objc");
5625 dependencies.push_back(
"c");
5626 dependencies.push_back(
"AppKit.framework");
5629 dependencies.push_back(LLVM_ROOT
"/lib/libprofile_rt.dylib");
5631 return dependencies;
5637 string VuoCompiler::getRuntimeMainDependency(
void)
5639 return "VuoRuntimeMain.o";
5649 string VuoCompiler::getRuntimeDependency(
void)
5651 return "VuoRuntime.o";
5660 string VuoCompiler::getVuoFrameworkPath(
void)
5662 if (! vuoFrameworkInProgressPath.empty())
5663 return vuoFrameworkInProgressPath;
5671 llvm::sys::Path VuoCompiler::getClangPath(
void)
5682 string dir, moduleKey, ext;
5690 while (nodeClassNameParts.size() > 1 && nodeClassNameParts.back() == ext)
5691 nodeClassNameParts.pop_back();
5694 for (
string &part : nodeClassNameParts)
5698 if (nodeClassNameParts.size() == 1)
5699 nodeClassNameParts.insert(nodeClassNameParts.begin(),
"isf");
5713 __block
bool isLocal =
false;
5715 dispatch_sync(environmentQueue, ^{
5716 if (environments.size() >= 5)
5717 isLocal = environments.at(3).at(0)->findModule(moduleKey);
5731 return lastCompositionBaseDir.empty() ?
"" : lastCompositionBaseDir +
"/Modules";
5746 return lastCompositionBaseDir;
5752 void VuoCompiler::addModuleSearchPath(
string path)
5754 dispatch_sync(environmentQueue, ^{
5755 environments.back().at(0)->addModuleSearchPath(path);
5756 environments.back().at(0)->addLibrarySearchPath(path);
5765 dispatch_sync(environmentQueue, ^{
5766 environments.back().at(0)->addHeaderSearchPath(path);
5775 dispatch_sync(environmentQueue, ^{
5776 environments.back().at(0)->addLibrarySearchPath(path);
5785 dispatch_sync(environmentQueue, ^{
5786 environments.back().at(0)->addFrameworkSearchPath(path);
5795 this->telemetry = telemetry;
5803 this->target = target;
5811 this->isVerbose = isVerbose;
5821 if (VuoPro::getProAccess())
5823 _shouldShowSplashWindow =
false;
5828 _shouldShowSplashWindow = potentiallyShow;
5836 return _shouldShowSplashWindow;
5842 void VuoCompiler::setClangPath(
const string &clangPath)
5844 this->clangPath = llvm::sys::Path(StringRef(clangPath));
5852 string vuoFrameworkPath = getVuoFrameworkPath();
5853 return (vuoFrameworkPath.empty() ?
5854 VUO_BUILD_DIR
"/bin/VuoCompositionLoader.app/Contents/MacOS/VuoCompositionLoader" :
5855 vuoFrameworkPath +
"/Helpers/VuoCompositionLoader.app/Contents/MacOS/VuoCompositionLoader");
5861 string VuoCompiler::getCompositionStubPath(
void)
5863 string vuoFrameworkPath = getVuoFrameworkPath();
5864 return (vuoFrameworkPath.empty() ?
5865 VUO_BUILD_DIR
"/lib/libVuoCompositionStub.dylib" :
5866 vuoFrameworkPath +
"/Modules/libVuoCompositionStub.dylib");
5872 string VuoCompiler::getCachePathForComposition(
const string compositionDir)
5874 string modifierLetterColon(
"꞉");
5875 string cachedModulesName = compositionDir;
5885 __block vector<string> moduleSearchPaths;
5886 void (^envGetModuleSearchPaths)(Environment *) = ^
void (Environment *env) {
5887 vector<string> result = env->getModuleSearchPaths();
5888 moduleSearchPaths.insert(moduleSearchPaths.end(), result.begin(), result.end());
5890 applyToInstalledEnvironments(envGetModuleSearchPaths);
5892 __block vector<string> headerSearchPaths;
5893 void (^envGetHeaderSearchPaths)(Environment *) = ^
void (Environment *env) {
5894 vector<string> result = env->getHeaderSearchPaths();
5895 headerSearchPaths.insert(headerSearchPaths.end(), result.begin(), result.end());
5897 applyToInstalledEnvironments(envGetHeaderSearchPaths);
5899 __block vector<string> librarySearchPaths;
5900 void (^envGetLibrarySearchPaths)(Environment *) = ^
void (Environment *env) {
5901 vector<string> result = env->getLibrarySearchPaths();
5902 librarySearchPaths.insert(librarySearchPaths.end(), result.begin(), result.end());
5904 applyToInstalledEnvironments(envGetLibrarySearchPaths);
5906 __block vector<string> frameworkSearchPaths;
5907 void (^envGetFrameworkSearchPaths)(Environment *) = ^
void (Environment *env) {
5908 vector<string> result = env->getFrameworkSearchPaths();
5909 frameworkSearchPaths.insert(frameworkSearchPaths.end(), result.begin(), result.end());
5911 applyToInstalledEnvironments(envGetFrameworkSearchPaths);
5913 fprintf(stderr,
"Module (node class, type, library) search paths:\n");
5914 for (vector<string>::iterator i = moduleSearchPaths.begin(); i != moduleSearchPaths.end(); ++i)
5915 fprintf(stderr,
" %s\n", (*i).c_str());
5916 fprintf(stderr,
"Header search paths:\n");
5917 for (vector<string>::iterator i = headerSearchPaths.begin(); i != headerSearchPaths.end(); ++i)
5918 fprintf(stderr,
" %s\n", (*i).c_str());
5919 fprintf(stderr,
"Other library search paths:\n");
5920 for (vector<string>::iterator i = librarySearchPaths.begin(); i != librarySearchPaths.end(); ++i)
5921 fprintf(stderr,
" %s\n", (*i).c_str());
5922 fprintf(stderr,
"Other framework search paths:\n");
5923 for (vector<string>::iterator i = frameworkSearchPaths.begin(); i != frameworkSearchPaths.end(); ++i)
5924 fprintf(stderr,
" %s\n", (*i).c_str());
5925 fprintf(stderr,
"Framework path:\n");
5926 if (! getVuoFrameworkPath().empty())
5927 fprintf(stderr,
" %s\n", getVuoFrameworkPath().c_str());
5928 fprintf(stderr,
"Clang path:\n");
5929 if (! getClangPath().str().empty())
5930 fprintf(stderr,
" %s\n", getClangPath().c_str());
5946 string directory, file, extension;
5950 compiler.
compileComposition(compositionFilePath, compiledCompositionPath,
true, issues);
5952 remove(compiledCompositionPath.c_str());
5981 string compositionPath = (workingDirectory.empty() ?
"." : workingDirectory) +
"/" + processName +
".vuo";
5987 remove(compiledCompositionPath.c_str());
6011 string directory, file, extension;
6014 compiler.
compileComposition(compositionFilePath, compiledCompositionPath,
true, issues);
6017 remove(compiledCompositionPath.c_str());
6046 string compositionPath = (workingDirectory.empty() ?
"." : workingDirectory) +
"/UntitledComposition.vuo";
6052 remove(compiledCompositionPath.c_str());
6065 pendingDataQueue = dispatch_queue_create(
"org.vuo.compiler.delegate.pending", 0);
6070 LoadedModulesData *data = dequeueData();
6077 void VuoCompilerDelegate::enqueueData(LoadedModulesData *data)
6079 dispatch_sync(pendingDataQueue, ^{
6080 pendingData.push_back(data);
6087 VuoCompilerDelegate::LoadedModulesData * VuoCompilerDelegate::dequeueData(
void)
6089 __block LoadedModulesData *ret;
6090 dispatch_sync(pendingDataQueue, ^{
6091 ret = pendingData.front();
6092 pendingData.pop_front();
6101 VuoCompilerDelegate::LoadedModulesData::LoadedModulesData(
const set< pair<VuoCompilerModule *, VuoCompilerModule *> > &modulesModified,
6104 referenceCountQueue = dispatch_queue_create(
"org.vuo.compiler.delegate.reference", 0);
6107 this->modulesModified = modulesModified;
6108 this->modulesRemoved = modulesRemoved;
6109 this->issues = issues;
6115 VuoCompilerDelegate::LoadedModulesData::~LoadedModulesData(
void)
6119 for (set< pair<VuoCompilerModule *, VuoCompilerModule *> >::iterator i = modulesModified.begin(); i != modulesModified.end(); ++i)
6122 for (set<VuoCompilerModule *>::iterator i = modulesRemoved.begin(); i != modulesRemoved.end(); ++i)
6129 void VuoCompilerDelegate::LoadedModulesData::retain(
void)
6131 dispatch_sync(referenceCountQueue, ^{
6139 void VuoCompilerDelegate::LoadedModulesData::release(
void)
6141 dispatch_sync(referenceCountQueue, ^{
6143 if (referenceCount == 0) {