cmQtAutomoc.cxx 26 KB

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