cmCPackIFWInstaller.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 "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. cmCPackIFWResourcesParser(cmCPackIFWInstaller* i)
  247. : installer(i)
  248. , file(false)
  249. {
  250. this->path = i->Directory + "/resources";
  251. }
  252. bool ParseResource(size_t r)
  253. {
  254. this->hasFiles = false;
  255. this->hasErrors = false;
  256. this->basePath =
  257. cmSystemTools::GetFilenamePath(this->installer->Resources[r]);
  258. this->ParseFile(this->installer->Resources[r].data());
  259. return this->hasFiles && !this->hasErrors;
  260. }
  261. cmCPackIFWInstaller* installer;
  262. bool file, hasFiles, hasErrors;
  263. std::string path, basePath;
  264. protected:
  265. void StartElement(const std::string& name, const char** /*atts*/) override
  266. {
  267. this->file = name == "file";
  268. if (this->file) {
  269. this->hasFiles = true;
  270. }
  271. }
  272. void CharacterDataHandler(const char* data, int length) override
  273. {
  274. if (this->file) {
  275. std::string content(data, data + length);
  276. content = cmTrimWhitespace(content);
  277. std::string source = this->basePath + "/" + content;
  278. std::string destination = this->path + "/" + content;
  279. if (!cmSystemTools::CopyFileIfDifferent(source, destination)) {
  280. this->hasErrors = true;
  281. }
  282. }
  283. }
  284. void EndElement(const std::string& /*name*/) override {}
  285. };
  286. void cmCPackIFWInstaller::GenerateInstallerFile()
  287. {
  288. // Lazy directory initialization
  289. if (this->Directory.empty() && this->Generator) {
  290. this->Directory = this->Generator->toplevel;
  291. }
  292. // Output stream
  293. cmGeneratedFileStream fout(this->Directory + "/config/config.xml");
  294. cmXMLWriter xout(fout);
  295. xout.StartDocument();
  296. this->WriteGeneratedByToStrim(xout);
  297. xout.StartElement("Installer");
  298. xout.Element("Name", this->Name);
  299. xout.Element("Version", this->Version);
  300. xout.Element("Title", this->Title);
  301. if (!this->Publisher.empty()) {
  302. xout.Element("Publisher", this->Publisher);
  303. }
  304. if (!this->ProductUrl.empty()) {
  305. xout.Element("ProductUrl", this->ProductUrl);
  306. }
  307. // ApplicationIcon
  308. if (!this->InstallerApplicationIcon.empty()) {
  309. std::string name =
  310. cmSystemTools::GetFilenameName(this->InstallerApplicationIcon);
  311. std::string path = this->Directory + "/config/" + name;
  312. name = cmSystemTools::GetFilenameWithoutExtension(name);
  313. cmsys::SystemTools::CopyFileIfDifferent(this->InstallerApplicationIcon,
  314. path);
  315. xout.Element("InstallerApplicationIcon", name);
  316. }
  317. // WindowIcon
  318. if (!this->InstallerWindowIcon.empty()) {
  319. std::string name =
  320. cmSystemTools::GetFilenameName(this->InstallerWindowIcon);
  321. std::string path = this->Directory + "/config/" + name;
  322. cmsys::SystemTools::CopyFileIfDifferent(this->InstallerWindowIcon, path);
  323. xout.Element("InstallerWindowIcon", name);
  324. }
  325. // Logo
  326. if (!this->Logo.empty()) {
  327. std::string name = cmSystemTools::GetFilenameName(this->Logo);
  328. std::string path = this->Directory + "/config/" + name;
  329. cmsys::SystemTools::CopyFileIfDifferent(this->Logo, path);
  330. xout.Element("Logo", name);
  331. }
  332. // Banner
  333. if (!this->Banner.empty()) {
  334. std::string name = cmSystemTools::GetFilenameName(this->Banner);
  335. std::string path = this->Directory + "/config/" + name;
  336. cmsys::SystemTools::CopyFileIfDifferent(this->Banner, path);
  337. xout.Element("Banner", name);
  338. }
  339. // Watermark
  340. if (!this->Watermark.empty()) {
  341. std::string name = cmSystemTools::GetFilenameName(this->Watermark);
  342. std::string path = this->Directory + "/config/" + name;
  343. cmsys::SystemTools::CopyFileIfDifferent(this->Watermark, path);
  344. xout.Element("Watermark", name);
  345. }
  346. // Background
  347. if (!this->Background.empty()) {
  348. std::string name = cmSystemTools::GetFilenameName(this->Background);
  349. std::string path = this->Directory + "/config/" + name;
  350. cmsys::SystemTools::CopyFileIfDifferent(this->Background, path);
  351. xout.Element("Background", name);
  352. }
  353. // WizardStyle
  354. if (!this->WizardStyle.empty()) {
  355. xout.Element("WizardStyle", this->WizardStyle);
  356. }
  357. // Stylesheet
  358. if (!this->StyleSheet.empty()) {
  359. std::string name = cmSystemTools::GetFilenameName(this->StyleSheet);
  360. std::string path = this->Directory + "/config/" + name;
  361. cmsys::SystemTools::CopyFileIfDifferent(this->StyleSheet, path);
  362. xout.Element("StyleSheet", name);
  363. }
  364. // WizardDefaultWidth
  365. if (!this->WizardDefaultWidth.empty()) {
  366. xout.Element("WizardDefaultWidth", this->WizardDefaultWidth);
  367. }
  368. // WizardDefaultHeight
  369. if (!this->WizardDefaultHeight.empty()) {
  370. xout.Element("WizardDefaultHeight", this->WizardDefaultHeight);
  371. }
  372. // WizardShowPageList
  373. if (!this->IsVersionLess("4.0") && !this->WizardShowPageList.empty()) {
  374. xout.Element("WizardShowPageList", this->WizardShowPageList);
  375. }
  376. // TitleColor
  377. if (!this->TitleColor.empty()) {
  378. xout.Element("TitleColor", this->TitleColor);
  379. }
  380. // Start menu
  381. if (!this->IsVersionLess("2.0")) {
  382. xout.Element("StartMenuDir", this->StartMenuDir);
  383. }
  384. // Target dir
  385. if (!this->TargetDir.empty()) {
  386. xout.Element("TargetDir", this->TargetDir);
  387. }
  388. // Admin target dir
  389. if (!this->AdminTargetDir.empty()) {
  390. xout.Element("AdminTargetDir", this->AdminTargetDir);
  391. }
  392. // Remote repositories
  393. if (!this->RemoteRepositories.empty()) {
  394. xout.StartElement("RemoteRepositories");
  395. for (cmCPackIFWRepository* r : this->RemoteRepositories) {
  396. r->WriteRepositoryConfig(xout);
  397. }
  398. xout.EndElement();
  399. }
  400. // Maintenance tool
  401. if (!this->IsVersionLess("2.0") && !this->MaintenanceToolName.empty()) {
  402. xout.Element("MaintenanceToolName", this->MaintenanceToolName);
  403. }
  404. // Maintenance tool ini file
  405. if (!this->IsVersionLess("2.0") && !this->MaintenanceToolIniFile.empty()) {
  406. xout.Element("MaintenanceToolIniFile", this->MaintenanceToolIniFile);
  407. }
  408. if (!this->RemoveTargetDir.empty()) {
  409. xout.Element("RemoveTargetDir", this->RemoveTargetDir);
  410. }
  411. // Different allows
  412. if (this->IsVersionLess("2.0")) {
  413. // CPack IFW default policy
  414. xout.Comment("CPack IFW default policy for QtIFW less 2.0");
  415. xout.Element("AllowNonAsciiCharacters", "true");
  416. xout.Element("AllowSpaceInPath", "true");
  417. } else {
  418. if (!this->AllowNonAsciiCharacters.empty()) {
  419. xout.Element("AllowNonAsciiCharacters", this->AllowNonAsciiCharacters);
  420. }
  421. if (!this->AllowSpaceInPath.empty()) {
  422. xout.Element("AllowSpaceInPath", this->AllowSpaceInPath);
  423. }
  424. }
  425. // Control script (copy to config dir)
  426. if (!this->IsVersionLess("2.0") && !this->ControlScript.empty()) {
  427. std::string name = cmSystemTools::GetFilenameName(this->ControlScript);
  428. std::string path = this->Directory + "/config/" + name;
  429. cmsys::SystemTools::CopyFileIfDifferent(this->ControlScript, path);
  430. xout.Element("ControlScript", name);
  431. }
  432. // Resources (copy to resources dir)
  433. if (!this->Resources.empty()) {
  434. std::vector<std::string> resources;
  435. cmCPackIFWResourcesParser parser(this);
  436. for (size_t i = 0; i < this->Resources.size(); i++) {
  437. if (parser.ParseResource(i)) {
  438. std::string name = cmSystemTools::GetFilenameName(this->Resources[i]);
  439. std::string path = this->Directory + "/resources/" + name;
  440. cmsys::SystemTools::CopyFileIfDifferent(this->Resources[i], path);
  441. resources.push_back(std::move(name));
  442. } else {
  443. cmCPackIFWLogger(WARNING,
  444. "Can't copy resources from \""
  445. << this->Resources[i]
  446. << "\". Resource will be skipped." << std::endl);
  447. }
  448. }
  449. this->Resources = resources;
  450. }
  451. xout.EndElement();
  452. xout.EndDocument();
  453. }
  454. void cmCPackIFWInstaller::GeneratePackageFiles()
  455. {
  456. if (this->Packages.empty() || this->Generator->IsOnePackage()) {
  457. // Generate default package
  458. cmCPackIFWPackage package;
  459. package.Generator = this->Generator;
  460. package.Installer = this;
  461. // Check package group
  462. if (const char* option = this->GetOption("CPACK_IFW_PACKAGE_GROUP")) {
  463. package.ConfigureFromGroup(option);
  464. std::string forcedOption = "CPACK_IFW_COMPONENT_GROUP_" +
  465. cmsys::SystemTools::UpperCase(option) + "_FORCED_INSTALLATION";
  466. if (!this->GetOption(forcedOption)) {
  467. package.ForcedInstallation = "true";
  468. }
  469. } else {
  470. package.ConfigureFromOptions();
  471. }
  472. package.GeneratePackageFile();
  473. return;
  474. }
  475. // Generate packages meta information
  476. for (auto& p : this->Packages) {
  477. cmCPackIFWPackage* package = p.second;
  478. package->GeneratePackageFile();
  479. }
  480. }