cmQtAutomoc.cxx 26 KB

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