cmCPackNSISGenerator.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCPackNSISGenerator.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmSystemTools.h"
  17. #include "cmMakefile.h"
  18. #include "cmGeneratedFileStream.h"
  19. #include "cmCPackLog.h"
  20. #include "cmCPackComponentGroup.h"
  21. #include <cmsys/SystemTools.hxx>
  22. #include <cmsys/Glob.hxx>
  23. #include <cmsys/Directory.hxx>
  24. #include <cmsys/RegularExpression.hxx>
  25. /* NSIS uses different command line syntax on Windows and others */
  26. #ifdef _WIN32
  27. # define NSIS_OPT "/"
  28. #else
  29. # define NSIS_OPT "-"
  30. #endif
  31. //----------------------------------------------------------------------
  32. cmCPackNSISGenerator::cmCPackNSISGenerator()
  33. {
  34. }
  35. //----------------------------------------------------------------------
  36. cmCPackNSISGenerator::~cmCPackNSISGenerator()
  37. {
  38. }
  39. //----------------------------------------------------------------------
  40. int cmCPackNSISGenerator::CompressFiles(const char* outFileName,
  41. const char* toplevel, const std::vector<std::string>& files)
  42. {
  43. (void)outFileName; // TODO: Fix nsis to force out file name
  44. (void)toplevel;
  45. std::string nsisInFileName = this->FindTemplate("NSIS.template.in");
  46. if ( nsisInFileName.size() == 0 )
  47. {
  48. cmCPackLogger(cmCPackLog::LOG_ERROR,
  49. "CPack error: Could not find NSIS installer template file."
  50. << std::endl);
  51. return false;
  52. }
  53. std::string nsisInInstallOptions
  54. = this->FindTemplate("NSIS.InstallOptions.ini.in");
  55. if ( nsisInInstallOptions.size() == 0 )
  56. {
  57. cmCPackLogger(cmCPackLog::LOG_ERROR,
  58. "CPack error: Could not find NSIS installer options file."
  59. << std::endl);
  60. return false;
  61. }
  62. std::string nsisFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  63. std::string tmpFile = nsisFileName;
  64. tmpFile += "/NSISOutput.log";
  65. std::string nsisInstallOptions = nsisFileName + "/NSIS.InstallOptions.ini";
  66. nsisFileName += "/project.nsi";
  67. cmOStringStream str;
  68. std::vector<std::string>::const_iterator it;
  69. for ( it = files.begin(); it != files.end(); ++ it )
  70. {
  71. std::string fileN = cmSystemTools::RelativePath(toplevel,
  72. it->c_str());
  73. if (!this->Components.empty())
  74. {
  75. // Strip off the component part of the path.
  76. fileN = fileN.substr(fileN.find('/')+1, std::string::npos);
  77. }
  78. cmSystemTools::ReplaceString(fileN, "/", "\\");
  79. str << " Delete \"$INSTDIR\\" << fileN.c_str() << "\"" << std::endl;
  80. }
  81. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Uninstall Files: "
  82. << str.str().c_str() << std::endl);
  83. this->SetOptionIfNotSet("CPACK_NSIS_DELETE_FILES", str.str().c_str());
  84. std::vector<std::string> dirs;
  85. this->GetListOfSubdirectories(toplevel, dirs);
  86. std::vector<std::string>::const_iterator sit;
  87. cmOStringStream dstr;
  88. for ( sit = dirs.begin(); sit != dirs.end(); ++ sit )
  89. {
  90. std::string componentName;
  91. std::string fileN = cmSystemTools::RelativePath(toplevel, sit->c_str());
  92. if ( fileN.empty() )
  93. {
  94. continue;
  95. }
  96. if (!Components.empty())
  97. {
  98. // If this is a component installation, strip off the component
  99. // part of the path.
  100. std::string::size_type slash = fileN.find('/');
  101. if (slash != std::string::npos)
  102. {
  103. // If this is a component installation, determine which component it is.
  104. componentName = fileN.substr(0, slash);
  105. // Strip off the component part of the path.
  106. fileN = fileN.substr(slash+1, std::string::npos);
  107. }
  108. }
  109. cmSystemTools::ReplaceString(fileN, "/", "\\");
  110. dstr << " RMDir \"$INSTDIR\\" << fileN.c_str() << "\"" << std::endl;
  111. if (!componentName.empty())
  112. {
  113. this->Components[componentName].Directories.push_back(fileN);
  114. }
  115. }
  116. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Uninstall Dirs: "
  117. << dstr.str().c_str() << std::endl);
  118. this->SetOptionIfNotSet("CPACK_NSIS_DELETE_DIRECTORIES",
  119. dstr.str().c_str());
  120. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: " << nsisInFileName
  121. << " to " << nsisFileName << std::endl);
  122. if(this->IsSet("CPACK_NSIS_MUI_ICON")
  123. && this->IsSet("CPACK_NSIS_MUI_UNIICON"))
  124. {
  125. std::string installerIconCode="!define MUI_ICON \"";
  126. installerIconCode += this->GetOption("CPACK_NSIS_MUI_ICON");
  127. installerIconCode += "\"\n";
  128. installerIconCode += "!define MUI_UNICON \"";
  129. installerIconCode += this->GetOption("CPACK_NSIS_MUI_UNIICON");
  130. installerIconCode += "\"\n";
  131. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_MUI_ICON_CODE",
  132. installerIconCode.c_str());
  133. }
  134. if(this->IsSet("CPACK_PACKAGE_ICON"))
  135. {
  136. std::string installerIconCode = "!define MUI_HEADERIMAGE_BITMAP \"";
  137. installerIconCode += this->GetOption("CPACK_PACKAGE_ICON");
  138. installerIconCode += "\"\n";
  139. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_ICON_CODE",
  140. installerIconCode.c_str());
  141. }
  142. // Setup all of the component sections
  143. if (this->Components.empty())
  144. {
  145. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLATION_TYPES", "");
  146. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_MUI_COMPONENTS_DESC", "");
  147. this->SetOptionIfNotSet("CPACK_NSIS_PAGE_COMPONENTS", "");
  148. this->SetOptionIfNotSet("CPACK_NSIS_FULL_INSTALL",
  149. "File /r \"${INST_DIR}\\*.*\"");
  150. this->SetOptionIfNotSet("CPACK_NSIS_COMPONENT_SECTIONS", "");
  151. this->SetOptionIfNotSet("CPACK_NSIS_COMPONENT_SECTION_LIST", "");
  152. this->SetOptionIfNotSet("CPACK_NSIS_SECTION_SELECTED_VARS", "");
  153. }
  154. else
  155. {
  156. std::string componentCode;
  157. std::string sectionList;
  158. std::string selectedVarsList;
  159. std::string componentDescriptions;
  160. std::string groupDescriptions;
  161. std::string installTypesCode;
  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. componentCode += this->CreateComponentGroupDescription(&groupIt->second);
  189. // Add the group description, if any.
  190. if (!groupIt->second.Description.empty())
  191. {
  192. groupDescriptions += " !insertmacro MUI_DESCRIPTION_TEXT ${"
  193. + groupIt->first + "} \""
  194. + this->TranslateNewlines(groupIt->second.Description) + "\"\n";
  195. }
  196. }
  197. // Create the remaining components, which aren't associated with groups.
  198. std::map<std::string, cmCPackComponent>::iterator compIt;
  199. for (compIt = this->Components.begin();
  200. compIt != this->Components.end();
  201. ++compIt)
  202. {
  203. if (!compIt->second.Group)
  204. {
  205. componentCode += this->CreateComponentDescription(&compIt->second);
  206. }
  207. // Add this component to the various section lists.
  208. sectionList += " !insertmacro \"${MacroName}\" \"";
  209. sectionList += compIt->first;
  210. sectionList += "\"\n";
  211. selectedVarsList += "Var " + compIt->first + "_selected\n";
  212. // Add the component description, if any.
  213. if (!compIt->second.Description.empty())
  214. {
  215. componentDescriptions += " !insertmacro MUI_DESCRIPTION_TEXT ${"
  216. + compIt->first + "} \""
  217. + this->TranslateNewlines(compIt->second.Description) + "\"\n";
  218. }
  219. }
  220. if (componentDescriptions.empty() && groupDescriptions.empty())
  221. {
  222. // Turn off the "Description" box
  223. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_MUI_COMPONENTS_DESC",
  224. "!define MUI_COMPONENTSPAGE_NODESC");
  225. }
  226. else
  227. {
  228. componentDescriptions =
  229. "!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN\n"
  230. + componentDescriptions
  231. + groupDescriptions
  232. + "!insertmacro MUI_FUNCTION_DESCRIPTION_END\n";
  233. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_MUI_COMPONENTS_DESC",
  234. componentDescriptions.c_str());
  235. }
  236. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLATION_TYPES", installTypesCode.c_str());
  237. this->SetOptionIfNotSet("CPACK_NSIS_PAGE_COMPONENTS", "!insertmacro MUI_PAGE_COMPONENTS");
  238. this->SetOptionIfNotSet("CPACK_NSIS_FULL_INSTALL", "");
  239. this->SetOptionIfNotSet("CPACK_NSIS_COMPONENT_SECTIONS", componentCode.c_str());
  240. this->SetOptionIfNotSet("CPACK_NSIS_COMPONENT_SECTION_LIST", sectionList.c_str());
  241. this->SetOptionIfNotSet("CPACK_NSIS_SECTION_SELECTED_VARS", selectedVarsList.c_str());
  242. }
  243. this->ConfigureFile(nsisInInstallOptions.c_str(),
  244. nsisInstallOptions.c_str());
  245. this->ConfigureFile(nsisInFileName.c_str(), nsisFileName.c_str());
  246. std::string nsisCmd = "\"";
  247. nsisCmd += this->GetOption("CPACK_INSTALLER_PROGRAM");
  248. nsisCmd += "\" \"" + nsisFileName + "\"";
  249. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Execute: " << nsisCmd.c_str()
  250. << std::endl);
  251. std::string output;
  252. int retVal = 1;
  253. bool res = cmSystemTools::RunSingleCommand(nsisCmd.c_str(), &output,
  254. &retVal, 0, this->GeneratorVerbose, 0);
  255. if ( !res || retVal )
  256. {
  257. cmGeneratedFileStream ofs(tmpFile.c_str());
  258. ofs << "# Run command: " << nsisCmd.c_str() << std::endl
  259. << "# Output:" << std::endl
  260. << output.c_str() << std::endl;
  261. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running NSIS command: "
  262. << nsisCmd.c_str() << std::endl
  263. << "Please check " << tmpFile.c_str() << " for errors" << std::endl);
  264. return 0;
  265. }
  266. return 1;
  267. }
  268. //----------------------------------------------------------------------
  269. int cmCPackNSISGenerator::InitializeInternal()
  270. {
  271. if ( cmSystemTools::IsOn(this->GetOption(
  272. "CPACK_INCLUDE_TOPLEVEL_DIRECTORY")) )
  273. {
  274. cmCPackLogger(cmCPackLog::LOG_ERROR,
  275. "NSIS Generator cannot work with CPACK_INCLUDE_TOPLEVEL_DIRECTORY. "
  276. "This option will be ignored."
  277. << std::endl);
  278. this->SetOption("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", 0);
  279. }
  280. cmCPackLogger(cmCPackLog::LOG_DEBUG, "cmCPackNSISGenerator::Initialize()"
  281. << std::endl);
  282. std::vector<std::string> path;
  283. std::string nsisPath;
  284. #ifdef _WIN32
  285. if ( !cmsys::SystemTools::ReadRegistryValue(
  286. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS", nsisPath,
  287. cmsys::SystemTools::KeyWOW64_32) )
  288. {
  289. cmCPackLogger
  290. (cmCPackLog::LOG_ERROR,
  291. "Cannot find NSIS registry value. This is usually caused by NSIS "
  292. "not being installed. Please install NSIS from "
  293. "http://nsis.sourceforge.net"
  294. << std::endl);
  295. return 0;
  296. }
  297. path.push_back(nsisPath);
  298. #endif
  299. nsisPath = cmSystemTools::FindProgram("makensis", path, false);
  300. if ( nsisPath.empty() )
  301. {
  302. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find NSIS compiler"
  303. << std::endl);
  304. return 0;
  305. }
  306. std::string nsisCmd = "\"" + nsisPath + "\" " NSIS_OPT "VERSION";
  307. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Test NSIS version: "
  308. << nsisCmd.c_str() << std::endl);
  309. std::string output;
  310. int retVal = 1;
  311. bool resS = cmSystemTools::RunSingleCommand(nsisCmd.c_str(),
  312. &output, &retVal, 0, this->GeneratorVerbose, 0);
  313. cmsys::RegularExpression versionRex("v([0-9]+.[0-9]+)");
  314. if ( !resS || retVal || !versionRex.find(output))
  315. {
  316. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  317. tmpFile += "/NSISOutput.log";
  318. cmGeneratedFileStream ofs(tmpFile.c_str());
  319. ofs << "# Run command: " << nsisCmd.c_str() << std::endl
  320. << "# Output:" << std::endl
  321. << output.c_str() << std::endl;
  322. cmCPackLogger(cmCPackLog::LOG_ERROR,
  323. "Problem checking NSIS version with command: "
  324. << nsisCmd.c_str() << std::endl
  325. << "Please check " << tmpFile.c_str() << " for errors" << std::endl);
  326. return 0;
  327. }
  328. double nsisVersion = atof(versionRex.match(1).c_str());
  329. double minNSISVersion = 2.09;
  330. cmCPackLogger(cmCPackLog::LOG_DEBUG, "NSIS Version: "
  331. << nsisVersion << std::endl);
  332. if ( nsisVersion < minNSISVersion )
  333. {
  334. cmCPackLogger(cmCPackLog::LOG_ERROR,
  335. "CPack requires NSIS Version 2.09 or greater. NSIS found on the system "
  336. "was: "
  337. << nsisVersion << std::endl);
  338. return 0;
  339. }
  340. this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM", nsisPath.c_str());
  341. const char* cpackPackageExecutables
  342. = this->GetOption("CPACK_PACKAGE_EXECUTABLES");
  343. const char* cpackPackageDeskTopLinks
  344. = this->GetOption("CPACK_CREATE_DESKTOP_LINKS");
  345. std::vector<std::string> cpackPackageDesktopLinksVector;
  346. if(cpackPackageDeskTopLinks)
  347. {
  348. cmCPackLogger(cmCPackLog::LOG_DEBUG, "CPACK_CREATE_DESKTOP_LINKS: "
  349. << cpackPackageDeskTopLinks << std::endl);
  350. cmSystemTools::
  351. ExpandListArgument(cpackPackageDeskTopLinks,
  352. cpackPackageDesktopLinksVector);
  353. for(std::vector<std::string>::iterator i =
  354. cpackPackageDesktopLinksVector.begin(); i !=
  355. cpackPackageDesktopLinksVector.end(); ++i)
  356. {
  357. cmCPackLogger(cmCPackLog::LOG_DEBUG, "CPACK_CREATE_DESKTOP_LINKS: "
  358. << *i << std::endl);
  359. }
  360. }
  361. else
  362. {
  363. cmCPackLogger(cmCPackLog::LOG_DEBUG, "CPACK_CREATE_DESKTOP_LINKS: "
  364. << "not set" << std::endl);
  365. }
  366. if ( cpackPackageExecutables )
  367. {
  368. cmCPackLogger(cmCPackLog::LOG_DEBUG, "The cpackPackageExecutables: "
  369. << cpackPackageExecutables << "." << std::endl);
  370. cmOStringStream str;
  371. cmOStringStream deleteStr;
  372. std::vector<std::string> cpackPackageExecutablesVector;
  373. cmSystemTools::ExpandListArgument(cpackPackageExecutables,
  374. cpackPackageExecutablesVector);
  375. if ( cpackPackageExecutablesVector.size() % 2 != 0 )
  376. {
  377. cmCPackLogger(cmCPackLog::LOG_ERROR,
  378. "CPACK_PACKAGE_EXECUTABLES should contain pairs of <executable> and "
  379. "<icon name>." << std::endl);
  380. return 0;
  381. }
  382. std::vector<std::string>::iterator it;
  383. for ( it = cpackPackageExecutablesVector.begin();
  384. it != cpackPackageExecutablesVector.end();
  385. ++it )
  386. {
  387. std::string execName = *it;
  388. ++ it;
  389. std::string linkName = *it;
  390. str << " CreateShortCut \"$SMPROGRAMS\\$STARTMENU_FOLDER\\"
  391. << linkName << ".lnk\" \"$INSTDIR\\bin\\" << execName << ".exe\""
  392. << std::endl;
  393. deleteStr << " Delete \"$SMPROGRAMS\\$MUI_TEMP\\" << linkName
  394. << ".lnk\"" << std::endl;
  395. // see if CPACK_CREATE_DESKTOP_LINK_ExeName is on
  396. // if so add a desktop link
  397. if(cpackPackageDesktopLinksVector.size() &&
  398. std::find(cpackPackageDesktopLinksVector.begin(),
  399. cpackPackageDesktopLinksVector.end(),
  400. execName)
  401. != cpackPackageDesktopLinksVector.end())
  402. {
  403. str << " StrCmp \"$INSTALL_DESKTOP\" \"1\" 0 +2\n";
  404. str << " CreateShortCut \"$DESKTOP\\"
  405. << linkName << ".lnk\" \"$INSTDIR\\bin\\" << execName << ".exe\""
  406. << std::endl;
  407. deleteStr << " StrCmp \"$INSTALL_DESKTOP\" \"1\" 0 +2\n";
  408. deleteStr << " Delete \"$DESKTOP\\" << linkName
  409. << ".lnk\"" << std::endl;
  410. }
  411. }
  412. this->CreateMenuLinks(str, deleteStr);
  413. this->SetOptionIfNotSet("CPACK_NSIS_CREATE_ICONS", str.str().c_str());
  414. this->SetOptionIfNotSet("CPACK_NSIS_DELETE_ICONS",
  415. deleteStr.str().c_str());
  416. }
  417. this->SetOptionIfNotSet("CPACK_NSIS_COMPRESSOR", "lzma");
  418. return this->Superclass::InitializeInternal();
  419. }
  420. //----------------------------------------------------------------------
  421. void cmCPackNSISGenerator::CreateMenuLinks( cmOStringStream& str,
  422. cmOStringStream& deleteStr)
  423. {
  424. const char* cpackMenuLinks
  425. = this->GetOption("CPACK_NSIS_MENU_LINKS");
  426. if(!cpackMenuLinks)
  427. {
  428. return;
  429. }
  430. cmCPackLogger(cmCPackLog::LOG_DEBUG, "The cpackMenuLinks: "
  431. << cpackMenuLinks << "." << std::endl);
  432. std::vector<std::string> cpackMenuLinksVector;
  433. cmSystemTools::ExpandListArgument(cpackMenuLinks,
  434. cpackMenuLinksVector);
  435. if ( cpackMenuLinksVector.size() % 2 != 0 )
  436. {
  437. cmCPackLogger(
  438. cmCPackLog::LOG_ERROR,
  439. "CPACK_PACKAGE_EXECUTABLES should contain pairs of <executable> and "
  440. "<icon name>." << std::endl);
  441. return;
  442. }
  443. std::vector<std::string>::iterator it;
  444. for ( it = cpackMenuLinksVector.begin();
  445. it != cpackMenuLinksVector.end();
  446. ++it )
  447. {
  448. std::string sourceName = *it;
  449. bool url = false;
  450. if(sourceName.find("http:") == 0)
  451. {
  452. url = true;
  453. }
  454. /* convert / to \\ */
  455. if(!url)
  456. {
  457. cmSystemTools::ReplaceString(sourceName, "/", "\\");
  458. }
  459. ++ it;
  460. std::string linkName = *it;
  461. if(!url)
  462. {
  463. str << " CreateShortCut \"$SMPROGRAMS\\$STARTMENU_FOLDER\\"
  464. << linkName << ".lnk\" \"$INSTDIR\\" << sourceName << "\""
  465. << std::endl;
  466. deleteStr << " Delete \"$SMPROGRAMS\\$MUI_TEMP\\" << linkName
  467. << ".lnk\"" << std::endl;
  468. }
  469. else
  470. {
  471. str << " WriteINIStr \"$SMPROGRAMS\\$STARTMENU_FOLDER\\"
  472. << linkName << ".url\" \"InternetShortcut\" \"URL\" \""
  473. << sourceName << "\""
  474. << std::endl;
  475. deleteStr << " Delete \"$SMPROGRAMS\\$MUI_TEMP\\" << linkName
  476. << ".url\"" << std::endl;
  477. }
  478. // see if CPACK_CREATE_DESKTOP_LINK_ExeName is on
  479. // if so add a desktop link
  480. std::string desktop = "CPACK_CREATE_DESKTOP_LINK_";
  481. desktop += linkName;
  482. if(this->IsSet(desktop.c_str()))
  483. {
  484. str << " StrCmp \"$INSTALL_DESKTOP\" \"1\" 0 +2\n";
  485. str << " CreateShortCut \"$DESKTOP\\"
  486. << linkName << ".lnk\" \"$INSTDIR\\" << sourceName << "\""
  487. << std::endl;
  488. deleteStr << " StrCmp \"$INSTALL_DESKTOP\" \"1\" 0 +2\n";
  489. deleteStr << " Delete \"$DESKTOP\\" << linkName
  490. << ".lnk\"" << std::endl;
  491. }
  492. }
  493. }
  494. //----------------------------------------------------------------------
  495. bool cmCPackNSISGenerator::GetListOfSubdirectories(const char* topdir,
  496. std::vector<std::string>& dirs)
  497. {
  498. cmsys::Directory dir;
  499. dir.Load(topdir);
  500. size_t fileNum;
  501. for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum)
  502. {
  503. if (strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".") &&
  504. strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".."))
  505. {
  506. cmsys_stl::string fullPath = topdir;
  507. fullPath += "/";
  508. fullPath += dir.GetFile(static_cast<unsigned long>(fileNum));
  509. if(cmsys::SystemTools::FileIsDirectory(fullPath.c_str()) &&
  510. !cmsys::SystemTools::FileIsSymlink(fullPath.c_str()))
  511. {
  512. if (!this->GetListOfSubdirectories(fullPath.c_str(), dirs))
  513. {
  514. return false;
  515. }
  516. }
  517. }
  518. }
  519. dirs.push_back(topdir);
  520. return true;
  521. }
  522. //----------------------------------------------------------------------
  523. bool cmCPackNSISGenerator::SupportsComponentInstallation() const
  524. {
  525. return true;
  526. }
  527. //----------------------------------------------------------------------
  528. std::string
  529. cmCPackNSISGenerator::
  530. CreateComponentDescription(cmCPackComponent *component) const
  531. {
  532. // Basic description of the component
  533. std::string componentCode = "Section ";
  534. if (component->IsDisabledByDefault)
  535. {
  536. componentCode += "/o ";
  537. }
  538. componentCode += "\"";
  539. if (component->IsHidden)
  540. {
  541. componentCode += "-";
  542. }
  543. componentCode += component->DisplayName + "\" " + component->Name + "\n";
  544. if (component->IsRequired)
  545. {
  546. componentCode += " SectionIn RO\n";
  547. }
  548. else if (!component->InstallationTypes.empty())
  549. {
  550. cmOStringStream out;
  551. std::vector<cmCPackInstallationType *>::iterator installTypeIter;
  552. for (installTypeIter = component->InstallationTypes.begin();
  553. installTypeIter != component->InstallationTypes.end();
  554. ++installTypeIter)
  555. {
  556. out << " " << (*installTypeIter)->Index;
  557. }
  558. componentCode += " SectionIn" + out.str() + "\n";
  559. }
  560. componentCode += " SetOutPath \"$INSTDIR\"\n";
  561. componentCode += " File /r \"${INST_DIR}\\" + component->Name + "\\*.*\"\n";
  562. componentCode += "SectionEnd\n";
  563. // Macro used to remove the component
  564. componentCode += "!macro Remove_${" + component->Name + "}\n";
  565. std::vector<std::string>::iterator pathIt;
  566. for (pathIt = component->Files.begin();
  567. pathIt != component->Files.end();
  568. ++pathIt)
  569. {
  570. componentCode += " Delete \"$INSTDIR\\" + *pathIt + "\"\n";
  571. }
  572. for (pathIt = component->Directories.begin();
  573. pathIt != component->Directories.end();
  574. ++pathIt)
  575. {
  576. componentCode += " RMDir \"$INSTDIR\\" + *pathIt + "\"\n";
  577. }
  578. componentCode += "!macroend\n";
  579. // Macro used to select each of the components that this component
  580. // depends on.
  581. std::set<cmCPackComponent *> visited;
  582. componentCode += "!macro Select_" + component->Name + "_depends\n";
  583. componentCode += CreateSelectionDependenciesDescription(component, visited);
  584. componentCode += "!macroend\n";
  585. // Macro used to deselect each of the components that depend on this
  586. // component.
  587. visited.clear();
  588. componentCode += "!macro Deselect_required_by_" + component->Name + "\n";
  589. componentCode += CreateDeselectionDependenciesDescription(component, visited);
  590. componentCode += "!macroend\n";
  591. return componentCode;
  592. }
  593. //----------------------------------------------------------------------
  594. std::string cmCPackNSISGenerator::CreateSelectionDependenciesDescription
  595. (cmCPackComponent *component,
  596. std::set<cmCPackComponent *>& visited) const
  597. {
  598. // Don't visit a component twice
  599. if (visited.count(component))
  600. {
  601. return std::string();
  602. }
  603. visited.insert(component);
  604. cmOStringStream out;
  605. std::vector<cmCPackComponent *>::iterator dependIt;
  606. for (dependIt = component->Dependencies.begin();
  607. dependIt != component->Dependencies.end();
  608. ++dependIt)
  609. {
  610. // Write NSIS code to select this dependency
  611. out << " SectionGetFlags ${" << (*dependIt)->Name << "} $0\n";
  612. out << " IntOp $0 $0 | ${SF_SELECTED}\n";
  613. out << " SectionSetFlags ${" << (*dependIt)->Name << "} $0\n";
  614. out << " IntOp $" << (*dependIt)->Name << "_selected 0 + ${SF_SELECTED}\n";
  615. // Recurse
  616. out << CreateSelectionDependenciesDescription(*dependIt, visited).c_str();
  617. }
  618. return out.str();
  619. }
  620. //----------------------------------------------------------------------
  621. std::string cmCPackNSISGenerator::CreateDeselectionDependenciesDescription
  622. (cmCPackComponent *component,
  623. std::set<cmCPackComponent *>& visited) const
  624. {
  625. // Don't visit a component twice
  626. if (visited.count(component))
  627. {
  628. return std::string();
  629. }
  630. visited.insert(component);
  631. cmOStringStream out;
  632. std::vector<cmCPackComponent *>::iterator dependIt;
  633. for (dependIt = component->ReverseDependencies.begin();
  634. dependIt != component->ReverseDependencies.end();
  635. ++dependIt)
  636. {
  637. // Write NSIS code to deselect this dependency
  638. out << " SectionGetFlags ${" << (*dependIt)->Name << "} $0\n";
  639. out << " IntOp $1 ${SF_SELECTED} ~\n";
  640. out << " IntOp $0 $0 & $1\n";
  641. out << " SectionSetFlags ${" << (*dependIt)->Name << "} $0\n";
  642. out << " IntOp $" << (*dependIt)->Name << "_selected 0 + 0\n";
  643. // Recurse
  644. out << CreateDeselectionDependenciesDescription(*dependIt, visited).c_str();
  645. }
  646. return out.str();
  647. }
  648. //----------------------------------------------------------------------
  649. std::string
  650. cmCPackNSISGenerator::
  651. CreateComponentGroupDescription(cmCPackComponentGroup *group) const
  652. {
  653. if (group->Components.empty())
  654. {
  655. // Silently skip empty groups. NSIS doesn't support them.
  656. return std::string();
  657. }
  658. std::string code = "SectionGroup ";
  659. if (group->IsExpandedByDefault)
  660. {
  661. code += "/e ";
  662. }
  663. if (group->IsBold)
  664. {
  665. code += "\"!" + group->DisplayName + "\" " + group->Name + "\n";
  666. }
  667. else
  668. {
  669. code += "\"" + group->DisplayName + "\" " + group->Name + "\n";
  670. }
  671. std::vector<cmCPackComponent*>::iterator comp;
  672. for (comp = group->Components.begin();
  673. comp != group->Components.end();
  674. ++comp)
  675. {
  676. code += this->CreateComponentDescription(*comp);
  677. }
  678. code += "SectionGroupEnd\n";
  679. return code;
  680. }
  681. std::string cmCPackNSISGenerator::TranslateNewlines(std::string str)
  682. {
  683. cmSystemTools::ReplaceString(str, "\n", "$\\r$\\n");
  684. return str;
  685. }