cmQtAutomoc.cxx 25 KB

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