cmCPackNSISGenerator.cxx 32 KB

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