cmQtAutoRcc.cxx 17 KB

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