cmCPackNSISGenerator.cxx 32 KB

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