cmCPackIFWInstaller.cxx 15 KB

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