cmQtAutoRcc.cxx 14 KB

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