cmQtAutoRcc.cxx 16 KB

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