cmCPackWIXGenerator.cxx 39 KB

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