cmCPackNSISGenerator.cxx 34 KB

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