Vuo  2.2.1
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.11")
158  return VuoCompilerTargetSet::MacVersion_10_11;
159  if (version == "10.12")
160  return VuoCompilerTargetSet::MacVersion_10_12;
161  if (version == "10.13")
162  return VuoCompilerTargetSet::MacVersion_10_13;
163  if (version == "10.14")
164  return VuoCompilerTargetSet::MacVersion_10_14;
165  if (version == "10.15")
166  return VuoCompilerTargetSet::MacVersion_10_15;
167  if (version == "11.0")
168  return VuoCompilerTargetSet::MacVersion_11_0;
169 
170  return VuoCompilerTargetSet::MacVersion_Any;
171 }
172 
178 {
179  set<string> globals;
180  globals.insert("moduleDetails");
181  return globals;
182 }
183 
187 string VuoCompilerModule::nameForGlobal(string genericGlobalVarOrFuncName)
188 {
189  return nameForGlobal(genericGlobalVarOrFuncName, base->getModuleKey());
190 }
191 
195 string VuoCompilerModule::nameForGlobal(string nameBeforeCompilation, string moduleKey)
196 {
197  return VuoStringUtilities::prefixSymbolName(nameBeforeCompilation, moduleKey);
198 }
199 
204 bool VuoCompilerModule::hasOriginalOrMangledGlobal(string nameBeforeCompilation, Module *module, string moduleKey)
205 {
206  string nameAfterCompilation = nameForGlobal(nameBeforeCompilation, moduleKey);
207  return (module->getNamedValue(nameBeforeCompilation) != NULL || module->getNamedValue(nameAfterCompilation) != NULL);
208 }
209 
215 {
216  set<string> globalsAndFuncsToRename = globalsToRename();
217 
218  Module::GlobalListType& globals = module->getGlobalList();
219 
220  // Iterate through global variables
221  for (Module::GlobalListType::iterator i = globals.begin(), e = globals.end(); i != e; ++i) {
222  string curGlobalVarName = i->getName();
223  // If current global variable is in the list of those to rename, do so
224  if (globalsAndFuncsToRename.find(curGlobalVarName) != globalsAndFuncsToRename.end()) {
225  string newGlobalVarName = nameForGlobal(curGlobalVarName);
226  i->setName(newGlobalVarName);
227  }
228 
229  }
230 
231  Module::FunctionListType& functions = module->getFunctionList();
232 
233  // Iterate through functions
234  for (Module::FunctionListType::iterator i = functions.begin(), e = functions.end(); i != e; ++i) {
235  string curFuncName = i->getName();
236  // If current function is in the list of those to rename, do so
237  if (globalsAndFuncsToRename.find(curFuncName) != globalsAndFuncsToRename.end()) {
238  string newFuncName = nameForGlobal(curFuncName);
239  i->setName(newFuncName);
240  }
241 
242  }
243 }
244 
249 Function * VuoCompilerModule::declareFunctionInModule(Module *module, Function *functionSrc)
250 {
251  Function *functionDst = module->getFunction(functionSrc->getName());
252  if (! functionDst)
253  {
254  functionDst = Function::Create(functionSrc->getFunctionType(),
255  GlobalValue::ExternalLinkage,
256  functionSrc->getName(),
257  module);
258  functionDst->setAttributes( functionSrc->getAttributes() );
259  }
260  return functionDst;
261 }
262 
267 {
268  return dependencies;
269 }
270 
276 {
277  return base->getModuleKey();
278 }
279 
284 {
285  return compatibleTargets;
286 }
287 
292 {
293  return module;
294 }
295 
300 {
301  return base;
302 }
303 
308 {
309  return builtIn;
310 }
311 
316 {
317  this->builtIn = builtIn;
318 }
319 
325 {
326  return modulePath;
327 }