cmCPackNSISGenerator.cxx 33 KB

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