cmCPackNSISGenerator.cxx 36 KB

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