cmCPackNSISGenerator.cxx 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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. cmList expandedArguments{ nsisArguments };
  222. for (auto& arg : expandedArguments) {
  223. if (!cmHasPrefix(arg, NSIS_OPT)) {
  224. nsisPreArguments = cmStrCat(nsisPreArguments, NSIS_OPT);
  225. }
  226. nsisPreArguments = cmStrCat(nsisPreArguments, arg, ' ');
  227. }
  228. }
  229. std::string nsisPostArguments;
  230. if (cmValue nsisArguments =
  231. this->GetOption("CPACK_NSIS_EXECUTABLE_POST_ARGUMENTS")) {
  232. cmList expandedArguments{ nsisArguments };
  233. for (auto& arg : expandedArguments) {
  234. if (!cmHasPrefix(arg, NSIS_OPT)) {
  235. nsisPostArguments = cmStrCat(nsisPostArguments, NSIS_OPT);
  236. }
  237. nsisPostArguments = cmStrCat(nsisPostArguments, arg, ' ');
  238. }
  239. }
  240. // Setup all of the component sections
  241. if (this->Components.empty()) {
  242. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLATION_TYPES", "");
  243. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_MUI_COMPONENTS_DESC", "");
  244. this->SetOptionIfNotSet("CPACK_NSIS_PAGE_COMPONENTS", "");
  245. this->SetOptionIfNotSet("CPACK_NSIS_FULL_INSTALL",
  246. R"(File /r "${INST_DIR}\*.*")");
  247. this->SetOptionIfNotSet("CPACK_NSIS_COMPONENT_SECTIONS", "");
  248. this->SetOptionIfNotSet("CPACK_NSIS_COMPONENT_SECTION_LIST", "");
  249. this->SetOptionIfNotSet("CPACK_NSIS_SECTION_SELECTED_VARS", "");
  250. } else {
  251. std::string componentCode;
  252. std::string sectionList;
  253. std::string selectedVarsList;
  254. std::string componentDescriptions;
  255. std::string groupDescriptions;
  256. std::string installTypesCode;
  257. std::string defines;
  258. std::ostringstream macrosOut;
  259. bool anyDownloadedComponents = false;
  260. // Create installation types. The order is significant, so we first fill
  261. // in a vector based on the indices, and print them in that order.
  262. std::vector<cmCPackInstallationType*> installTypes(
  263. this->InstallationTypes.size());
  264. for (auto& installType : this->InstallationTypes) {
  265. installTypes[installType.second.Index - 1] = &installType.second;
  266. }
  267. for (cmCPackInstallationType* installType : installTypes) {
  268. installTypesCode += "InstType \"";
  269. installTypesCode += installType->DisplayName;
  270. installTypesCode += "\"\n";
  271. }
  272. // Create installation groups first
  273. for (auto& group : this->ComponentGroups) {
  274. if (group.second.ParentGroup == nullptr) {
  275. componentCode +=
  276. this->CreateComponentGroupDescription(&group.second, macrosOut);
  277. }
  278. // Add the group description, if any.
  279. if (!group.second.Description.empty()) {
  280. groupDescriptions += " !insertmacro MUI_DESCRIPTION_TEXT ${" +
  281. group.first + "} \"" +
  282. cmCPackNSISGenerator::TranslateNewlines(group.second.Description) +
  283. "\"\n";
  284. }
  285. }
  286. // Create the remaining components, which aren't associated with groups.
  287. for (auto& comp : this->Components) {
  288. if (comp.second.Files.empty()) {
  289. // NSIS cannot cope with components that have no files.
  290. continue;
  291. }
  292. anyDownloadedComponents =
  293. anyDownloadedComponents || comp.second.IsDownloaded;
  294. if (!comp.second.Group) {
  295. componentCode +=
  296. this->CreateComponentDescription(&comp.second, macrosOut);
  297. }
  298. // Add this component to the various section lists.
  299. sectionList += R"( !insertmacro "${MacroName}" ")";
  300. sectionList += comp.first;
  301. sectionList += "\"\n";
  302. selectedVarsList += "Var " + comp.first + "_selected\n";
  303. selectedVarsList += "Var " + comp.first + "_was_installed\n";
  304. // Add the component description, if any.
  305. if (!comp.second.Description.empty()) {
  306. componentDescriptions += " !insertmacro MUI_DESCRIPTION_TEXT ${" +
  307. comp.first + "} \"" +
  308. cmCPackNSISGenerator::TranslateNewlines(comp.second.Description) +
  309. "\"\n";
  310. }
  311. }
  312. componentCode += macrosOut.str();
  313. if (componentDescriptions.empty() && groupDescriptions.empty()) {
  314. // Turn off the "Description" box
  315. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_MUI_COMPONENTS_DESC",
  316. "!define MUI_COMPONENTSPAGE_NODESC");
  317. } else {
  318. componentDescriptions = "!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN\n" +
  319. componentDescriptions + groupDescriptions +
  320. "!insertmacro MUI_FUNCTION_DESCRIPTION_END\n";
  321. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_MUI_COMPONENTS_DESC",
  322. componentDescriptions);
  323. }
  324. if (anyDownloadedComponents) {
  325. defines += "!define CPACK_USES_DOWNLOAD\n";
  326. if (cmIsOn(this->GetOption("CPACK_ADD_REMOVE"))) {
  327. defines += "!define CPACK_NSIS_ADD_REMOVE\n";
  328. }
  329. }
  330. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLATION_TYPES", installTypesCode);
  331. this->SetOptionIfNotSet("CPACK_NSIS_PAGE_COMPONENTS",
  332. "!insertmacro MUI_PAGE_COMPONENTS");
  333. this->SetOptionIfNotSet("CPACK_NSIS_FULL_INSTALL", "");
  334. this->SetOptionIfNotSet("CPACK_NSIS_COMPONENT_SECTIONS", componentCode);
  335. this->SetOptionIfNotSet("CPACK_NSIS_COMPONENT_SECTION_LIST", sectionList);
  336. this->SetOptionIfNotSet("CPACK_NSIS_SECTION_SELECTED_VARS",
  337. selectedVarsList);
  338. this->SetOption("CPACK_NSIS_DEFINES", defines);
  339. }
  340. this->ConfigureFile(nsisInInstallOptions, nsisInstallOptions);
  341. this->ConfigureFile(nsisInFileName, nsisFileName);
  342. std::string nsisCmd =
  343. cmStrCat('"', this->GetOption("CPACK_INSTALLER_PROGRAM"), "\" ",
  344. nsisPreArguments, " \"", nsisFileName, '"');
  345. if (!nsisPostArguments.empty()) {
  346. nsisCmd = cmStrCat(nsisCmd, " ", nsisPostArguments);
  347. }
  348. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Execute: " << nsisCmd << std::endl);
  349. std::string output;
  350. int retVal = 1;
  351. bool res = cmSystemTools::RunSingleCommand(
  352. nsisCmd, &output, &output, &retVal, nullptr, this->GeneratorVerbose,
  353. cmDuration::zero());
  354. if (!res || retVal) {
  355. cmGeneratedFileStream ofs(tmpFile);
  356. ofs << "# Run command: " << nsisCmd << std::endl
  357. << "# Output:" << std::endl
  358. << output << std::endl;
  359. cmCPackLogger(cmCPackLog::LOG_ERROR,
  360. "Problem running NSIS command: " << nsisCmd << std::endl
  361. << "Please check "
  362. << tmpFile << " for errors"
  363. << std::endl);
  364. return 0;
  365. }
  366. return 1;
  367. }
  368. int cmCPackNSISGenerator::InitializeInternal()
  369. {
  370. if (cmIsOn(this->GetOption("CPACK_INCLUDE_TOPLEVEL_DIRECTORY"))) {
  371. cmCPackLogger(
  372. cmCPackLog::LOG_WARNING,
  373. "NSIS Generator cannot work with CPACK_INCLUDE_TOPLEVEL_DIRECTORY set. "
  374. "This option will be reset to 0 (for this generator only)."
  375. << std::endl);
  376. this->SetOption("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", nullptr);
  377. }
  378. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  379. "cmCPackNSISGenerator::Initialize()" << std::endl);
  380. std::vector<std::string> path;
  381. std::string nsisPath;
  382. bool gotRegValue = false;
  383. #ifdef _WIN32
  384. if (Nsis64) {
  385. if (!gotRegValue &&
  386. cmsys::SystemTools::ReadRegistryValue(
  387. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS\\Unicode", nsisPath,
  388. cmsys::SystemTools::KeyWOW64_64)) {
  389. gotRegValue = true;
  390. }
  391. if (!gotRegValue &&
  392. cmsys::SystemTools::ReadRegistryValue(
  393. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS", nsisPath,
  394. cmsys::SystemTools::KeyWOW64_64)) {
  395. gotRegValue = true;
  396. }
  397. }
  398. if (!gotRegValue &&
  399. cmsys::SystemTools::ReadRegistryValue(
  400. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS\\Unicode", nsisPath,
  401. cmsys::SystemTools::KeyWOW64_32)) {
  402. gotRegValue = true;
  403. }
  404. if (!gotRegValue &&
  405. cmsys::SystemTools::ReadRegistryValue(
  406. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS\\Unicode", nsisPath)) {
  407. gotRegValue = true;
  408. }
  409. if (!gotRegValue &&
  410. cmsys::SystemTools::ReadRegistryValue(
  411. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS", nsisPath,
  412. cmsys::SystemTools::KeyWOW64_32)) {
  413. gotRegValue = true;
  414. }
  415. if (!gotRegValue &&
  416. cmsys::SystemTools::ReadRegistryValue(
  417. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS", nsisPath)) {
  418. gotRegValue = true;
  419. }
  420. if (gotRegValue) {
  421. path.push_back(nsisPath);
  422. }
  423. #endif
  424. this->SetOptionIfNotSet("CPACK_NSIS_EXECUTABLE", "makensis");
  425. nsisPath = cmSystemTools::FindProgram(
  426. *this->GetOption("CPACK_NSIS_EXECUTABLE"), path, false);
  427. if (nsisPath.empty()) {
  428. cmCPackLogger(
  429. cmCPackLog::LOG_ERROR,
  430. "Cannot find NSIS compiler makensis: likely it is not installed, "
  431. "or not in your PATH"
  432. << std::endl);
  433. if (!gotRegValue) {
  434. cmCPackLogger(
  435. cmCPackLog::LOG_ERROR,
  436. "Could not read NSIS registry value. This is usually caused by "
  437. "NSIS not being installed. Please install NSIS from "
  438. "http://nsis.sourceforge.net"
  439. << std::endl);
  440. }
  441. return 0;
  442. }
  443. std::string nsisCmd = "\"" + nsisPath + "\" " NSIS_OPT "VERSION";
  444. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  445. "Test NSIS version: " << nsisCmd << std::endl);
  446. std::string output;
  447. int retVal = 1;
  448. bool resS = cmSystemTools::RunSingleCommand(
  449. nsisCmd, &output, &output, &retVal, nullptr, this->GeneratorVerbose,
  450. cmDuration::zero());
  451. cmsys::RegularExpression versionRex("v([0-9]+.[0-9]+)");
  452. cmsys::RegularExpression versionRexCVS("v(.*)\\.cvs");
  453. if (!resS || retVal ||
  454. (!versionRex.find(output) && !versionRexCVS.find(output))) {
  455. cmValue topDir = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  456. std::string tmpFile = cmStrCat(topDir ? *topDir : ".", "/NSISOutput.log");
  457. cmGeneratedFileStream ofs(tmpFile);
  458. ofs << "# Run command: " << nsisCmd << std::endl
  459. << "# Output:" << std::endl
  460. << output << std::endl;
  461. cmCPackLogger(cmCPackLog::LOG_ERROR,
  462. "Problem checking NSIS version with command: "
  463. << nsisCmd << std::endl
  464. << "Please check " << tmpFile << " for errors"
  465. << std::endl);
  466. return 0;
  467. }
  468. if (versionRex.find(output)) {
  469. double nsisVersion = atof(versionRex.match(1).c_str());
  470. double minNSISVersion = 3.03;
  471. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  472. "NSIS Version: " << nsisVersion << std::endl);
  473. if (nsisVersion < minNSISVersion) {
  474. cmCPackLogger(cmCPackLog::LOG_ERROR,
  475. "CPack requires NSIS Version 3.03 or greater. "
  476. "NSIS found on the system was: "
  477. << nsisVersion << std::endl);
  478. return 0;
  479. }
  480. }
  481. if (versionRexCVS.find(output)) {
  482. // No version check for NSIS cvs build
  483. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  484. "NSIS Version: CVS " << versionRexCVS.match(1) << std::endl);
  485. }
  486. this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM", nsisPath);
  487. this->SetOptionIfNotSet("CPACK_NSIS_EXECUTABLES_DIRECTORY", "bin");
  488. cmValue cpackPackageExecutables =
  489. this->GetOption("CPACK_PACKAGE_EXECUTABLES");
  490. cmValue cpackPackageDeskTopLinks =
  491. this->GetOption("CPACK_CREATE_DESKTOP_LINKS");
  492. cmValue cpackNsisExecutablesDirectory =
  493. this->GetOption("CPACK_NSIS_EXECUTABLES_DIRECTORY");
  494. cmList cpackPackageDesktopLinksList;
  495. if (cpackPackageDeskTopLinks) {
  496. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  497. "CPACK_CREATE_DESKTOP_LINKS: " << cpackPackageDeskTopLinks
  498. << std::endl);
  499. cpackPackageDesktopLinksList.assign(cpackPackageDeskTopLinks);
  500. for (std::string const& cpdl : cpackPackageDesktopLinksList) {
  501. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  502. "CPACK_CREATE_DESKTOP_LINKS: " << cpdl << std::endl);
  503. }
  504. } else {
  505. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  506. "CPACK_CREATE_DESKTOP_LINKS: "
  507. << "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. const bool 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. const char* 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. const char* 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. const std::string 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 = cmIsOn(this->GetOption("CPACK_ZIP_NEED_QUOTES"));
  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 +=
  787. " File /r \"${INST_DIR}\\" + 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. }