cmCPackIFWInstaller.cxx 16 KB

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