cmCPackNSISGenerator.cxx 33 KB

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