cmCPackWIXGenerator.cxx 34 KB

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