cmCPackNSISGenerator.cxx 36 KB

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