cmCPackNSISGenerator.cxx 34 KB

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