hqlecl.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "build-config.h"
  14. #include "jliball.hpp"
  15. #include "jmisc.hpp"
  16. #include "jstream.hpp"
  17. #include "hql.hpp"
  18. #include "hqlexpr.hpp"
  19. #include "hqlecl.hpp"
  20. #include "hqlthql.hpp"
  21. #include "hqlcerrors.hpp"
  22. #include "hqlcatom.hpp"
  23. #include "hqllib.ipp"
  24. #include "hqlutil.hpp"
  25. #include "hqlhtcpp.ipp"
  26. #include "hqlwcpp.hpp"
  27. #include "hqllib.ipp"
  28. #include "hqlttcpp.ipp"
  29. #include "workunit.hpp"
  30. #include "thorplugin.hpp"
  31. #ifdef _DEBUG
  32. #define IS_DEBUG_BUILD true
  33. #else
  34. #define IS_DEBUG_BUILD false
  35. #endif
  36. #define MAIN_MODULE_TEMPLATE "thortpl.cpp"
  37. #define HEADER_TEMPLATE "thortpl.hpp"
  38. #define CHILD_MODULE_TEMPLATE "childtpl.cpp"
  39. #define PROTO_TEMPLATE "prototpl.cpp"
  40. class NullContextCallback : public CInterface, implements ICodegenContextCallback
  41. {
  42. IMPLEMENT_IINTERFACE
  43. virtual void noteCluster(const char *clusterName) {}
  44. virtual void registerFile(const char * filename, const char * description) {}
  45. virtual bool allowAccess(const char * category) { return true; }
  46. };
  47. class HqlDllGenerator : public CInterface, implements IHqlExprDllGenerator, implements IAbortRequestCallback
  48. {
  49. public:
  50. HqlDllGenerator(IErrorReceiver * _errs, const char * _wuname, const char * _targetdir, IWorkUnit * _wu, const char * _template_dir, ClusterType _targetClusterType, ICodegenContextCallback * _ctxCallback, bool _checkForLocalFileUploads) :
  51. errs(_errs), wuname(_wuname), targetDir(_targetdir), wu(_wu), template_dir(_template_dir), targetClusterType(_targetClusterType), ctxCallback(_ctxCallback), checkForLocalFileUploads(_checkForLocalFileUploads)
  52. {
  53. if (!ctxCallback)
  54. ctxCallback.setown(new NullContextCallback);
  55. noOutput = true;
  56. defaultMaxCompileThreads = 1;
  57. generateTarget = EclGenerateNone;
  58. code.setown(createCppInstance(wu, wuname));
  59. deleteGenerated = false;
  60. totalGeneratedSize = 0;
  61. }
  62. IMPLEMENT_IINTERFACE
  63. virtual void addLibrary(const char * name);
  64. virtual bool processQuery(OwnedHqlExpr & parsedQuery, EclGenerateTarget _generateTarget);
  65. virtual bool generateDll(ICppCompiler * compiler);
  66. virtual bool generateExe(ICppCompiler * compiler);
  67. virtual bool generatePackage(const char * packageName);
  68. virtual void setMaxCompileThreads(unsigned value) { defaultMaxCompileThreads = value; }
  69. virtual void addManifest(const char *filename) { code->addManifest(filename); }
  70. virtual void addManifestFromArchive(IPropertyTree *archive) { code->addManifestFromArchive(archive); }
  71. virtual void addWebServiceInfo(IPropertyTree *wsinfo){ code->addWebServiceInfo(wsinfo); }
  72. virtual double getECLcomplexity(IHqlExpression * exprs);
  73. virtual void setSaveGeneratedFiles(bool value) { deleteGenerated = !value; }
  74. protected:
  75. void addCppName(const char * filename);
  76. void addLibrariesToCompiler();
  77. void addWorkUnitAsResource();
  78. void calculateHash(IHqlExpression * expr);
  79. bool doCompile(ICppCompiler * compiler);
  80. void doExpand(HqlCppTranslator & translator);
  81. void expandCode(const char * templateName, const char * ext, IHqlCppInstance * code, bool multiFile, unsigned pass, CompilerType compiler);
  82. void flushResources();
  83. bool generateCode(HqlQueryContext & query);
  84. bool generateFullFieldUsageStatistics(HqlCppTranslator & translator, IHqlExpression * query);
  85. IPropertyTree * generateSingleFieldUsageStatistics(IHqlExpression * expr, const char * variety, const IPropertyTree * exclude);
  86. offset_t getGeneratedSize() const;
  87. void insertStandAloneCode();
  88. void setWuState(bool ok);
  89. inline bool abortRequested();
  90. protected:
  91. Linked<IErrorReceiver> errs;
  92. const char * wuname;
  93. StringAttr targetDir;
  94. Linked<IWorkUnit> wu;
  95. Owned<IHqlCppInstance> code;
  96. const char * template_dir;
  97. ClusterType targetClusterType;
  98. Linked<ICodegenContextCallback> ctxCallback;
  99. unsigned defaultMaxCompileThreads;
  100. StringArray sourceFiles;
  101. StringArray libraries;
  102. offset_t totalGeneratedSize;
  103. bool checkForLocalFileUploads;
  104. bool noOutput;
  105. EclGenerateTarget generateTarget;
  106. bool deleteGenerated;
  107. };
  108. //---------------------------------------------------------------------------
  109. static void processMetaCommands(HqlCppTranslator & translator, IWorkUnit * wu, HqlQueryContext & query, ICodegenContextCallback *ctxCallback);
  110. void HqlDllGenerator::addCppName(const char * filename)
  111. {
  112. if (wu)
  113. {
  114. Owned<IWUQuery> query = wu->updateQuery();
  115. associateLocalFile(query, FileTypeCpp, filename, pathTail(filename), 0);
  116. ctxCallback->registerFile(filename, "Workunit CPP");
  117. }
  118. }
  119. void HqlDllGenerator::addLibrary(const char * name)
  120. {
  121. libraries.append(name);
  122. }
  123. void HqlDllGenerator::addLibrariesToCompiler()
  124. {
  125. unsigned idx=0;
  126. loop
  127. {
  128. const char * lib = code->queryLibrary(idx);
  129. if (!lib)
  130. break;
  131. PrintLog("Adding library: %s", lib);
  132. addLibrary(lib);
  133. idx++;
  134. }
  135. }
  136. void HqlDllGenerator::expandCode(const char * templateName, const char * ext, IHqlCppInstance * code, bool multiFile, unsigned pass, CompilerType compiler)
  137. {
  138. StringBuffer fullname;
  139. addDirectoryPrefix(fullname, targetDir).append(wuname).append(ext);
  140. Owned<IFile> out = createIFile(fullname.str());
  141. Owned<ITemplateExpander> expander = createTemplateExpander(out, templateName, template_dir);
  142. if (!expander)
  143. throwError2(HQLERR_CouldNotOpenTemplateXatY, templateName, template_dir ? template_dir : ".");
  144. Owned<ISectionWriter> writer = createCppWriter(*code, compiler);
  145. Owned<IProperties> props = createProperties(true);
  146. if (multiFile)
  147. {
  148. props->setProp("multiFile", true);
  149. props->setProp("pass", pass);
  150. }
  151. StringBuffer headerName;
  152. headerName.append(wuname).append(".hpp");
  153. props->setProp("headerName", headerName.str());
  154. props->setProp("outputName", fullname.str());
  155. expander->generate(*writer, pass, props);
  156. totalGeneratedSize += out->size();
  157. if (!deleteGenerated)
  158. addCppName(fullname);
  159. }
  160. //---------------------------------------------------------------------------
  161. bool HqlDllGenerator::processQuery(OwnedHqlExpr & parsedQuery, EclGenerateTarget _generateTarget)
  162. {
  163. generateTarget = _generateTarget;
  164. assertex(wu->getHash());
  165. unsigned prevCount = errs->errCount();
  166. HqlQueryContext query;
  167. query.expr.setown(parsedQuery.getClear());
  168. bool ok = generateCode(query);
  169. code->flushHints();
  170. wu->commit();
  171. if (!ok)
  172. return false;
  173. if (errs->errCount() != prevCount)
  174. return false;
  175. switch (generateTarget)
  176. {
  177. case EclGenerateNone:
  178. case EclGenerateCpp:
  179. return true;
  180. }
  181. flushResources();
  182. addLibrariesToCompiler();
  183. // Free up memory before the c++ compile occurs
  184. code.clear();
  185. return true;
  186. }
  187. bool HqlDllGenerator::generateDll(ICppCompiler * compiler)
  188. {
  189. if (noOutput || !compiler)
  190. return true;
  191. bool ok = doCompile(compiler);
  192. setWuState(ok);
  193. return ok;
  194. }
  195. bool HqlDllGenerator::generateExe(ICppCompiler * compiler)
  196. {
  197. if (noOutput || !compiler)
  198. return true;
  199. compiler->setCreateExe(true);
  200. bool ok = doCompile(compiler);
  201. setWuState(ok);
  202. return ok;
  203. }
  204. bool HqlDllGenerator::generatePackage(const char * packageName)
  205. {
  206. return false;
  207. }
  208. static bool isSetMeta(IHqlExpression * expr)
  209. {
  210. switch (expr->getOperator())
  211. {
  212. case no_compound:
  213. case no_comma:
  214. return isSetMeta(expr->queryChild(0)) && isSetMeta(expr->queryChild(1));
  215. case no_setmeta:
  216. return true;
  217. default:
  218. return false;
  219. }
  220. }
  221. IPropertyTree * HqlDllGenerator::generateSingleFieldUsageStatistics(IHqlExpression * expr, const char * variety, const IPropertyTree * exclude)
  222. {
  223. //Generate each into a new workunit
  224. // Owned<IWorkUnit> localWu = createLocalWorkUnit();
  225. IWorkUnit * localWu = wu;
  226. //Generate the code into a new instance each time so it doesn't hang around eating memory
  227. Owned<IHqlCppInstance> localCode = createCppInstance(localWu, wuname);
  228. HqlQueryContext query;
  229. query.expr.set(expr);
  230. resetUniqueId();
  231. wu->resetBeforeGeneration();
  232. HqlCppTranslator translator(errs, wuname, localCode, targetClusterType, ctxCallback);
  233. try
  234. {
  235. translator.buildCpp(*localCode, query);
  236. return translator.gatherFieldUsage(variety, exclude);
  237. }
  238. catch (IException * e)
  239. {
  240. if (e->errorCode() != HQLERR_ErrorAlreadyReported)
  241. {
  242. unsigned errcode = e->errorCode() ? e->errorCode() : 1;
  243. StringBuffer s;
  244. errs->reportError(errcode, e->errorMessage(s).str(), NULL, 0, 0, 1);
  245. }
  246. e->Release();
  247. return NULL;
  248. }
  249. catch (RELEASE_CATCH_ALL)
  250. {
  251. errs->reportError(99, "Unknown error", NULL, 0, 0, 1);
  252. return NULL;
  253. }
  254. }
  255. bool HqlDllGenerator::generateFullFieldUsageStatistics(HqlCppTranslator & translator, IHqlExpression* query)
  256. {
  257. LinkedHqlExpr savedQuery = query;
  258. //Strip any #options etc. and check that the query consists of a single OUTPUT() action
  259. while ((savedQuery->getOperator() == no_compound) && isSetMeta(savedQuery->queryChild(0)))
  260. savedQuery.set(savedQuery->queryChild(1));
  261. if (savedQuery->getOperator() != no_output)
  262. throw MakeStringException(0, "#option('GenerateFullFieldUsage') requires the query to be a single OUTPUT()");
  263. Owned<IPropertyTree> allFields = createPTree("usage");
  264. IHqlExpression * dataset = savedQuery->queryChild(0);
  265. //First calculate which files are used if no fields are extracted from the output i.e. shared by all output fields
  266. //Use COUNT(dataset) as the query to test that
  267. OwnedHqlExpr countDataset = createValue(no_count, makeIntType(8, false), LINK(dataset));
  268. Owned<IPropertyTree> countFilesUsed = generateSingleFieldUsageStatistics(countDataset, "__shared__", NULL);
  269. if (countFilesUsed)
  270. allFields->addPropTree(countFilesUsed->queryName(), LINK(countFilesUsed));
  271. //Now project the output dataset down to a single field, and generate the file/field dependencies for each of those
  272. OwnedHqlExpr selSeq = createUniqueSelectorSequence();
  273. OwnedHqlExpr left = createSelector(no_left, dataset, selSeq);
  274. RecordSelectIterator iter(dataset->queryRecord(), left);
  275. ForEach(iter)
  276. {
  277. IHqlExpression * cur = iter.query();
  278. IHqlExpression * field = cur->queryChild(1);
  279. OwnedHqlExpr record = createRecord(field);
  280. OwnedHqlExpr self = getSelf(record);
  281. OwnedHqlExpr assign = createAssign(createSelectExpr(LINK(self), LINK(field)), LINK(cur));
  282. OwnedHqlExpr transform = createValue(no_transform, makeTransformType(record->getType()), LINK(assign), NULL);
  283. HqlExprArray args;
  284. args.append(*LINK(dataset));
  285. args.append(*LINK(transform));
  286. args.append(*LINK(selSeq));
  287. OwnedHqlExpr project = createDataset(no_hqlproject,args);
  288. OwnedHqlExpr projectedOutput = replaceChildDataset(savedQuery, project, 0);
  289. StringBuffer variety;
  290. getExprIdentifier(variety, cur);
  291. //Generate each into a new property tree, and only include fields not shared by all other output fields.
  292. Owned<IPropertyTree> filesUsed = generateSingleFieldUsageStatistics(projectedOutput, variety.str(), countFilesUsed);
  293. if (filesUsed)
  294. allFields->addPropTree(filesUsed->queryName(), LINK(filesUsed));
  295. //Generate progress so far
  296. translator.writeFieldUsage(targetDir, allFields, NULL);
  297. }
  298. translator.writeFieldUsage(targetDir, allFields, NULL);
  299. return false;
  300. }
  301. bool HqlDllGenerator::generateCode(HqlQueryContext & query)
  302. {
  303. noOutput = true;
  304. {
  305. // ensure warnings/errors are available before we do the processing...
  306. wu->commit();
  307. MTIME_SECTION (timer, "Generate_code");
  308. unsigned time = msTick();
  309. HqlCppTranslator translator(errs, wuname, code, targetClusterType, ctxCallback);
  310. processMetaCommands(translator, wu, query, ctxCallback);
  311. if (wu->getDebugValueBool("generateFullFieldUsage", false))
  312. {
  313. //Ensure file information is generated. It only partially works with field information at the moment.
  314. //(The merging/difference logic would need to improve, but would be relatively simple if it was useful.)
  315. wu->setDebugValueInt("reportFileUsage", 1, true);
  316. return generateFullFieldUsageStatistics(translator, query.expr);
  317. }
  318. bool ok = false;
  319. try
  320. {
  321. if (!translator.buildCpp(*code, query))
  322. {
  323. wu->setState(WUStateCompleted);
  324. return true;
  325. }
  326. translator.generateStatistics(targetDir, NULL);
  327. translator.finalizeResources();
  328. translator.expandFunctions(true);
  329. }
  330. catch (IECLError * e)
  331. {
  332. if (e->errorCode() != HQLERR_ErrorAlreadyReported)
  333. {
  334. StringBuffer s;
  335. errs->reportError(e->errorCode(), e->errorMessage(s).str(), e->getFilename(), e->getLine(), e->getColumn(), e->getPosition());
  336. }
  337. e->Release();
  338. return false;
  339. }
  340. catch (IException * e)
  341. {
  342. if (e->errorCode() != HQLERR_ErrorAlreadyReported)
  343. {
  344. unsigned errcode = e->errorCode() ? e->errorCode() : 1;
  345. StringBuffer s;
  346. errs->reportError(errcode, e->errorMessage(s).str(), NULL, 0, 0, 1);
  347. }
  348. e->Release();
  349. return false;
  350. }
  351. catch (RELEASE_CATCH_ALL)
  352. {
  353. errs->reportError(99, "Unknown error", NULL, 0, 0, 1);
  354. return false;
  355. }
  356. if (generateTarget == EclGenerateExe)
  357. insertStandAloneCode();
  358. wu->commit();
  359. //Commit work unit so can view graphs etc. while compiling the C++
  360. if ((generateTarget == EclGenerateNone) || wu->getDebugValueBool("OnlyCheckQuery", false))
  361. {
  362. wu->setState(WUStateCompleted);
  363. return false;
  364. }
  365. doExpand(translator);
  366. if (wu->getDebugValueBool("addTimingToWorkunit", true))
  367. wu->setTimerInfo("EclServer: generate code", NULL, msTick()-time, 1, 0);
  368. wu->commit();
  369. addWorkUnitAsResource();
  370. }
  371. noOutput = false;
  372. return true;
  373. }
  374. void HqlDllGenerator::addWorkUnitAsResource()
  375. {
  376. SCMStringBuffer wuXML;
  377. exportWorkUnitToXML(wu, wuXML, false);
  378. code->addCompressResource("WORKUNIT", wuXML.length(), wuXML.str(), NULL, 1000);
  379. }
  380. void HqlDllGenerator::insertStandAloneCode()
  381. {
  382. BuildCtx ctx(static_cast<HqlCppInstance &>(*code), goAtom);
  383. ctx.addQuotedCompound("int main(int argc, const char *argv[])");
  384. ctx.addQuoted("return start_query(argc, argv);\n");
  385. }
  386. void HqlDllGenerator::doExpand(HqlCppTranslator & translator)
  387. {
  388. unsigned startExpandTime = msTick();
  389. unsigned numExtraFiles = translator.getNumExtraCppFiles();
  390. bool isMultiFile = translator.spanMultipleCppFiles() && (numExtraFiles != 0);
  391. expandCode(MAIN_MODULE_TEMPLATE, ".cpp", code, isMultiFile, 0, translator.queryOptions().targetCompiler);
  392. if (isMultiFile)
  393. {
  394. expandCode(HEADER_TEMPLATE, ".hpp", code, true, 0, translator.queryOptions().targetCompiler);
  395. for (unsigned i= 0; i < translator.getNumExtraCppFiles(); i++)
  396. {
  397. StringBuffer fullext;
  398. fullext.append("_").append(i+1).append(".cpp");
  399. expandCode(CHILD_MODULE_TEMPLATE, fullext, code, true, i+1, translator.queryOptions().targetCompiler);
  400. StringBuffer fullname;
  401. fullname.append(wuname).append("_").append(i+1);
  402. sourceFiles.append(fullname);
  403. }
  404. }
  405. unsigned endExpandTime = msTick();
  406. if (wu->getDebugValueBool("addTimingToWorkunit", true))
  407. wu->setTimerInfo("EclServer: write c++", NULL, endExpandTime-startExpandTime, 1, 0);
  408. }
  409. bool HqlDllGenerator::abortRequested()
  410. {
  411. return (wu && wu->aborting());
  412. }
  413. bool HqlDllGenerator::doCompile(ICppCompiler * compiler)
  414. {
  415. ForEachItemIn(i, sourceFiles)
  416. compiler->addSourceFile(sourceFiles.item(i));
  417. unsigned maxThreads = wu->getDebugValueInt("maxCompileThreads", defaultMaxCompileThreads);
  418. compiler->setMaxCompileThreads(maxThreads);
  419. bool debug = wu->getDebugValueBool("debugQuery", false);
  420. bool debugLibrary = debug; // should be wu->getDebugValueBool("debugLibrary", IS_DEBUG_BUILD); change for 3.8
  421. compiler->setDebug(debug);
  422. compiler->setDebugLibrary(debugLibrary);
  423. if (!debug)
  424. {
  425. int optimizeLevel = wu->getDebugValueInt("optimizeLevel", targetClusterType == RoxieCluster ? 3 : -1);
  426. if (optimizeLevel != -1)
  427. compiler->setOptimizeLevel(optimizeLevel);
  428. }
  429. #ifdef __64BIT__
  430. bool target64bit = wu->getDebugValueBool("target64bit", true);
  431. #else
  432. bool target64bit = wu->getDebugValueBool("target64bit", false);
  433. #endif
  434. compiler->setTargetBitLength(target64bit ? 64 : 32);
  435. ForEachItemIn(idx, libraries)
  436. compiler->addLibrary(libraries.item(idx));
  437. StringBuffer options;
  438. StringBufferAdaptor linkOptionAdaptor(options);
  439. wu->getDebugValue("linkOptions", linkOptionAdaptor);
  440. compiler->addLinkOption(options.str());
  441. options.clear();
  442. StringBufferAdaptor optionAdaptor(options);
  443. wu->getDebugValue("compileOptions", optionAdaptor);
  444. compiler->addCompileOption(options.str());
  445. compiler->setAbortChecker(this);
  446. MTIME_SECTION (timer, "Compile_code");
  447. unsigned time = msTick();
  448. PrintLog("Compiling %s", wuname);
  449. bool ok = compiler->compile();
  450. if(ok)
  451. PrintLog("Compiled %s", wuname);
  452. else
  453. PrintLog("Failed to compile %s", wuname);
  454. time = msTick()-time;
  455. if (wu->getDebugValueBool("addTimingToWorkunit", true))
  456. wu->setTimerInfo("EclServer: compile code", NULL, time, 1, 0);
  457. //Keep the files if there was a compile error.
  458. if (ok && deleteGenerated)
  459. {
  460. StringBuffer temp;
  461. remove(temp.clear().append(wuname).append(".cpp").str());
  462. remove(temp.clear().append(wuname).append(".hpp").str());
  463. ForEachItemIn(i, sourceFiles)
  464. {
  465. temp.clear().append(sourceFiles.item(i)).append(".cpp");
  466. remove(temp.str());
  467. }
  468. }
  469. return ok;
  470. }
  471. void HqlDllGenerator::flushResources()
  472. {
  473. StringBuffer resname(wuname);
  474. #ifdef _WIN32
  475. resname.append(".res");
  476. #else
  477. resname.append(".res.o");
  478. #endif
  479. if (code)
  480. code->flushResources(resname.str(), ctxCallback);
  481. }
  482. void HqlDllGenerator::setWuState(bool ok)
  483. {
  484. if(ok)
  485. {
  486. if(checkForLocalFileUploads && wu->requiresLocalFileUpload())
  487. wu->setState(WUStateUploadingFiles);
  488. else
  489. wu->setState(WUStateCompiled);
  490. }
  491. else
  492. wu->setState(WUStateFailed);
  493. }
  494. double HqlDllGenerator::getECLcomplexity(IHqlExpression * exprs)
  495. {
  496. Owned<IHqlCppInstance> code = createCppInstance(wu, NULL);
  497. HqlCppTranslator translator(errs, "temp", code, targetClusterType, NULL);
  498. HqlQueryContext query;
  499. query.expr.set(exprs);
  500. processMetaCommands(translator, wu, query, ctxCallback);
  501. return translator.getComplexity(*code, query.expr);
  502. }
  503. offset_t HqlDllGenerator::getGeneratedSize() const
  504. {
  505. return totalGeneratedSize;
  506. }
  507. extern HQLCPP_API double getECLcomplexity(IHqlExpression * exprs, IErrorReceiver * errs, IWorkUnit *wu, ClusterType targetClusterType)
  508. {
  509. HqlDllGenerator generator(errs, "unknown", NULL, wu, NULL, targetClusterType, NULL, false);
  510. return generator.getECLcomplexity(exprs);
  511. }
  512. extern HQLCPP_API IHqlExprDllGenerator * createDllGenerator(IErrorReceiver * errs, const char *wuname, const char * targetdir, IWorkUnit *wu, const char * template_dir, ClusterType targetClusterType, ICodegenContextCallback *ctxCallback, bool checkForLocalFileUploads)
  513. {
  514. return new HqlDllGenerator(errs, wuname, targetdir, wu, template_dir, targetClusterType, ctxCallback, checkForLocalFileUploads);
  515. }
  516. /*
  517. extern HQLCPP_API void addLibraryReference(IWorkUnit * wu, IHqlLibraryInfo * library, const char * lid)
  518. {
  519. StringBuffer libraryName;
  520. library->getName(libraryName);
  521. Owned<IWULibrary> entry = wu->updateLibraryByName(libraryName.str());
  522. entry->setMajorVersion(library->queryMajorVersion());
  523. entry->setMinorVersion(library->queryMinorVersion());
  524. entry->setCrc(library->queryInterfaceCrc());
  525. if (lid && *lid)
  526. entry->setLID(lid);
  527. }
  528. */
  529. extern HQLCPP_API ClusterType queryClusterType(IConstWorkUnit * wu, ClusterType prevType)
  530. {
  531. ClusterType targetClusterType = prevType;
  532. if (wu->getDebugValueBool("forceRoxie", false))
  533. targetClusterType = RoxieCluster;
  534. else if (prevType == NoCluster)
  535. targetClusterType = ThorLCRCluster;
  536. SCMStringBuffer targetText;
  537. wu->getDebugValue("targetClusterType", targetText);
  538. targetClusterType = getClusterType(targetText.s.str(), targetClusterType);
  539. if ((targetClusterType != RoxieCluster) && wu->getDebugValueBool("forceFakeThor", false))
  540. targetClusterType = HThorCluster;
  541. return targetClusterType;
  542. }
  543. static void processMetaCommands(HqlCppTranslator & translator, IWorkUnit * wu, HqlQueryContext & query, ICodegenContextCallback *ctxCallback)
  544. {
  545. NewThorStoredReplacer transformer(translator, wu, ctxCallback);
  546. translator.traceExpression("before process meta commands", query.expr);
  547. transformer.analyse(query.expr);
  548. if (transformer.needToTransform())
  549. query.expr.setown(transformer.transform(query.expr));
  550. }
  551. extern HQLCPP_API unsigned getLibraryCRC(IHqlExpression * library)
  552. {
  553. assertex(library->getOperator() == no_funcdef);
  554. return getExpressionCRC(library->queryChild(0));
  555. }
  556. void setWorkunitHash(IWorkUnit * wu, IHqlExpression * expr)
  557. {
  558. //Assuming builds come from different branches this will change the crc for each one.
  559. unsigned cacheCRC = crc32(BUILD_TAG, strlen(BUILD_TAG), ACTIVITY_INTERFACE_VERSION);
  560. cacheCRC += getExpressionCRC(expr);
  561. #ifdef _WIN32
  562. cacheCRC++; // make sure CRC is different in windows/linux
  563. #endif
  564. #ifdef __64BIT__
  565. cacheCRC += 2; // make sure CRC is different for different host platform (shouldn't really matter if cross-compiling working properly, but fairly harmless)
  566. #endif
  567. IExtendedWUInterface *ewu = queryExtendedWU(wu);
  568. cacheCRC = ewu->calculateHash(cacheCRC);
  569. wu->setHash(cacheCRC);
  570. }