cmQtAutomoc.cxx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. #include "cmGlobalGenerator.h"
  2. #include "cmLocalGenerator.h"
  3. #include "cmMakefile.h"
  4. #include "cmSystemTools.h"
  5. #include "cmQtAutomoc.h"
  6. #define TRACE_LINE() printf(" %s %d\n", __PRETTY_FUNCTION__, __LINE__)
  7. cmQtAutomoc::cmQtAutomoc()
  8. :Verbose(true)
  9. ,RunMocFailed(false)
  10. ,GenerateAll(false)
  11. {
  12. }
  13. bool cmQtAutomoc::Run(const char* targetDirectory)
  14. {
  15. cmake cm;
  16. cmGlobalGenerator* gg = this->CreateGlobalGenerator(&cm, targetDirectory);
  17. cmMakefile* makefile = gg->GetCurrentLocalGenerator()->GetMakefile();
  18. this->ReadAutomocInfoFile(makefile, targetDirectory);
  19. this->ReadOldMocDefinitionsFile(makefile, targetDirectory);
  20. this->Init();
  21. if (this->QtMajorVersion == "4")
  22. {
  23. this->RunAutomocQt4();
  24. }
  25. this->WriteOldMocDefinitionsFile(targetDirectory);
  26. delete gg;
  27. gg = NULL;
  28. makefile = NULL;
  29. }
  30. cmGlobalGenerator* cmQtAutomoc::CreateGlobalGenerator(cmake* cm,
  31. const char* targetDirectory)
  32. {
  33. cmGlobalGenerator* gg = new cmGlobalGenerator();
  34. gg->SetCMakeInstance(cm);
  35. cmLocalGenerator* lg = gg->CreateLocalGenerator();
  36. lg->GetMakefile()->SetHomeOutputDirectory(targetDirectory);
  37. lg->GetMakefile()->SetStartOutputDirectory(targetDirectory);
  38. lg->GetMakefile()->SetHomeDirectory(targetDirectory);
  39. lg->GetMakefile()->SetStartDirectory(targetDirectory);
  40. gg->SetCurrentLocalGenerator(lg);
  41. return gg;
  42. }
  43. bool cmQtAutomoc::ReadAutomocInfoFile(cmMakefile* makefile,
  44. const char* targetDirectory)
  45. {
  46. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  47. cmSystemTools::ConvertToUnixSlashes(filename);
  48. filename += "/AutomocInfo.cmake";
  49. if (!makefile->ReadListFile(0, filename.c_str()))
  50. {
  51. cmSystemTools::Error("Error processing file:", filename.c_str());
  52. return false;
  53. }
  54. this->QtMajorVersion = makefile->GetSafeDefinition("AM_QT_VERSION_MAJOR");
  55. this->Sources = makefile->GetSafeDefinition("AM_SOURCES");
  56. this->IncludeProjectDirsBefore = makefile->IsOn("AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE");
  57. this->Srcdir = makefile->GetSafeDefinition("AM_CMAKE_CURRENT_SOURCE_DIR");
  58. this->Builddir = makefile->GetSafeDefinition("AM_CMAKE_BINARY_DIR");
  59. this->MocExecutable = makefile->GetSafeDefinition("AM_QT_MOC_EXECUTABLE");
  60. this->MocCompileDefinitionsStr = makefile->GetSafeDefinition("AM_MOC_COMPILE_DEFINITIONS");
  61. this->MocDefinitionsStr = makefile->GetSafeDefinition("AM_MOC_DEFINITIONS");
  62. this->MocIncludesStr = makefile->GetSafeDefinition("AM_MOC_INCLUDES");
  63. this->ProjectBinaryDir = makefile->GetSafeDefinition("AM_CMAKE_BINARY_DIR");
  64. this->ProjectSourceDir = makefile->GetSafeDefinition("AM_CMAKE_SOURCE_DIR");
  65. this->TargetName = makefile->GetSafeDefinition("AM_TARGET_NAME");
  66. return true;
  67. }
  68. bool cmQtAutomoc::ReadOldMocDefinitionsFile(cmMakefile* makefile,
  69. const char* targetDirectory)
  70. {
  71. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  72. cmSystemTools::ConvertToUnixSlashes(filename);
  73. filename += "/AutomocOldMocDefinitions.cmake";
  74. if (makefile->ReadListFile(0, filename.c_str()))
  75. {
  76. this->OldMocDefinitionsStr =
  77. makefile->GetSafeDefinition("AM_OLD_MOC_DEFINITIONS");
  78. }
  79. return true;
  80. }
  81. void cmQtAutomoc::WriteOldMocDefinitionsFile(const char* targetDirectory)
  82. {
  83. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  84. cmSystemTools::ConvertToUnixSlashes(filename);
  85. filename += "/AutomocOldMocDefinitions.cmake";
  86. std::fstream outfile;
  87. outfile.open(filename.c_str(),
  88. std::ios_base::out | std::ios_base::trunc);
  89. outfile << "set(AM_OLD_MOC_DEFINITIONS \""
  90. << this->Join(this->MocDefinitions, ' ') << "\")\n";
  91. outfile.close();
  92. }
  93. void cmQtAutomoc::Init()
  94. {
  95. this->OutMocCppFilename = this->Builddir;
  96. this->OutMocCppFilename += this->TargetName;
  97. this->OutMocCppFilename += ".cpp";
  98. std::vector<std::string> cdefList;
  99. cmSystemTools::ExpandListArgument(this->MocCompileDefinitionsStr, cdefList);
  100. if (!cdefList.empty())
  101. {
  102. for(std::vector<std::string>::const_iterator it = cdefList.begin();
  103. it != cdefList.end();
  104. ++it)
  105. {
  106. this->MocDefinitions.push_back("-D" + (*it));
  107. }
  108. }
  109. else
  110. {
  111. std::string tmpMocDefs = this->MocDefinitionsStr;
  112. cmSystemTools::ReplaceString(tmpMocDefs, " ", ";");
  113. std::vector<std::string> defList;
  114. cmSystemTools::ExpandListArgument(tmpMocDefs, defList);
  115. for(std::vector<std::string>::const_iterator it = defList.begin();
  116. it != defList.end();
  117. ++it)
  118. {
  119. if (this->StartsWith(*it, "-D"))
  120. {
  121. this->MocDefinitions.push_back(*it);
  122. }
  123. }
  124. }
  125. std::vector<std::string> incPaths;
  126. cmSystemTools::ExpandListArgument(this->MocIncludesStr, incPaths);
  127. std::set<std::string> frameworkPaths;
  128. for(std::vector<std::string>::const_iterator it = incPaths.begin();
  129. it != incPaths.end();
  130. ++it)
  131. {
  132. const std::string &path = *it;
  133. this->MocIncludes.push_back("-I" + path);
  134. if (this->EndsWith(path, ".framework/Headers"))
  135. {
  136. // Go up twice to get to the framework root
  137. std::vector<std::string> pathComponents;
  138. cmsys::SystemTools::SplitPath(path.c_str(), pathComponents);
  139. std::string frameworkPath =cmsys::SystemTools::JoinPath(
  140. pathComponents.begin(), pathComponents.end() - 2);
  141. frameworkPaths.insert(frameworkPath);
  142. }
  143. }
  144. for (std::set<std::string>::const_iterator it = frameworkPaths.begin();
  145. it != frameworkPaths.end(); ++it)
  146. {
  147. this->MocIncludes.push_back("-F");
  148. this->MocIncludes.push_back(*it);
  149. }
  150. if (this->IncludeProjectDirsBefore)
  151. {
  152. const std::string &binDir = "-I" + this->ProjectBinaryDir;
  153. const std::string srcDir = "-I" + this->ProjectSourceDir;
  154. std::list<std::string> sortedMocIncludes;
  155. std::list<std::string>::iterator it = this->MocIncludes.begin();
  156. while (it != this->MocIncludes.end())
  157. {
  158. if (this->StartsWith(*it, binDir))
  159. {
  160. sortedMocIncludes.push_back(*it);
  161. it = this->MocIncludes.erase(it);
  162. }
  163. else
  164. {
  165. ++it;
  166. }
  167. }
  168. it = this->MocIncludes.begin();
  169. while (it != this->MocIncludes.end())
  170. {
  171. if (this->StartsWith(*it, srcDir))
  172. {
  173. sortedMocIncludes.push_back(*it);
  174. it = this->MocIncludes.erase(it);
  175. }
  176. else
  177. {
  178. ++it;
  179. }
  180. }
  181. sortedMocIncludes.insert(sortedMocIncludes.end(),
  182. this->MocIncludes.begin(), this->MocIncludes.end());
  183. this->MocIncludes = sortedMocIncludes;
  184. }
  185. }
  186. bool cmQtAutomoc::RunAutomocQt4()
  187. {
  188. if (!cmsys::SystemTools::FileExists(this->OutMocCppFilename.c_str())
  189. || (this->OldMocDefinitionsStr != this->Join(this->MocDefinitions, ' ')))
  190. {
  191. this->GenerateAll = true;
  192. }
  193. // the program goes through all .cpp files to see which moc files are included. It is not really
  194. // interesting how the moc file is named, but what file the moc is created from. Once a moc is
  195. // included the same moc may not be included in the _automoc.cpp file anymore. OTOH if there's a
  196. // header containing Q_OBJECT where no corresponding moc file is included anywhere a
  197. // moc_<filename>.cpp file is created and included in the _automoc.cpp file.
  198. std::map<std::string, std::string> includedMocs; // key = moc source filepath, value = moc output filepath
  199. std::map<std::string, std::string> notIncludedMocs; // key = moc source filepath, value = moc output filename
  200. cmsys::RegularExpression mocIncludeRegExp(
  201. "[\n][ \t]*#[ \t]*include[ \t]+"
  202. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  203. cmsys::RegularExpression qObjectRegExp("[\n][ \t]*Q_OBJECT[^a-zA-Z0-9_]");
  204. std::list<std::string> headerExtensions;
  205. #if defined(_WIN32)
  206. // not case sensitive
  207. headerExtensions.push_back(".h");
  208. headerExtensions.push_back(".hpp");
  209. headerExtensions.push_back(".hxx");
  210. #elif defined(__APPLE__)
  211. headerExtensions.push_back(".h");
  212. headerExtensions.push_back(".hpp");
  213. headerExtensions.push_back(".hxx");
  214. // detect case-sensitive filesystem
  215. long caseSensitive = pathconf(this->Srcdir.c_str(), _PC_CASE_SENSITIVE);
  216. if (caseSensitive == 1)
  217. {
  218. headerExtensions.push_back(".H");
  219. }
  220. #else
  221. headerExtensions.push_back(".h");
  222. headerExtensions.push_back(".hpp");
  223. headerExtensions.push_back(".hxx");
  224. headerExtensions.push_back(".H");
  225. #endif
  226. std::vector<std::string> sourceFiles;
  227. cmSystemTools::ExpandListArgument(this->Sources, sourceFiles);
  228. for (std::vector<std::string>::const_iterator it = sourceFiles.begin();
  229. it != sourceFiles.end();
  230. ++it)
  231. {
  232. const std::string &absFilename = *it;
  233. if (this->Verbose)
  234. {
  235. printf("Checking -%s-\n", absFilename.c_str());
  236. }
  237. std::string extension = absFilename.substr(absFilename.find_last_of('.'));
  238. if (extension == ".cpp" || extension == ".cc" || extension == ".mm"
  239. || extension == ".cxx" || extension == ".C")
  240. {
  241. const std::string contentsString = this->ReadAll(absFilename);
  242. if (contentsString.empty())
  243. {
  244. std::cerr << "automoc4: empty source file: " << absFilename << std::endl;
  245. continue;
  246. }
  247. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  248. cmsys::SystemTools::GetRealPath(absFilename.c_str())) + '/';
  249. int matchOffset = 0;
  250. if (!mocIncludeRegExp.find(contentsString.c_str()))
  251. {
  252. // no moc #include, look whether we need to create a moc from the .h nevertheless
  253. //std::cout << "no moc #include in the .cpp file";
  254. const std::string basename =
  255. cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
  256. for(std::list<std::string>::const_iterator ext =
  257. headerExtensions.begin();
  258. ext != headerExtensions.end();
  259. ++ext)
  260. {
  261. const std::string headername = absPath + basename + (*ext);
  262. if (cmsys::SystemTools::FileExists(headername.c_str())
  263. && includedMocs.find(headername) == includedMocs.end()
  264. && notIncludedMocs.find(headername) == notIncludedMocs.end())
  265. {
  266. const std::string currentMoc = "moc_" + basename + ".cpp";
  267. const std::string contents = this->ReadAll(headername);
  268. if (qObjectRegExp.find(contents))
  269. {
  270. //std::cout << "header contains Q_OBJECT macro";
  271. notIncludedMocs[headername] = currentMoc;
  272. }
  273. break;
  274. }
  275. }
  276. for(std::list<std::string>::const_iterator ext =
  277. headerExtensions.begin();
  278. ext != headerExtensions.end();
  279. ++ext)
  280. {
  281. const std::string privateHeaderName = absPath+basename+"_p"+(*ext);
  282. if (cmsys::SystemTools::FileExists(privateHeaderName.c_str())
  283. && includedMocs.find(privateHeaderName) == includedMocs.end()
  284. && notIncludedMocs.find(privateHeaderName) == notIncludedMocs.end())
  285. {
  286. const std::string currentMoc = "moc_" + basename + "_p.cpp";
  287. const std::string contents = this->ReadAll(privateHeaderName);
  288. if (qObjectRegExp.find(contents))
  289. {
  290. //std::cout << "header contains Q_OBJECT macro";
  291. notIncludedMocs[privateHeaderName] = currentMoc;
  292. }
  293. break;
  294. }
  295. }
  296. }
  297. else
  298. {
  299. // for every moc include in the file
  300. do
  301. {
  302. const std::string currentMoc = mocIncludeRegExp.match(1);
  303. //std::cout << "found moc include: " << currentMoc << std::endl;
  304. std::string basename = cmsys::SystemTools::
  305. GetFilenameWithoutLastExtension(currentMoc);
  306. const bool moc_style = this->StartsWith(basename, "moc_");
  307. // If the moc include is of the moc_foo.cpp style we expect the Q_OBJECT class
  308. // declaration in a header file.
  309. // If the moc include is of the foo.moc style we need to look for a Q_OBJECT
  310. // macro in the current source file, if it contains the macro we generate the
  311. // moc file from the source file, else from the header.
  312. //
  313. // TODO: currently any .moc file name will be used if the source contains
  314. // Q_OBJECT
  315. if (moc_style || !qObjectRegExp.find(contentsString))
  316. {
  317. if (moc_style)
  318. {
  319. // basename should be the part of the moc filename used for finding the
  320. // correct header, so we need to remove the moc_ part
  321. basename = basename.substr(4);
  322. }
  323. bool headerFound = false;
  324. for(std::list<std::string>::const_iterator ext =
  325. headerExtensions.begin();
  326. ext != headerExtensions.end();
  327. ++ext)
  328. {
  329. const std::string &sourceFilePath = absPath + basename + (*ext);
  330. if (cmsys::SystemTools::FileExists(sourceFilePath.c_str()))
  331. {
  332. headerFound = true;
  333. includedMocs[sourceFilePath] = currentMoc;
  334. notIncludedMocs.erase(sourceFilePath);
  335. break;
  336. }
  337. }
  338. if (!headerFound)
  339. {
  340. // the moc file is in a subdir => look for the header in the same subdir
  341. if (currentMoc.find_first_of('/') != std::string::npos)
  342. {
  343. const std::string &filepath = absPath
  344. + cmsys::SystemTools::GetFilenamePath(currentMoc)
  345. + '/' + basename;
  346. for(std::list<std::string>::const_iterator ext =
  347. headerExtensions.begin();
  348. ext != headerExtensions.end();
  349. ++ext)
  350. {
  351. const std::string &sourceFilePath = filepath + (*ext);
  352. if (cmsys::SystemTools::FileExists(sourceFilePath.c_str()))
  353. {
  354. headerFound = true;
  355. includedMocs[sourceFilePath] = currentMoc;
  356. notIncludedMocs.erase(sourceFilePath);
  357. break;
  358. }
  359. }
  360. if (!headerFound)
  361. {
  362. std::cerr << "automoc4: The file \"" << absFilename <<
  363. "\" includes the moc file \"" << currentMoc << "\", but neither \"" <<
  364. absPath + basename + '{' + this->Join(headerExtensions, ',') + "}\" nor \"" <<
  365. filepath + '{' + this->Join(headerExtensions, ',') + '}' <<
  366. "\" exist." << std::endl;
  367. ::exit(EXIT_FAILURE);
  368. }
  369. }
  370. else
  371. {
  372. std::cerr << "automoc4: The file \"" << absFilename <<
  373. "\" includes the moc file \"" << currentMoc << "\", but \"" <<
  374. absPath + basename + '{' + this->Join(headerExtensions, ',') + '}' <<
  375. "\" does not exist." << std::endl;
  376. ::exit(EXIT_FAILURE);
  377. }
  378. }
  379. }
  380. else
  381. {
  382. includedMocs[absFilename] = currentMoc;
  383. notIncludedMocs.erase(absFilename);
  384. }
  385. matchOffset += mocIncludeRegExp.end();
  386. } while(mocIncludeRegExp.find(contentsString.c_str() + matchOffset));
  387. }
  388. }
  389. else if (extension == ".h" || extension == ".hpp"
  390. || extension == ".hxx" || extension == ".H")
  391. {
  392. if (includedMocs.find(absFilename) == includedMocs.end()
  393. && notIncludedMocs.find(absFilename) == notIncludedMocs.end())
  394. {
  395. // if this header is not getting processed yet and is explicitly mentioned for the
  396. // automoc the moc is run unconditionally on the header and the resulting file is
  397. // included in the _automoc.cpp file (unless there's a .cpp file later on that
  398. // includes the moc from this header)
  399. const std::string currentMoc = "moc_" + cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename) + ".cpp";
  400. notIncludedMocs[absFilename] = currentMoc;
  401. }
  402. }
  403. else
  404. {
  405. if (this->Verbose)
  406. {
  407. std::cout << "automoc4: ignoring file '" << absFilename << "' with unknown suffix" << std::endl;
  408. }
  409. }
  410. }
  411. // run moc on all the moc's that are #included in source files
  412. for(std::map<std::string, std::string>::const_iterator it = includedMocs.begin();
  413. it != includedMocs.end();
  414. ++it)
  415. {
  416. this->GenerateMoc(it->first, it->second);
  417. }
  418. std::stringstream outStream(std::stringstream::out);
  419. outStream << "/* This file is autogenerated, do not edit*/\n";
  420. bool automocCppChanged = false;
  421. if (notIncludedMocs.empty())
  422. {
  423. outStream << "enum some_compilers { need_more_than_nothing };\n";
  424. }
  425. else
  426. {
  427. // run moc on the remaining headers and include them in the _automoc.cpp file
  428. for(std::map<std::string, std::string>::const_iterator it = notIncludedMocs.begin();
  429. it != notIncludedMocs.end();
  430. ++it)
  431. {
  432. bool mocSuccess = this->GenerateMoc(it->first, it->second);
  433. if (mocSuccess)
  434. {
  435. automocCppChanged = true;
  436. }
  437. outStream << "#include \"" << it->second << "\"\n";
  438. }
  439. }
  440. if (this->RunMocFailed)
  441. {
  442. // if any moc process failed we don't want to touch the _automoc.cpp file so that
  443. // automoc4 is rerun until the issue is fixed
  444. std::cerr << "returning failed.."<< std::endl;
  445. return false;
  446. }
  447. outStream.flush();
  448. std::string automocSource = outStream.str();
  449. if (!automocCppChanged)
  450. {
  451. // compare contents of the _automoc.cpp file
  452. const std::string oldContents = this->ReadAll(this->OutMocCppFilename);
  453. if (oldContents == automocSource)
  454. {
  455. // nothing changed: don't touch the _automoc.cpp file
  456. return true;
  457. }
  458. }
  459. // either the contents of the _automoc.cpp file or one of the mocs included by it have changed
  460. // source file that includes all remaining moc files (_automoc.cpp file)
  461. std::fstream outfile;
  462. outfile.open(this->OutMocCppFilename.c_str(),
  463. std::ios_base::out | std::ios_base::trunc);
  464. outfile << automocSource;
  465. outfile.close();
  466. return true;
  467. }
  468. bool cmQtAutomoc::GenerateMoc(const std::string& sourceFile,
  469. const std::string& mocFileName)
  470. {
  471. //std::cout << "AutoMoc::generateMoc" << sourceFile << mocFileName << std::endl;
  472. const std::string mocFilePath = this->Builddir + mocFileName;
  473. int sourceNewerThanMoc = 0;
  474. bool success = cmsys::SystemTools::FileTimeCompare(sourceFile.c_str(),
  475. mocFilePath.c_str(),
  476. &sourceNewerThanMoc);
  477. if (this->GenerateAll || !success || sourceNewerThanMoc >= 0)
  478. {
  479. // make sure the directory for the resulting moc file exists
  480. std::string mocDir = mocFilePath.substr(0, mocFilePath.rfind('/'));
  481. if (!cmsys::SystemTools::FileExists(mocDir.c_str(), false))
  482. {
  483. cmsys::SystemTools::MakeDirectory(mocDir.c_str());
  484. }
  485. /* if (this->Verbose)
  486. {
  487. echoColor("Generating " + mocFilePath + " from " + sourceFile);
  488. }
  489. else
  490. {
  491. echoColor("Generating " + mocFileName);
  492. }*/
  493. std::vector<cmStdString> command;
  494. command.push_back(this->MocExecutable);
  495. for (std::list<std::string>::const_iterator it = this->MocIncludes.begin();
  496. it != this->MocIncludes.end();
  497. ++it)
  498. {
  499. command.push_back(*it);
  500. }
  501. for(std::list<std::string>::const_iterator it=this->MocDefinitions.begin();
  502. it != this->MocDefinitions.end();
  503. ++it)
  504. {
  505. command.push_back(*it);
  506. }
  507. #ifdef _WIN32
  508. command.push_back("-DWIN32");
  509. #endif
  510. command.push_back("-o");
  511. command.push_back(mocFilePath);
  512. command.push_back(sourceFile);
  513. if (this->Verbose)
  514. {
  515. for(int i=0; i<command.size(); i++)
  516. {
  517. printf("%s ", command[i].c_str());
  518. }
  519. printf("\n");
  520. }
  521. std::string output;
  522. int retVal = 0;
  523. const bool result = cmSystemTools::RunSingleCommand(command, &output, &retVal);
  524. if (!result || retVal)
  525. {
  526. std::cerr << "automoc4: process for " << mocFilePath << " failed:\n" << output << std::endl;
  527. this->RunMocFailed = true;
  528. cmSystemTools::RemoveFile(mocFilePath.c_str());
  529. }
  530. return true;
  531. }
  532. return false;
  533. }
  534. std::string cmQtAutomoc::Join(const std::list<std::string>& lst,char separator)
  535. {
  536. if (lst.empty())
  537. {
  538. return "";
  539. }
  540. std::string result;
  541. for (std::list<std::string>::const_iterator it = lst.begin();
  542. it != lst.end();
  543. ++it)
  544. {
  545. result += (*it) + separator;
  546. }
  547. result.erase(result.end() - 1);
  548. return result;
  549. }
  550. bool cmQtAutomoc::StartsWith(const std::string& str, const std::string& with)
  551. {
  552. return (str.substr(0, with.length()) == with);
  553. }
  554. bool cmQtAutomoc::EndsWith(const std::string& str, const std::string& with)
  555. {
  556. if (with.length() > (str.length()))
  557. {
  558. return false;
  559. }
  560. return (str.substr(str.length() - with.length(), with.length()) == with);
  561. }
  562. std::string cmQtAutomoc::ReadAll(const std::string& filename)
  563. {
  564. std::ifstream file(filename.c_str());
  565. std::stringstream stream;
  566. stream << file.rdbuf();
  567. file.close();
  568. return stream.str();
  569. }