cmCPackIFWInstaller.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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 "cmCPackIFWInstaller.h"
  4. #include <cstddef>
  5. #include <sstream>
  6. #include <utility>
  7. #include "cmCPackIFWCommon.h"
  8. #include "cmCPackIFWGenerator.h"
  9. #include "cmCPackIFWPackage.h"
  10. #include "cmCPackIFWRepository.h"
  11. #include "cmCPackLog.h" // IWYU pragma: keep
  12. #include "cmGeneratedFileStream.h"
  13. #include "cmStringAlgorithms.h"
  14. #include "cmSystemTools.h"
  15. #include "cmXMLParser.h"
  16. #include "cmXMLWriter.h"
  17. cmCPackIFWInstaller::cmCPackIFWInstaller() = default;
  18. void cmCPackIFWInstaller::printSkippedOptionWarning(
  19. const std::string& optionName, const std::string& optionValue)
  20. {
  21. cmCPackIFWLogger(
  22. WARNING,
  23. "Option "
  24. << optionName << " is set to \"" << optionValue
  25. << "\" but will be skipped because the specified file does not exist."
  26. << std::endl);
  27. }
  28. void cmCPackIFWInstaller::ConfigureFromOptions()
  29. {
  30. // Name;
  31. if (const char* optIFW_PACKAGE_NAME =
  32. this->GetOption("CPACK_IFW_PACKAGE_NAME")) {
  33. this->Name = optIFW_PACKAGE_NAME;
  34. } else if (const char* optPACKAGE_NAME =
  35. this->GetOption("CPACK_PACKAGE_NAME")) {
  36. this->Name = optPACKAGE_NAME;
  37. } else {
  38. this->Name = "Your package";
  39. }
  40. // Title;
  41. if (const char* optIFW_PACKAGE_TITLE =
  42. this->GetOption("CPACK_IFW_PACKAGE_TITLE")) {
  43. this->Title = optIFW_PACKAGE_TITLE;
  44. } else if (const char* optPACKAGE_DESCRIPTION_SUMMARY =
  45. this->GetOption("CPACK_PACKAGE_DESCRIPTION_SUMMARY")) {
  46. this->Title = optPACKAGE_DESCRIPTION_SUMMARY;
  47. } else {
  48. this->Title = "Your package description";
  49. }
  50. // Version;
  51. if (const char* option = this->GetOption("CPACK_PACKAGE_VERSION")) {
  52. this->Version = option;
  53. } else {
  54. this->Version = "1.0.0";
  55. }
  56. // Publisher
  57. if (const char* optIFW_PACKAGE_PUBLISHER =
  58. this->GetOption("CPACK_IFW_PACKAGE_PUBLISHER")) {
  59. this->Publisher = optIFW_PACKAGE_PUBLISHER;
  60. } else if (const char* optPACKAGE_VENDOR =
  61. this->GetOption("CPACK_PACKAGE_VENDOR")) {
  62. this->Publisher = optPACKAGE_VENDOR;
  63. }
  64. // ProductUrl
  65. if (const char* option = this->GetOption("CPACK_IFW_PRODUCT_URL")) {
  66. this->ProductUrl = option;
  67. }
  68. // ApplicationIcon
  69. if (const char* option = this->GetOption("CPACK_IFW_PACKAGE_ICON")) {
  70. if (cmSystemTools::FileExists(option)) {
  71. this->InstallerApplicationIcon = option;
  72. } else {
  73. this->printSkippedOptionWarning("CPACK_IFW_PACKAGE_ICON", option);
  74. }
  75. }
  76. // WindowIcon
  77. if (const char* option = this->GetOption("CPACK_IFW_PACKAGE_WINDOW_ICON")) {
  78. if (cmSystemTools::FileExists(option)) {
  79. this->InstallerWindowIcon = option;
  80. } else {
  81. this->printSkippedOptionWarning("CPACK_IFW_PACKAGE_WINDOW_ICON", option);
  82. }
  83. }
  84. // RemoveTargetDir
  85. if (this->IsSetToOff("CPACK_IFW_PACKAGE_REMOVE_TARGET_DIR")) {
  86. this->RemoveTargetDir = "false";
  87. } else if (this->IsOn("CPACK_IFW_PACKAGE_REMOVE_TARGET_DIR")) {
  88. this->RemoveTargetDir = "true";
  89. } else {
  90. this->RemoveTargetDir.clear();
  91. }
  92. // Logo
  93. if (const char* option = this->GetOption("CPACK_IFW_PACKAGE_LOGO")) {
  94. if (cmSystemTools::FileExists(option)) {
  95. this->Logo = option;
  96. } else {
  97. this->printSkippedOptionWarning("CPACK_IFW_PACKAGE_LOGO", option);
  98. }
  99. }
  100. // Watermark
  101. if (const char* option = this->GetOption("CPACK_IFW_PACKAGE_WATERMARK")) {
  102. if (cmSystemTools::FileExists(option)) {
  103. this->Watermark = option;
  104. } else {
  105. this->printSkippedOptionWarning("CPACK_IFW_PACKAGE_WATERMARK", option);
  106. }
  107. }
  108. // Banner
  109. if (const char* option = this->GetOption("CPACK_IFW_PACKAGE_BANNER")) {
  110. if (cmSystemTools::FileExists(option)) {
  111. this->Banner = option;
  112. } else {
  113. this->printSkippedOptionWarning("CPACK_IFW_PACKAGE_BANNER", option);
  114. }
  115. }
  116. // Background
  117. if (const char* option = this->GetOption("CPACK_IFW_PACKAGE_BACKGROUND")) {
  118. if (cmSystemTools::FileExists(option)) {
  119. this->Background = option;
  120. } else {
  121. this->printSkippedOptionWarning("CPACK_IFW_PACKAGE_BACKGROUND", option);
  122. }
  123. }
  124. // WizardStyle
  125. if (const char* option = this->GetOption("CPACK_IFW_PACKAGE_WIZARD_STYLE")) {
  126. // Setting the user value in any case
  127. this->WizardStyle = option;
  128. // Check known values
  129. if (this->WizardStyle != "Modern" && this->WizardStyle != "Aero" &&
  130. this->WizardStyle != "Mac" && this->WizardStyle != "Classic") {
  131. cmCPackIFWLogger(
  132. WARNING,
  133. "Option CPACK_IFW_PACKAGE_WIZARD_STYLE has unknown value \""
  134. << option << "\". Expected values are: Modern, Aero, Mac, Classic."
  135. << std::endl);
  136. }
  137. }
  138. // StyleSheet
  139. if (const char* option = this->GetOption("CPACK_IFW_PACKAGE_STYLE_SHEET")) {
  140. if (cmSystemTools::FileExists(option)) {
  141. this->StyleSheet = option;
  142. } else {
  143. this->printSkippedOptionWarning("CPACK_IFW_PACKAGE_STYLE_SHEET", option);
  144. }
  145. }
  146. // WizardDefaultWidth
  147. if (const char* option =
  148. this->GetOption("CPACK_IFW_PACKAGE_WIZARD_DEFAULT_WIDTH")) {
  149. this->WizardDefaultWidth = option;
  150. }
  151. // WizardDefaultHeight
  152. if (const char* option =
  153. this->GetOption("CPACK_IFW_PACKAGE_WIZARD_DEFAULT_HEIGHT")) {
  154. this->WizardDefaultHeight = option;
  155. }
  156. // WizardShowPageList
  157. if (const char* option =
  158. this->GetOption("CPACK_IFW_PACKAGE_WIZARD_SHOW_PAGE_LIST")) {
  159. if (!this->IsVersionLess("4.0")) {
  160. if (this->IsSetToOff("CPACK_IFW_PACKAGE_WIZARD_SHOW_PAGE_LIST")) {
  161. this->WizardShowPageList = "false";
  162. } else if (this->IsOn("CPACK_IFW_PACKAGE_WIZARD_SHOW_PAGE_LIST")) {
  163. this->WizardShowPageList = "true";
  164. } else {
  165. this->WizardShowPageList.clear();
  166. }
  167. } else {
  168. cmCPackIFWLogger(
  169. WARNING,
  170. "Option CPACK_IFW_PACKAGE_WIZARD_SHOW_PAGE_LIST is set to \""
  171. << option
  172. << "\", but it is only supported with QtIFW version 4.0 or later. "
  173. "It is being ignored because you are using QtIFW version "
  174. << this->Generator->FrameworkVersion.data() << std::endl);
  175. }
  176. }
  177. // TitleColor
  178. if (const char* option = this->GetOption("CPACK_IFW_PACKAGE_TITLE_COLOR")) {
  179. this->TitleColor = option;
  180. }
  181. // Start menu
  182. if (const char* optIFW_START_MENU_DIR =
  183. this->GetOption("CPACK_IFW_PACKAGE_START_MENU_DIRECTORY")) {
  184. this->StartMenuDir = optIFW_START_MENU_DIR;
  185. } else {
  186. this->StartMenuDir = this->Name;
  187. }
  188. // Default target directory for installation
  189. if (const char* optIFW_TARGET_DIRECTORY =
  190. this->GetOption("CPACK_IFW_TARGET_DIRECTORY")) {
  191. this->TargetDir = optIFW_TARGET_DIRECTORY;
  192. } else if (const char* optPACKAGE_INSTALL_DIRECTORY =
  193. this->GetOption("CPACK_PACKAGE_INSTALL_DIRECTORY")) {
  194. this->TargetDir =
  195. cmStrCat("@ApplicationsDir@/", optPACKAGE_INSTALL_DIRECTORY);
  196. } else {
  197. this->TargetDir = "@RootDir@/usr/local";
  198. }
  199. // Default target directory for installation with administrator rights
  200. if (const char* option =
  201. this->GetOption("CPACK_IFW_ADMIN_TARGET_DIRECTORY")) {
  202. this->AdminTargetDir = option;
  203. }
  204. // Maintenance tool
  205. if (const char* optIFW_MAINTENANCE_TOOL =
  206. this->GetOption("CPACK_IFW_PACKAGE_MAINTENANCE_TOOL_NAME")) {
  207. this->MaintenanceToolName = optIFW_MAINTENANCE_TOOL;
  208. }
  209. // Maintenance tool ini file
  210. if (const char* optIFW_MAINTENANCE_TOOL_INI =
  211. this->GetOption("CPACK_IFW_PACKAGE_MAINTENANCE_TOOL_INI_FILE")) {
  212. this->MaintenanceToolIniFile = optIFW_MAINTENANCE_TOOL_INI;
  213. }
  214. // Allow non-ASCII characters
  215. if (this->GetOption("CPACK_IFW_PACKAGE_ALLOW_NON_ASCII_CHARACTERS")) {
  216. if (this->IsOn("CPACK_IFW_PACKAGE_ALLOW_NON_ASCII_CHARACTERS")) {
  217. this->AllowNonAsciiCharacters = "true";
  218. } else {
  219. this->AllowNonAsciiCharacters = "false";
  220. }
  221. }
  222. // Space in path
  223. if (this->GetOption("CPACK_IFW_PACKAGE_ALLOW_SPACE_IN_PATH")) {
  224. if (this->IsOn("CPACK_IFW_PACKAGE_ALLOW_SPACE_IN_PATH")) {
  225. this->AllowSpaceInPath = "true";
  226. } else {
  227. this->AllowSpaceInPath = "false";
  228. }
  229. }
  230. // Control script
  231. if (const char* optIFW_CONTROL_SCRIPT =
  232. this->GetOption("CPACK_IFW_PACKAGE_CONTROL_SCRIPT")) {
  233. this->ControlScript = optIFW_CONTROL_SCRIPT;
  234. }
  235. // Resources
  236. if (const char* optIFW_PACKAGE_RESOURCES =
  237. this->GetOption("CPACK_IFW_PACKAGE_RESOURCES")) {
  238. this->Resources.clear();
  239. cmExpandList(optIFW_PACKAGE_RESOURCES, this->Resources);
  240. }
  241. }
  242. /** \class cmCPackIFWResourcesParser
  243. * \brief Helper class that parse resources form .qrc (Qt)
  244. */
  245. class cmCPackIFWResourcesParser : public cmXMLParser
  246. {
  247. public:
  248. explicit cmCPackIFWResourcesParser(cmCPackIFWInstaller* i)
  249. : installer(i)
  250. {
  251. this->path = i->Directory + "/resources";
  252. }
  253. bool ParseResource(size_t r)
  254. {
  255. this->hasFiles = false;
  256. this->hasErrors = false;
  257. this->basePath =
  258. cmSystemTools::GetFilenamePath(this->installer->Resources[r]);
  259. this->ParseFile(this->installer->Resources[r].data());
  260. return this->hasFiles && !this->hasErrors;
  261. }
  262. cmCPackIFWInstaller* installer;
  263. bool file = false;
  264. bool hasFiles = false;
  265. bool hasErrors = false;
  266. std::string path, basePath;
  267. protected:
  268. void StartElement(const std::string& name, const char** /*atts*/) override
  269. {
  270. this->file = name == "file";
  271. if (this->file) {
  272. this->hasFiles = true;
  273. }
  274. }
  275. void CharacterDataHandler(const char* data, int length) override
  276. {
  277. if (this->file) {
  278. std::string content(data, data + length);
  279. content = cmTrimWhitespace(content);
  280. std::string source = this->basePath + "/" + content;
  281. std::string destination = this->path + "/" + content;
  282. if (!cmSystemTools::CopyFileIfDifferent(source, destination)) {
  283. this->hasErrors = true;
  284. }
  285. }
  286. }
  287. void EndElement(const std::string& /*name*/) override {}
  288. };
  289. void cmCPackIFWInstaller::GenerateInstallerFile()
  290. {
  291. // Lazy directory initialization
  292. if (this->Directory.empty() && this->Generator) {
  293. this->Directory = this->Generator->toplevel;
  294. }
  295. // Output stream
  296. cmGeneratedFileStream fout(this->Directory + "/config/config.xml");
  297. cmXMLWriter xout(fout);
  298. xout.StartDocument();
  299. this->WriteGeneratedByToStrim(xout);
  300. xout.StartElement("Installer");
  301. xout.Element("Name", this->Name);
  302. xout.Element("Version", this->Version);
  303. xout.Element("Title", this->Title);
  304. if (!this->Publisher.empty()) {
  305. xout.Element("Publisher", this->Publisher);
  306. }
  307. if (!this->ProductUrl.empty()) {
  308. xout.Element("ProductUrl", this->ProductUrl);
  309. }
  310. // ApplicationIcon
  311. if (!this->InstallerApplicationIcon.empty()) {
  312. std::string name =
  313. cmSystemTools::GetFilenameName(this->InstallerApplicationIcon);
  314. std::string path = this->Directory + "/config/" + name;
  315. name = cmSystemTools::GetFilenameWithoutExtension(name);
  316. cmsys::SystemTools::CopyFileIfDifferent(this->InstallerApplicationIcon,
  317. path);
  318. xout.Element("InstallerApplicationIcon", name);
  319. }
  320. // WindowIcon
  321. if (!this->InstallerWindowIcon.empty()) {
  322. std::string name =
  323. cmSystemTools::GetFilenameName(this->InstallerWindowIcon);
  324. std::string path = this->Directory + "/config/" + name;
  325. cmsys::SystemTools::CopyFileIfDifferent(this->InstallerWindowIcon, path);
  326. xout.Element("InstallerWindowIcon", name);
  327. }
  328. // Logo
  329. if (!this->Logo.empty()) {
  330. std::string name = cmSystemTools::GetFilenameName(this->Logo);
  331. std::string path = this->Directory + "/config/" + name;
  332. cmsys::SystemTools::CopyFileIfDifferent(this->Logo, path);
  333. xout.Element("Logo", name);
  334. }
  335. // Banner
  336. if (!this->Banner.empty()) {
  337. std::string name = cmSystemTools::GetFilenameName(this->Banner);
  338. std::string path = this->Directory + "/config/" + name;
  339. cmsys::SystemTools::CopyFileIfDifferent(this->Banner, path);
  340. xout.Element("Banner", name);
  341. }
  342. // Watermark
  343. if (!this->Watermark.empty()) {
  344. std::string name = cmSystemTools::GetFilenameName(this->Watermark);
  345. std::string path = this->Directory + "/config/" + name;
  346. cmsys::SystemTools::CopyFileIfDifferent(this->Watermark, path);
  347. xout.Element("Watermark", name);
  348. }
  349. // Background
  350. if (!this->Background.empty()) {
  351. std::string name = cmSystemTools::GetFilenameName(this->Background);
  352. std::string path = this->Directory + "/config/" + name;
  353. cmsys::SystemTools::CopyFileIfDifferent(this->Background, path);
  354. xout.Element("Background", name);
  355. }
  356. // WizardStyle
  357. if (!this->WizardStyle.empty()) {
  358. xout.Element("WizardStyle", this->WizardStyle);
  359. }
  360. // Stylesheet
  361. if (!this->StyleSheet.empty()) {
  362. std::string name = cmSystemTools::GetFilenameName(this->StyleSheet);
  363. std::string path = this->Directory + "/config/" + name;
  364. cmsys::SystemTools::CopyFileIfDifferent(this->StyleSheet, path);
  365. xout.Element("StyleSheet", name);
  366. }
  367. // WizardDefaultWidth
  368. if (!this->WizardDefaultWidth.empty()) {
  369. xout.Element("WizardDefaultWidth", this->WizardDefaultWidth);
  370. }
  371. // WizardDefaultHeight
  372. if (!this->WizardDefaultHeight.empty()) {
  373. xout.Element("WizardDefaultHeight", this->WizardDefaultHeight);
  374. }
  375. // WizardShowPageList
  376. if (!this->IsVersionLess("4.0") && !this->WizardShowPageList.empty()) {
  377. xout.Element("WizardShowPageList", this->WizardShowPageList);
  378. }
  379. // TitleColor
  380. if (!this->TitleColor.empty()) {
  381. xout.Element("TitleColor", this->TitleColor);
  382. }
  383. // Start menu
  384. if (!this->IsVersionLess("2.0")) {
  385. xout.Element("StartMenuDir", this->StartMenuDir);
  386. }
  387. // Target dir
  388. if (!this->TargetDir.empty()) {
  389. xout.Element("TargetDir", this->TargetDir);
  390. }
  391. // Admin target dir
  392. if (!this->AdminTargetDir.empty()) {
  393. xout.Element("AdminTargetDir", this->AdminTargetDir);
  394. }
  395. // Remote repositories
  396. if (!this->RemoteRepositories.empty()) {
  397. xout.StartElement("RemoteRepositories");
  398. for (cmCPackIFWRepository* r : this->RemoteRepositories) {
  399. r->WriteRepositoryConfig(xout);
  400. }
  401. xout.EndElement();
  402. }
  403. // Maintenance tool
  404. if (!this->IsVersionLess("2.0") && !this->MaintenanceToolName.empty()) {
  405. xout.Element("MaintenanceToolName", this->MaintenanceToolName);
  406. }
  407. // Maintenance tool ini file
  408. if (!this->IsVersionLess("2.0") && !this->MaintenanceToolIniFile.empty()) {
  409. xout.Element("MaintenanceToolIniFile", this->MaintenanceToolIniFile);
  410. }
  411. if (!this->RemoveTargetDir.empty()) {
  412. xout.Element("RemoveTargetDir", this->RemoveTargetDir);
  413. }
  414. // Different allows
  415. if (this->IsVersionLess("2.0")) {
  416. // CPack IFW default policy
  417. xout.Comment("CPack IFW default policy for QtIFW less 2.0");
  418. xout.Element("AllowNonAsciiCharacters", "true");
  419. xout.Element("AllowSpaceInPath", "true");
  420. } else {
  421. if (!this->AllowNonAsciiCharacters.empty()) {
  422. xout.Element("AllowNonAsciiCharacters", this->AllowNonAsciiCharacters);
  423. }
  424. if (!this->AllowSpaceInPath.empty()) {
  425. xout.Element("AllowSpaceInPath", this->AllowSpaceInPath);
  426. }
  427. }
  428. // Control script (copy to config dir)
  429. if (!this->IsVersionLess("2.0") && !this->ControlScript.empty()) {
  430. std::string name = cmSystemTools::GetFilenameName(this->ControlScript);
  431. std::string path = this->Directory + "/config/" + name;
  432. cmsys::SystemTools::CopyFileIfDifferent(this->ControlScript, path);
  433. xout.Element("ControlScript", name);
  434. }
  435. // Resources (copy to resources dir)
  436. if (!this->Resources.empty()) {
  437. std::vector<std::string> resources;
  438. cmCPackIFWResourcesParser parser(this);
  439. for (size_t i = 0; i < this->Resources.size(); i++) {
  440. if (parser.ParseResource(i)) {
  441. std::string name = cmSystemTools::GetFilenameName(this->Resources[i]);
  442. std::string path = this->Directory + "/resources/" + name;
  443. cmsys::SystemTools::CopyFileIfDifferent(this->Resources[i], path);
  444. resources.push_back(std::move(name));
  445. } else {
  446. cmCPackIFWLogger(WARNING,
  447. "Can't copy resources from \""
  448. << this->Resources[i]
  449. << "\". Resource will be skipped." << std::endl);
  450. }
  451. }
  452. this->Resources = resources;
  453. }
  454. xout.EndElement();
  455. xout.EndDocument();
  456. }
  457. void cmCPackIFWInstaller::GeneratePackageFiles()
  458. {
  459. if (this->Packages.empty() || this->Generator->IsOnePackage()) {
  460. // Generate default package
  461. cmCPackIFWPackage package;
  462. package.Generator = this->Generator;
  463. package.Installer = this;
  464. // Check package group
  465. if (const char* option = this->GetOption("CPACK_IFW_PACKAGE_GROUP")) {
  466. package.ConfigureFromGroup(option);
  467. std::string forcedOption = "CPACK_IFW_COMPONENT_GROUP_" +
  468. cmsys::SystemTools::UpperCase(option) + "_FORCED_INSTALLATION";
  469. if (!this->GetOption(forcedOption)) {
  470. package.ForcedInstallation = "true";
  471. }
  472. } else {
  473. package.ConfigureFromOptions();
  474. }
  475. package.GeneratePackageFile();
  476. return;
  477. }
  478. // Generate packages meta information
  479. for (auto& p : this->Packages) {
  480. cmCPackIFWPackage* package = p.second;
  481. package->GeneratePackageFile();
  482. }
  483. }