cmQtAutoRcc.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmQtAutoRcc.h"
  4. #include <algorithm>
  5. #include <string>
  6. #include <vector>
  7. #include <cmext/algorithm>
  8. #include "cmCryptoHash.h"
  9. #include "cmDuration.h"
  10. #include "cmFileLock.h"
  11. #include "cmFileLockResult.h"
  12. #include "cmFileTime.h"
  13. #include "cmProcessOutput.h"
  14. #include "cmQtAutoGen.h"
  15. #include "cmQtAutoGenerator.h"
  16. #include "cmStringAlgorithms.h"
  17. #include "cmSystemTools.h"
  18. namespace {
  19. /** \class cmQtAutoRccT
  20. * \brief AUTORCC generator
  21. */
  22. class cmQtAutoRccT : public cmQtAutoGenerator
  23. {
  24. public:
  25. cmQtAutoRccT();
  26. ~cmQtAutoRccT() override;
  27. cmQtAutoRccT(cmQtAutoRccT const&) = delete;
  28. cmQtAutoRccT& operator=(cmQtAutoRccT const&) = delete;
  29. private:
  30. // -- Utility
  31. bool IsMultiConfig() const { return this->MultiConfig_; }
  32. std::string MultiConfigOutput() const;
  33. // -- Abstract processing interface
  34. bool InitFromInfo(InfoT const& info) override;
  35. bool Process() override;
  36. // -- Settings file
  37. bool SettingsFileRead();
  38. bool SettingsFileWrite();
  39. // -- Tests
  40. bool TestQrcRccFiles(bool& generate);
  41. bool TestResources(bool& generate);
  42. bool TestInfoFile();
  43. // -- Generation
  44. bool GenerateRcc();
  45. bool GenerateWrapper();
  46. private:
  47. // -- Config settings
  48. bool MultiConfig_ = false;
  49. // -- Directories
  50. std::string AutogenBuildDir_;
  51. std::string IncludeDir_;
  52. // -- Qt environment
  53. std::string RccExecutable_;
  54. cmFileTime RccExecutableTime_;
  55. std::vector<std::string> RccListOptions_;
  56. // -- Job
  57. std::string LockFile_;
  58. cmFileLock LockFileLock_;
  59. std::string QrcFile_;
  60. std::string QrcFileName_;
  61. std::string QrcFileDir_;
  62. cmFileTime QrcFileTime_;
  63. std::string RccPathChecksum_;
  64. std::string RccFileName_;
  65. std::string RccFileOutput_;
  66. std::string RccFilePublic_;
  67. cmFileTime RccFileTime_;
  68. std::string Reason;
  69. std::vector<std::string> Options_;
  70. std::vector<std::string> Inputs_;
  71. // -- Settings file
  72. std::string SettingsFile_;
  73. std::string SettingsString_;
  74. bool SettingsChanged_ = false;
  75. bool BuildFileChanged_ = false;
  76. };
  77. cmQtAutoRccT::cmQtAutoRccT()
  78. : cmQtAutoGenerator(GenT::RCC)
  79. {
  80. }
  81. cmQtAutoRccT::~cmQtAutoRccT() = default;
  82. bool cmQtAutoRccT::InitFromInfo(InfoT const& info)
  83. {
  84. // -- Required settings
  85. if (!info.GetBool("MULTI_CONFIG", this->MultiConfig_, true) ||
  86. !info.GetString("BUILD_DIR", this->AutogenBuildDir_, true) ||
  87. !info.GetStringConfig("INCLUDE_DIR", this->IncludeDir_, true) ||
  88. !info.GetString("RCC_EXECUTABLE", this->RccExecutable_, true) ||
  89. !info.GetArray("RCC_LIST_OPTIONS", this->RccListOptions_, false) ||
  90. !info.GetString("LOCK_FILE", this->LockFile_, true) ||
  91. !info.GetStringConfig("SETTINGS_FILE", this->SettingsFile_, true) ||
  92. !info.GetString("SOURCE", this->QrcFile_, true) ||
  93. !info.GetString("OUTPUT_CHECKSUM", this->RccPathChecksum_, true) ||
  94. !info.GetString("OUTPUT_NAME", this->RccFileName_, true) ||
  95. !info.GetArray("OPTIONS", this->Options_, false) ||
  96. !info.GetArray("INPUTS", this->Inputs_, false)) {
  97. return false;
  98. }
  99. // -- Derive information
  100. this->QrcFileName_ = cmSystemTools::GetFilenameName(this->QrcFile_);
  101. this->QrcFileDir_ = cmSystemTools::GetFilenamePath(this->QrcFile_);
  102. this->RccFilePublic_ =
  103. cmStrCat(this->AutogenBuildDir_, '/', this->RccPathChecksum_, '/',
  104. this->RccFileName_);
  105. // rcc output file name
  106. if (this->IsMultiConfig()) {
  107. this->RccFileOutput_ =
  108. cmStrCat(this->IncludeDir_, '/', this->MultiConfigOutput());
  109. } else {
  110. this->RccFileOutput_ = this->RccFilePublic_;
  111. }
  112. // -- Checks
  113. if (!this->RccExecutableTime_.Load(this->RccExecutable_)) {
  114. return info.LogError(cmStrCat("The rcc executable ",
  115. this->MessagePath(this->RccExecutable_),
  116. " does not exist."));
  117. }
  118. return true;
  119. }
  120. bool cmQtAutoRccT::Process()
  121. {
  122. if (!this->SettingsFileRead()) {
  123. return false;
  124. }
  125. // Test if the rcc output needs to be regenerated
  126. bool generate = false;
  127. if (!this->TestQrcRccFiles(generate)) {
  128. return false;
  129. }
  130. if (!generate && !this->TestResources(generate)) {
  131. return false;
  132. }
  133. // Generate on demand
  134. if (generate) {
  135. if (!this->GenerateRcc()) {
  136. return false;
  137. }
  138. } else {
  139. // Test if the info file is newer than the output file
  140. if (!this->TestInfoFile()) {
  141. return false;
  142. }
  143. }
  144. if (!this->GenerateWrapper()) {
  145. return false;
  146. }
  147. return this->SettingsFileWrite();
  148. }
  149. std::string cmQtAutoRccT::MultiConfigOutput() const
  150. {
  151. return cmStrCat(this->RccPathChecksum_, '/',
  152. AppendFilenameSuffix(this->RccFileName_, "_CMAKE_"));
  153. }
  154. bool cmQtAutoRccT::SettingsFileRead()
  155. {
  156. // Compose current settings strings
  157. {
  158. cmCryptoHash cryptoHash(cmCryptoHash::AlgoSHA256);
  159. auto cha = [&cryptoHash](cm::string_view value) {
  160. cryptoHash.Append(value);
  161. cryptoHash.Append(";");
  162. };
  163. cha(this->RccExecutable_);
  164. std::for_each(this->RccListOptions_.begin(), this->RccListOptions_.end(),
  165. cha);
  166. cha(this->QrcFile_);
  167. cha(this->RccPathChecksum_);
  168. cha(this->RccFileName_);
  169. std::for_each(this->Options_.begin(), this->Options_.end(), cha);
  170. std::for_each(this->Inputs_.begin(), this->Inputs_.end(), cha);
  171. this->SettingsString_ = cryptoHash.FinalizeHex();
  172. }
  173. // Make sure the settings file exists
  174. if (!cmSystemTools::FileExists(this->SettingsFile_, true)) {
  175. // Touch the settings file to make sure it exists
  176. if (!cmSystemTools::Touch(this->SettingsFile_, true)) {
  177. this->Log().Error(GenT::RCC,
  178. cmStrCat("Touching the settings file ",
  179. this->MessagePath(this->SettingsFile_),
  180. " failed."));
  181. return false;
  182. }
  183. }
  184. // Lock the lock file
  185. {
  186. // Make sure the lock file exists
  187. if (!cmSystemTools::FileExists(this->LockFile_, true)) {
  188. if (!cmSystemTools::Touch(this->LockFile_, true)) {
  189. this->Log().Error(GenT::RCC,
  190. cmStrCat("Touching the lock file ",
  191. this->MessagePath(this->LockFile_),
  192. " failed."));
  193. return false;
  194. }
  195. }
  196. // Lock the lock file
  197. cmFileLockResult lockResult = this->LockFileLock_.Lock(
  198. this->LockFile_, static_cast<unsigned long>(-1));
  199. if (!lockResult.IsOk()) {
  200. this->Log().Error(GenT::RCC,
  201. cmStrCat("Locking of the lock file ",
  202. this->MessagePath(this->LockFile_),
  203. " failed.\n", lockResult.GetOutputMessage()));
  204. return false;
  205. }
  206. }
  207. // Read old settings
  208. {
  209. std::string content;
  210. if (FileRead(content, this->SettingsFile_)) {
  211. this->SettingsChanged_ =
  212. (this->SettingsString_ != SettingsFind(content, "rcc"));
  213. // In case any setting changed clear the old settings file.
  214. // This triggers a full rebuild on the next run if the current
  215. // build is aborted before writing the current settings in the end.
  216. if (this->SettingsChanged_) {
  217. std::string error;
  218. if (!FileWrite(this->SettingsFile_, "", &error)) {
  219. this->Log().Error(GenT::RCC,
  220. cmStrCat("Clearing of the settings file ",
  221. this->MessagePath(this->SettingsFile_),
  222. " failed.\n", error));
  223. return false;
  224. }
  225. }
  226. } else {
  227. this->SettingsChanged_ = true;
  228. }
  229. }
  230. return true;
  231. }
  232. bool cmQtAutoRccT::SettingsFileWrite()
  233. {
  234. // Only write if any setting changed
  235. if (this->SettingsChanged_) {
  236. if (this->Log().Verbose()) {
  237. this->Log().Info(GenT::RCC,
  238. "Writing settings file " +
  239. this->MessagePath(this->SettingsFile_));
  240. }
  241. // Write settings file
  242. std::string content = cmStrCat("rcc:", this->SettingsString_, '\n');
  243. std::string error;
  244. if (!FileWrite(this->SettingsFile_, content, &error)) {
  245. this->Log().Error(GenT::RCC,
  246. cmStrCat("Writing of the settings file ",
  247. this->MessagePath(this->SettingsFile_),
  248. " failed.\n", error));
  249. // Remove old settings file to trigger a full rebuild on the next run
  250. cmSystemTools::RemoveFile(this->SettingsFile_);
  251. return false;
  252. }
  253. }
  254. // Unlock the lock file
  255. this->LockFileLock_.Release();
  256. return true;
  257. }
  258. /// Do basic checks if rcc generation is required
  259. bool cmQtAutoRccT::TestQrcRccFiles(bool& generate)
  260. {
  261. // Test if the rcc input file exists
  262. if (!this->QrcFileTime_.Load(this->QrcFile_)) {
  263. this->Log().Error(GenT::RCC,
  264. cmStrCat("The resources file ",
  265. this->MessagePath(this->QrcFile_),
  266. " does not exist"));
  267. return false;
  268. }
  269. // Test if the rcc output file exists
  270. if (!this->RccFileTime_.Load(this->RccFileOutput_)) {
  271. if (this->Log().Verbose()) {
  272. this->Reason =
  273. cmStrCat("Generating ", this->MessagePath(this->RccFileOutput_),
  274. ", because it doesn't exist, from ",
  275. this->MessagePath(this->QrcFile_));
  276. }
  277. generate = true;
  278. return true;
  279. }
  280. // Test if the settings changed
  281. if (this->SettingsChanged_) {
  282. if (this->Log().Verbose()) {
  283. this->Reason =
  284. cmStrCat("Generating ", this->MessagePath(this->RccFileOutput_),
  285. ", because the rcc settings changed, from ",
  286. this->MessagePath(this->QrcFile_));
  287. }
  288. generate = true;
  289. return true;
  290. }
  291. // Test if the rcc output file is older than the .qrc file
  292. if (this->RccFileTime_.Older(this->QrcFileTime_)) {
  293. if (this->Log().Verbose()) {
  294. this->Reason = cmStrCat(
  295. "Generating ", this->MessagePath(this->RccFileOutput_),
  296. ", because it is older than ", this->MessagePath(this->QrcFile_),
  297. ", from ", this->MessagePath(this->QrcFile_));
  298. }
  299. generate = true;
  300. return true;
  301. }
  302. // Test if the rcc output file is older than the rcc executable
  303. if (this->RccFileTime_.Older(this->RccExecutableTime_)) {
  304. if (this->Log().Verbose()) {
  305. this->Reason =
  306. cmStrCat("Generating ", this->MessagePath(this->RccFileOutput_),
  307. ", because it is older than the rcc executable, from ",
  308. this->MessagePath(this->QrcFile_));
  309. }
  310. generate = true;
  311. return true;
  312. }
  313. return true;
  314. }
  315. bool cmQtAutoRccT::TestResources(bool& generate)
  316. {
  317. // Read resource files list
  318. if (this->Inputs_.empty()) {
  319. std::string error;
  320. RccLister const lister(this->RccExecutable_, this->RccListOptions_);
  321. if (!lister.list(this->QrcFile_, this->Inputs_, error,
  322. this->Log().Verbose())) {
  323. this->Log().Error(GenT::RCC,
  324. cmStrCat("Listing of ",
  325. this->MessagePath(this->QrcFile_),
  326. " failed.\n", error));
  327. return false;
  328. }
  329. }
  330. // Check if any resource file is newer than the rcc output file
  331. for (std::string const& resFile : this->Inputs_) {
  332. // Check if the resource file exists
  333. cmFileTime fileTime;
  334. if (!fileTime.Load(resFile)) {
  335. this->Log().Error(GenT::RCC,
  336. cmStrCat("The resource file ",
  337. this->MessagePath(resFile), " listed in ",
  338. this->MessagePath(this->QrcFile_),
  339. " does not exist."));
  340. return false;
  341. }
  342. // Check if the resource file is newer than the rcc output file
  343. if (this->RccFileTime_.Older(fileTime)) {
  344. if (this->Log().Verbose()) {
  345. this->Reason =
  346. cmStrCat("Generating ", this->MessagePath(this->RccFileOutput_),
  347. ", because it is older than ", this->MessagePath(resFile),
  348. ", from ", this->MessagePath(this->QrcFile_));
  349. }
  350. generate = true;
  351. break;
  352. }
  353. }
  354. return true;
  355. }
  356. bool cmQtAutoRccT::TestInfoFile()
  357. {
  358. // Test if the rcc output file is older than the info file
  359. if (this->RccFileTime_.Older(this->InfoFileTime())) {
  360. if (this->Log().Verbose()) {
  361. this->Log().Info(GenT::RCC,
  362. cmStrCat("Touching ",
  363. this->MessagePath(this->RccFileOutput_),
  364. " because it is older than ",
  365. this->MessagePath(this->InfoFile())));
  366. }
  367. // Touch build file
  368. if (!cmSystemTools::Touch(this->RccFileOutput_, false)) {
  369. this->Log().Error(GenT::RCC,
  370. cmStrCat("Touching ",
  371. this->MessagePath(this->RccFileOutput_),
  372. " failed."));
  373. return false;
  374. }
  375. this->BuildFileChanged_ = true;
  376. }
  377. return true;
  378. }
  379. bool cmQtAutoRccT::GenerateRcc()
  380. {
  381. // Make parent directory
  382. if (!MakeParentDirectory(this->RccFileOutput_)) {
  383. this->Log().Error(GenT::RCC,
  384. cmStrCat("Could not create parent directory of ",
  385. this->MessagePath(this->RccFileOutput_)));
  386. return false;
  387. }
  388. // Compose rcc command
  389. std::vector<std::string> cmd;
  390. cmd.push_back(this->RccExecutable_);
  391. cm::append(cmd, this->Options_);
  392. cmd.emplace_back("-o");
  393. cmd.push_back(this->RccFileOutput_);
  394. cmd.push_back(this->QrcFile_);
  395. // Log reason and command
  396. if (this->Log().Verbose()) {
  397. this->Log().Info(GenT::RCC,
  398. cmStrCat(this->Reason,
  399. cmHasSuffix(this->Reason, '\n') ? "" : "\n",
  400. QuotedCommand(cmd), '\n'));
  401. }
  402. std::string rccStdOut;
  403. std::string rccStdErr;
  404. int retVal = 0;
  405. bool result = cmSystemTools::RunSingleCommand(
  406. cmd, &rccStdOut, &rccStdErr, &retVal, this->AutogenBuildDir_.c_str(),
  407. cmSystemTools::OUTPUT_NONE, cmDuration::zero(), cmProcessOutput::Auto);
  408. if (!result || (retVal != 0)) {
  409. // rcc process failed
  410. this->Log().ErrorCommand(GenT::RCC,
  411. cmStrCat("The rcc process failed to compile\n ",
  412. this->MessagePath(this->QrcFile_),
  413. "\ninto\n ",
  414. this->MessagePath(this->RccFileOutput_)),
  415. cmd, rccStdOut + rccStdErr);
  416. cmSystemTools::RemoveFile(this->RccFileOutput_);
  417. return false;
  418. }
  419. // rcc process success
  420. // Print rcc output
  421. if (!rccStdOut.empty()) {
  422. this->Log().Info(GenT::RCC, rccStdOut);
  423. }
  424. this->BuildFileChanged_ = true;
  425. return true;
  426. }
  427. bool cmQtAutoRccT::GenerateWrapper()
  428. {
  429. // Generate a wrapper source file on demand
  430. if (this->IsMultiConfig()) {
  431. // Wrapper file content
  432. std::string content =
  433. cmStrCat("// This is an autogenerated configuration wrapper file.\n",
  434. "// Changes will be overwritten.\n", "#include <",
  435. this->MultiConfigOutput(), ">\n");
  436. // Compare with existing file content
  437. bool fileDiffers = true;
  438. {
  439. std::string oldContents;
  440. if (FileRead(oldContents, this->RccFilePublic_)) {
  441. fileDiffers = (oldContents != content);
  442. }
  443. }
  444. if (fileDiffers) {
  445. // Write new wrapper file
  446. if (this->Log().Verbose()) {
  447. this->Log().Info(GenT::RCC,
  448. cmStrCat("Generating RCC wrapper file ",
  449. this->MessagePath(this->RccFilePublic_)));
  450. }
  451. std::string error;
  452. if (!FileWrite(this->RccFilePublic_, content, &error)) {
  453. this->Log().Error(GenT::RCC,
  454. cmStrCat("Generating RCC wrapper file ",
  455. this->MessagePath(this->RccFilePublic_),
  456. " failed.\n", error));
  457. return false;
  458. }
  459. } else if (this->BuildFileChanged_) {
  460. // Just touch the wrapper file
  461. if (this->Log().Verbose()) {
  462. this->Log().Info(GenT::RCC,
  463. cmStrCat("Touching RCC wrapper file ",
  464. this->MessagePath(this->RccFilePublic_)));
  465. }
  466. if (!cmSystemTools::Touch(this->RccFilePublic_, false)) {
  467. this->Log().Error(GenT::RCC,
  468. cmStrCat("Touching RCC wrapper file ",
  469. this->MessagePath(this->RccFilePublic_),
  470. " failed."));
  471. return false;
  472. }
  473. }
  474. }
  475. return true;
  476. }
  477. } // End of unnamed namespace
  478. bool cmQtAutoRcc(cm::string_view infoFile, cm::string_view config)
  479. {
  480. return cmQtAutoRccT().Run(infoFile, config);
  481. }