cmCPackNSISGenerator.cxx 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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 "cmCPackNSISGenerator.h"
  4. #include <algorithm>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <map>
  8. #include <sstream>
  9. #include <utility>
  10. #include <cmext/algorithm>
  11. #include "cmsys/Directory.hxx"
  12. #include "cmsys/RegularExpression.hxx"
  13. #include "cmCPackComponentGroup.h"
  14. #include "cmCPackGenerator.h"
  15. #include "cmCPackLog.h"
  16. #include "cmDuration.h"
  17. #include "cmGeneratedFileStream.h"
  18. #include "cmList.h"
  19. #include "cmStringAlgorithms.h"
  20. #include "cmSystemTools.h"
  21. #include "cmValue.h"
  22. /* NSIS uses different command line syntax on Windows and others */
  23. #ifdef _WIN32
  24. # define NSIS_OPT "/"
  25. #else
  26. # define NSIS_OPT "-"
  27. #endif
  28. cmCPackNSISGenerator::cmCPackNSISGenerator(bool nsis64)
  29. {
  30. this->Nsis64 = nsis64;
  31. }
  32. cmCPackNSISGenerator::~cmCPackNSISGenerator() = default;
  33. int cmCPackNSISGenerator::PackageFiles()
  34. {
  35. // TODO: Fix nsis to force out file name
  36. std::string nsisInFileName = this->FindTemplate("NSIS.template.in");
  37. if (nsisInFileName.empty()) {
  38. cmCPackLogger(cmCPackLog::LOG_ERROR,
  39. "CPack error: Could not find NSIS installer template file."
  40. << std::endl);
  41. return false;
  42. }
  43. std::string nsisInInstallOptions =
  44. this->FindTemplate("NSIS.InstallOptions.ini.in");
  45. if (nsisInInstallOptions.empty()) {
  46. cmCPackLogger(cmCPackLog::LOG_ERROR,
  47. "CPack error: Could not find NSIS installer options file."
  48. << std::endl);
  49. return false;
  50. }
  51. std::string nsisFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  52. std::string tmpFile = cmStrCat(nsisFileName, "/NSISOutput.log");
  53. std::string nsisInstallOptions = nsisFileName + "/NSIS.InstallOptions.ini";
  54. nsisFileName += "/project.nsi";
  55. std::ostringstream str;
  56. for (std::string const& file : this->files) {
  57. std::string outputDir = "$INSTDIR";
  58. std::string fileN = cmSystemTools::RelativePath(this->toplevel, file);
  59. if (!this->Components.empty()) {
  60. const std::string::size_type pos = fileN.find('/');
  61. // Use the custom component install directory if we have one
  62. if (pos != std::string::npos) {
  63. auto componentName = cm::string_view(fileN).substr(0, pos);
  64. outputDir = this->CustomComponentInstallDirectory(componentName);
  65. } else {
  66. outputDir = this->CustomComponentInstallDirectory(fileN);
  67. }
  68. // Strip off the component part of the path.
  69. fileN = fileN.substr(pos + 1);
  70. }
  71. std::replace(fileN.begin(), fileN.end(), '/', '\\');
  72. str << " Delete \"" << outputDir << "\\" << fileN << "\"" << std::endl;
  73. }
  74. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  75. "Uninstall Files: " << str.str() << std::endl);
  76. this->SetOptionIfNotSet("CPACK_NSIS_DELETE_FILES", str.str());
  77. std::vector<std::string> dirs;
  78. this->GetListOfSubdirectories(this->toplevel.c_str(), dirs);
  79. std::ostringstream dstr;
  80. for (std::string const& dir : dirs) {
  81. std::string componentName;
  82. std::string fileN = cmSystemTools::RelativePath(this->toplevel, dir);
  83. if (fileN.empty()) {
  84. continue;
  85. }
  86. if (!this->Components.empty()) {
  87. // If this is a component installation, strip off the component
  88. // part of the path.
  89. std::string::size_type slash = fileN.find('/');
  90. if (slash != std::string::npos) {
  91. // If this is a component installation, determine which component it
  92. // is.
  93. componentName = fileN.substr(0, slash);
  94. // Strip off the component part of the path.
  95. fileN.erase(0, slash + 1);
  96. }
  97. }
  98. std::replace(fileN.begin(), fileN.end(), '/', '\\');
  99. const std::string componentOutputDir =
  100. this->CustomComponentInstallDirectory(componentName);
  101. dstr << " RMDir \"" << componentOutputDir << "\\" << fileN << "\""
  102. << std::endl;
  103. if (!componentName.empty()) {
  104. this->Components[componentName].Directories.push_back(std::move(fileN));
  105. }
  106. }
  107. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  108. "Uninstall Dirs: " << dstr.str() << std::endl);
  109. this->SetOptionIfNotSet("CPACK_NSIS_DELETE_DIRECTORIES", dstr.str());
  110. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  111. "Configure file: " << nsisInFileName << " to " << nsisFileName
  112. << std::endl);
  113. if (this->IsSet("CPACK_NSIS_MUI_ICON") ||
  114. this->IsSet("CPACK_NSIS_MUI_UNIICON")) {
  115. std::string installerIconCode;
  116. if (this->IsSet("CPACK_NSIS_MUI_ICON")) {
  117. installerIconCode += cmStrCat(
  118. "!define MUI_ICON \"", this->GetOption("CPACK_NSIS_MUI_ICON"), "\"\n");
  119. }
  120. if (this->IsSet("CPACK_NSIS_MUI_UNIICON")) {
  121. installerIconCode +=
  122. cmStrCat("!define MUI_UNICON \"",
  123. this->GetOption("CPACK_NSIS_MUI_UNIICON"), "\"\n");
  124. }
  125. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_MUI_ICON_CODE",
  126. installerIconCode.c_str());
  127. }
  128. std::string installerHeaderImage;
  129. if (this->IsSet("CPACK_NSIS_MUI_HEADERIMAGE")) {
  130. installerHeaderImage = *this->GetOption("CPACK_NSIS_MUI_HEADERIMAGE");
  131. } else if (this->IsSet("CPACK_PACKAGE_ICON")) {
  132. installerHeaderImage = *this->GetOption("CPACK_PACKAGE_ICON");
  133. }
  134. if (!installerHeaderImage.empty()) {
  135. std::string installerIconCode = cmStrCat(
  136. "!define MUI_HEADERIMAGE_BITMAP \"", installerHeaderImage, "\"\n");
  137. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_ICON_CODE",
  138. installerIconCode);
  139. }
  140. if (this->IsSet("CPACK_NSIS_MUI_WELCOMEFINISHPAGE_BITMAP")) {
  141. std::string installerBitmapCode = cmStrCat(
  142. "!define MUI_WELCOMEFINISHPAGE_BITMAP \"",
  143. this->GetOption("CPACK_NSIS_MUI_WELCOMEFINISHPAGE_BITMAP"), "\"\n");
  144. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_MUI_WELCOMEFINISH_CODE",
  145. installerBitmapCode);
  146. }
  147. if (this->IsSet("CPACK_NSIS_MUI_UNWELCOMEFINISHPAGE_BITMAP")) {
  148. std::string installerBitmapCode = cmStrCat(
  149. "!define MUI_UNWELCOMEFINISHPAGE_BITMAP \"",
  150. this->GetOption("CPACK_NSIS_MUI_UNWELCOMEFINISHPAGE_BITMAP"), "\"\n");
  151. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_MUI_UNWELCOMEFINISH_CODE",
  152. installerBitmapCode);
  153. }
  154. if (this->IsSet("CPACK_NSIS_MUI_FINISHPAGE_RUN")) {
  155. std::string installerRunCode =
  156. cmStrCat("!define MUI_FINISHPAGE_RUN \"$INSTDIR\\",
  157. this->GetOption("CPACK_NSIS_EXECUTABLES_DIRECTORY"), '\\',
  158. this->GetOption("CPACK_NSIS_MUI_FINISHPAGE_RUN"), "\"\n");
  159. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_MUI_FINISHPAGE_RUN_CODE",
  160. installerRunCode);
  161. }
  162. if (this->IsSet("CPACK_NSIS_WELCOME_TITLE")) {
  163. std::string welcomeTitleCode =
  164. cmStrCat("!define MUI_WELCOMEPAGE_TITLE \"",
  165. this->GetOption("CPACK_NSIS_WELCOME_TITLE"), "\"");
  166. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_WELCOME_TITLE_CODE",
  167. welcomeTitleCode);
  168. }
  169. if (this->IsSet("CPACK_NSIS_WELCOME_TITLE_3LINES")) {
  170. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_WELCOME_TITLE_3LINES_CODE",
  171. "!define MUI_WELCOMEPAGE_TITLE_3LINES");
  172. }
  173. if (this->IsSet("CPACK_NSIS_FINISH_TITLE")) {
  174. std::string finishTitleCode =
  175. cmStrCat("!define MUI_FINISHPAGE_TITLE \"",
  176. this->GetOption("CPACK_NSIS_FINISH_TITLE"), "\"");
  177. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_FINISH_TITLE_CODE",
  178. finishTitleCode);
  179. }
  180. if (this->IsSet("CPACK_NSIS_FINISH_TITLE_3LINES")) {
  181. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_FINISH_TITLE_3LINES_CODE",
  182. "!define MUI_FINISHPAGE_TITLE_3LINES");
  183. }
  184. if (this->IsSet("CPACK_NSIS_MANIFEST_DPI_AWARE")) {
  185. this->SetOptionIfNotSet("CPACK_NSIS_MANIFEST_DPI_AWARE_CODE",
  186. "ManifestDPIAware true");
  187. }
  188. if (this->IsSet("CPACK_NSIS_BRANDING_TEXT")) {
  189. // Default position to LEFT
  190. std::string brandingTextPosition = "LEFT";
  191. if (this->IsSet("CPACK_NSIS_BRANDING_TEXT_TRIM_POSITION")) {
  192. std::string wantedPosition =
  193. this->GetOption("CPACK_NSIS_BRANDING_TEXT_TRIM_POSITION");
  194. if (!wantedPosition.empty()) {
  195. const std::set<std::string> possiblePositions{ "CENTER", "LEFT",
  196. "RIGHT" };
  197. if (possiblePositions.find(wantedPosition) ==
  198. possiblePositions.end()) {
  199. cmCPackLogger(cmCPackLog::LOG_ERROR,
  200. "Unsupported branding text trim position "
  201. << wantedPosition << std::endl);
  202. return false;
  203. }
  204. brandingTextPosition = wantedPosition;
  205. }
  206. }
  207. std::string brandingTextCode =
  208. cmStrCat("BrandingText /TRIM", brandingTextPosition, " \"",
  209. this->GetOption("CPACK_NSIS_BRANDING_TEXT"), "\"\n");
  210. this->SetOptionIfNotSet("CPACK_NSIS_BRANDING_TEXT_CODE", brandingTextCode);
  211. }
  212. if (!this->IsSet("CPACK_NSIS_IGNORE_LICENSE_PAGE")) {
  213. std::string licenceCode =
  214. cmStrCat("!insertmacro MUI_PAGE_LICENSE \"",
  215. this->GetOption("CPACK_RESOURCE_FILE_LICENSE"), "\"\n");
  216. this->SetOptionIfNotSet("CPACK_NSIS_LICENSE_PAGE", licenceCode);
  217. }
  218. std::string nsisPreArguments;
  219. if (cmValue nsisArguments =
  220. this->GetOption("CPACK_NSIS_EXECUTABLE_PRE_ARGUMENTS")) {
  221. std::vector<std::string> expandedArguments;
  222. cmExpandList(nsisArguments, expandedArguments);
  223. for (auto& arg : expandedArguments) {
  224. if (!cmHasPrefix(arg, NSIS_OPT)) {
  225. nsisPreArguments = cmStrCat(nsisPreArguments, NSIS_OPT);
  226. }
  227. nsisPreArguments = cmStrCat(nsisPreArguments, arg, ' ');
  228. }
  229. }
  230. std::string nsisPostArguments;
  231. if (cmValue nsisArguments =
  232. this->GetOption("CPACK_NSIS_EXECUTABLE_POST_ARGUMENTS")) {
  233. std::vector<std::string> expandedArguments;
  234. cmExpandList(nsisArguments, expandedArguments);
  235. for (auto& arg : expandedArguments) {
  236. if (!cmHasPrefix(arg, NSIS_OPT)) {
  237. nsisPostArguments = cmStrCat(nsisPostArguments, NSIS_OPT);
  238. }
  239. nsisPostArguments = cmStrCat(nsisPostArguments, arg, ' ');
  240. }
  241. }
  242. // Setup all of the component sections
  243. if (this->Components.empty()) {
  244. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLATION_TYPES", "");
  245. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_MUI_COMPONENTS_DESC", "");
  246. this->SetOptionIfNotSet("CPACK_NSIS_PAGE_COMPONENTS", "");
  247. this->SetOptionIfNotSet("CPACK_NSIS_FULL_INSTALL",
  248. R"(File /r "${INST_DIR}\*.*")");
  249. this->SetOptionIfNotSet("CPACK_NSIS_COMPONENT_SECTIONS", "");
  250. this->SetOptionIfNotSet("CPACK_NSIS_COMPONENT_SECTION_LIST", "");
  251. this->SetOptionIfNotSet("CPACK_NSIS_SECTION_SELECTED_VARS", "");
  252. } else {
  253. std::string componentCode;
  254. std::string sectionList;
  255. std::string selectedVarsList;
  256. std::string componentDescriptions;
  257. std::string groupDescriptions;
  258. std::string installTypesCode;
  259. std::string defines;
  260. std::ostringstream macrosOut;
  261. bool anyDownloadedComponents = false;
  262. // Create installation types. The order is significant, so we first fill
  263. // in a vector based on the indices, and print them in that order.
  264. std::vector<cmCPackInstallationType*> installTypes(
  265. this->InstallationTypes.size());
  266. for (auto& installType : this->InstallationTypes) {
  267. installTypes[installType.second.Index - 1] = &installType.second;
  268. }
  269. for (cmCPackInstallationType* installType : installTypes) {
  270. installTypesCode += "InstType \"";
  271. installTypesCode += installType->DisplayName;
  272. installTypesCode += "\"\n";
  273. }
  274. // Create installation groups first
  275. for (auto& group : this->ComponentGroups) {
  276. if (group.second.ParentGroup == nullptr) {
  277. componentCode +=
  278. this->CreateComponentGroupDescription(&group.second, macrosOut);
  279. }
  280. // Add the group description, if any.
  281. if (!group.second.Description.empty()) {
  282. groupDescriptions += " !insertmacro MUI_DESCRIPTION_TEXT ${" +
  283. group.first + "} \"" +
  284. cmCPackNSISGenerator::TranslateNewlines(group.second.Description) +
  285. "\"\n";
  286. }
  287. }
  288. // Create the remaining components, which aren't associated with groups.
  289. for (auto& comp : this->Components) {
  290. if (comp.second.Files.empty()) {
  291. // NSIS cannot cope with components that have no files.
  292. continue;
  293. }
  294. anyDownloadedComponents =
  295. anyDownloadedComponents || comp.second.IsDownloaded;
  296. if (!comp.second.Group) {
  297. componentCode +=
  298. this->CreateComponentDescription(&comp.second, macrosOut);
  299. }
  300. // Add this component to the various section lists.
  301. sectionList += R"( !insertmacro "${MacroName}" ")";
  302. sectionList += comp.first;
  303. sectionList += "\"\n";
  304. selectedVarsList += "Var " + comp.first + "_selected\n";
  305. selectedVarsList += "Var " + comp.first + "_was_installed\n";
  306. // Add the component description, if any.
  307. if (!comp.second.Description.empty()) {
  308. componentDescriptions += " !insertmacro MUI_DESCRIPTION_TEXT ${" +
  309. comp.first + "} \"" +
  310. cmCPackNSISGenerator::TranslateNewlines(comp.second.Description) +
  311. "\"\n";
  312. }
  313. }
  314. componentCode += macrosOut.str();
  315. if (componentDescriptions.empty() && groupDescriptions.empty()) {
  316. // Turn off the "Description" box
  317. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_MUI_COMPONENTS_DESC",
  318. "!define MUI_COMPONENTSPAGE_NODESC");
  319. } else {
  320. componentDescriptions = "!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN\n" +
  321. componentDescriptions + groupDescriptions +
  322. "!insertmacro MUI_FUNCTION_DESCRIPTION_END\n";
  323. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_MUI_COMPONENTS_DESC",
  324. componentDescriptions);
  325. }
  326. if (anyDownloadedComponents) {
  327. defines += "!define CPACK_USES_DOWNLOAD\n";
  328. if (cmIsOn(this->GetOption("CPACK_ADD_REMOVE"))) {
  329. defines += "!define CPACK_NSIS_ADD_REMOVE\n";
  330. }
  331. }
  332. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLATION_TYPES", installTypesCode);
  333. this->SetOptionIfNotSet("CPACK_NSIS_PAGE_COMPONENTS",
  334. "!insertmacro MUI_PAGE_COMPONENTS");
  335. this->SetOptionIfNotSet("CPACK_NSIS_FULL_INSTALL", "");
  336. this->SetOptionIfNotSet("CPACK_NSIS_COMPONENT_SECTIONS", componentCode);
  337. this->SetOptionIfNotSet("CPACK_NSIS_COMPONENT_SECTION_LIST", sectionList);
  338. this->SetOptionIfNotSet("CPACK_NSIS_SECTION_SELECTED_VARS",
  339. selectedVarsList);
  340. this->SetOption("CPACK_NSIS_DEFINES", defines);
  341. }
  342. this->ConfigureFile(nsisInInstallOptions, nsisInstallOptions);
  343. this->ConfigureFile(nsisInFileName, nsisFileName);
  344. std::string nsisCmd =
  345. cmStrCat('"', this->GetOption("CPACK_INSTALLER_PROGRAM"), "\" ",
  346. nsisPreArguments, " \"", nsisFileName, '"');
  347. if (!nsisPostArguments.empty()) {
  348. nsisCmd = cmStrCat(nsisCmd, " ", nsisPostArguments);
  349. }
  350. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Execute: " << nsisCmd << std::endl);
  351. std::string output;
  352. int retVal = 1;
  353. bool res = cmSystemTools::RunSingleCommand(
  354. nsisCmd, &output, &output, &retVal, nullptr, this->GeneratorVerbose,
  355. cmDuration::zero());
  356. if (!res || retVal) {
  357. cmGeneratedFileStream ofs(tmpFile);
  358. ofs << "# Run command: " << nsisCmd << std::endl
  359. << "# Output:" << std::endl
  360. << output << std::endl;
  361. cmCPackLogger(cmCPackLog::LOG_ERROR,
  362. "Problem running NSIS command: " << nsisCmd << std::endl
  363. << "Please check "
  364. << tmpFile << " for errors"
  365. << std::endl);
  366. return 0;
  367. }
  368. return 1;
  369. }
  370. int cmCPackNSISGenerator::InitializeInternal()
  371. {
  372. if (cmIsOn(this->GetOption("CPACK_INCLUDE_TOPLEVEL_DIRECTORY"))) {
  373. cmCPackLogger(
  374. cmCPackLog::LOG_WARNING,
  375. "NSIS Generator cannot work with CPACK_INCLUDE_TOPLEVEL_DIRECTORY set. "
  376. "This option will be reset to 0 (for this generator only)."
  377. << std::endl);
  378. this->SetOption("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", nullptr);
  379. }
  380. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  381. "cmCPackNSISGenerator::Initialize()" << std::endl);
  382. std::vector<std::string> path;
  383. std::string nsisPath;
  384. bool gotRegValue = false;
  385. #ifdef _WIN32
  386. if (Nsis64) {
  387. if (!gotRegValue &&
  388. cmsys::SystemTools::ReadRegistryValue(
  389. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS\\Unicode", nsisPath,
  390. cmsys::SystemTools::KeyWOW64_64)) {
  391. gotRegValue = true;
  392. }
  393. if (!gotRegValue &&
  394. cmsys::SystemTools::ReadRegistryValue(
  395. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS", nsisPath,
  396. cmsys::SystemTools::KeyWOW64_64)) {
  397. gotRegValue = true;
  398. }
  399. }
  400. if (!gotRegValue &&
  401. cmsys::SystemTools::ReadRegistryValue(
  402. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS\\Unicode", nsisPath,
  403. cmsys::SystemTools::KeyWOW64_32)) {
  404. gotRegValue = true;
  405. }
  406. if (!gotRegValue &&
  407. cmsys::SystemTools::ReadRegistryValue(
  408. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS\\Unicode", nsisPath)) {
  409. gotRegValue = true;
  410. }
  411. if (!gotRegValue &&
  412. cmsys::SystemTools::ReadRegistryValue(
  413. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS", nsisPath,
  414. cmsys::SystemTools::KeyWOW64_32)) {
  415. gotRegValue = true;
  416. }
  417. if (!gotRegValue &&
  418. cmsys::SystemTools::ReadRegistryValue(
  419. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS", nsisPath)) {
  420. gotRegValue = true;
  421. }
  422. if (gotRegValue) {
  423. path.push_back(nsisPath);
  424. }
  425. #endif
  426. this->SetOptionIfNotSet("CPACK_NSIS_EXECUTABLE", "makensis");
  427. nsisPath = cmSystemTools::FindProgram(
  428. *this->GetOption("CPACK_NSIS_EXECUTABLE"), path, false);
  429. if (nsisPath.empty()) {
  430. cmCPackLogger(
  431. cmCPackLog::LOG_ERROR,
  432. "Cannot find NSIS compiler makensis: likely it is not installed, "
  433. "or not in your PATH"
  434. << std::endl);
  435. if (!gotRegValue) {
  436. cmCPackLogger(
  437. cmCPackLog::LOG_ERROR,
  438. "Could not read NSIS registry value. This is usually caused by "
  439. "NSIS not being installed. Please install NSIS from "
  440. "http://nsis.sourceforge.net"
  441. << std::endl);
  442. }
  443. return 0;
  444. }
  445. std::string nsisCmd = "\"" + nsisPath + "\" " NSIS_OPT "VERSION";
  446. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  447. "Test NSIS version: " << nsisCmd << std::endl);
  448. std::string output;
  449. int retVal = 1;
  450. bool resS = cmSystemTools::RunSingleCommand(
  451. nsisCmd, &output, &output, &retVal, nullptr, this->GeneratorVerbose,
  452. cmDuration::zero());
  453. cmsys::RegularExpression versionRex("v([0-9]+.[0-9]+)");
  454. cmsys::RegularExpression versionRexCVS("v(.*)\\.cvs");
  455. if (!resS || retVal ||
  456. (!versionRex.find(output) && !versionRexCVS.find(output))) {
  457. cmValue topDir = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  458. std::string tmpFile = cmStrCat(topDir ? *topDir : ".", "/NSISOutput.log");
  459. cmGeneratedFileStream ofs(tmpFile);
  460. ofs << "# Run command: " << nsisCmd << std::endl
  461. << "# Output:" << std::endl
  462. << output << std::endl;
  463. cmCPackLogger(cmCPackLog::LOG_ERROR,
  464. "Problem checking NSIS version with command: "
  465. << nsisCmd << std::endl
  466. << "Please check " << tmpFile << " for errors"
  467. << std::endl);
  468. return 0;
  469. }
  470. if (versionRex.find(output)) {
  471. double nsisVersion = atof(versionRex.match(1).c_str());
  472. double minNSISVersion = 3.03;
  473. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  474. "NSIS Version: " << nsisVersion << std::endl);
  475. if (nsisVersion < minNSISVersion) {
  476. cmCPackLogger(cmCPackLog::LOG_ERROR,
  477. "CPack requires NSIS Version 3.03 or greater. "
  478. "NSIS found on the system was: "
  479. << nsisVersion << std::endl);
  480. return 0;
  481. }
  482. }
  483. if (versionRexCVS.find(output)) {
  484. // No version check for NSIS cvs build
  485. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  486. "NSIS Version: CVS " << versionRexCVS.match(1) << std::endl);
  487. }
  488. this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM", nsisPath);
  489. this->SetOptionIfNotSet("CPACK_NSIS_EXECUTABLES_DIRECTORY", "bin");
  490. cmValue cpackPackageExecutables =
  491. this->GetOption("CPACK_PACKAGE_EXECUTABLES");
  492. cmValue cpackPackageDeskTopLinks =
  493. this->GetOption("CPACK_CREATE_DESKTOP_LINKS");
  494. cmValue cpackNsisExecutablesDirectory =
  495. this->GetOption("CPACK_NSIS_EXECUTABLES_DIRECTORY");
  496. std::vector<std::string> cpackPackageDesktopLinksVector;
  497. if (cpackPackageDeskTopLinks) {
  498. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  499. "CPACK_CREATE_DESKTOP_LINKS: " << cpackPackageDeskTopLinks
  500. << std::endl);
  501. cmExpandList(cpackPackageDeskTopLinks, cpackPackageDesktopLinksVector);
  502. for (std::string const& cpdl : cpackPackageDesktopLinksVector) {
  503. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  504. "CPACK_CREATE_DESKTOP_LINKS: " << cpdl << std::endl);
  505. }
  506. } else {
  507. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  508. "CPACK_CREATE_DESKTOP_LINKS: "
  509. << "not set" << std::endl);
  510. }
  511. std::ostringstream str;
  512. std::ostringstream deleteStr;
  513. if (cpackPackageExecutables) {
  514. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  515. "The cpackPackageExecutables: " << cpackPackageExecutables
  516. << "." << std::endl);
  517. cmList cpackPackageExecutablesList{ cpackPackageExecutables };
  518. if (cpackPackageExecutablesList.size() % 2 != 0) {
  519. cmCPackLogger(
  520. cmCPackLog::LOG_ERROR,
  521. "CPACK_PACKAGE_EXECUTABLES should contain pairs of <executable> and "
  522. "<icon name>."
  523. << std::endl);
  524. return 0;
  525. }
  526. cmList::iterator it;
  527. for (it = cpackPackageExecutablesList.begin();
  528. it != cpackPackageExecutablesList.end(); ++it) {
  529. std::string execName = *it;
  530. ++it;
  531. std::string linkName = *it;
  532. str << R"( CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\)" << linkName
  533. << R"(.lnk" "$INSTDIR\)" << cpackNsisExecutablesDirectory << "\\"
  534. << execName << ".exe\"" << std::endl;
  535. deleteStr << R"( Delete "$SMPROGRAMS\$MUI_TEMP\)" << linkName
  536. << ".lnk\"" << std::endl;
  537. // see if CPACK_CREATE_DESKTOP_LINK_ExeName is on
  538. // if so add a desktop link
  539. if (cm::contains(cpackPackageDesktopLinksVector, execName)) {
  540. str << " StrCmp \"$INSTALL_DESKTOP\" \"1\" 0 +2\n";
  541. str << " CreateShortCut \"$DESKTOP\\" << linkName
  542. << R"(.lnk" "$INSTDIR\)" << cpackNsisExecutablesDirectory << "\\"
  543. << execName << ".exe\"" << std::endl;
  544. deleteStr << " StrCmp \"$INSTALL_DESKTOP\" \"1\" 0 +2\n";
  545. deleteStr << " Delete \"$DESKTOP\\" << linkName << ".lnk\""
  546. << std::endl;
  547. }
  548. }
  549. }
  550. this->CreateMenuLinks(str, deleteStr);
  551. this->SetOptionIfNotSet("CPACK_NSIS_CREATE_ICONS", str.str());
  552. this->SetOptionIfNotSet("CPACK_NSIS_DELETE_ICONS", deleteStr.str());
  553. this->SetOptionIfNotSet("CPACK_NSIS_COMPRESSOR", "lzma");
  554. return this->Superclass::InitializeInternal();
  555. }
  556. void cmCPackNSISGenerator::CreateMenuLinks(std::ostream& str,
  557. std::ostream& deleteStr)
  558. {
  559. cmValue cpackMenuLinks = this->GetOption("CPACK_NSIS_MENU_LINKS");
  560. if (!cpackMenuLinks) {
  561. return;
  562. }
  563. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  564. "The cpackMenuLinks: " << cpackMenuLinks << "." << std::endl);
  565. cmList cpackMenuLinksList{ cpackMenuLinks };
  566. if (cpackMenuLinksList.size() % 2 != 0) {
  567. cmCPackLogger(
  568. cmCPackLog::LOG_ERROR,
  569. "CPACK_NSIS_MENU_LINKS should contain pairs of <shortcut target> and "
  570. "<shortcut label>."
  571. << std::endl);
  572. return;
  573. }
  574. static cmsys::RegularExpression urlRegex(
  575. "^(mailto:|(ftps?|https?|news)://).*$");
  576. cmList::iterator it;
  577. for (it = cpackMenuLinksList.begin(); it != cpackMenuLinksList.end(); ++it) {
  578. std::string sourceName = *it;
  579. const bool url = urlRegex.find(sourceName);
  580. // Convert / to \ in filenames, but not in urls:
  581. //
  582. if (!url) {
  583. std::replace(sourceName.begin(), sourceName.end(), '/', '\\');
  584. }
  585. ++it;
  586. std::string linkName = *it;
  587. if (!url) {
  588. str << R"( CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\)" << linkName
  589. << R"(.lnk" "$INSTDIR\)" << sourceName << "\"" << std::endl;
  590. deleteStr << R"( Delete "$SMPROGRAMS\$MUI_TEMP\)" << linkName
  591. << ".lnk\"" << std::endl;
  592. } else {
  593. str << R"( WriteINIStr "$SMPROGRAMS\$STARTMENU_FOLDER\)" << linkName
  594. << R"(.url" "InternetShortcut" "URL" ")" << sourceName << "\""
  595. << std::endl;
  596. deleteStr << R"( Delete "$SMPROGRAMS\$MUI_TEMP\)" << linkName
  597. << ".url\"" << std::endl;
  598. }
  599. // see if CPACK_CREATE_DESKTOP_LINK_ExeName is on
  600. // if so add a desktop link
  601. std::string desktop = cmStrCat("CPACK_CREATE_DESKTOP_LINK_", linkName);
  602. if (this->IsSet(desktop)) {
  603. str << " StrCmp \"$INSTALL_DESKTOP\" \"1\" 0 +2\n";
  604. str << " CreateShortCut \"$DESKTOP\\" << linkName
  605. << R"(.lnk" "$INSTDIR\)" << sourceName << "\"" << std::endl;
  606. deleteStr << " StrCmp \"$INSTALL_DESKTOP\" \"1\" 0 +2\n";
  607. deleteStr << " Delete \"$DESKTOP\\" << linkName << ".lnk\""
  608. << std::endl;
  609. }
  610. }
  611. }
  612. bool cmCPackNSISGenerator::GetListOfSubdirectories(
  613. const char* topdir, std::vector<std::string>& dirs)
  614. {
  615. cmsys::Directory dir;
  616. dir.Load(topdir);
  617. for (unsigned long i = 0; i < dir.GetNumberOfFiles(); ++i) {
  618. const char* fileName = dir.GetFile(i);
  619. if (strcmp(fileName, ".") != 0 && strcmp(fileName, "..") != 0) {
  620. std::string const fullPath =
  621. std::string(topdir).append("/").append(fileName);
  622. if (cmsys::SystemTools::FileIsDirectory(fullPath) &&
  623. !cmsys::SystemTools::FileIsSymlink(fullPath)) {
  624. if (!this->GetListOfSubdirectories(fullPath.c_str(), dirs)) {
  625. return false;
  626. }
  627. }
  628. }
  629. }
  630. dirs.emplace_back(topdir);
  631. return true;
  632. }
  633. enum cmCPackGenerator::CPackSetDestdirSupport
  634. cmCPackNSISGenerator::SupportsSetDestdir() const
  635. {
  636. return cmCPackGenerator::SETDESTDIR_SHOULD_NOT_BE_USED;
  637. }
  638. bool cmCPackNSISGenerator::SupportsAbsoluteDestination() const
  639. {
  640. return false;
  641. }
  642. bool cmCPackNSISGenerator::SupportsComponentInstallation() const
  643. {
  644. return true;
  645. }
  646. std::string cmCPackNSISGenerator::CreateComponentDescription(
  647. cmCPackComponent* component, std::ostream& macrosOut)
  648. {
  649. // Basic description of the component
  650. std::string componentCode = "Section ";
  651. if (component->IsDisabledByDefault) {
  652. componentCode += "/o ";
  653. }
  654. componentCode += "\"";
  655. if (component->IsHidden) {
  656. componentCode += "-";
  657. }
  658. componentCode += component->DisplayName + "\" " + component->Name + "\n";
  659. if (component->IsRequired) {
  660. componentCode += " SectionIn RO\n";
  661. } else if (!component->InstallationTypes.empty()) {
  662. std::ostringstream out;
  663. for (cmCPackInstallationType const* installType :
  664. component->InstallationTypes) {
  665. out << " " << installType->Index;
  666. }
  667. componentCode += " SectionIn" + out.str() + "\n";
  668. }
  669. const std::string componentOutputDir =
  670. this->CustomComponentInstallDirectory(component->Name);
  671. componentCode += cmStrCat(" SetOutPath \"", componentOutputDir, "\"\n");
  672. // Create the actual installation commands
  673. if (component->IsDownloaded) {
  674. if (component->ArchiveFile.empty()) {
  675. // Compute the name of the archive.
  676. std::string packagesDir =
  677. cmStrCat(this->GetOption("CPACK_TEMPORARY_DIRECTORY"), ".dummy");
  678. std::ostringstream out;
  679. out << cmSystemTools::GetFilenameWithoutLastExtension(packagesDir) << "-"
  680. << component->Name << ".zip";
  681. component->ArchiveFile = out.str();
  682. }
  683. // Create the directory for the upload area
  684. cmValue userUploadDirectory = this->GetOption("CPACK_UPLOAD_DIRECTORY");
  685. std::string uploadDirectory;
  686. if (cmNonempty(userUploadDirectory)) {
  687. uploadDirectory = *userUploadDirectory;
  688. } else {
  689. uploadDirectory =
  690. cmStrCat(this->GetOption("CPACK_PACKAGE_DIRECTORY"), "/CPackUploads");
  691. }
  692. if (!cmSystemTools::FileExists(uploadDirectory)) {
  693. if (!cmSystemTools::MakeDirectory(uploadDirectory)) {
  694. cmCPackLogger(cmCPackLog::LOG_ERROR,
  695. "Unable to create NSIS upload directory "
  696. << uploadDirectory << std::endl);
  697. return "";
  698. }
  699. }
  700. // Remove the old archive, if one exists
  701. std::string archiveFile = uploadDirectory + '/' + component->ArchiveFile;
  702. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  703. "- Building downloaded component archive: " << archiveFile
  704. << std::endl);
  705. if (cmSystemTools::FileExists(archiveFile, true)) {
  706. if (!cmSystemTools::RemoveFile(archiveFile)) {
  707. cmCPackLogger(cmCPackLog::LOG_ERROR,
  708. "Unable to remove archive file " << archiveFile
  709. << std::endl);
  710. return "";
  711. }
  712. }
  713. // Find a ZIP program
  714. if (!this->IsSet("ZIP_EXECUTABLE")) {
  715. this->ReadListFile("Internal/CPack/CPackZIP.cmake");
  716. if (!this->IsSet("ZIP_EXECUTABLE")) {
  717. cmCPackLogger(cmCPackLog::LOG_ERROR,
  718. "Unable to find ZIP program" << std::endl);
  719. return "";
  720. }
  721. }
  722. // The directory where this component's files reside
  723. std::string dirName = cmStrCat(
  724. this->GetOption("CPACK_TEMPORARY_DIRECTORY"), '/', component->Name, '/');
  725. // Build the list of files to go into this archive, and determine the
  726. // size of the installed component.
  727. std::string zipListFileName = cmStrCat(
  728. this->GetOption("CPACK_TEMPORARY_DIRECTORY"), "/winZip.filelist");
  729. bool needQuotesInFile = cmIsOn(this->GetOption("CPACK_ZIP_NEED_QUOTES"));
  730. unsigned long totalSize = 0;
  731. { // the scope is needed for cmGeneratedFileStream
  732. cmGeneratedFileStream out(zipListFileName);
  733. for (std::string const& file : component->Files) {
  734. if (needQuotesInFile) {
  735. out << "\"";
  736. }
  737. out << file;
  738. if (needQuotesInFile) {
  739. out << "\"";
  740. }
  741. out << std::endl;
  742. totalSize += cmSystemTools::FileLength(dirName + file);
  743. }
  744. }
  745. // Build the archive in the upload area
  746. std::string cmd = this->GetOption("CPACK_ZIP_COMMAND");
  747. cmsys::SystemTools::ReplaceString(cmd, "<ARCHIVE>", archiveFile.c_str());
  748. cmsys::SystemTools::ReplaceString(cmd, "<FILELIST>",
  749. zipListFileName.c_str());
  750. std::string output;
  751. int retVal = -1;
  752. int res = cmSystemTools::RunSingleCommand(
  753. cmd, &output, &output, &retVal, dirName.c_str(),
  754. cmSystemTools::OUTPUT_NONE, cmDuration::zero());
  755. if (!res || retVal) {
  756. std::string tmpFile = cmStrCat(
  757. this->GetOption("CPACK_TOPLEVEL_DIRECTORY"), "/CompressZip.log");
  758. cmGeneratedFileStream ofs(tmpFile);
  759. ofs << "# Run command: " << cmd << std::endl
  760. << "# Output:" << std::endl
  761. << output << std::endl;
  762. cmCPackLogger(cmCPackLog::LOG_ERROR,
  763. "Problem running zip command: " << cmd << std::endl
  764. << "Please check "
  765. << tmpFile << " for errors"
  766. << std::endl);
  767. return "";
  768. }
  769. // Create the NSIS code to download this file on-the-fly.
  770. unsigned long totalSizeInKbytes = (totalSize + 512) / 1024;
  771. if (totalSizeInKbytes == 0) {
  772. totalSizeInKbytes = 1;
  773. }
  774. std::ostringstream out;
  775. /* clang-format off */
  776. out << " AddSize " << totalSizeInKbytes << "\n"
  777. << " Push \"" << component->ArchiveFile << "\"\n"
  778. << " Call DownloadFile\n"
  779. << " ZipDLL::extractall \"$INSTDIR\\"
  780. << component->ArchiveFile << "\" \"$INSTDIR\"\n"
  781. << " Pop $2 ; error message\n"
  782. " StrCmp $2 \"success\" +2 0\n"
  783. " MessageBox MB_OK \"Failed to unzip $2\"\n"
  784. " Delete $INSTDIR\\$0\n";
  785. /* clang-format on */
  786. componentCode += out.str();
  787. } else {
  788. componentCode +=
  789. " File /r \"${INST_DIR}\\" + component->Name + "\\*.*\"\n";
  790. }
  791. componentCode += "SectionEnd\n";
  792. // Macro used to remove the component
  793. macrosOut << "!macro Remove_${" << component->Name << "}\n";
  794. macrosOut << " IntCmp $" << component->Name << "_was_installed 0 noremove_"
  795. << component->Name << "\n";
  796. std::string path;
  797. for (std::string const& pathIt : component->Files) {
  798. path = pathIt;
  799. std::replace(path.begin(), path.end(), '/', '\\');
  800. macrosOut << " Delete \"" << componentOutputDir << "\\" << path << "\"\n";
  801. }
  802. for (std::string const& pathIt : component->Directories) {
  803. path = pathIt;
  804. std::replace(path.begin(), path.end(), '/', '\\');
  805. macrosOut << " RMDir \"" << componentOutputDir << "\\" << path << "\"\n";
  806. }
  807. macrosOut << " noremove_" << component->Name << ":\n";
  808. macrosOut << "!macroend\n";
  809. // Macro used to select each of the components that this component
  810. // depends on.
  811. std::set<cmCPackComponent*> visited;
  812. macrosOut << "!macro Select_" << component->Name << "_depends\n";
  813. macrosOut << this->CreateSelectionDependenciesDescription(component,
  814. visited);
  815. macrosOut << "!macroend\n";
  816. // Macro used to deselect each of the components that depend on this
  817. // component.
  818. visited.clear();
  819. macrosOut << "!macro Deselect_required_by_" << component->Name << "\n";
  820. macrosOut << this->CreateDeselectionDependenciesDescription(component,
  821. visited);
  822. macrosOut << "!macroend\n";
  823. return componentCode;
  824. }
  825. std::string cmCPackNSISGenerator::CreateSelectionDependenciesDescription(
  826. cmCPackComponent* component, std::set<cmCPackComponent*>& visited)
  827. {
  828. // Don't visit a component twice
  829. if (visited.count(component)) {
  830. return {};
  831. }
  832. visited.insert(component);
  833. std::ostringstream out;
  834. for (cmCPackComponent* depend : component->Dependencies) {
  835. // Write NSIS code to select this dependency
  836. out << " SectionGetFlags ${" << depend->Name << "} $0\n";
  837. out << " IntOp $0 $0 | ${SF_SELECTED}\n";
  838. out << " SectionSetFlags ${" << depend->Name << "} $0\n";
  839. out << " IntOp $" << depend->Name << "_selected 0 + ${SF_SELECTED}\n";
  840. // Recurse
  841. out
  842. << this->CreateSelectionDependenciesDescription(depend, visited).c_str();
  843. }
  844. return out.str();
  845. }
  846. std::string cmCPackNSISGenerator::CreateDeselectionDependenciesDescription(
  847. cmCPackComponent* component, std::set<cmCPackComponent*>& visited)
  848. {
  849. // Don't visit a component twice
  850. if (visited.count(component)) {
  851. return {};
  852. }
  853. visited.insert(component);
  854. std::ostringstream out;
  855. for (cmCPackComponent* depend : component->ReverseDependencies) {
  856. // Write NSIS code to deselect this dependency
  857. out << " SectionGetFlags ${" << depend->Name << "} $0\n";
  858. out << " IntOp $1 ${SF_SELECTED} ~\n";
  859. out << " IntOp $0 $0 & $1\n";
  860. out << " SectionSetFlags ${" << depend->Name << "} $0\n";
  861. out << " IntOp $" << depend->Name << "_selected 0 + 0\n";
  862. // Recurse
  863. out << this->CreateDeselectionDependenciesDescription(depend, visited)
  864. .c_str();
  865. }
  866. return out.str();
  867. }
  868. std::string cmCPackNSISGenerator::CreateComponentGroupDescription(
  869. cmCPackComponentGroup* group, std::ostream& macrosOut)
  870. {
  871. if (group->Components.empty() && group->Subgroups.empty()) {
  872. // Silently skip empty groups. NSIS doesn't support them.
  873. return {};
  874. }
  875. std::string code = "SectionGroup ";
  876. if (group->IsExpandedByDefault) {
  877. code += "/e ";
  878. }
  879. if (group->IsBold) {
  880. code += "\"!" + group->DisplayName + "\" " + group->Name + "\n";
  881. } else {
  882. code += "\"" + group->DisplayName + "\" " + group->Name + "\n";
  883. }
  884. for (cmCPackComponentGroup* g : group->Subgroups) {
  885. code += this->CreateComponentGroupDescription(g, macrosOut);
  886. }
  887. for (cmCPackComponent* comp : group->Components) {
  888. if (comp->Files.empty()) {
  889. continue;
  890. }
  891. code += this->CreateComponentDescription(comp, macrosOut);
  892. }
  893. code += "SectionGroupEnd\n";
  894. return code;
  895. }
  896. std::string cmCPackNSISGenerator::CustomComponentInstallDirectory(
  897. cm::string_view componentName)
  898. {
  899. cmValue outputDir = this->GetOption(
  900. cmStrCat("CPACK_NSIS_", componentName, "_INSTALL_DIRECTORY"));
  901. return outputDir ? *outputDir : "$INSTDIR";
  902. }
  903. std::string cmCPackNSISGenerator::TranslateNewlines(std::string str)
  904. {
  905. cmSystemTools::ReplaceString(str, "\n", "$\\r$\\n");
  906. return str;
  907. }