cmCPackNSISGenerator.cxx 34 KB

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