cmCPackWIXGenerator.cxx 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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 "cmCPackWIXGenerator.h"
  4. #include <CPack/cmCPackComponentGroup.h>
  5. #include <CPack/cmCPackLog.h>
  6. #include <algorithm>
  7. #include <cmCryptoHash.h>
  8. #include <cmGeneratedFileStream.h>
  9. #include <cmInstalledFile.h>
  10. #include <cmSystemTools.h>
  11. #include <cmUuid.h>
  12. #include "cmWIXDirectoriesSourceWriter.h"
  13. #include "cmWIXFeaturesSourceWriter.h"
  14. #include "cmWIXFilesSourceWriter.h"
  15. #include "cmWIXRichTextFormatWriter.h"
  16. #include "cmWIXSourceWriter.h"
  17. #include <cmsys/Directory.hxx>
  18. #include <cmsys/Encoding.hxx>
  19. #include <cmsys/FStream.hxx>
  20. #include <cmsys/SystemTools.hxx>
  21. #include <rpc.h> // for GUID generation
  22. cmCPackWIXGenerator::cmCPackWIXGenerator()
  23. : Patch(0)
  24. , ComponentGuidType(cmWIXSourceWriter::WIX_GENERATED_GUID)
  25. {
  26. }
  27. cmCPackWIXGenerator::~cmCPackWIXGenerator()
  28. {
  29. if (this->Patch) {
  30. delete this->Patch;
  31. }
  32. }
  33. int cmCPackWIXGenerator::InitializeInternal()
  34. {
  35. componentPackageMethod = ONE_PACKAGE;
  36. this->Patch = new cmWIXPatch(this->Logger);
  37. return this->Superclass::InitializeInternal();
  38. }
  39. bool cmCPackWIXGenerator::RunWiXCommand(std::string const& command)
  40. {
  41. std::string logFileName = this->CPackTopLevel + "/wix.log";
  42. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Running WiX command: " << command
  43. << std::endl);
  44. std::string output;
  45. int returnValue = 0;
  46. bool status = cmSystemTools::RunSingleCommand(command.c_str(), &output,
  47. &output, &returnValue, 0,
  48. cmSystemTools::OUTPUT_NONE);
  49. cmsys::ofstream logFile(logFileName.c_str(), std::ios::app);
  50. logFile << command << std::endl;
  51. logFile << output;
  52. logFile.close();
  53. if (!status || returnValue) {
  54. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running WiX candle. "
  55. "Please check '"
  56. << logFileName << "' for errors." << std::endl);
  57. return false;
  58. }
  59. return true;
  60. }
  61. bool cmCPackWIXGenerator::RunCandleCommand(std::string const& sourceFile,
  62. std::string const& objectFile)
  63. {
  64. std::string executable;
  65. if (!RequireOption("CPACK_WIX_CANDLE_EXECUTABLE", executable)) {
  66. return false;
  67. }
  68. std::ostringstream command;
  69. command << QuotePath(executable);
  70. command << " -nologo";
  71. command << " -arch " << GetArchitecture();
  72. command << " -out " << QuotePath(objectFile);
  73. for (extension_set_t::const_iterator i = CandleExtensions.begin();
  74. i != CandleExtensions.end(); ++i) {
  75. command << " -ext " << QuotePath(*i);
  76. }
  77. AddCustomFlags("CPACK_WIX_CANDLE_EXTRA_FLAGS", command);
  78. command << " " << QuotePath(sourceFile);
  79. return RunWiXCommand(command.str());
  80. }
  81. bool cmCPackWIXGenerator::RunLightCommand(std::string const& objectFiles)
  82. {
  83. std::string executable;
  84. if (!RequireOption("CPACK_WIX_LIGHT_EXECUTABLE", executable)) {
  85. return false;
  86. }
  87. std::ostringstream command;
  88. command << QuotePath(executable);
  89. command << " -nologo";
  90. command << " -out " << QuotePath(packageFileNames.at(0));
  91. for (extension_set_t::const_iterator i = this->LightExtensions.begin();
  92. i != this->LightExtensions.end(); ++i) {
  93. command << " -ext " << QuotePath(*i);
  94. }
  95. const char* const cultures = GetOption("CPACK_WIX_CULTURES");
  96. if (cultures) {
  97. command << " -cultures:" << cultures;
  98. }
  99. AddCustomFlags("CPACK_WIX_LIGHT_EXTRA_FLAGS", command);
  100. command << " " << objectFiles;
  101. return RunWiXCommand(command.str());
  102. }
  103. int cmCPackWIXGenerator::PackageFiles()
  104. {
  105. if (!PackageFilesImpl() || cmSystemTools::GetErrorOccuredFlag()) {
  106. cmCPackLogger(cmCPackLog::LOG_ERROR, "Fatal WiX Generator Error"
  107. << std::endl);
  108. return false;
  109. }
  110. return true;
  111. }
  112. bool cmCPackWIXGenerator::InitializeWiXConfiguration()
  113. {
  114. if (!ReadListFile("CPackWIX.cmake")) {
  115. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error while executing CPackWIX.cmake"
  116. << std::endl);
  117. return false;
  118. }
  119. if (GetOption("CPACK_WIX_PRODUCT_GUID") == 0) {
  120. std::string guid = GenerateGUID();
  121. SetOption("CPACK_WIX_PRODUCT_GUID", guid.c_str());
  122. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  123. "CPACK_WIX_PRODUCT_GUID implicitly set to " << guid << " . "
  124. << std::endl);
  125. }
  126. if (GetOption("CPACK_WIX_UPGRADE_GUID") == 0) {
  127. std::string guid = GenerateGUID();
  128. SetOption("CPACK_WIX_UPGRADE_GUID", guid.c_str());
  129. cmCPackLogger(
  130. cmCPackLog::LOG_WARNING, "CPACK_WIX_UPGRADE_GUID implicitly set to "
  131. << guid << " . "
  132. "Please refer to the documentation on how and why "
  133. "you might want to set this explicitly."
  134. << std::endl);
  135. }
  136. if (!RequireOption("CPACK_TOPLEVEL_DIRECTORY", this->CPackTopLevel)) {
  137. return false;
  138. }
  139. if (GetOption("CPACK_WIX_LICENSE_RTF") == 0) {
  140. std::string licenseFilename = this->CPackTopLevel + "/License.rtf";
  141. SetOption("CPACK_WIX_LICENSE_RTF", licenseFilename.c_str());
  142. if (!CreateLicenseFile()) {
  143. return false;
  144. }
  145. }
  146. if (GetOption("CPACK_PACKAGE_VENDOR") == 0) {
  147. std::string defaultVendor = "Humanity";
  148. SetOption("CPACK_PACKAGE_VENDOR", defaultVendor.c_str());
  149. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  150. "CPACK_PACKAGE_VENDOR implicitly set to "
  151. << defaultVendor << " . " << std::endl);
  152. }
  153. if (GetOption("CPACK_WIX_UI_REF") == 0) {
  154. std::string defaultRef = "WixUI_InstallDir";
  155. if (!this->Components.empty()) {
  156. defaultRef = "WixUI_FeatureTree";
  157. }
  158. SetOption("CPACK_WIX_UI_REF", defaultRef.c_str());
  159. }
  160. const char* packageContact = GetOption("CPACK_PACKAGE_CONTACT");
  161. if (packageContact != 0 && GetOption("CPACK_WIX_PROPERTY_ARPCONTACT") == 0) {
  162. SetOption("CPACK_WIX_PROPERTY_ARPCONTACT", packageContact);
  163. }
  164. CollectExtensions("CPACK_WIX_EXTENSIONS", this->CandleExtensions);
  165. CollectExtensions("CPACK_WIX_CANDLE_EXTENSIONS", this->CandleExtensions);
  166. this->LightExtensions.insert("WixUIExtension");
  167. CollectExtensions("CPACK_WIX_EXTENSIONS", this->LightExtensions);
  168. CollectExtensions("CPACK_WIX_LIGHT_EXTENSIONS", this->LightExtensions);
  169. const char* patchFilePath = GetOption("CPACK_WIX_PATCH_FILE");
  170. if (patchFilePath) {
  171. std::vector<std::string> patchFilePaths;
  172. cmSystemTools::ExpandListArgument(patchFilePath, patchFilePaths);
  173. for (size_t i = 0; i < patchFilePaths.size(); ++i) {
  174. if (!this->Patch->LoadFragments(patchFilePaths[i])) {
  175. return false;
  176. }
  177. }
  178. }
  179. // if install folder is supposed to be set absolutely, the default
  180. // component guid "*" cannot be used
  181. if (cmSystemTools::IsOn(GetOption("CPACK_WIX_SKIP_PROGRAM_FOLDER"))) {
  182. this->ComponentGuidType = cmWIXSourceWriter::CMAKE_GENERATED_GUID;
  183. }
  184. return true;
  185. }
  186. bool cmCPackWIXGenerator::PackageFilesImpl()
  187. {
  188. if (!InitializeWiXConfiguration()) {
  189. return false;
  190. }
  191. CreateWiXVariablesIncludeFile();
  192. CreateWiXPropertiesIncludeFile();
  193. CreateWiXProductFragmentIncludeFile();
  194. if (!CreateWiXSourceFiles()) {
  195. return false;
  196. }
  197. AppendUserSuppliedExtraSources();
  198. std::set<std::string> usedBaseNames;
  199. std::ostringstream objectFiles;
  200. for (size_t i = 0; i < this->WixSources.size(); ++i) {
  201. std::string const& sourceFilename = this->WixSources[i];
  202. std::string baseName =
  203. cmSystemTools::GetFilenameWithoutLastExtension(sourceFilename);
  204. unsigned int counter = 0;
  205. std::string uniqueBaseName = baseName;
  206. while (usedBaseNames.find(uniqueBaseName) != usedBaseNames.end()) {
  207. std::ostringstream tmp;
  208. tmp << baseName << ++counter;
  209. uniqueBaseName = tmp.str();
  210. }
  211. usedBaseNames.insert(uniqueBaseName);
  212. std::string objectFilename =
  213. this->CPackTopLevel + "/" + uniqueBaseName + ".wixobj";
  214. if (!RunCandleCommand(sourceFilename, objectFilename)) {
  215. return false;
  216. }
  217. objectFiles << " " << QuotePath(objectFilename);
  218. }
  219. AppendUserSuppliedExtraObjects(objectFiles);
  220. return RunLightCommand(objectFiles.str());
  221. }
  222. void cmCPackWIXGenerator::AppendUserSuppliedExtraSources()
  223. {
  224. const char* cpackWixExtraSources = GetOption("CPACK_WIX_EXTRA_SOURCES");
  225. if (!cpackWixExtraSources)
  226. return;
  227. cmSystemTools::ExpandListArgument(cpackWixExtraSources, this->WixSources);
  228. }
  229. void cmCPackWIXGenerator::AppendUserSuppliedExtraObjects(std::ostream& stream)
  230. {
  231. const char* cpackWixExtraObjects = GetOption("CPACK_WIX_EXTRA_OBJECTS");
  232. if (!cpackWixExtraObjects)
  233. return;
  234. std::vector<std::string> expandedExtraObjects;
  235. cmSystemTools::ExpandListArgument(cpackWixExtraObjects,
  236. expandedExtraObjects);
  237. for (size_t i = 0; i < expandedExtraObjects.size(); ++i) {
  238. stream << " " << QuotePath(expandedExtraObjects[i]);
  239. }
  240. }
  241. void cmCPackWIXGenerator::CreateWiXVariablesIncludeFile()
  242. {
  243. std::string includeFilename = this->CPackTopLevel + "/cpack_variables.wxi";
  244. cmWIXSourceWriter includeFile(this->Logger, includeFilename,
  245. this->ComponentGuidType,
  246. cmWIXSourceWriter::INCLUDE_ELEMENT_ROOT);
  247. CopyDefinition(includeFile, "CPACK_WIX_PRODUCT_GUID");
  248. CopyDefinition(includeFile, "CPACK_WIX_UPGRADE_GUID");
  249. CopyDefinition(includeFile, "CPACK_PACKAGE_VENDOR");
  250. CopyDefinition(includeFile, "CPACK_PACKAGE_NAME");
  251. CopyDefinition(includeFile, "CPACK_PACKAGE_VERSION");
  252. CopyDefinition(includeFile, "CPACK_WIX_LICENSE_RTF");
  253. CopyDefinition(includeFile, "CPACK_WIX_PRODUCT_ICON");
  254. CopyDefinition(includeFile, "CPACK_WIX_UI_BANNER");
  255. CopyDefinition(includeFile, "CPACK_WIX_UI_DIALOG");
  256. SetOptionIfNotSet("CPACK_WIX_PROGRAM_MENU_FOLDER",
  257. GetOption("CPACK_PACKAGE_NAME"));
  258. CopyDefinition(includeFile, "CPACK_WIX_PROGRAM_MENU_FOLDER");
  259. CopyDefinition(includeFile, "CPACK_WIX_UI_REF");
  260. }
  261. void cmCPackWIXGenerator::CreateWiXPropertiesIncludeFile()
  262. {
  263. std::string includeFilename = this->CPackTopLevel + "/properties.wxi";
  264. cmWIXSourceWriter includeFile(this->Logger, includeFilename,
  265. this->ComponentGuidType,
  266. cmWIXSourceWriter::INCLUDE_ELEMENT_ROOT);
  267. std::string prefix = "CPACK_WIX_PROPERTY_";
  268. std::vector<std::string> options = GetOptions();
  269. for (size_t i = 0; i < options.size(); ++i) {
  270. std::string const& name = options[i];
  271. if (name.length() > prefix.length() &&
  272. name.substr(0, prefix.length()) == prefix) {
  273. std::string id = name.substr(prefix.length());
  274. std::string value = GetOption(name.c_str());
  275. includeFile.BeginElement("Property");
  276. includeFile.AddAttribute("Id", id);
  277. includeFile.AddAttribute("Value", value);
  278. includeFile.EndElement("Property");
  279. }
  280. }
  281. if (GetOption("CPACK_WIX_PROPERTY_ARPINSTALLLOCATION") == 0) {
  282. includeFile.BeginElement("Property");
  283. includeFile.AddAttribute("Id", "INSTALL_ROOT");
  284. includeFile.AddAttribute("Secure", "yes");
  285. includeFile.BeginElement("RegistrySearch");
  286. includeFile.AddAttribute("Id", "FindInstallLocation");
  287. includeFile.AddAttribute("Root", "HKLM");
  288. includeFile.AddAttribute(
  289. "Key", "Software\\Microsoft\\Windows\\"
  290. "CurrentVersion\\Uninstall\\[WIX_UPGRADE_DETECTED]");
  291. includeFile.AddAttribute("Name", "InstallLocation");
  292. includeFile.AddAttribute("Type", "raw");
  293. includeFile.EndElement("RegistrySearch");
  294. includeFile.EndElement("Property");
  295. includeFile.BeginElement("SetProperty");
  296. includeFile.AddAttribute("Id", "ARPINSTALLLOCATION");
  297. includeFile.AddAttribute("Value", "[INSTALL_ROOT]");
  298. includeFile.AddAttribute("After", "CostFinalize");
  299. includeFile.EndElement("SetProperty");
  300. }
  301. }
  302. void cmCPackWIXGenerator::CreateWiXProductFragmentIncludeFile()
  303. {
  304. std::string includeFilename = this->CPackTopLevel + "/product_fragment.wxi";
  305. cmWIXSourceWriter includeFile(this->Logger, includeFilename,
  306. this->ComponentGuidType,
  307. cmWIXSourceWriter::INCLUDE_ELEMENT_ROOT);
  308. this->Patch->ApplyFragment("#PRODUCT", includeFile);
  309. }
  310. void cmCPackWIXGenerator::CopyDefinition(cmWIXSourceWriter& source,
  311. std::string const& name)
  312. {
  313. const char* value = GetOption(name.c_str());
  314. if (value) {
  315. AddDefinition(source, name, value);
  316. }
  317. }
  318. void cmCPackWIXGenerator::AddDefinition(cmWIXSourceWriter& source,
  319. std::string const& name,
  320. std::string const& value)
  321. {
  322. std::ostringstream tmp;
  323. tmp << name << "=\"" << value << '"';
  324. source.AddProcessingInstruction("define", tmp.str());
  325. }
  326. bool cmCPackWIXGenerator::CreateWiXSourceFiles()
  327. {
  328. // if install folder is supposed to be set absolutely, the default
  329. // component guid "*" cannot be used
  330. std::string directoryDefinitionsFilename =
  331. this->CPackTopLevel + "/directories.wxs";
  332. this->WixSources.push_back(directoryDefinitionsFilename);
  333. cmWIXDirectoriesSourceWriter directoryDefinitions(
  334. this->Logger, directoryDefinitionsFilename, this->ComponentGuidType);
  335. directoryDefinitions.BeginElement("Fragment");
  336. std::string installRoot;
  337. if (!RequireOption("CPACK_PACKAGE_INSTALL_DIRECTORY", installRoot)) {
  338. return false;
  339. }
  340. directoryDefinitions.BeginElement("Directory");
  341. directoryDefinitions.AddAttribute("Id", "TARGETDIR");
  342. directoryDefinitions.AddAttribute("Name", "SourceDir");
  343. size_t installRootSize =
  344. directoryDefinitions.BeginInstallationPrefixDirectory(GetRootFolderId(),
  345. installRoot);
  346. std::string fileDefinitionsFilename = this->CPackTopLevel + "/files.wxs";
  347. this->WixSources.push_back(fileDefinitionsFilename);
  348. cmWIXFilesSourceWriter fileDefinitions(this->Logger, fileDefinitionsFilename,
  349. this->ComponentGuidType);
  350. fileDefinitions.BeginElement("Fragment");
  351. std::string featureDefinitionsFilename =
  352. this->CPackTopLevel + "/features.wxs";
  353. this->WixSources.push_back(featureDefinitionsFilename);
  354. cmWIXFeaturesSourceWriter featureDefinitions(
  355. this->Logger, featureDefinitionsFilename, this->ComponentGuidType);
  356. featureDefinitions.BeginElement("Fragment");
  357. featureDefinitions.BeginElement("Feature");
  358. featureDefinitions.AddAttribute("Id", "ProductFeature");
  359. featureDefinitions.AddAttribute("Display", "expand");
  360. featureDefinitions.AddAttribute("Absent", "disallow");
  361. featureDefinitions.AddAttribute("ConfigurableDirectory", "INSTALL_ROOT");
  362. std::string cpackPackageName;
  363. if (!RequireOption("CPACK_PACKAGE_NAME", cpackPackageName)) {
  364. return false;
  365. }
  366. std::string featureTitle = cpackPackageName;
  367. if (const char* title = GetOption("CPACK_WIX_ROOT_FEATURE_TITLE")) {
  368. featureTitle = title;
  369. }
  370. featureDefinitions.AddAttribute("Title", featureTitle);
  371. if (const char* desc = GetOption("CPACK_WIX_ROOT_FEATURE_DESCRIPTION")) {
  372. featureDefinitions.AddAttribute("Description", desc);
  373. }
  374. featureDefinitions.AddAttribute("Level", "1");
  375. this->Patch->ApplyFragment("#PRODUCTFEATURE", featureDefinitions);
  376. const char* package = GetOption("CPACK_WIX_CMAKE_PACKAGE_REGISTRY");
  377. if (package) {
  378. featureDefinitions.CreateCMakePackageRegistryEntry(
  379. package, GetOption("CPACK_WIX_UPGRADE_GUID"));
  380. }
  381. if (!CreateFeatureHierarchy(featureDefinitions)) {
  382. return false;
  383. }
  384. featureDefinitions.EndElement("Feature");
  385. std::set<cmWIXShortcuts::Type> emittedShortcutTypes;
  386. cmWIXShortcuts globalShortcuts;
  387. if (Components.empty()) {
  388. AddComponentsToFeature(toplevel, "ProductFeature", directoryDefinitions,
  389. fileDefinitions, featureDefinitions,
  390. globalShortcuts);
  391. globalShortcuts.AddShortcutTypes(emittedShortcutTypes);
  392. } else {
  393. for (std::map<std::string, cmCPackComponent>::const_iterator i =
  394. this->Components.begin();
  395. i != this->Components.end(); ++i) {
  396. cmCPackComponent const& component = i->second;
  397. std::string componentPath = toplevel;
  398. componentPath += "/";
  399. componentPath += component.Name;
  400. std::string componentFeatureId = "CM_C_" + component.Name;
  401. cmWIXShortcuts featureShortcuts;
  402. AddComponentsToFeature(componentPath, componentFeatureId,
  403. directoryDefinitions, fileDefinitions,
  404. featureDefinitions, featureShortcuts);
  405. featureShortcuts.AddShortcutTypes(emittedShortcutTypes);
  406. if (!CreateShortcuts(component.Name, componentFeatureId,
  407. featureShortcuts, false, fileDefinitions,
  408. featureDefinitions)) {
  409. return false;
  410. }
  411. }
  412. }
  413. bool emitUninstallShortcut =
  414. emittedShortcutTypes.find(cmWIXShortcuts::START_MENU) !=
  415. emittedShortcutTypes.end();
  416. if (!CreateShortcuts(std::string(), "ProductFeature", globalShortcuts,
  417. emitUninstallShortcut, fileDefinitions,
  418. featureDefinitions)) {
  419. return false;
  420. }
  421. featureDefinitions.EndElement("Fragment");
  422. fileDefinitions.EndElement("Fragment");
  423. directoryDefinitions.EndInstallationPrefixDirectory(installRootSize);
  424. if (emittedShortcutTypes.find(cmWIXShortcuts::START_MENU) !=
  425. emittedShortcutTypes.end()) {
  426. directoryDefinitions.EmitStartMenuFolder(
  427. GetOption("CPACK_WIX_PROGRAM_MENU_FOLDER"));
  428. }
  429. if (emittedShortcutTypes.find(cmWIXShortcuts::DESKTOP) !=
  430. emittedShortcutTypes.end()) {
  431. directoryDefinitions.EmitDesktopFolder();
  432. }
  433. if (emittedShortcutTypes.find(cmWIXShortcuts::STARTUP) !=
  434. emittedShortcutTypes.end()) {
  435. directoryDefinitions.EmitStartupFolder();
  436. }
  437. directoryDefinitions.EndElement("Directory");
  438. directoryDefinitions.EndElement("Fragment");
  439. if (!GenerateMainSourceFileFromTemplate()) {
  440. return false;
  441. }
  442. return this->Patch->CheckForUnappliedFragments();
  443. }
  444. std::string cmCPackWIXGenerator::GetRootFolderId() const
  445. {
  446. if (cmSystemTools::IsOn(GetOption("CPACK_WIX_SKIP_PROGRAM_FOLDER"))) {
  447. return "";
  448. }
  449. std::string result = "ProgramFiles<64>Folder";
  450. const char* rootFolderId = GetOption("CPACK_WIX_ROOT_FOLDER_ID");
  451. if (rootFolderId) {
  452. result = rootFolderId;
  453. }
  454. if (GetArchitecture() == "x86") {
  455. cmSystemTools::ReplaceString(result, "<64>", "");
  456. } else {
  457. cmSystemTools::ReplaceString(result, "<64>", "64");
  458. }
  459. return result;
  460. }
  461. bool cmCPackWIXGenerator::GenerateMainSourceFileFromTemplate()
  462. {
  463. std::string wixTemplate = FindTemplate("WIX.template.in");
  464. if (GetOption("CPACK_WIX_TEMPLATE") != 0) {
  465. wixTemplate = GetOption("CPACK_WIX_TEMPLATE");
  466. }
  467. if (wixTemplate.empty()) {
  468. cmCPackLogger(cmCPackLog::LOG_ERROR,
  469. "Could not find CPack WiX template file WIX.template.in"
  470. << std::endl);
  471. return false;
  472. }
  473. std::string mainSourceFilePath = this->CPackTopLevel + "/main.wxs";
  474. if (!ConfigureFile(wixTemplate.c_str(), mainSourceFilePath.c_str())) {
  475. cmCPackLogger(cmCPackLog::LOG_ERROR, "Failed creating '"
  476. << mainSourceFilePath << "'' from template." << std::endl);
  477. return false;
  478. }
  479. this->WixSources.push_back(mainSourceFilePath);
  480. return true;
  481. }
  482. bool cmCPackWIXGenerator::CreateFeatureHierarchy(
  483. cmWIXFeaturesSourceWriter& featureDefinitions)
  484. {
  485. for (std::map<std::string, cmCPackComponentGroup>::const_iterator i =
  486. ComponentGroups.begin();
  487. i != ComponentGroups.end(); ++i) {
  488. cmCPackComponentGroup const& group = i->second;
  489. if (group.ParentGroup == 0) {
  490. featureDefinitions.EmitFeatureForComponentGroup(group, *this->Patch);
  491. }
  492. }
  493. for (std::map<std::string, cmCPackComponent>::const_iterator i =
  494. this->Components.begin();
  495. i != this->Components.end(); ++i) {
  496. cmCPackComponent const& component = i->second;
  497. if (!component.Group) {
  498. featureDefinitions.EmitFeatureForComponent(component, *this->Patch);
  499. }
  500. }
  501. return true;
  502. }
  503. bool cmCPackWIXGenerator::AddComponentsToFeature(
  504. std::string const& rootPath, std::string const& featureId,
  505. cmWIXDirectoriesSourceWriter& directoryDefinitions,
  506. cmWIXFilesSourceWriter& fileDefinitions,
  507. cmWIXFeaturesSourceWriter& featureDefinitions, cmWIXShortcuts& shortcuts)
  508. {
  509. featureDefinitions.BeginElement("FeatureRef");
  510. featureDefinitions.AddAttribute("Id", featureId);
  511. std::vector<std::string> cpackPackageExecutablesList;
  512. const char* cpackPackageExecutables = GetOption("CPACK_PACKAGE_EXECUTABLES");
  513. if (cpackPackageExecutables) {
  514. cmSystemTools::ExpandListArgument(cpackPackageExecutables,
  515. cpackPackageExecutablesList);
  516. if (cpackPackageExecutablesList.size() % 2 != 0) {
  517. cmCPackLogger(
  518. cmCPackLog::LOG_ERROR,
  519. "CPACK_PACKAGE_EXECUTABLES should contain pairs of <executable> and "
  520. "<text label>."
  521. << std::endl);
  522. return false;
  523. }
  524. }
  525. std::vector<std::string> cpackPackageDesktopLinksList;
  526. const char* cpackPackageDesktopLinks =
  527. GetOption("CPACK_CREATE_DESKTOP_LINKS");
  528. if (cpackPackageDesktopLinks) {
  529. cmSystemTools::ExpandListArgument(cpackPackageDesktopLinks,
  530. cpackPackageDesktopLinksList);
  531. }
  532. AddDirectoryAndFileDefinitons(rootPath, "INSTALL_ROOT", directoryDefinitions,
  533. fileDefinitions, featureDefinitions,
  534. cpackPackageExecutablesList,
  535. cpackPackageDesktopLinksList, shortcuts);
  536. featureDefinitions.EndElement("FeatureRef");
  537. return true;
  538. }
  539. bool cmCPackWIXGenerator::CreateShortcuts(
  540. std::string const& cpackComponentName, std::string const& featureId,
  541. cmWIXShortcuts const& shortcuts, bool emitUninstallShortcut,
  542. cmWIXFilesSourceWriter& fileDefinitions,
  543. cmWIXFeaturesSourceWriter& featureDefinitions)
  544. {
  545. if (!shortcuts.empty(cmWIXShortcuts::START_MENU)) {
  546. if (!this->CreateShortcutsOfSpecificType(
  547. cmWIXShortcuts::START_MENU, cpackComponentName, featureId, "",
  548. shortcuts, emitUninstallShortcut, fileDefinitions,
  549. featureDefinitions)) {
  550. return false;
  551. }
  552. }
  553. if (!shortcuts.empty(cmWIXShortcuts::DESKTOP)) {
  554. if (!this->CreateShortcutsOfSpecificType(
  555. cmWIXShortcuts::DESKTOP, cpackComponentName, featureId, "DESKTOP",
  556. shortcuts, false, fileDefinitions, featureDefinitions)) {
  557. return false;
  558. }
  559. }
  560. if (!shortcuts.empty(cmWIXShortcuts::STARTUP)) {
  561. if (!this->CreateShortcutsOfSpecificType(
  562. cmWIXShortcuts::STARTUP, cpackComponentName, featureId, "STARTUP",
  563. shortcuts, false, fileDefinitions, featureDefinitions)) {
  564. return false;
  565. }
  566. }
  567. return true;
  568. }
  569. bool cmCPackWIXGenerator::CreateShortcutsOfSpecificType(
  570. cmWIXShortcuts::Type type, std::string const& cpackComponentName,
  571. std::string const& featureId, std::string const& idPrefix,
  572. cmWIXShortcuts const& shortcuts, bool emitUninstallShortcut,
  573. cmWIXFilesSourceWriter& fileDefinitions,
  574. cmWIXFeaturesSourceWriter& featureDefinitions)
  575. {
  576. std::string directoryId;
  577. switch (type) {
  578. case cmWIXShortcuts::START_MENU:
  579. directoryId = "PROGRAM_MENU_FOLDER";
  580. break;
  581. case cmWIXShortcuts::DESKTOP:
  582. directoryId = "DesktopFolder";
  583. break;
  584. case cmWIXShortcuts::STARTUP:
  585. directoryId = "StartupFolder";
  586. break;
  587. default:
  588. return false;
  589. }
  590. featureDefinitions.BeginElement("FeatureRef");
  591. featureDefinitions.AddAttribute("Id", featureId);
  592. std::string cpackVendor;
  593. if (!RequireOption("CPACK_PACKAGE_VENDOR", cpackVendor)) {
  594. return false;
  595. }
  596. std::string cpackPackageName;
  597. if (!RequireOption("CPACK_PACKAGE_NAME", cpackPackageName)) {
  598. return false;
  599. }
  600. std::string idSuffix;
  601. if (!cpackComponentName.empty()) {
  602. idSuffix += "_";
  603. idSuffix += cpackComponentName;
  604. }
  605. std::string componentId = "CM_SHORTCUT";
  606. if (idPrefix.size()) {
  607. componentId += "_" + idPrefix;
  608. }
  609. componentId += idSuffix;
  610. fileDefinitions.BeginElement("DirectoryRef");
  611. fileDefinitions.AddAttribute("Id", directoryId);
  612. fileDefinitions.BeginElement("Component");
  613. fileDefinitions.AddAttribute("Id", componentId);
  614. fileDefinitions.AddAttribute(
  615. "Guid", fileDefinitions.CreateGuidFromComponentId(componentId));
  616. this->Patch->ApplyFragment(componentId, fileDefinitions);
  617. std::string registryKey =
  618. std::string("Software\\") + cpackVendor + "\\" + cpackPackageName;
  619. shortcuts.EmitShortcuts(type, registryKey, cpackComponentName,
  620. fileDefinitions);
  621. if (type == cmWIXShortcuts::START_MENU) {
  622. fileDefinitions.EmitRemoveFolder("CM_REMOVE_PROGRAM_MENU_FOLDER" +
  623. idSuffix);
  624. }
  625. if (emitUninstallShortcut) {
  626. fileDefinitions.EmitUninstallShortcut(cpackPackageName);
  627. }
  628. fileDefinitions.EndElement("Component");
  629. fileDefinitions.EndElement("DirectoryRef");
  630. featureDefinitions.EmitComponentRef(componentId);
  631. featureDefinitions.EndElement("FeatureRef");
  632. return true;
  633. }
  634. bool cmCPackWIXGenerator::CreateLicenseFile()
  635. {
  636. std::string licenseSourceFilename;
  637. if (!RequireOption("CPACK_RESOURCE_FILE_LICENSE", licenseSourceFilename)) {
  638. return false;
  639. }
  640. std::string licenseDestinationFilename;
  641. if (!RequireOption("CPACK_WIX_LICENSE_RTF", licenseDestinationFilename)) {
  642. return false;
  643. }
  644. std::string extension = GetRightmostExtension(licenseSourceFilename);
  645. if (extension == ".rtf") {
  646. cmSystemTools::CopyAFile(licenseSourceFilename.c_str(),
  647. licenseDestinationFilename.c_str());
  648. } else if (extension == ".txt") {
  649. cmWIXRichTextFormatWriter rtfWriter(licenseDestinationFilename);
  650. cmsys::ifstream licenseSource(licenseSourceFilename.c_str());
  651. std::string line;
  652. while (std::getline(licenseSource, line)) {
  653. rtfWriter.AddText(line);
  654. rtfWriter.AddText("\n");
  655. }
  656. } else {
  657. cmCPackLogger(cmCPackLog::LOG_ERROR,
  658. "unsupported WiX License file extension '"
  659. << extension << "'" << std::endl);
  660. return false;
  661. }
  662. return true;
  663. }
  664. void cmCPackWIXGenerator::AddDirectoryAndFileDefinitons(
  665. std::string const& topdir, std::string const& directoryId,
  666. cmWIXDirectoriesSourceWriter& directoryDefinitions,
  667. cmWIXFilesSourceWriter& fileDefinitions,
  668. cmWIXFeaturesSourceWriter& featureDefinitions,
  669. std::vector<std::string> const& packageExecutables,
  670. std::vector<std::string> const& desktopExecutables,
  671. cmWIXShortcuts& shortcuts)
  672. {
  673. cmsys::Directory dir;
  674. dir.Load(topdir.c_str());
  675. std::string relativeDirectoryPath =
  676. cmSystemTools::RelativePath(toplevel.c_str(), topdir.c_str());
  677. if (relativeDirectoryPath.empty()) {
  678. relativeDirectoryPath = ".";
  679. }
  680. cmInstalledFile const* directoryInstalledFile = this->GetInstalledFile(
  681. this->RelativePathWithoutComponentPrefix(relativeDirectoryPath));
  682. bool emptyDirectory = dir.GetNumberOfFiles() == 2;
  683. bool createDirectory = false;
  684. if (emptyDirectory) {
  685. createDirectory = true;
  686. }
  687. if (directoryInstalledFile) {
  688. if (directoryInstalledFile->HasProperty("CPACK_WIX_ACL")) {
  689. createDirectory = true;
  690. }
  691. }
  692. if (createDirectory) {
  693. std::string componentId = fileDefinitions.EmitComponentCreateFolder(
  694. directoryId, GenerateGUID(), directoryInstalledFile);
  695. featureDefinitions.EmitComponentRef(componentId);
  696. }
  697. if (emptyDirectory) {
  698. return;
  699. }
  700. for (size_t i = 0; i < dir.GetNumberOfFiles(); ++i) {
  701. std::string fileName = dir.GetFile(static_cast<unsigned long>(i));
  702. if (fileName == "." || fileName == "..") {
  703. continue;
  704. }
  705. std::string fullPath = topdir + "/" + fileName;
  706. std::string relativePath =
  707. cmSystemTools::RelativePath(toplevel.c_str(), fullPath.c_str());
  708. std::string id = PathToId(relativePath);
  709. if (cmSystemTools::FileIsDirectory(fullPath.c_str())) {
  710. std::string subDirectoryId = std::string("CM_D") + id;
  711. directoryDefinitions.BeginElement("Directory");
  712. directoryDefinitions.AddAttribute("Id", subDirectoryId);
  713. directoryDefinitions.AddAttribute("Name", fileName);
  714. AddDirectoryAndFileDefinitons(
  715. fullPath, subDirectoryId, directoryDefinitions, fileDefinitions,
  716. featureDefinitions, packageExecutables, desktopExecutables, shortcuts);
  717. this->Patch->ApplyFragment(subDirectoryId, directoryDefinitions);
  718. directoryDefinitions.EndElement("Directory");
  719. } else {
  720. cmInstalledFile const* installedFile = this->GetInstalledFile(
  721. this->RelativePathWithoutComponentPrefix(relativePath));
  722. if (installedFile) {
  723. shortcuts.CreateFromProperties(id, directoryId, *installedFile);
  724. }
  725. std::string componentId = fileDefinitions.EmitComponentFile(
  726. directoryId, id, fullPath, *(this->Patch), installedFile);
  727. featureDefinitions.EmitComponentRef(componentId);
  728. for (size_t j = 0; j < packageExecutables.size(); ++j) {
  729. std::string const& executableName = packageExecutables[j++];
  730. std::string const& textLabel = packageExecutables[j];
  731. if (cmSystemTools::LowerCase(fileName) ==
  732. cmSystemTools::LowerCase(executableName) + ".exe") {
  733. cmWIXShortcut shortcut;
  734. shortcut.label = textLabel;
  735. shortcut.workingDirectoryId = directoryId;
  736. shortcuts.insert(cmWIXShortcuts::START_MENU, id, shortcut);
  737. if (!desktopExecutables.empty() &&
  738. std::find(desktopExecutables.begin(), desktopExecutables.end(),
  739. executableName) != desktopExecutables.end()) {
  740. shortcuts.insert(cmWIXShortcuts::DESKTOP, id, shortcut);
  741. }
  742. }
  743. }
  744. }
  745. }
  746. }
  747. bool cmCPackWIXGenerator::RequireOption(std::string const& name,
  748. std::string& value) const
  749. {
  750. const char* tmp = GetOption(name.c_str());
  751. if (tmp) {
  752. value = tmp;
  753. return true;
  754. } else {
  755. cmCPackLogger(cmCPackLog::LOG_ERROR, "Required variable "
  756. << name << " not set" << std::endl);
  757. return false;
  758. }
  759. }
  760. std::string cmCPackWIXGenerator::GetArchitecture() const
  761. {
  762. std::string void_p_size;
  763. RequireOption("CPACK_WIX_SIZEOF_VOID_P", void_p_size);
  764. if (void_p_size == "8") {
  765. return "x64";
  766. } else {
  767. return "x86";
  768. }
  769. }
  770. std::string cmCPackWIXGenerator::GenerateGUID()
  771. {
  772. UUID guid;
  773. UuidCreate(&guid);
  774. unsigned short* tmp = 0;
  775. UuidToStringW(&guid, &tmp);
  776. std::string result =
  777. cmsys::Encoding::ToNarrow(reinterpret_cast<wchar_t*>(tmp));
  778. RpcStringFreeW(&tmp);
  779. return cmSystemTools::UpperCase(result);
  780. }
  781. std::string cmCPackWIXGenerator::QuotePath(std::string const& path)
  782. {
  783. return std::string("\"") + path + '"';
  784. }
  785. std::string cmCPackWIXGenerator::GetRightmostExtension(
  786. std::string const& filename)
  787. {
  788. std::string extension;
  789. std::string::size_type i = filename.rfind(".");
  790. if (i != std::string::npos) {
  791. extension = filename.substr(i);
  792. }
  793. return cmSystemTools::LowerCase(extension);
  794. }
  795. std::string cmCPackWIXGenerator::PathToId(std::string const& path)
  796. {
  797. id_map_t::const_iterator i = PathToIdMap.find(path);
  798. if (i != PathToIdMap.end())
  799. return i->second;
  800. std::string id = CreateNewIdForPath(path);
  801. return id;
  802. }
  803. std::string cmCPackWIXGenerator::CreateNewIdForPath(std::string const& path)
  804. {
  805. std::vector<std::string> components;
  806. cmSystemTools::SplitPath(path.c_str(), components, false);
  807. size_t replacementCount = 0;
  808. std::string identifier;
  809. std::string currentComponent;
  810. for (size_t i = 1; i < components.size(); ++i) {
  811. if (i != 1)
  812. identifier += '.';
  813. currentComponent =
  814. NormalizeComponentForId(components[i], replacementCount);
  815. identifier += currentComponent;
  816. }
  817. std::string idPrefix = "P";
  818. size_t replacementPercent = replacementCount * 100 / identifier.size();
  819. if (replacementPercent > 33 || identifier.size() > 60) {
  820. identifier = CreateHashedId(path, currentComponent);
  821. idPrefix = "H";
  822. }
  823. std::ostringstream result;
  824. result << idPrefix << "_" << identifier;
  825. size_t ambiguityCount = ++IdAmbiguityCounter[identifier];
  826. if (ambiguityCount > 999) {
  827. cmCPackLogger(cmCPackLog::LOG_ERROR,
  828. "Error while trying to generate a unique Id for '"
  829. << path << "'" << std::endl);
  830. return std::string();
  831. } else if (ambiguityCount > 1) {
  832. result << "_" << ambiguityCount;
  833. }
  834. std::string resultString = result.str();
  835. PathToIdMap[path] = resultString;
  836. return resultString;
  837. }
  838. std::string cmCPackWIXGenerator::CreateHashedId(
  839. std::string const& path, std::string const& normalizedFilename)
  840. {
  841. cmCryptoHash sha1(cmCryptoHash::AlgoSHA1);
  842. std::string const hash = sha1.HashString(path);
  843. std::string identifier;
  844. identifier += hash.substr(0, 7) + "_";
  845. const size_t maxFileNameLength = 52;
  846. if (normalizedFilename.length() > maxFileNameLength) {
  847. identifier += normalizedFilename.substr(0, maxFileNameLength - 3);
  848. identifier += "...";
  849. } else {
  850. identifier += normalizedFilename;
  851. }
  852. return identifier;
  853. }
  854. std::string cmCPackWIXGenerator::NormalizeComponentForId(
  855. std::string const& component, size_t& replacementCount)
  856. {
  857. std::string result;
  858. result.resize(component.size());
  859. for (size_t i = 0; i < component.size(); ++i) {
  860. char c = component[i];
  861. if (IsLegalIdCharacter(c)) {
  862. result[i] = c;
  863. } else {
  864. result[i] = '_';
  865. ++replacementCount;
  866. }
  867. }
  868. return result;
  869. }
  870. bool cmCPackWIXGenerator::IsLegalIdCharacter(char c)
  871. {
  872. return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') ||
  873. (c >= 'A' && c <= 'Z') || c == '_' || c == '.';
  874. }
  875. void cmCPackWIXGenerator::CollectExtensions(std::string const& variableName,
  876. extension_set_t& extensions)
  877. {
  878. const char* variableContent = GetOption(variableName.c_str());
  879. if (!variableContent)
  880. return;
  881. std::vector<std::string> list;
  882. cmSystemTools::ExpandListArgument(variableContent, list);
  883. extensions.insert(list.begin(), list.end());
  884. }
  885. void cmCPackWIXGenerator::AddCustomFlags(std::string const& variableName,
  886. std::ostream& stream)
  887. {
  888. const char* variableContent = GetOption(variableName.c_str());
  889. if (!variableContent)
  890. return;
  891. std::vector<std::string> list;
  892. cmSystemTools::ExpandListArgument(variableContent, list);
  893. for (std::vector<std::string>::const_iterator i = list.begin();
  894. i != list.end(); ++i) {
  895. stream << " " << QuotePath(*i);
  896. }
  897. }
  898. std::string cmCPackWIXGenerator::RelativePathWithoutComponentPrefix(
  899. std::string const& path)
  900. {
  901. if (this->Components.empty()) {
  902. return path;
  903. }
  904. std::string::size_type pos = path.find('/');
  905. return path.substr(pos + 1);
  906. }