Vuo  2.0.2
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  init_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 #if VUO_PRO
45  fini_Pro();
46 #endif
47  delete parser;
48  json_object_put(moduleDetails);
49 }
50 
56 VuoCompilerModule * VuoCompilerModule::newModule(const string &moduleKey, Module *module, const string &modulePath)
57 {
58  VuoCompilerModule *m = NULL;
59 
60  if (isModule(module, moduleKey))
61  {
63  if (nodeClass)
64  m = nodeClass->getCompiler();
65  else
66  {
68  if (type)
69  m = type;
70  else
71  m = new VuoCompilerModule(new VuoModule(moduleKey), module);
72  }
73 
74  m->modulePath = modulePath;
75  }
76  else if (moduleKey != "libmodule")
77  VUserLog("Warning: No VuoModuleMetadata found in \"%s\" so I'm not going to load it.", moduleKey.c_str());
78 
79  return m;
80 }
81 
86 bool VuoCompilerModule::isModule(Module *module, string moduleKey)
87 {
88  return hasOriginalOrMangledGlobal("moduleDetails", module, moduleKey);
89 }
90 
96 {
99  parseMetadata();
100 }
101 
106 {
107  string moduleDetailsStr = parser->getGlobalString( nameForGlobal("moduleDetails") );
108  json_object_put(moduleDetails);
109  moduleDetails = json_tokener_parse(moduleDetailsStr.c_str());
110 
111  if (! moduleDetails)
112  {
113  VUserLog("Error: Couldn't parse VuoModuleMetadata as JSON: %s", moduleDetailsStr.c_str());
114  return;
115  }
116 
121  vector<string> dependenciesVector = VuoJsonUtilities::parseArrayOfStrings(moduleDetails, "dependencies");
122  dependencies = set<string>(dependenciesVector.begin(), dependenciesVector.end());
123  compatibleTargets = parseTargetSet(moduleDetails, "compatibleOperatingSystems");
124 }
125 
132 {
134  json_object *operatingSystemsObject = NULL;
135  if (json_object_object_get_ex(o, key.c_str(), &operatingSystemsObject))
136  {
137  if (json_object_get_type(operatingSystemsObject) == json_type_object)
138  {
139  json_object *macosObject = NULL;
140  if (json_object_object_get_ex(operatingSystemsObject, "macosx", &macosObject))
141  {
144  }
145  }
146  }
147  return t;
148 }
149 
156 {
157  if (version == "10.7")
158  return VuoCompilerTargetSet::MacVersion_10_7;
159  if (version == "10.8")
160  return VuoCompilerTargetSet::MacVersion_10_8;
161  if (version == "10.9")
162  return VuoCompilerTargetSet::MacVersion_10_9;
163  if (version == "10.10")
164  return VuoCompilerTargetSet::MacVersion_10_10;
165  if (version == "10.11")
166  return VuoCompilerTargetSet::MacVersion_10_11;
167  if (version == "10.12")
168  return VuoCompilerTargetSet::MacVersion_10_12;
169  if (version == "10.13")
170  return VuoCompilerTargetSet::MacVersion_10_13;
171  if (version == "10.14")
172  return VuoCompilerTargetSet::MacVersion_10_14;
173  if (version == "10.15")
174  return VuoCompilerTargetSet::MacVersion_10_15;
175 
176  return VuoCompilerTargetSet::MacVersion_Any;
177 }
178 
184 {
185  set<string> globals;
186  globals.insert("moduleDetails");
187  return globals;
188 }
189 
193 string VuoCompilerModule::nameForGlobal(string genericGlobalVarOrFuncName)
194 {
195  return nameForGlobal(genericGlobalVarOrFuncName, base->getModuleKey());
196 }
197 
201 string VuoCompilerModule::nameForGlobal(string nameBeforeCompilation, string moduleKey)
202 {
203  return VuoStringUtilities::prefixSymbolName(nameBeforeCompilation, moduleKey);
204 }
205 
210 bool VuoCompilerModule::hasOriginalOrMangledGlobal(string nameBeforeCompilation, Module *module, string moduleKey)
211 {
212  string nameAfterCompilation = nameForGlobal(nameBeforeCompilation, moduleKey);
213  return (module->getNamedValue(nameBeforeCompilation) != NULL || module->getNamedValue(nameAfterCompilation) != NULL);
214 }
215 
221 {
222  set<string> globalsAndFuncsToRename = globalsToRename();
223 
224  Module::GlobalListType& globals = module->getGlobalList();
225 
226  // Iterate through global variables
227  for (Module::GlobalListType::iterator i = globals.begin(), e = globals.end(); i != e; ++i) {
228  string curGlobalVarName = i->getName();
229  // If current global variable is in the list of those to rename, do so
230  if (globalsAndFuncsToRename.find(curGlobalVarName) != globalsAndFuncsToRename.end()) {
231  string newGlobalVarName = nameForGlobal(curGlobalVarName);
232  i->setName(newGlobalVarName);
233  }
234 
235  }
236 
237  Module::FunctionListType& functions = module->getFunctionList();
238 
239  // Iterate through functions
240  for (Module::FunctionListType::iterator i = functions.begin(), e = functions.end(); i != e; ++i) {
241  string curFuncName = i->getName();
242  // If current function is in the list of those to rename, do so
243  if (globalsAndFuncsToRename.find(curFuncName) != globalsAndFuncsToRename.end()) {
244  string newFuncName = nameForGlobal(curFuncName);
245  i->setName(newFuncName);
246  }
247 
248  }
249 }
250 
255 Function * VuoCompilerModule::declareFunctionInModule(Module *module, Function *functionSrc)
256 {
257  Function *functionDst = module->getFunction(functionSrc->getName());
258  if (! functionDst)
259  {
260  functionDst = Function::Create(functionSrc->getFunctionType(),
261  GlobalValue::ExternalLinkage,
262  functionSrc->getName(),
263  module);
264  functionDst->setAttributes( functionSrc->getAttributes() );
265  }
266  return functionDst;
267 }
268 
273 {
274  return dependencies;
275 }
276 
282 {
283  return base->getModuleKey();
284 }
285 
290 {
291  return compatibleTargets;
292 }
293 
298 {
299  return module;
300 }
301 
306 {
307  return base;
308 }
309 
314 {
315  return builtIn;
316 }
317 
322 {
323  this->builtIn = builtIn;
324 }
325 
331 {
332  return modulePath;
333 }