cmCPackNSISGenerator.cxx 33 KB

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