cmCPackNSISGenerator.cxx 33 KB

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