cmQtAutomoc.cxx 26 KB

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