cmWIXFilesSourceWriter.cxx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2014-2015 Kitware, Inc.
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmWIXFilesSourceWriter.h"
  11. #include "cmWIXAccessControlList.h"
  12. #include <cmInstalledFile.h>
  13. #include <cmSystemTools.h>
  14. #include <cmUuid.h>
  15. #include <sys/types.h>
  16. // include sys/stat.h after sys/types.h
  17. #include <sys/stat.h>
  18. cmWIXFilesSourceWriter::cmWIXFilesSourceWriter(cmCPackLog* logger,
  19. std::string const& filename,
  20. GuidType componentGuidType)
  21. : cmWIXSourceWriter(logger, filename, componentGuidType)
  22. {
  23. }
  24. void cmWIXFilesSourceWriter::EmitShortcut(std::string const& id,
  25. cmWIXShortcut const& shortcut,
  26. std::string const& shortcutPrefix,
  27. size_t shortcutIndex)
  28. {
  29. std::ostringstream shortcutId;
  30. shortcutId << shortcutPrefix << id;
  31. if (shortcutIndex > 0) {
  32. shortcutId << "_" << shortcutIndex;
  33. }
  34. std::string fileId = std::string("CM_F") + id;
  35. BeginElement("Shortcut");
  36. AddAttribute("Id", shortcutId.str());
  37. AddAttribute("Name", shortcut.label);
  38. std::string target = "[#" + fileId + "]";
  39. AddAttribute("Target", target);
  40. AddAttribute("WorkingDirectory", shortcut.workingDirectoryId);
  41. EndElement("Shortcut");
  42. }
  43. void cmWIXFilesSourceWriter::EmitRemoveFolder(std::string const& id)
  44. {
  45. BeginElement("RemoveFolder");
  46. AddAttribute("Id", id);
  47. AddAttribute("On", "uninstall");
  48. EndElement("RemoveFolder");
  49. }
  50. void cmWIXFilesSourceWriter::EmitInstallRegistryValue(
  51. std::string const& registryKey, std::string const& cpackComponentName,
  52. std::string const& suffix)
  53. {
  54. std::string valueName;
  55. if (!cpackComponentName.empty()) {
  56. valueName = cpackComponentName + "_";
  57. }
  58. valueName += "installed";
  59. valueName += suffix;
  60. BeginElement("RegistryValue");
  61. AddAttribute("Root", "HKCU");
  62. AddAttribute("Key", registryKey);
  63. AddAttribute("Name", valueName);
  64. AddAttribute("Type", "integer");
  65. AddAttribute("Value", "1");
  66. AddAttribute("KeyPath", "yes");
  67. EndElement("RegistryValue");
  68. }
  69. void cmWIXFilesSourceWriter::EmitUninstallShortcut(
  70. std::string const& packageName)
  71. {
  72. BeginElement("Shortcut");
  73. AddAttribute("Id", "UNINSTALL");
  74. AddAttribute("Name", "Uninstall " + packageName);
  75. AddAttribute("Description", "Uninstalls " + packageName);
  76. AddAttribute("Target", "[SystemFolder]msiexec.exe");
  77. AddAttribute("Arguments", "/x [ProductCode]");
  78. EndElement("Shortcut");
  79. }
  80. std::string cmWIXFilesSourceWriter::EmitComponentCreateFolder(
  81. std::string const& directoryId, std::string const& guid,
  82. cmInstalledFile const* installedFile)
  83. {
  84. std::string componentId = std::string("CM_C_EMPTY_") + directoryId;
  85. BeginElement("DirectoryRef");
  86. AddAttribute("Id", directoryId);
  87. BeginElement("Component");
  88. AddAttribute("Id", componentId);
  89. AddAttribute("Guid", guid);
  90. BeginElement("CreateFolder");
  91. if (installedFile) {
  92. cmWIXAccessControlList acl(Logger, *installedFile, *this);
  93. acl.Apply();
  94. }
  95. EndElement("CreateFolder");
  96. EndElement("Component");
  97. EndElement("DirectoryRef");
  98. return componentId;
  99. }
  100. std::string cmWIXFilesSourceWriter::EmitComponentFile(
  101. std::string const& directoryId, std::string const& id,
  102. std::string const& filePath, cmWIXPatch& patch,
  103. cmInstalledFile const* installedFile)
  104. {
  105. std::string componentId = std::string("CM_C") + id;
  106. std::string fileId = std::string("CM_F") + id;
  107. std::string guid = CreateGuidFromComponentId(componentId);
  108. BeginElement("DirectoryRef");
  109. AddAttribute("Id", directoryId);
  110. BeginElement("Component");
  111. AddAttribute("Id", componentId);
  112. AddAttribute("Guid", guid);
  113. if (installedFile) {
  114. if (installedFile->GetPropertyAsBool("CPACK_NEVER_OVERWRITE")) {
  115. AddAttribute("NeverOverwrite", "yes");
  116. }
  117. if (installedFile->GetPropertyAsBool("CPACK_PERMANENT")) {
  118. AddAttribute("Permanent", "yes");
  119. }
  120. }
  121. BeginElement("File");
  122. AddAttribute("Id", fileId);
  123. AddAttribute("Source", filePath);
  124. AddAttribute("KeyPath", "yes");
  125. mode_t fileMode = 0;
  126. cmSystemTools::GetPermissions(filePath.c_str(), fileMode);
  127. if (!(fileMode & S_IWRITE)) {
  128. AddAttribute("ReadOnly", "yes");
  129. }
  130. if (installedFile) {
  131. cmWIXAccessControlList acl(Logger, *installedFile, *this);
  132. acl.Apply();
  133. }
  134. patch.ApplyFragment(fileId, *this);
  135. EndElement("File");
  136. patch.ApplyFragment(componentId, *this);
  137. EndElement("Component");
  138. EndElement("DirectoryRef");
  139. return componentId;
  140. }