cmCPackNSISGenerator.cxx 35 KB

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