cmCPackNSISGenerator.cxx 33 KB

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