cmCPackNSISGenerator.cxx 35 KB

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