cmCPackNSISGenerator.cxx 34 KB

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