cmCPackIFWInstaller.cxx 16 KB

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