cmQtAutoRcc.cxx 18 KB

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