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