cmCPackNSISGenerator.cxx 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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 "cmsys/Directory.hxx"
  10. #include "cmsys/RegularExpression.hxx"
  11. #include <algorithm>
  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. for (unsigned long i = 0; i < dir.GetNumberOfFiles(); ++i) {
  548. const char* fileName = dir.GetFile(i);
  549. if (strcmp(fileName, ".") != 0 && strcmp(fileName, "..") != 0) {
  550. std::string const fullPath =
  551. std::string(topdir).append("/").append(fileName);
  552. if (cmsys::SystemTools::FileIsDirectory(fullPath) &&
  553. !cmsys::SystemTools::FileIsSymlink(fullPath)) {
  554. if (!this->GetListOfSubdirectories(fullPath.c_str(), dirs)) {
  555. return false;
  556. }
  557. }
  558. }
  559. }
  560. dirs.push_back(topdir);
  561. return true;
  562. }
  563. enum cmCPackGenerator::CPackSetDestdirSupport
  564. cmCPackNSISGenerator::SupportsSetDestdir() const
  565. {
  566. return cmCPackGenerator::SETDESTDIR_SHOULD_NOT_BE_USED;
  567. }
  568. bool cmCPackNSISGenerator::SupportsAbsoluteDestination() const
  569. {
  570. return false;
  571. }
  572. bool cmCPackNSISGenerator::SupportsComponentInstallation() const
  573. {
  574. return true;
  575. }
  576. std::string cmCPackNSISGenerator::CreateComponentDescription(
  577. cmCPackComponent* component, std::ostream& macrosOut)
  578. {
  579. // Basic description of the component
  580. std::string componentCode = "Section ";
  581. if (component->IsDisabledByDefault) {
  582. componentCode += "/o ";
  583. }
  584. componentCode += "\"";
  585. if (component->IsHidden) {
  586. componentCode += "-";
  587. }
  588. componentCode += component->DisplayName + "\" " + component->Name + "\n";
  589. if (component->IsRequired) {
  590. componentCode += " SectionIn RO\n";
  591. } else if (!component->InstallationTypes.empty()) {
  592. std::ostringstream out;
  593. std::vector<cmCPackInstallationType*>::iterator installTypeIter;
  594. for (installTypeIter = component->InstallationTypes.begin();
  595. installTypeIter != component->InstallationTypes.end();
  596. ++installTypeIter) {
  597. out << " " << (*installTypeIter)->Index;
  598. }
  599. componentCode += " SectionIn" + out.str() + "\n";
  600. }
  601. const std::string componentOutputDir =
  602. CustomComponentInstallDirectory(component->Name);
  603. componentCode += " SetOutPath \"" + componentOutputDir + "\"\n";
  604. // Create the actual installation commands
  605. if (component->IsDownloaded) {
  606. if (component->ArchiveFile.empty()) {
  607. // Compute the name of the archive.
  608. std::string packagesDir = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  609. packagesDir += ".dummy";
  610. std::ostringstream out;
  611. out << cmSystemTools::GetFilenameWithoutLastExtension(packagesDir) << "-"
  612. << component->Name << ".zip";
  613. component->ArchiveFile = out.str();
  614. }
  615. // Create the directory for the upload area
  616. const char* userUploadDirectory =
  617. this->GetOption("CPACK_UPLOAD_DIRECTORY");
  618. std::string uploadDirectory;
  619. if (userUploadDirectory && *userUploadDirectory) {
  620. uploadDirectory = userUploadDirectory;
  621. } else {
  622. uploadDirectory = this->GetOption("CPACK_PACKAGE_DIRECTORY");
  623. uploadDirectory += "/CPackUploads";
  624. }
  625. if (!cmSystemTools::FileExists(uploadDirectory.c_str())) {
  626. if (!cmSystemTools::MakeDirectory(uploadDirectory.c_str())) {
  627. cmCPackLogger(cmCPackLog::LOG_ERROR,
  628. "Unable to create NSIS upload directory "
  629. << uploadDirectory << std::endl);
  630. return "";
  631. }
  632. }
  633. // Remove the old archive, if one exists
  634. std::string archiveFile = uploadDirectory + '/' + component->ArchiveFile;
  635. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  636. "- Building downloaded component archive: " << archiveFile
  637. << std::endl);
  638. if (cmSystemTools::FileExists(archiveFile.c_str(), true)) {
  639. if (!cmSystemTools::RemoveFile(archiveFile)) {
  640. cmCPackLogger(cmCPackLog::LOG_ERROR, "Unable to remove archive file "
  641. << archiveFile << std::endl);
  642. return "";
  643. }
  644. }
  645. // Find a ZIP program
  646. if (!this->IsSet("ZIP_EXECUTABLE")) {
  647. this->ReadListFile("CPackZIP.cmake");
  648. if (!this->IsSet("ZIP_EXECUTABLE")) {
  649. cmCPackLogger(cmCPackLog::LOG_ERROR, "Unable to find ZIP program"
  650. << std::endl);
  651. return "";
  652. }
  653. }
  654. // The directory where this component's files reside
  655. std::string dirName = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  656. dirName += '/';
  657. dirName += component->Name;
  658. dirName += '/';
  659. // Build the list of files to go into this archive, and determine the
  660. // size of the installed component.
  661. std::string zipListFileName = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  662. zipListFileName += "/winZip.filelist";
  663. bool needQuotesInFile =
  664. cmSystemTools::IsOn(this->GetOption("CPACK_ZIP_NEED_QUOTES"));
  665. unsigned long totalSize = 0;
  666. { // the scope is needed for cmGeneratedFileStream
  667. cmGeneratedFileStream out(zipListFileName.c_str());
  668. std::vector<std::string>::iterator fileIt;
  669. for (fileIt = component->Files.begin(); fileIt != component->Files.end();
  670. ++fileIt) {
  671. if (needQuotesInFile) {
  672. out << "\"";
  673. }
  674. out << *fileIt;
  675. if (needQuotesInFile) {
  676. out << "\"";
  677. }
  678. out << std::endl;
  679. totalSize += cmSystemTools::FileLength(dirName + *fileIt);
  680. }
  681. }
  682. // Build the archive in the upload area
  683. std::string cmd = this->GetOption("CPACK_ZIP_COMMAND");
  684. cmsys::SystemTools::ReplaceString(cmd, "<ARCHIVE>", archiveFile.c_str());
  685. cmsys::SystemTools::ReplaceString(cmd, "<FILELIST>",
  686. zipListFileName.c_str());
  687. std::string output;
  688. int retVal = -1;
  689. int res = cmSystemTools::RunSingleCommand(cmd.c_str(), &output, &output,
  690. &retVal, dirName.c_str(),
  691. cmSystemTools::OUTPUT_NONE, 0);
  692. if (!res || retVal) {
  693. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  694. tmpFile += "/CompressZip.log";
  695. cmGeneratedFileStream ofs(tmpFile.c_str());
  696. ofs << "# Run command: " << cmd << std::endl
  697. << "# Output:" << std::endl
  698. << output << std::endl;
  699. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running zip command: "
  700. << cmd << std::endl
  701. << "Please check " << tmpFile << " for errors"
  702. << std::endl);
  703. return "";
  704. }
  705. // Create the NSIS code to download this file on-the-fly.
  706. unsigned long totalSizeInKbytes = (totalSize + 512) / 1024;
  707. if (totalSizeInKbytes == 0) {
  708. totalSizeInKbytes = 1;
  709. }
  710. std::ostringstream out;
  711. /* clang-format off */
  712. out << " AddSize " << totalSizeInKbytes << "\n"
  713. << " Push \"" << component->ArchiveFile << "\"\n"
  714. << " Call DownloadFile\n"
  715. << " ZipDLL::extractall \"$INSTDIR\\"
  716. << component->ArchiveFile << "\" \"$INSTDIR\"\n"
  717. << " Pop $2 ; error message\n"
  718. " StrCmp $2 \"success\" +2 0\n"
  719. " MessageBox MB_OK \"Failed to unzip $2\"\n"
  720. " Delete $INSTDIR\\$0\n";
  721. /* clang-format on */
  722. componentCode += out.str();
  723. } else {
  724. componentCode +=
  725. " File /r \"${INST_DIR}\\" + component->Name + "\\*.*\"\n";
  726. }
  727. componentCode += "SectionEnd\n";
  728. // Macro used to remove the component
  729. macrosOut << "!macro Remove_${" << component->Name << "}\n";
  730. macrosOut << " IntCmp $" << component->Name << "_was_installed 0 noremove_"
  731. << component->Name << "\n";
  732. std::vector<std::string>::iterator pathIt;
  733. std::string path;
  734. for (pathIt = component->Files.begin(); pathIt != component->Files.end();
  735. ++pathIt) {
  736. path = *pathIt;
  737. std::replace(path.begin(), path.end(), '/', '\\');
  738. macrosOut << " Delete \"" << componentOutputDir << "\\" << path << "\"\n";
  739. }
  740. for (pathIt = component->Directories.begin();
  741. pathIt != component->Directories.end(); ++pathIt) {
  742. path = *pathIt;
  743. std::replace(path.begin(), path.end(), '/', '\\');
  744. macrosOut << " RMDir \"" << componentOutputDir << "\\" << path << "\"\n";
  745. }
  746. macrosOut << " noremove_" << component->Name << ":\n";
  747. macrosOut << "!macroend\n";
  748. // Macro used to select each of the components that this component
  749. // depends on.
  750. std::set<cmCPackComponent*> visited;
  751. macrosOut << "!macro Select_" << component->Name << "_depends\n";
  752. macrosOut << CreateSelectionDependenciesDescription(component, visited);
  753. macrosOut << "!macroend\n";
  754. // Macro used to deselect each of the components that depend on this
  755. // component.
  756. visited.clear();
  757. macrosOut << "!macro Deselect_required_by_" << component->Name << "\n";
  758. macrosOut << CreateDeselectionDependenciesDescription(component, visited);
  759. macrosOut << "!macroend\n";
  760. return componentCode;
  761. }
  762. std::string cmCPackNSISGenerator::CreateSelectionDependenciesDescription(
  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. std::vector<cmCPackComponent*>::iterator dependIt;
  772. for (dependIt = component->Dependencies.begin();
  773. dependIt != component->Dependencies.end(); ++dependIt) {
  774. // Write NSIS code to select this dependency
  775. out << " SectionGetFlags ${" << (*dependIt)->Name << "} $0\n";
  776. out << " IntOp $0 $0 | ${SF_SELECTED}\n";
  777. out << " SectionSetFlags ${" << (*dependIt)->Name << "} $0\n";
  778. out << " IntOp $" << (*dependIt)->Name
  779. << "_selected 0 + ${SF_SELECTED}\n";
  780. // Recurse
  781. out << CreateSelectionDependenciesDescription(*dependIt, visited).c_str();
  782. }
  783. return out.str();
  784. }
  785. std::string cmCPackNSISGenerator::CreateDeselectionDependenciesDescription(
  786. cmCPackComponent* component, std::set<cmCPackComponent*>& visited)
  787. {
  788. // Don't visit a component twice
  789. if (visited.count(component)) {
  790. return std::string();
  791. }
  792. visited.insert(component);
  793. std::ostringstream out;
  794. std::vector<cmCPackComponent*>::iterator dependIt;
  795. for (dependIt = component->ReverseDependencies.begin();
  796. dependIt != component->ReverseDependencies.end(); ++dependIt) {
  797. // Write NSIS code to deselect this dependency
  798. out << " SectionGetFlags ${" << (*dependIt)->Name << "} $0\n";
  799. out << " IntOp $1 ${SF_SELECTED} ~\n";
  800. out << " IntOp $0 $0 & $1\n";
  801. out << " SectionSetFlags ${" << (*dependIt)->Name << "} $0\n";
  802. out << " IntOp $" << (*dependIt)->Name << "_selected 0 + 0\n";
  803. // Recurse
  804. out
  805. << CreateDeselectionDependenciesDescription(*dependIt, visited).c_str();
  806. }
  807. return out.str();
  808. }
  809. std::string cmCPackNSISGenerator::CreateComponentGroupDescription(
  810. cmCPackComponentGroup* group, std::ostream& macrosOut)
  811. {
  812. if (group->Components.empty() && group->Subgroups.empty()) {
  813. // Silently skip empty groups. NSIS doesn't support them.
  814. return std::string();
  815. }
  816. std::string code = "SectionGroup ";
  817. if (group->IsExpandedByDefault) {
  818. code += "/e ";
  819. }
  820. if (group->IsBold) {
  821. code += "\"!" + group->DisplayName + "\" " + group->Name + "\n";
  822. } else {
  823. code += "\"" + group->DisplayName + "\" " + group->Name + "\n";
  824. }
  825. std::vector<cmCPackComponentGroup*>::iterator groupIt;
  826. for (groupIt = group->Subgroups.begin(); groupIt != group->Subgroups.end();
  827. ++groupIt) {
  828. code += this->CreateComponentGroupDescription(*groupIt, macrosOut);
  829. }
  830. std::vector<cmCPackComponent*>::iterator comp;
  831. for (comp = group->Components.begin(); comp != group->Components.end();
  832. ++comp) {
  833. if ((*comp)->Files.empty()) {
  834. continue;
  835. }
  836. code += this->CreateComponentDescription(*comp, macrosOut);
  837. }
  838. code += "SectionGroupEnd\n";
  839. return code;
  840. }
  841. std::string cmCPackNSISGenerator::CustomComponentInstallDirectory(
  842. const std::string& componentName)
  843. {
  844. const char* outputDir =
  845. this->GetOption("CPACK_NSIS_" + componentName + "_INSTALL_DIRECTORY");
  846. const std::string componentOutputDir = (outputDir ? outputDir : "$INSTDIR");
  847. return componentOutputDir;
  848. }
  849. std::string cmCPackNSISGenerator::TranslateNewlines(std::string str)
  850. {
  851. cmSystemTools::ReplaceString(str, "\n", "$\\r$\\n");
  852. return str;
  853. }