Vuo  2.0.0
VuoCompilerModule.cc
Go to the documentation of this file.
1 
11 #include "VuoCompilerNodeClass.hh"
12 #include "VuoCompilerType.hh"
13 #include "VuoJsonUtilities.hh"
14 #include "VuoNodeClass.hh"
15 #include "VuoStringUtilities.hh"
16 
22 {
23 #if VUO_PRO
24  VuoCompilerModule_Pro();
25 #endif
26  this->base = base;
27  this->module = module;
28  this->moduleDetails = NULL;
29  this->parser = NULL;
30  this->builtIn = false;
31 
32  if (module)
33  {
34  module->setModuleIdentifier(getPseudoBase()->getModuleKey());
35  parse();
36  }
37 }
38 
43 {
44  delete parser;
45  json_object_put(moduleDetails);
46 }
47 
53 VuoCompilerModule * VuoCompilerModule::newModule(const string &moduleKey, Module *module, const string &modulePath)
54 {
55  VuoCompilerModule *m = NULL;
56 
57  if (isModule(module, moduleKey))
58  {
60  if (nodeClass)
61  m = nodeClass->getCompiler();
62  else
63  {
65  if (type)
66  m = type;
67  else
68  m = new VuoCompilerModule(new VuoModule(moduleKey), module);
69  }
70 
71  m->modulePath = modulePath;
72  }
73  else if (moduleKey != "libmodule")
74  VUserLog("Warning: No VuoModuleMetadata found in \"%s\" so I'm not going to load it.", moduleKey.c_str());
75 
76  return m;
77 }
78 
83 bool VuoCompilerModule::isModule(Module *module, string moduleKey)
84 {
85  return hasOriginalOrMangledGlobal("moduleDetails", module, moduleKey);
86 }
87 
93 {
96  parseMetadata();
97 }
98 
103 {
104  string moduleDetailsStr = parser->getGlobalString( nameForGlobal("moduleDetails") );
105  json_object_put(moduleDetails);
106  moduleDetails = json_tokener_parse(moduleDetailsStr.c_str());
107 
108  if (! moduleDetails)
109  {
110  VUserLog("Error: Couldn't parse VuoModuleMetadata as JSON: %s", moduleDetailsStr.c_str());
111  return;
112  }
113 
118  vector<string> dependenciesVector = VuoJsonUtilities::parseArrayOfStrings(moduleDetails, "dependencies");
119  dependencies = set<string>(dependenciesVector.begin(), dependenciesVector.end());
120  compatibleTargets = parseTargetSet(moduleDetails, "compatibleOperatingSystems");
121 }
122 
129 {
131  json_object *operatingSystemsObject = NULL;
132  if (json_object_object_get_ex(o, key.c_str(), &operatingSystemsObject))
133  {
134  if (json_object_get_type(operatingSystemsObject) == json_type_object)
135  {
136  json_object *macosObject = NULL;
137  if (json_object_object_get_ex(operatingSystemsObject, "macosx", &macosObject))
138  {
141  }
142  }
143  }
144  return t;
145 }
146 
153 {
154  if (version == "10.7")
155  return VuoCompilerTargetSet::MacVersion_10_7;
156  if (version == "10.8")
157  return VuoCompilerTargetSet::MacVersion_10_8;
158  if (version == "10.9")
159  return VuoCompilerTargetSet::MacVersion_10_9;
160  if (version == "10.10")
161  return VuoCompilerTargetSet::MacVersion_10_10;
162  if (version == "10.11")
163  return VuoCompilerTargetSet::MacVersion_10_11;
164  if (version == "10.12")
165  return VuoCompilerTargetSet::MacVersion_10_12;
166  if (version == "10.13")
167  return VuoCompilerTargetSet::MacVersion_10_13;
168  if (version == "10.14")
169  return VuoCompilerTargetSet::MacVersion_10_14;
170  if (version == "10.15")
171  return VuoCompilerTargetSet::MacVersion_10_15;
172 
173  return VuoCompilerTargetSet::MacVersion_Any;
174 }
175 
181 {
182  set<string> globals;
183  globals.insert("moduleDetails");
184  return globals;
185 }
186 
190 string VuoCompilerModule::nameForGlobal(string genericGlobalVarOrFuncName)
191 {
192  return nameForGlobal(genericGlobalVarOrFuncName, base->getModuleKey());
193 }
194 
198 string VuoCompilerModule::nameForGlobal(string nameBeforeCompilation, string moduleKey)
199 {
200  return VuoStringUtilities::prefixSymbolName(nameBeforeCompilation, moduleKey);
201 }
202 
207 bool VuoCompilerModule::hasOriginalOrMangledGlobal(string nameBeforeCompilation, Module *module, string moduleKey)
208 {
209  string nameAfterCompilation = nameForGlobal(nameBeforeCompilation, moduleKey);
210  return (module->getNamedValue(nameBeforeCompilation) != NULL || module->getNamedValue(nameAfterCompilation) != NULL);
211 }
212 
218 {
219  set<string> globalsAndFuncsToRename = globalsToRename();
220 
221  Module::GlobalListType& globals = module->getGlobalList();
222 
223  // Iterate through global variables
224  for (Module::GlobalListType::iterator i = globals.begin(), e = globals.end(); i != e; ++i) {
225  string curGlobalVarName = i->getName();
226  // If current global variable is in the list of those to rename, do so
227  if (globalsAndFuncsToRename.find(curGlobalVarName) != globalsAndFuncsToRename.end()) {
228  string newGlobalVarName = nameForGlobal(curGlobalVarName);
229  i->setName(newGlobalVarName);
230  }
231 
232  }
233 
234  Module::FunctionListType& functions = module->getFunctionList();
235 
236  // Iterate through functions
237  for (Module::FunctionListType::iterator i = functions.begin(), e = functions.end(); i != e; ++i) {
238  string curFuncName = i->getName();
239  // If current function is in the list of those to rename, do so
240  if (globalsAndFuncsToRename.find(curFuncName) != globalsAndFuncsToRename.end()) {
241  string newFuncName = nameForGlobal(curFuncName);
242  i->setName(newFuncName);
243  }
244 
245  }
246 }
247 
252 Function * VuoCompilerModule::declareFunctionInModule(Module *module, Function *functionSrc)
253 {
254  Function *functionDst = module->getFunction(functionSrc->getName());
255  if (! functionDst)
256  {
257  functionDst = Function::Create(functionSrc->getFunctionType(),
258  GlobalValue::ExternalLinkage,
259  functionSrc->getName(),
260  module);
261  functionDst->setAttributes( functionSrc->getAttributes() );
262  }
263  return functionDst;
264 }
265 
270 {
271  return dependencies;
272 }
273 
279 {
280  return base->getModuleKey();
281 }
282 
287 {
288  return compatibleTargets;
289 }
290 
295 {
296  return module;
297 }
298 
303 {
304  return base;
305 }
306 
311 {
312  return builtIn;
313 }
314 
319 {
320  this->builtIn = builtIn;
321 }
322 
328 {
329  return modulePath;
330 }