cmCPackNSISGenerator.cxx 36 KB

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