cmCPackIFWInstaller.cxx 15 KB

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