cmCPackIFWInstaller.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 value \""
  171. << option << "\". But has no any effect for QtIFW less than 4.0 "
  172. << "and will be skipped." << std::endl);
  173. }
  174. }
  175. // TitleColor
  176. if (const char* option = this->GetOption("CPACK_IFW_PACKAGE_TITLE_COLOR")) {
  177. this->TitleColor = option;
  178. }
  179. // Start menu
  180. if (const char* optIFW_START_MENU_DIR =
  181. this->GetOption("CPACK_IFW_PACKAGE_START_MENU_DIRECTORY")) {
  182. this->StartMenuDir = optIFW_START_MENU_DIR;
  183. } else {
  184. this->StartMenuDir = this->Name;
  185. }
  186. // Default target directory for installation
  187. if (const char* optIFW_TARGET_DIRECTORY =
  188. this->GetOption("CPACK_IFW_TARGET_DIRECTORY")) {
  189. this->TargetDir = optIFW_TARGET_DIRECTORY;
  190. } else if (const char* optPACKAGE_INSTALL_DIRECTORY =
  191. this->GetOption("CPACK_PACKAGE_INSTALL_DIRECTORY")) {
  192. this->TargetDir =
  193. cmStrCat("@ApplicationsDir@/", optPACKAGE_INSTALL_DIRECTORY);
  194. } else {
  195. this->TargetDir = "@RootDir@/usr/local";
  196. }
  197. // Default target directory for installation with administrator rights
  198. if (const char* option =
  199. this->GetOption("CPACK_IFW_ADMIN_TARGET_DIRECTORY")) {
  200. this->AdminTargetDir = option;
  201. }
  202. // Maintenance tool
  203. if (const char* optIFW_MAINTENANCE_TOOL =
  204. this->GetOption("CPACK_IFW_PACKAGE_MAINTENANCE_TOOL_NAME")) {
  205. this->MaintenanceToolName = optIFW_MAINTENANCE_TOOL;
  206. }
  207. // Maintenance tool ini file
  208. if (const char* optIFW_MAINTENANCE_TOOL_INI =
  209. this->GetOption("CPACK_IFW_PACKAGE_MAINTENANCE_TOOL_INI_FILE")) {
  210. this->MaintenanceToolIniFile = optIFW_MAINTENANCE_TOOL_INI;
  211. }
  212. // Allow non-ASCII characters
  213. if (this->GetOption("CPACK_IFW_PACKAGE_ALLOW_NON_ASCII_CHARACTERS")) {
  214. if (this->IsOn("CPACK_IFW_PACKAGE_ALLOW_NON_ASCII_CHARACTERS")) {
  215. this->AllowNonAsciiCharacters = "true";
  216. } else {
  217. this->AllowNonAsciiCharacters = "false";
  218. }
  219. }
  220. // Space in path
  221. if (this->GetOption("CPACK_IFW_PACKAGE_ALLOW_SPACE_IN_PATH")) {
  222. if (this->IsOn("CPACK_IFW_PACKAGE_ALLOW_SPACE_IN_PATH")) {
  223. this->AllowSpaceInPath = "true";
  224. } else {
  225. this->AllowSpaceInPath = "false";
  226. }
  227. }
  228. // Control script
  229. if (const char* optIFW_CONTROL_SCRIPT =
  230. this->GetOption("CPACK_IFW_PACKAGE_CONTROL_SCRIPT")) {
  231. this->ControlScript = optIFW_CONTROL_SCRIPT;
  232. }
  233. // Resources
  234. if (const char* optIFW_PACKAGE_RESOURCES =
  235. this->GetOption("CPACK_IFW_PACKAGE_RESOURCES")) {
  236. this->Resources.clear();
  237. cmExpandList(optIFW_PACKAGE_RESOURCES, this->Resources);
  238. }
  239. }
  240. /** \class cmCPackIFWResourcesParser
  241. * \brief Helper class that parse resources form .qrc (Qt)
  242. */
  243. class cmCPackIFWResourcesParser : public cmXMLParser
  244. {
  245. public:
  246. explicit cmCPackIFWResourcesParser(cmCPackIFWInstaller* i)
  247. : installer(i)
  248. {
  249. this->path = i->Directory + "/resources";
  250. }
  251. bool ParseResource(size_t r)
  252. {
  253. this->hasFiles = false;
  254. this->hasErrors = false;
  255. this->basePath =
  256. cmSystemTools::GetFilenamePath(this->installer->Resources[r]);
  257. this->ParseFile(this->installer->Resources[r].data());
  258. return this->hasFiles && !this->hasErrors;
  259. }
  260. cmCPackIFWInstaller* installer;
  261. bool file = false;
  262. bool hasFiles = false;
  263. bool hasErrors = false;
  264. std::string path, basePath;
  265. protected:
  266. void StartElement(const std::string& name, const char** /*atts*/) override
  267. {
  268. this->file = name == "file";
  269. if (this->file) {
  270. this->hasFiles = true;
  271. }
  272. }
  273. void CharacterDataHandler(const char* data, int length) override
  274. {
  275. if (this->file) {
  276. std::string content(data, data + length);
  277. content = cmTrimWhitespace(content);
  278. std::string source = this->basePath + "/" + content;
  279. std::string destination = this->path + "/" + content;
  280. if (!cmSystemTools::CopyFileIfDifferent(source, destination)) {
  281. this->hasErrors = true;
  282. }
  283. }
  284. }
  285. void EndElement(const std::string& /*name*/) override {}
  286. };
  287. void cmCPackIFWInstaller::GenerateInstallerFile()
  288. {
  289. // Lazy directory initialization
  290. if (this->Directory.empty() && this->Generator) {
  291. this->Directory = this->Generator->toplevel;
  292. }
  293. // Output stream
  294. cmGeneratedFileStream fout(this->Directory + "/config/config.xml");
  295. cmXMLWriter xout(fout);
  296. xout.StartDocument();
  297. this->WriteGeneratedByToStrim(xout);
  298. xout.StartElement("Installer");
  299. xout.Element("Name", this->Name);
  300. xout.Element("Version", this->Version);
  301. xout.Element("Title", this->Title);
  302. if (!this->Publisher.empty()) {
  303. xout.Element("Publisher", this->Publisher);
  304. }
  305. if (!this->ProductUrl.empty()) {
  306. xout.Element("ProductUrl", this->ProductUrl);
  307. }
  308. // ApplicationIcon
  309. if (!this->InstallerApplicationIcon.empty()) {
  310. std::string name =
  311. cmSystemTools::GetFilenameName(this->InstallerApplicationIcon);
  312. std::string path = this->Directory + "/config/" + name;
  313. name = cmSystemTools::GetFilenameWithoutExtension(name);
  314. cmsys::SystemTools::CopyFileIfDifferent(this->InstallerApplicationIcon,
  315. path);
  316. xout.Element("InstallerApplicationIcon", name);
  317. }
  318. // WindowIcon
  319. if (!this->InstallerWindowIcon.empty()) {
  320. std::string name =
  321. cmSystemTools::GetFilenameName(this->InstallerWindowIcon);
  322. std::string path = this->Directory + "/config/" + name;
  323. cmsys::SystemTools::CopyFileIfDifferent(this->InstallerWindowIcon, path);
  324. xout.Element("InstallerWindowIcon", name);
  325. }
  326. // Logo
  327. if (!this->Logo.empty()) {
  328. std::string name = cmSystemTools::GetFilenameName(this->Logo);
  329. std::string path = this->Directory + "/config/" + name;
  330. cmsys::SystemTools::CopyFileIfDifferent(this->Logo, path);
  331. xout.Element("Logo", name);
  332. }
  333. // Banner
  334. if (!this->Banner.empty()) {
  335. std::string name = cmSystemTools::GetFilenameName(this->Banner);
  336. std::string path = this->Directory + "/config/" + name;
  337. cmsys::SystemTools::CopyFileIfDifferent(this->Banner, path);
  338. xout.Element("Banner", name);
  339. }
  340. // Watermark
  341. if (!this->Watermark.empty()) {
  342. std::string name = cmSystemTools::GetFilenameName(this->Watermark);
  343. std::string path = this->Directory + "/config/" + name;
  344. cmsys::SystemTools::CopyFileIfDifferent(this->Watermark, path);
  345. xout.Element("Watermark", name);
  346. }
  347. // Background
  348. if (!this->Background.empty()) {
  349. std::string name = cmSystemTools::GetFilenameName(this->Background);
  350. std::string path = this->Directory + "/config/" + name;
  351. cmsys::SystemTools::CopyFileIfDifferent(this->Background, path);
  352. xout.Element("Background", name);
  353. }
  354. // WizardStyle
  355. if (!this->WizardStyle.empty()) {
  356. xout.Element("WizardStyle", this->WizardStyle);
  357. }
  358. // Stylesheet
  359. if (!this->StyleSheet.empty()) {
  360. std::string name = cmSystemTools::GetFilenameName(this->StyleSheet);
  361. std::string path = this->Directory + "/config/" + name;
  362. cmsys::SystemTools::CopyFileIfDifferent(this->StyleSheet, path);
  363. xout.Element("StyleSheet", name);
  364. }
  365. // WizardDefaultWidth
  366. if (!this->WizardDefaultWidth.empty()) {
  367. xout.Element("WizardDefaultWidth", this->WizardDefaultWidth);
  368. }
  369. // WizardDefaultHeight
  370. if (!this->WizardDefaultHeight.empty()) {
  371. xout.Element("WizardDefaultHeight", this->WizardDefaultHeight);
  372. }
  373. // WizardShowPageList
  374. if (!this->IsVersionLess("4.0") && !this->WizardShowPageList.empty()) {
  375. xout.Element("WizardShowPageList", this->WizardShowPageList);
  376. }
  377. // TitleColor
  378. if (!this->TitleColor.empty()) {
  379. xout.Element("TitleColor", this->TitleColor);
  380. }
  381. // Start menu
  382. if (!this->IsVersionLess("2.0")) {
  383. xout.Element("StartMenuDir", this->StartMenuDir);
  384. }
  385. // Target dir
  386. if (!this->TargetDir.empty()) {
  387. xout.Element("TargetDir", this->TargetDir);
  388. }
  389. // Admin target dir
  390. if (!this->AdminTargetDir.empty()) {
  391. xout.Element("AdminTargetDir", this->AdminTargetDir);
  392. }
  393. // Remote repositories
  394. if (!this->RemoteRepositories.empty()) {
  395. xout.StartElement("RemoteRepositories");
  396. for (cmCPackIFWRepository* r : this->RemoteRepositories) {
  397. r->WriteRepositoryConfig(xout);
  398. }
  399. xout.EndElement();
  400. }
  401. // Maintenance tool
  402. if (!this->IsVersionLess("2.0") && !this->MaintenanceToolName.empty()) {
  403. xout.Element("MaintenanceToolName", this->MaintenanceToolName);
  404. }
  405. // Maintenance tool ini file
  406. if (!this->IsVersionLess("2.0") && !this->MaintenanceToolIniFile.empty()) {
  407. xout.Element("MaintenanceToolIniFile", this->MaintenanceToolIniFile);
  408. }
  409. if (!this->RemoveTargetDir.empty()) {
  410. xout.Element("RemoveTargetDir", this->RemoveTargetDir);
  411. }
  412. // Different allows
  413. if (this->IsVersionLess("2.0")) {
  414. // CPack IFW default policy
  415. xout.Comment("CPack IFW default policy for QtIFW less 2.0");
  416. xout.Element("AllowNonAsciiCharacters", "true");
  417. xout.Element("AllowSpaceInPath", "true");
  418. } else {
  419. if (!this->AllowNonAsciiCharacters.empty()) {
  420. xout.Element("AllowNonAsciiCharacters", this->AllowNonAsciiCharacters);
  421. }
  422. if (!this->AllowSpaceInPath.empty()) {
  423. xout.Element("AllowSpaceInPath", this->AllowSpaceInPath);
  424. }
  425. }
  426. // Control script (copy to config dir)
  427. if (!this->IsVersionLess("2.0") && !this->ControlScript.empty()) {
  428. std::string name = cmSystemTools::GetFilenameName(this->ControlScript);
  429. std::string path = this->Directory + "/config/" + name;
  430. cmsys::SystemTools::CopyFileIfDifferent(this->ControlScript, path);
  431. xout.Element("ControlScript", name);
  432. }
  433. // Resources (copy to resources dir)
  434. if (!this->Resources.empty()) {
  435. std::vector<std::string> resources;
  436. cmCPackIFWResourcesParser parser(this);
  437. for (size_t i = 0; i < this->Resources.size(); i++) {
  438. if (parser.ParseResource(i)) {
  439. std::string name = cmSystemTools::GetFilenameName(this->Resources[i]);
  440. std::string path = this->Directory + "/resources/" + name;
  441. cmsys::SystemTools::CopyFileIfDifferent(this->Resources[i], path);
  442. resources.push_back(std::move(name));
  443. } else {
  444. cmCPackIFWLogger(WARNING,
  445. "Can't copy resources from \""
  446. << this->Resources[i]
  447. << "\". Resource will be skipped." << std::endl);
  448. }
  449. }
  450. this->Resources = resources;
  451. }
  452. xout.EndElement();
  453. xout.EndDocument();
  454. }
  455. void cmCPackIFWInstaller::GeneratePackageFiles()
  456. {
  457. if (this->Packages.empty() || this->Generator->IsOnePackage()) {
  458. // Generate default package
  459. cmCPackIFWPackage package;
  460. package.Generator = this->Generator;
  461. package.Installer = this;
  462. // Check package group
  463. if (const char* option = this->GetOption("CPACK_IFW_PACKAGE_GROUP")) {
  464. package.ConfigureFromGroup(option);
  465. std::string forcedOption = "CPACK_IFW_COMPONENT_GROUP_" +
  466. cmsys::SystemTools::UpperCase(option) + "_FORCED_INSTALLATION";
  467. if (!this->GetOption(forcedOption)) {
  468. package.ForcedInstallation = "true";
  469. }
  470. } else {
  471. package.ConfigureFromOptions();
  472. }
  473. package.GeneratePackageFile();
  474. return;
  475. }
  476. // Generate packages meta information
  477. for (auto& p : this->Packages) {
  478. cmCPackIFWPackage* package = p.second;
  479. package->GeneratePackageFile();
  480. }
  481. }