cmQtAutomoc.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. #include "cmGlobalGenerator.h"
  2. #include "cmLocalGenerator.h"
  3. #include "cmMakefile.h"
  4. #include "cmSourceFile.h"
  5. #include "cmSystemTools.h"
  6. # include <cmsys/Terminal.h>
  7. #include "cmQtAutomoc.h"
  8. cmQtAutomoc::cmQtAutomoc()
  9. :Verbose(cmsys::SystemTools::GetEnv("VERBOSE") != 0)
  10. ,ColorOutput(true)
  11. ,RunMocFailed(false)
  12. ,GenerateAll(false)
  13. {
  14. std::string colorEnv = "";
  15. cmsys::SystemTools::GetEnv("COLOR", colorEnv);
  16. if(!colorEnv.empty())
  17. {
  18. if(cmSystemTools::IsOn(colorEnv.c_str()))
  19. {
  20. this->ColorOutput = true;
  21. }
  22. else
  23. {
  24. this->ColorOutput = false;
  25. }
  26. }
  27. }
  28. void cmQtAutomoc::SetupAutomocTarget(cmTarget* target)
  29. {
  30. cmMakefile* makefile = target->GetMakefile();
  31. const char* targetName = target->GetName();
  32. // don't do anything if there is no Qt4:
  33. std::string qtMajorVersion = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  34. if (qtMajorVersion != "4")
  35. {
  36. return;
  37. }
  38. // create a custom target for running automoc at buildtime:
  39. std::string automocTargetName = targetName;
  40. automocTargetName += "_automoc";
  41. std::string targetDir = makefile->GetCurrentOutputDirectory();
  42. targetDir += makefile->GetCMakeInstance()->GetCMakeFilesDirectory();
  43. targetDir += "/";
  44. targetDir += automocTargetName;
  45. targetDir += ".dir/";
  46. cmCustomCommandLine currentLine;
  47. currentLine.push_back(makefile->GetCMakeInstance()->GetCMakeCommand());
  48. currentLine.push_back("-E");
  49. currentLine.push_back("cmake_automoc");
  50. currentLine.push_back(targetDir);
  51. cmCustomCommandLines commandLines;
  52. commandLines.push_back(currentLine);
  53. std::string workingDirectory = cmSystemTools::CollapseFullPath(
  54. "", makefile->GetCurrentOutputDirectory());
  55. std::vector<std::string> depends;
  56. cmTarget* mocTarget = makefile->AddUtilityCommand(automocTargetName.c_str(),
  57. true,
  58. workingDirectory.c_str(), depends,
  59. commandLines, false, "Automoc target");
  60. target->AddUtility(automocTargetName.c_str());
  61. // configure a file to get all information to automoc at buildtime:
  62. std::string _moc_files;
  63. std::string _moc_headers;
  64. const char* sepFiles = "";
  65. const char* sepHeaders = "";
  66. const std::vector<cmSourceFile*>& srcFiles = target->GetSourceFiles();
  67. for(std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  68. fileIt != srcFiles.end();
  69. ++fileIt)
  70. {
  71. cmSourceFile* sf = *fileIt;
  72. std::string absFile = sf->GetFullPath();
  73. bool skip = cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTOMOC"));
  74. bool generated = cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED"));
  75. if ((skip==false) && (generated == false))
  76. {
  77. std::string ext = sf->GetExtension();
  78. cmSystemTools::FileFormat fileType = cmSystemTools::GetFileFormat(
  79. ext.c_str());
  80. if (fileType == cmSystemTools::CXX_FILE_FORMAT)
  81. {
  82. _moc_files += sepFiles;
  83. _moc_files += absFile;
  84. sepFiles = ";";
  85. }
  86. else if (fileType == cmSystemTools::HEADER_FILE_FORMAT)
  87. {
  88. _moc_headers += sepHeaders;
  89. _moc_headers += absFile;
  90. sepHeaders = ";";
  91. }
  92. }
  93. }
  94. std::string _moc_incs = makefile->GetProperty("INCLUDE_DIRECTORIES");
  95. std::string _moc_defs = makefile->GetProperty("DEFINITIONS");
  96. std::string _moc_compile_defs = makefile->GetProperty("COMPILE_DEFINITIONS");
  97. // forget the variables added here afterwards again:
  98. cmMakefile::ScopePushPop varScope(makefile);
  99. static_cast<void>(varScope);
  100. makefile->AddDefinition("_moc_target_name", automocTargetName.c_str());
  101. makefile->AddDefinition("_moc_incs", _moc_incs.c_str());
  102. makefile->AddDefinition("_moc_defs", _moc_defs.c_str());
  103. makefile->AddDefinition("_moc_compile_defs", _moc_compile_defs.c_str());
  104. makefile->AddDefinition("_moc_files", _moc_files.c_str());
  105. makefile->AddDefinition("_moc_headers", _moc_headers.c_str());
  106. const char* cmakeRoot = makefile->GetDefinition("CMAKE_ROOT");
  107. std::string inputFile = cmakeRoot;
  108. inputFile += "/Modules/AutomocInfo.cmake.in";
  109. std::string outputFile = targetDir;
  110. outputFile += "/AutomocInfo.cmake";
  111. makefile->ConfigureFile(inputFile.c_str(), outputFile.c_str(),
  112. false, true, false);
  113. std::string mocCppFile = makefile->GetCurrentOutputDirectory();
  114. mocCppFile += "/";
  115. mocCppFile += automocTargetName;
  116. mocCppFile += ".cpp";
  117. cmSourceFile* mocCppSource = makefile->GetOrCreateSource(mocCppFile.c_str(),
  118. true);
  119. target->AddSourceFile(mocCppSource);
  120. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES",
  121. mocCppFile.c_str(), false);
  122. }
  123. bool cmQtAutomoc::Run(const char* targetDirectory)
  124. {
  125. cmake cm;
  126. cmGlobalGenerator* gg = this->CreateGlobalGenerator(&cm, targetDirectory);
  127. cmMakefile* makefile = gg->GetCurrentLocalGenerator()->GetMakefile();
  128. this->ReadAutomocInfoFile(makefile, targetDirectory);
  129. this->ReadOldMocDefinitionsFile(makefile, targetDirectory);
  130. this->Init();
  131. if (this->QtMajorVersion == "4")
  132. {
  133. this->RunAutomocQt4();
  134. }
  135. this->WriteOldMocDefinitionsFile(targetDirectory);
  136. delete gg;
  137. gg = NULL;
  138. makefile = NULL;
  139. }
  140. cmGlobalGenerator* cmQtAutomoc::CreateGlobalGenerator(cmake* cm,
  141. const char* targetDirectory)
  142. {
  143. cmGlobalGenerator* gg = new cmGlobalGenerator();
  144. gg->SetCMakeInstance(cm);
  145. cmLocalGenerator* lg = gg->CreateLocalGenerator();
  146. lg->GetMakefile()->SetHomeOutputDirectory(targetDirectory);
  147. lg->GetMakefile()->SetStartOutputDirectory(targetDirectory);
  148. lg->GetMakefile()->SetHomeDirectory(targetDirectory);
  149. lg->GetMakefile()->SetStartDirectory(targetDirectory);
  150. gg->SetCurrentLocalGenerator(lg);
  151. return gg;
  152. }
  153. bool cmQtAutomoc::ReadAutomocInfoFile(cmMakefile* makefile,
  154. const char* targetDirectory)
  155. {
  156. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  157. cmSystemTools::ConvertToUnixSlashes(filename);
  158. filename += "/AutomocInfo.cmake";
  159. if (!makefile->ReadListFile(0, filename.c_str()))
  160. {
  161. cmSystemTools::Error("Error processing file:", filename.c_str());
  162. return false;
  163. }
  164. this->QtMajorVersion = makefile->GetSafeDefinition("AM_QT_VERSION_MAJOR");
  165. this->Sources = makefile->GetSafeDefinition("AM_SOURCES");
  166. this->Headers = makefile->GetSafeDefinition("AM_HEADERS");
  167. this->IncludeProjectDirsBefore = makefile->IsOn(
  168. "AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE");
  169. this->Srcdir = makefile->GetSafeDefinition("AM_CMAKE_CURRENT_SOURCE_DIR");
  170. this->Builddir = makefile->GetSafeDefinition("AM_CMAKE_BINARY_DIR");
  171. this->MocExecutable = makefile->GetSafeDefinition("AM_QT_MOC_EXECUTABLE");
  172. this->MocCompileDefinitionsStr = makefile->GetSafeDefinition(
  173. "AM_MOC_COMPILE_DEFINITIONS");
  174. this->MocDefinitionsStr = makefile->GetSafeDefinition("AM_MOC_DEFINITIONS");
  175. this->MocIncludesStr = makefile->GetSafeDefinition("AM_MOC_INCLUDES");
  176. this->ProjectBinaryDir = makefile->GetSafeDefinition("AM_CMAKE_BINARY_DIR");
  177. this->ProjectSourceDir = makefile->GetSafeDefinition("AM_CMAKE_SOURCE_DIR");
  178. this->TargetName = makefile->GetSafeDefinition("AM_TARGET_NAME");
  179. return true;
  180. }
  181. bool cmQtAutomoc::ReadOldMocDefinitionsFile(cmMakefile* makefile,
  182. const char* targetDirectory)
  183. {
  184. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  185. cmSystemTools::ConvertToUnixSlashes(filename);
  186. filename += "/AutomocOldMocDefinitions.cmake";
  187. if (makefile->ReadListFile(0, filename.c_str()))
  188. {
  189. this->OldMocDefinitionsStr =
  190. makefile->GetSafeDefinition("AM_OLD_MOC_DEFINITIONS");
  191. }
  192. return true;
  193. }
  194. void cmQtAutomoc::WriteOldMocDefinitionsFile(const char* targetDirectory)
  195. {
  196. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  197. cmSystemTools::ConvertToUnixSlashes(filename);
  198. filename += "/AutomocOldMocDefinitions.cmake";
  199. std::fstream outfile;
  200. outfile.open(filename.c_str(),
  201. std::ios_base::out | std::ios_base::trunc);
  202. outfile << "set(AM_OLD_MOC_DEFINITIONS \""
  203. << this->Join(this->MocDefinitions, ' ') << "\")\n";
  204. outfile.close();
  205. }
  206. void cmQtAutomoc::Init()
  207. {
  208. this->OutMocCppFilename = this->Builddir;
  209. this->OutMocCppFilename += this->TargetName;
  210. this->OutMocCppFilename += ".cpp";
  211. std::vector<std::string> cdefList;
  212. cmSystemTools::ExpandListArgument(this->MocCompileDefinitionsStr, cdefList);
  213. if (!cdefList.empty())
  214. {
  215. for(std::vector<std::string>::const_iterator it = cdefList.begin();
  216. it != cdefList.end();
  217. ++it)
  218. {
  219. this->MocDefinitions.push_back("-D" + (*it));
  220. }
  221. }
  222. else
  223. {
  224. std::string tmpMocDefs = this->MocDefinitionsStr;
  225. cmSystemTools::ReplaceString(tmpMocDefs, " ", ";");
  226. std::vector<std::string> defList;
  227. cmSystemTools::ExpandListArgument(tmpMocDefs, defList);
  228. for(std::vector<std::string>::const_iterator it = defList.begin();
  229. it != defList.end();
  230. ++it)
  231. {
  232. if (this->StartsWith(*it, "-D"))
  233. {
  234. this->MocDefinitions.push_back(*it);
  235. }
  236. }
  237. }
  238. std::vector<std::string> incPaths;
  239. cmSystemTools::ExpandListArgument(this->MocIncludesStr, incPaths);
  240. std::set<std::string> frameworkPaths;
  241. for(std::vector<std::string>::const_iterator it = incPaths.begin();
  242. it != incPaths.end();
  243. ++it)
  244. {
  245. const std::string &path = *it;
  246. this->MocIncludes.push_back("-I" + path);
  247. if (this->EndsWith(path, ".framework/Headers"))
  248. {
  249. // Go up twice to get to the framework root
  250. std::vector<std::string> pathComponents;
  251. cmsys::SystemTools::SplitPath(path.c_str(), pathComponents);
  252. std::string frameworkPath =cmsys::SystemTools::JoinPath(
  253. pathComponents.begin(), pathComponents.end() - 2);
  254. frameworkPaths.insert(frameworkPath);
  255. }
  256. }
  257. for (std::set<std::string>::const_iterator it = frameworkPaths.begin();
  258. it != frameworkPaths.end(); ++it)
  259. {
  260. this->MocIncludes.push_back("-F");
  261. this->MocIncludes.push_back(*it);
  262. }
  263. if (this->IncludeProjectDirsBefore)
  264. {
  265. const std::string &binDir = "-I" + this->ProjectBinaryDir;
  266. const std::string srcDir = "-I" + this->ProjectSourceDir;
  267. std::list<std::string> sortedMocIncludes;
  268. std::list<std::string>::iterator it = this->MocIncludes.begin();
  269. while (it != this->MocIncludes.end())
  270. {
  271. if (this->StartsWith(*it, binDir))
  272. {
  273. sortedMocIncludes.push_back(*it);
  274. it = this->MocIncludes.erase(it);
  275. }
  276. else
  277. {
  278. ++it;
  279. }
  280. }
  281. it = this->MocIncludes.begin();
  282. while (it != this->MocIncludes.end())
  283. {
  284. if (this->StartsWith(*it, srcDir))
  285. {
  286. sortedMocIncludes.push_back(*it);
  287. it = this->MocIncludes.erase(it);
  288. }
  289. else
  290. {
  291. ++it;
  292. }
  293. }
  294. sortedMocIncludes.insert(sortedMocIncludes.end(),
  295. this->MocIncludes.begin(), this->MocIncludes.end());
  296. this->MocIncludes = sortedMocIncludes;
  297. }
  298. }
  299. bool cmQtAutomoc::RunAutomocQt4()
  300. {
  301. if (!cmsys::SystemTools::FileExists(this->OutMocCppFilename.c_str())
  302. || (this->OldMocDefinitionsStr != this->Join(this->MocDefinitions, ' ')))
  303. {
  304. this->GenerateAll = true;
  305. }
  306. // the program goes through all .cpp files to see which moc files are
  307. // included. It is not really interesting how the moc file is named, but
  308. // what file the moc is created from. Once a moc is included the same moc
  309. // may not be included in the _automoc.cpp file anymore. OTOH if there's a
  310. // header containing Q_OBJECT where no corresponding moc file is included
  311. // anywhere a moc_<filename>.cpp file is created and included in
  312. // the _automoc.cpp file.
  313. // key = moc source filepath, value = moc output filepath
  314. std::map<std::string, std::string> includedMocs;
  315. // key = moc source filepath, value = moc output filename
  316. std::map<std::string, std::string> notIncludedMocs;
  317. std::vector<std::string> sourceFiles;
  318. cmSystemTools::ExpandListArgument(this->Sources, sourceFiles);
  319. for (std::vector<std::string>::const_iterator it = sourceFiles.begin();
  320. it != sourceFiles.end();
  321. ++it)
  322. {
  323. const std::string &absFilename = *it;
  324. if (this->Verbose)
  325. {
  326. std::cout << "AUTOMOC: Checking " << absFilename << std::endl;
  327. }
  328. this->ParseCppFile(absFilename, includedMocs, notIncludedMocs);
  329. }
  330. std::vector<std::string> headerFiles;
  331. cmSystemTools::ExpandListArgument(this->Headers, headerFiles);
  332. for (std::vector<std::string>::const_iterator it = headerFiles.begin();
  333. it != headerFiles.end();
  334. ++it)
  335. {
  336. const std::string &absFilename = *it;
  337. if (this->Verbose)
  338. {
  339. std::cout << "AUTOMOC: Checking " << absFilename << std::endl;
  340. }
  341. if (includedMocs.find(absFilename) == includedMocs.end()
  342. && notIncludedMocs.find(absFilename) == notIncludedMocs.end())
  343. {
  344. // if this header is not getting processed yet and is explicitly
  345. // mentioned for the automoc the moc is run unconditionally on the
  346. // header and the resulting file is included in the _automoc.cpp file
  347. // (unless there's a .cpp file later on that includes the moc from
  348. // this header)
  349. const std::string currentMoc = "moc_" + cmsys::SystemTools::
  350. GetFilenameWithoutLastExtension(absFilename) + ".cpp";
  351. notIncludedMocs[absFilename] = currentMoc;
  352. }
  353. }
  354. // run moc on all the moc's that are #included in source files
  355. for(std::map<std::string, std::string>::const_iterator
  356. it = includedMocs.begin();
  357. it != includedMocs.end();
  358. ++it)
  359. {
  360. this->GenerateMoc(it->first, it->second);
  361. }
  362. std::stringstream outStream(std::stringstream::out);
  363. outStream << "/* This file is autogenerated, do not edit*/\n";
  364. bool automocCppChanged = false;
  365. if (notIncludedMocs.empty())
  366. {
  367. outStream << "enum some_compilers { need_more_than_nothing };\n";
  368. }
  369. else
  370. {
  371. // run moc on the remaining headers and include them in
  372. // the _automoc.cpp file
  373. for(std::map<std::string, std::string>::const_iterator
  374. it = notIncludedMocs.begin();
  375. it != notIncludedMocs.end();
  376. ++it)
  377. {
  378. bool mocSuccess = this->GenerateMoc(it->first, it->second);
  379. if (mocSuccess)
  380. {
  381. automocCppChanged = true;
  382. }
  383. outStream << "#include \"" << it->second << "\"\n";
  384. }
  385. }
  386. if (this->RunMocFailed)
  387. {
  388. std::cerr << "returning failed.."<< std::endl;
  389. return false;
  390. }
  391. outStream.flush();
  392. std::string automocSource = outStream.str();
  393. if (!automocCppChanged)
  394. {
  395. // compare contents of the _automoc.cpp file
  396. const std::string oldContents = this->ReadAll(this->OutMocCppFilename);
  397. if (oldContents == automocSource)
  398. {
  399. // nothing changed: don't touch the _automoc.cpp file
  400. return true;
  401. }
  402. }
  403. // source file that includes all remaining moc files (_automoc.cpp file)
  404. std::fstream outfile;
  405. outfile.open(this->OutMocCppFilename.c_str(),
  406. std::ios_base::out | std::ios_base::trunc);
  407. outfile << automocSource;
  408. outfile.close();
  409. return true;
  410. }
  411. void cmQtAutomoc::ParseCppFile(const std::string& absFilename,
  412. std::map<std::string, std::string>& includedMocs,
  413. std::map<std::string, std::string>& notIncludedMocs)
  414. {
  415. cmsys::RegularExpression mocIncludeRegExp(
  416. "[\n][ \t]*#[ \t]*include[ \t]+"
  417. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  418. cmsys::RegularExpression qObjectRegExp("[\n][ \t]*Q_OBJECT[^a-zA-Z0-9_]");
  419. std::list<std::string> headerExtensions;
  420. headerExtensions.push_back(".h");
  421. headerExtensions.push_back(".hpp");
  422. headerExtensions.push_back(".hxx");
  423. #if defined(_WIN32)
  424. // not case sensitive, don't add ".H"
  425. #elif defined(__APPLE__)
  426. // detect case-sensitive filesystem
  427. long caseSensitive = pathconf(this->Srcdir.c_str(), _PC_CASE_SENSITIVE);
  428. if (caseSensitive == 1)
  429. {
  430. headerExtensions.push_back(".H");
  431. }
  432. #else
  433. headerExtensions.push_back(".H");
  434. #endif
  435. const std::string contentsString = this->ReadAll(absFilename);
  436. if (contentsString.empty())
  437. {
  438. std::cerr << "AUTOMOC: empty source file: " << absFilename << std::endl;
  439. return;
  440. }
  441. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  442. cmsys::SystemTools::GetRealPath(absFilename.c_str())) + '/';
  443. int matchOffset = 0;
  444. if (!mocIncludeRegExp.find(contentsString.c_str()))
  445. {
  446. // no moc #include, look whether we need to create a moc from
  447. // the .h nevertheless
  448. const std::string basename =
  449. cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
  450. for(std::list<std::string>::const_iterator ext = headerExtensions.begin();
  451. ext != headerExtensions.end();
  452. ++ext)
  453. {
  454. const std::string headername = absPath + basename + (*ext);
  455. if (cmsys::SystemTools::FileExists(headername.c_str())
  456. && includedMocs.find(headername) == includedMocs.end()
  457. && notIncludedMocs.find(headername) == notIncludedMocs.end())
  458. {
  459. const std::string currentMoc = "moc_" + basename + ".cpp";
  460. const std::string contents = this->ReadAll(headername);
  461. if (qObjectRegExp.find(contents))
  462. {
  463. //std::cout << "header contains Q_OBJECT macro";
  464. notIncludedMocs[headername] = currentMoc;
  465. }
  466. break;
  467. }
  468. }
  469. for(std::list<std::string>::const_iterator ext = headerExtensions.begin();
  470. ext != headerExtensions.end();
  471. ++ext)
  472. {
  473. const std::string privateHeaderName = absPath+basename+"_p"+(*ext);
  474. if (cmsys::SystemTools::FileExists(privateHeaderName.c_str())
  475. && includedMocs.find(privateHeaderName) == includedMocs.end()
  476. && notIncludedMocs.find(privateHeaderName) == notIncludedMocs.end())
  477. {
  478. const std::string currentMoc = "moc_" + basename + "_p.cpp";
  479. const std::string contents = this->ReadAll(privateHeaderName);
  480. if (qObjectRegExp.find(contents))
  481. {
  482. //std::cout << "header contains Q_OBJECT macro";
  483. notIncludedMocs[privateHeaderName] = currentMoc;
  484. }
  485. break;
  486. }
  487. }
  488. }
  489. else
  490. {
  491. // for every moc include in the file
  492. do
  493. {
  494. const std::string currentMoc = mocIncludeRegExp.match(1);
  495. //std::cout << "found moc include: " << currentMoc << std::endl;
  496. std::string basename = cmsys::SystemTools::
  497. GetFilenameWithoutLastExtension(currentMoc);
  498. const bool moc_style = this->StartsWith(basename, "moc_");
  499. // If the moc include is of the moc_foo.cpp style we expect
  500. // the Q_OBJECT class declaration in a header file.
  501. // If the moc include is of the foo.moc style we need to look for
  502. // a Q_OBJECT macro in the current source file, if it contains the
  503. // macro we generate the moc file from the source file, else from the
  504. // header.
  505. // Q_OBJECT
  506. if (moc_style || !qObjectRegExp.find(contentsString))
  507. {
  508. if (moc_style)
  509. {
  510. // basename should be the part of the moc filename used for
  511. // finding the correct header, so we need to remove the moc_ part
  512. basename = basename.substr(4);
  513. }
  514. bool headerFound = false;
  515. for(std::list<std::string>::const_iterator ext =
  516. headerExtensions.begin();
  517. ext != headerExtensions.end();
  518. ++ext)
  519. {
  520. const std::string &sourceFilePath = absPath + basename + (*ext);
  521. if (cmsys::SystemTools::FileExists(sourceFilePath.c_str()))
  522. {
  523. headerFound = true;
  524. includedMocs[sourceFilePath] = currentMoc;
  525. notIncludedMocs.erase(sourceFilePath);
  526. break;
  527. }
  528. }
  529. if (!headerFound)
  530. {
  531. // the moc file is in a subdir => look for the header in the
  532. // same subdir
  533. if (currentMoc.find_first_of('/') != std::string::npos)
  534. {
  535. const std::string &filepath = absPath
  536. + cmsys::SystemTools::GetFilenamePath(currentMoc)
  537. + '/' + basename;
  538. for(std::list<std::string>::const_iterator ext =
  539. headerExtensions.begin();
  540. ext != headerExtensions.end();
  541. ++ext)
  542. {
  543. const std::string &sourceFilePath = filepath + (*ext);
  544. if (cmsys::SystemTools::FileExists(sourceFilePath.c_str()))
  545. {
  546. headerFound = true;
  547. includedMocs[sourceFilePath] = currentMoc;
  548. notIncludedMocs.erase(sourceFilePath);
  549. break;
  550. }
  551. }
  552. if (!headerFound)
  553. {
  554. std::cerr << "AUTOMOC: The file \"" << absFilename
  555. << "\" includes the moc file \"" << currentMoc
  556. << "\", but neither \"" << absPath << basename
  557. << '{' << this->Join(headerExtensions, ',')
  558. << "}\" nor \"" << filepath << '{'
  559. << this->Join(headerExtensions, ',') << '}'
  560. << "\" exist." << std::endl;
  561. ::exit(EXIT_FAILURE);
  562. }
  563. }
  564. else
  565. {
  566. std::cerr << "AUTOMOC: The file \"" << absFilename
  567. << "\" includes the moc file \"" << currentMoc
  568. << "\", but \"" << absPath << basename << '{'
  569. << this->Join(headerExtensions, ',') << '}'
  570. << "\" does not exist." << std::endl;
  571. ::exit(EXIT_FAILURE);
  572. }
  573. }
  574. }
  575. else
  576. {
  577. includedMocs[absFilename] = currentMoc;
  578. notIncludedMocs.erase(absFilename);
  579. }
  580. matchOffset += mocIncludeRegExp.end();
  581. } while(mocIncludeRegExp.find(contentsString.c_str() + matchOffset));
  582. }
  583. }
  584. bool cmQtAutomoc::GenerateMoc(const std::string& sourceFile,
  585. const std::string& mocFileName)
  586. {
  587. const std::string mocFilePath = this->Builddir + mocFileName;
  588. int sourceNewerThanMoc = 0;
  589. bool success = cmsys::SystemTools::FileTimeCompare(sourceFile.c_str(),
  590. mocFilePath.c_str(),
  591. &sourceNewerThanMoc);
  592. if (this->GenerateAll || !success || sourceNewerThanMoc >= 0)
  593. {
  594. // make sure the directory for the resulting moc file exists
  595. std::string mocDir = mocFilePath.substr(0, mocFilePath.rfind('/'));
  596. if (!cmsys::SystemTools::FileExists(mocDir.c_str(), false))
  597. {
  598. cmsys::SystemTools::MakeDirectory(mocDir.c_str());
  599. }
  600. std::string msg = "Generating ";
  601. msg += mocFileName;
  602. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue
  603. |cmsysTerminal_Color_ForegroundBold,
  604. msg.c_str(), true, this->ColorOutput);
  605. std::vector<cmStdString> command;
  606. command.push_back(this->MocExecutable);
  607. for (std::list<std::string>::const_iterator it = this->MocIncludes.begin();
  608. it != this->MocIncludes.end();
  609. ++it)
  610. {
  611. command.push_back(*it);
  612. }
  613. for(std::list<std::string>::const_iterator it=this->MocDefinitions.begin();
  614. it != this->MocDefinitions.end();
  615. ++it)
  616. {
  617. command.push_back(*it);
  618. }
  619. #ifdef _WIN32
  620. command.push_back("-DWIN32");
  621. #endif
  622. command.push_back("-o");
  623. command.push_back(mocFilePath);
  624. command.push_back(sourceFile);
  625. if (this->Verbose)
  626. {
  627. for(int i=0; i<command.size(); i++)
  628. {
  629. std::cout << command[i] << " ";
  630. }
  631. std::cout << std::endl;
  632. }
  633. std::string output;
  634. int retVal = 0;
  635. bool result = cmSystemTools::RunSingleCommand(command, &output, &retVal);
  636. if (!result || retVal)
  637. {
  638. std::cerr << "AUTOMOC: process for " << mocFilePath << " failed:\n"
  639. << output << std::endl;
  640. this->RunMocFailed = true;
  641. cmSystemTools::RemoveFile(mocFilePath.c_str());
  642. }
  643. return true;
  644. }
  645. return false;
  646. }
  647. std::string cmQtAutomoc::Join(const std::list<std::string>& lst,char separator)
  648. {
  649. if (lst.empty())
  650. {
  651. return "";
  652. }
  653. std::string result;
  654. for (std::list<std::string>::const_iterator it = lst.begin();
  655. it != lst.end();
  656. ++it)
  657. {
  658. result += (*it) + separator;
  659. }
  660. result.erase(result.end() - 1);
  661. return result;
  662. }
  663. bool cmQtAutomoc::StartsWith(const std::string& str, const std::string& with)
  664. {
  665. return (str.substr(0, with.length()) == with);
  666. }
  667. bool cmQtAutomoc::EndsWith(const std::string& str, const std::string& with)
  668. {
  669. if (with.length() > (str.length()))
  670. {
  671. return false;
  672. }
  673. return (str.substr(str.length() - with.length(), with.length()) == with);
  674. }
  675. std::string cmQtAutomoc::ReadAll(const std::string& filename)
  676. {
  677. std::ifstream file(filename.c_str());
  678. std::stringstream stream;
  679. stream << file.rdbuf();
  680. file.close();
  681. return stream.str();
  682. }