cmCPackNSISGenerator.cxx 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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. std::replace(fileN.begin(), fileN.end(), '/', '\\');
  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. std::replace(fileN.begin(), fileN.end(), '/', '\\');
  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 == CM_NULLPTR) {
  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 =
  275. cmSystemTools::RunSingleCommand(nsisCmd.c_str(), &output, &output, &retVal,
  276. CM_NULLPTR, this->GeneratorVerbose, 0);
  277. if (!res || retVal) {
  278. cmGeneratedFileStream ofs(tmpFile.c_str());
  279. ofs << "# Run command: " << nsisCmd << std::endl
  280. << "# Output:" << std::endl
  281. << output << std::endl;
  282. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running NSIS command: "
  283. << nsisCmd << std::endl
  284. << "Please check " << tmpFile << " for errors"
  285. << std::endl);
  286. return 0;
  287. }
  288. return 1;
  289. }
  290. int cmCPackNSISGenerator::InitializeInternal()
  291. {
  292. if (cmSystemTools::IsOn(
  293. this->GetOption("CPACK_INCLUDE_TOPLEVEL_DIRECTORY"))) {
  294. cmCPackLogger(
  295. cmCPackLog::LOG_WARNING,
  296. "NSIS Generator cannot work with CPACK_INCLUDE_TOPLEVEL_DIRECTORY set. "
  297. "This option will be reset to 0 (for this generator only)."
  298. << std::endl);
  299. this->SetOption("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", CM_NULLPTR);
  300. }
  301. cmCPackLogger(cmCPackLog::LOG_DEBUG, "cmCPackNSISGenerator::Initialize()"
  302. << std::endl);
  303. std::vector<std::string> path;
  304. std::string nsisPath;
  305. bool gotRegValue = false;
  306. #ifdef _WIN32
  307. if (Nsis64) {
  308. if (!gotRegValue && cmsys::SystemTools::ReadRegistryValue(
  309. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS\\Unicode",
  310. nsisPath, cmsys::SystemTools::KeyWOW64_64)) {
  311. gotRegValue = true;
  312. }
  313. if (!gotRegValue && cmsys::SystemTools::ReadRegistryValue(
  314. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS", nsisPath,
  315. cmsys::SystemTools::KeyWOW64_64)) {
  316. gotRegValue = true;
  317. }
  318. }
  319. if (!gotRegValue && cmsys::SystemTools::ReadRegistryValue(
  320. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS\\Unicode",
  321. nsisPath, cmsys::SystemTools::KeyWOW64_32)) {
  322. gotRegValue = true;
  323. }
  324. if (!gotRegValue &&
  325. cmsys::SystemTools::ReadRegistryValue(
  326. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS\\Unicode", nsisPath)) {
  327. gotRegValue = true;
  328. }
  329. if (!gotRegValue && cmsys::SystemTools::ReadRegistryValue(
  330. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS", nsisPath,
  331. cmsys::SystemTools::KeyWOW64_32)) {
  332. gotRegValue = true;
  333. }
  334. if (!gotRegValue && cmsys::SystemTools::ReadRegistryValue(
  335. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS", nsisPath)) {
  336. gotRegValue = true;
  337. }
  338. if (gotRegValue) {
  339. path.push_back(nsisPath);
  340. }
  341. #endif
  342. nsisPath = cmSystemTools::FindProgram("makensis", path, false);
  343. if (nsisPath.empty()) {
  344. cmCPackLogger(
  345. cmCPackLog::LOG_ERROR,
  346. "Cannot find NSIS compiler makensis: likely it is not installed, "
  347. "or not in your PATH"
  348. << std::endl);
  349. if (!gotRegValue) {
  350. cmCPackLogger(
  351. cmCPackLog::LOG_ERROR,
  352. "Could not read NSIS registry value. This is usually caused by "
  353. "NSIS not being installed. Please install NSIS from "
  354. "http://nsis.sourceforge.net"
  355. << std::endl);
  356. }
  357. return 0;
  358. }
  359. std::string nsisCmd = "\"" + nsisPath + "\" " NSIS_OPT "VERSION";
  360. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Test NSIS version: " << nsisCmd
  361. << std::endl);
  362. std::string output;
  363. int retVal = 1;
  364. bool resS =
  365. cmSystemTools::RunSingleCommand(nsisCmd.c_str(), &output, &output, &retVal,
  366. CM_NULLPTR, this->GeneratorVerbose, 0);
  367. cmsys::RegularExpression versionRex("v([0-9]+.[0-9]+)");
  368. cmsys::RegularExpression versionRexCVS("v(.*)\\.cvs");
  369. if (!resS || retVal ||
  370. (!versionRex.find(output) && !versionRexCVS.find(output))) {
  371. const char* topDir = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  372. std::string tmpFile = topDir ? topDir : ".";
  373. tmpFile += "/NSISOutput.log";
  374. cmGeneratedFileStream ofs(tmpFile.c_str());
  375. ofs << "# Run command: " << nsisCmd << std::endl
  376. << "# Output:" << std::endl
  377. << output << std::endl;
  378. cmCPackLogger(
  379. cmCPackLog::LOG_ERROR, "Problem checking NSIS version with command: "
  380. << nsisCmd << std::endl
  381. << "Please check " << tmpFile << " for errors" << std::endl);
  382. return 0;
  383. }
  384. if (versionRex.find(output)) {
  385. double nsisVersion = atof(versionRex.match(1).c_str());
  386. double minNSISVersion = 2.09;
  387. cmCPackLogger(cmCPackLog::LOG_DEBUG, "NSIS Version: " << nsisVersion
  388. << std::endl);
  389. if (nsisVersion < minNSISVersion) {
  390. cmCPackLogger(cmCPackLog::LOG_ERROR,
  391. "CPack requires NSIS Version 2.09 or greater. "
  392. "NSIS found on the system was: "
  393. << nsisVersion << std::endl);
  394. return 0;
  395. }
  396. }
  397. if (versionRexCVS.find(output)) {
  398. // No version check for NSIS cvs build
  399. cmCPackLogger(cmCPackLog::LOG_DEBUG, "NSIS Version: CVS "
  400. << versionRexCVS.match(1) << std::endl);
  401. }
  402. this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM", nsisPath.c_str());
  403. this->SetOptionIfNotSet("CPACK_NSIS_EXECUTABLES_DIRECTORY", "bin");
  404. const char* cpackPackageExecutables =
  405. this->GetOption("CPACK_PACKAGE_EXECUTABLES");
  406. const char* cpackPackageDeskTopLinks =
  407. this->GetOption("CPACK_CREATE_DESKTOP_LINKS");
  408. const char* cpackNsisExecutablesDirectory =
  409. this->GetOption("CPACK_NSIS_EXECUTABLES_DIRECTORY");
  410. std::vector<std::string> cpackPackageDesktopLinksVector;
  411. if (cpackPackageDeskTopLinks) {
  412. cmCPackLogger(cmCPackLog::LOG_DEBUG, "CPACK_CREATE_DESKTOP_LINKS: "
  413. << cpackPackageDeskTopLinks << std::endl);
  414. cmSystemTools::ExpandListArgument(cpackPackageDeskTopLinks,
  415. cpackPackageDesktopLinksVector);
  416. for (std::vector<std::string>::iterator i =
  417. cpackPackageDesktopLinksVector.begin();
  418. i != cpackPackageDesktopLinksVector.end(); ++i) {
  419. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  420. "CPACK_CREATE_DESKTOP_LINKS: " << *i << std::endl);
  421. }
  422. } else {
  423. cmCPackLogger(cmCPackLog::LOG_DEBUG, "CPACK_CREATE_DESKTOP_LINKS: "
  424. << "not set" << std::endl);
  425. }
  426. std::ostringstream str;
  427. std::ostringstream deleteStr;
  428. if (cpackPackageExecutables) {
  429. cmCPackLogger(cmCPackLog::LOG_DEBUG, "The cpackPackageExecutables: "
  430. << cpackPackageExecutables << "." << std::endl);
  431. std::vector<std::string> cpackPackageExecutablesVector;
  432. cmSystemTools::ExpandListArgument(cpackPackageExecutables,
  433. cpackPackageExecutablesVector);
  434. if (cpackPackageExecutablesVector.size() % 2 != 0) {
  435. cmCPackLogger(
  436. cmCPackLog::LOG_ERROR,
  437. "CPACK_PACKAGE_EXECUTABLES should contain pairs of <executable> and "
  438. "<icon name>."
  439. << std::endl);
  440. return 0;
  441. }
  442. std::vector<std::string>::iterator it;
  443. for (it = cpackPackageExecutablesVector.begin();
  444. it != cpackPackageExecutablesVector.end(); ++it) {
  445. std::string execName = *it;
  446. ++it;
  447. std::string linkName = *it;
  448. str << " CreateShortCut \"$SMPROGRAMS\\$STARTMENU_FOLDER\\" << linkName
  449. << ".lnk\" \"$INSTDIR\\" << cpackNsisExecutablesDirectory << "\\"
  450. << execName << ".exe\"" << std::endl;
  451. deleteStr << " Delete \"$SMPROGRAMS\\$MUI_TEMP\\" << linkName
  452. << ".lnk\"" << std::endl;
  453. // see if CPACK_CREATE_DESKTOP_LINK_ExeName is on
  454. // if so add a desktop link
  455. if (!cpackPackageDesktopLinksVector.empty() &&
  456. std::find(cpackPackageDesktopLinksVector.begin(),
  457. cpackPackageDesktopLinksVector.end(),
  458. execName) != cpackPackageDesktopLinksVector.end()) {
  459. str << " StrCmp \"$INSTALL_DESKTOP\" \"1\" 0 +2\n";
  460. str << " CreateShortCut \"$DESKTOP\\" << linkName
  461. << ".lnk\" \"$INSTDIR\\" << cpackNsisExecutablesDirectory << "\\"
  462. << execName << ".exe\"" << std::endl;
  463. deleteStr << " StrCmp \"$INSTALL_DESKTOP\" \"1\" 0 +2\n";
  464. deleteStr << " Delete \"$DESKTOP\\" << linkName << ".lnk\""
  465. << std::endl;
  466. }
  467. }
  468. }
  469. this->CreateMenuLinks(str, deleteStr);
  470. this->SetOptionIfNotSet("CPACK_NSIS_CREATE_ICONS", str.str().c_str());
  471. this->SetOptionIfNotSet("CPACK_NSIS_DELETE_ICONS", deleteStr.str().c_str());
  472. this->SetOptionIfNotSet("CPACK_NSIS_COMPRESSOR", "lzma");
  473. return this->Superclass::InitializeInternal();
  474. }
  475. void cmCPackNSISGenerator::CreateMenuLinks(std::ostream& str,
  476. std::ostream& deleteStr)
  477. {
  478. const char* cpackMenuLinks = this->GetOption("CPACK_NSIS_MENU_LINKS");
  479. if (!cpackMenuLinks) {
  480. return;
  481. }
  482. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  483. "The cpackMenuLinks: " << cpackMenuLinks << "." << std::endl);
  484. std::vector<std::string> cpackMenuLinksVector;
  485. cmSystemTools::ExpandListArgument(cpackMenuLinks, cpackMenuLinksVector);
  486. if (cpackMenuLinksVector.size() % 2 != 0) {
  487. cmCPackLogger(
  488. cmCPackLog::LOG_ERROR,
  489. "CPACK_NSIS_MENU_LINKS should contain pairs of <shortcut target> and "
  490. "<shortcut label>."
  491. << std::endl);
  492. return;
  493. }
  494. static cmsys::RegularExpression urlRegex(
  495. "^(mailto:|(ftps?|https?|news)://).*$");
  496. std::vector<std::string>::iterator it;
  497. for (it = cpackMenuLinksVector.begin(); it != cpackMenuLinksVector.end();
  498. ++it) {
  499. std::string sourceName = *it;
  500. const bool url = urlRegex.find(sourceName);
  501. // Convert / to \ in filenames, but not in urls:
  502. //
  503. if (!url) {
  504. std::replace(sourceName.begin(), sourceName.end(), '/', '\\');
  505. }
  506. ++it;
  507. std::string linkName = *it;
  508. if (!url) {
  509. str << " CreateShortCut \"$SMPROGRAMS\\$STARTMENU_FOLDER\\" << linkName
  510. << ".lnk\" \"$INSTDIR\\" << sourceName << "\"" << std::endl;
  511. deleteStr << " Delete \"$SMPROGRAMS\\$MUI_TEMP\\" << linkName
  512. << ".lnk\"" << std::endl;
  513. } else {
  514. str << " WriteINIStr \"$SMPROGRAMS\\$STARTMENU_FOLDER\\" << linkName
  515. << ".url\" \"InternetShortcut\" \"URL\" \"" << sourceName << "\""
  516. << std::endl;
  517. deleteStr << " Delete \"$SMPROGRAMS\\$MUI_TEMP\\" << linkName
  518. << ".url\"" << std::endl;
  519. }
  520. // see if CPACK_CREATE_DESKTOP_LINK_ExeName is on
  521. // if so add a desktop link
  522. std::string desktop = "CPACK_CREATE_DESKTOP_LINK_";
  523. desktop += linkName;
  524. if (this->IsSet(desktop)) {
  525. str << " StrCmp \"$INSTALL_DESKTOP\" \"1\" 0 +2\n";
  526. str << " CreateShortCut \"$DESKTOP\\" << linkName
  527. << ".lnk\" \"$INSTDIR\\" << sourceName << "\"" << std::endl;
  528. deleteStr << " StrCmp \"$INSTALL_DESKTOP\" \"1\" 0 +2\n";
  529. deleteStr << " Delete \"$DESKTOP\\" << linkName << ".lnk\""
  530. << std::endl;
  531. }
  532. }
  533. }
  534. bool cmCPackNSISGenerator::GetListOfSubdirectories(
  535. const char* topdir, std::vector<std::string>& dirs)
  536. {
  537. cmsys::Directory dir;
  538. dir.Load(topdir);
  539. size_t fileNum;
  540. for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum) {
  541. if (strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)), ".") &&
  542. strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)), "..")) {
  543. std::string fullPath = topdir;
  544. fullPath += "/";
  545. fullPath += dir.GetFile(static_cast<unsigned long>(fileNum));
  546. if (cmsys::SystemTools::FileIsDirectory(fullPath) &&
  547. !cmsys::SystemTools::FileIsSymlink(fullPath)) {
  548. if (!this->GetListOfSubdirectories(fullPath.c_str(), dirs)) {
  549. return false;
  550. }
  551. }
  552. }
  553. }
  554. dirs.push_back(topdir);
  555. return true;
  556. }
  557. enum cmCPackGenerator::CPackSetDestdirSupport
  558. cmCPackNSISGenerator::SupportsSetDestdir() const
  559. {
  560. return cmCPackGenerator::SETDESTDIR_SHOULD_NOT_BE_USED;
  561. }
  562. bool cmCPackNSISGenerator::SupportsAbsoluteDestination() const
  563. {
  564. return false;
  565. }
  566. bool cmCPackNSISGenerator::SupportsComponentInstallation() const
  567. {
  568. return true;
  569. }
  570. std::string cmCPackNSISGenerator::CreateComponentDescription(
  571. cmCPackComponent* component, std::ostream& macrosOut)
  572. {
  573. // Basic description of the component
  574. std::string componentCode = "Section ";
  575. if (component->IsDisabledByDefault) {
  576. componentCode += "/o ";
  577. }
  578. componentCode += "\"";
  579. if (component->IsHidden) {
  580. componentCode += "-";
  581. }
  582. componentCode += component->DisplayName + "\" " + component->Name + "\n";
  583. if (component->IsRequired) {
  584. componentCode += " SectionIn RO\n";
  585. } else if (!component->InstallationTypes.empty()) {
  586. std::ostringstream out;
  587. std::vector<cmCPackInstallationType*>::iterator installTypeIter;
  588. for (installTypeIter = component->InstallationTypes.begin();
  589. installTypeIter != component->InstallationTypes.end();
  590. ++installTypeIter) {
  591. out << " " << (*installTypeIter)->Index;
  592. }
  593. componentCode += " SectionIn" + out.str() + "\n";
  594. }
  595. componentCode += " SetOutPath \"$INSTDIR\"\n";
  596. // Create the actual installation commands
  597. if (component->IsDownloaded) {
  598. if (component->ArchiveFile.empty()) {
  599. // Compute the name of the archive.
  600. std::string packagesDir = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  601. packagesDir += ".dummy";
  602. std::ostringstream out;
  603. out << cmSystemTools::GetFilenameWithoutLastExtension(packagesDir) << "-"
  604. << component->Name << ".zip";
  605. component->ArchiveFile = out.str();
  606. }
  607. // Create the directory for the upload area
  608. const char* userUploadDirectory =
  609. this->GetOption("CPACK_UPLOAD_DIRECTORY");
  610. std::string uploadDirectory;
  611. if (userUploadDirectory && *userUploadDirectory) {
  612. uploadDirectory = userUploadDirectory;
  613. } else {
  614. uploadDirectory = this->GetOption("CPACK_PACKAGE_DIRECTORY");
  615. uploadDirectory += "/CPackUploads";
  616. }
  617. if (!cmSystemTools::FileExists(uploadDirectory.c_str())) {
  618. if (!cmSystemTools::MakeDirectory(uploadDirectory.c_str())) {
  619. cmCPackLogger(cmCPackLog::LOG_ERROR,
  620. "Unable to create NSIS upload directory "
  621. << uploadDirectory << std::endl);
  622. return "";
  623. }
  624. }
  625. // Remove the old archive, if one exists
  626. std::string archiveFile = uploadDirectory + '/' + component->ArchiveFile;
  627. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  628. "- Building downloaded component archive: " << archiveFile
  629. << std::endl);
  630. if (cmSystemTools::FileExists(archiveFile.c_str(), true)) {
  631. if (!cmSystemTools::RemoveFile(archiveFile)) {
  632. cmCPackLogger(cmCPackLog::LOG_ERROR, "Unable to remove archive file "
  633. << archiveFile << std::endl);
  634. return "";
  635. }
  636. }
  637. // Find a ZIP program
  638. if (!this->IsSet("ZIP_EXECUTABLE")) {
  639. this->ReadListFile("CPackZIP.cmake");
  640. if (!this->IsSet("ZIP_EXECUTABLE")) {
  641. cmCPackLogger(cmCPackLog::LOG_ERROR, "Unable to find ZIP program"
  642. << std::endl);
  643. return "";
  644. }
  645. }
  646. // The directory where this component's files reside
  647. std::string dirName = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  648. dirName += '/';
  649. dirName += component->Name;
  650. dirName += '/';
  651. // Build the list of files to go into this archive, and determine the
  652. // size of the installed component.
  653. std::string zipListFileName = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  654. zipListFileName += "/winZip.filelist";
  655. bool needQuotesInFile =
  656. cmSystemTools::IsOn(this->GetOption("CPACK_ZIP_NEED_QUOTES"));
  657. unsigned long totalSize = 0;
  658. { // the scope is needed for cmGeneratedFileStream
  659. cmGeneratedFileStream out(zipListFileName.c_str());
  660. std::vector<std::string>::iterator fileIt;
  661. for (fileIt = component->Files.begin(); fileIt != component->Files.end();
  662. ++fileIt) {
  663. if (needQuotesInFile) {
  664. out << "\"";
  665. }
  666. out << *fileIt;
  667. if (needQuotesInFile) {
  668. out << "\"";
  669. }
  670. out << std::endl;
  671. totalSize += cmSystemTools::FileLength(dirName + *fileIt);
  672. }
  673. }
  674. // Build the archive in the upload area
  675. std::string cmd = this->GetOption("CPACK_ZIP_COMMAND");
  676. cmsys::SystemTools::ReplaceString(cmd, "<ARCHIVE>", archiveFile.c_str());
  677. cmsys::SystemTools::ReplaceString(cmd, "<FILELIST>",
  678. zipListFileName.c_str());
  679. std::string output;
  680. int retVal = -1;
  681. int res = cmSystemTools::RunSingleCommand(cmd.c_str(), &output, &output,
  682. &retVal, dirName.c_str(),
  683. cmSystemTools::OUTPUT_NONE, 0);
  684. if (!res || retVal) {
  685. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  686. tmpFile += "/CompressZip.log";
  687. cmGeneratedFileStream ofs(tmpFile.c_str());
  688. ofs << "# Run command: " << cmd << std::endl
  689. << "# Output:" << std::endl
  690. << output << std::endl;
  691. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running zip command: "
  692. << cmd << std::endl
  693. << "Please check " << tmpFile << " for errors"
  694. << std::endl);
  695. return "";
  696. }
  697. // Create the NSIS code to download this file on-the-fly.
  698. unsigned long totalSizeInKbytes = (totalSize + 512) / 1024;
  699. if (totalSizeInKbytes == 0) {
  700. totalSizeInKbytes = 1;
  701. }
  702. std::ostringstream out;
  703. /* clang-format off */
  704. out << " AddSize " << totalSizeInKbytes << "\n"
  705. << " Push \"" << component->ArchiveFile << "\"\n"
  706. << " Call DownloadFile\n"
  707. << " ZipDLL::extractall \"$INSTDIR\\"
  708. << component->ArchiveFile << "\" \"$INSTDIR\"\n"
  709. << " Pop $2 ; error message\n"
  710. " StrCmp $2 \"success\" +2 0\n"
  711. " MessageBox MB_OK \"Failed to unzip $2\"\n"
  712. " Delete $INSTDIR\\$0\n";
  713. /* clang-format on */
  714. componentCode += out.str();
  715. } else {
  716. componentCode +=
  717. " File /r \"${INST_DIR}\\" + component->Name + "\\*.*\"\n";
  718. }
  719. componentCode += "SectionEnd\n";
  720. // Macro used to remove the component
  721. macrosOut << "!macro Remove_${" << component->Name << "}\n";
  722. macrosOut << " IntCmp $" << component->Name << "_was_installed 0 noremove_"
  723. << component->Name << "\n";
  724. std::vector<std::string>::iterator pathIt;
  725. std::string path;
  726. for (pathIt = component->Files.begin(); pathIt != component->Files.end();
  727. ++pathIt) {
  728. path = *pathIt;
  729. std::replace(path.begin(), path.end(), '/', '\\');
  730. macrosOut << " Delete \"$INSTDIR\\" << path << "\"\n";
  731. }
  732. for (pathIt = component->Directories.begin();
  733. pathIt != component->Directories.end(); ++pathIt) {
  734. path = *pathIt;
  735. std::replace(path.begin(), path.end(), '/', '\\');
  736. macrosOut << " RMDir \"$INSTDIR\\" << path << "\"\n";
  737. }
  738. macrosOut << " noremove_" << component->Name << ":\n";
  739. macrosOut << "!macroend\n";
  740. // Macro used to select each of the components that this component
  741. // depends on.
  742. std::set<cmCPackComponent*> visited;
  743. macrosOut << "!macro Select_" << component->Name << "_depends\n";
  744. macrosOut << CreateSelectionDependenciesDescription(component, visited);
  745. macrosOut << "!macroend\n";
  746. // Macro used to deselect each of the components that depend on this
  747. // component.
  748. visited.clear();
  749. macrosOut << "!macro Deselect_required_by_" << component->Name << "\n";
  750. macrosOut << CreateDeselectionDependenciesDescription(component, visited);
  751. macrosOut << "!macroend\n";
  752. return componentCode;
  753. }
  754. std::string cmCPackNSISGenerator::CreateSelectionDependenciesDescription(
  755. cmCPackComponent* component, std::set<cmCPackComponent*>& visited)
  756. {
  757. // Don't visit a component twice
  758. if (visited.count(component)) {
  759. return std::string();
  760. }
  761. visited.insert(component);
  762. std::ostringstream out;
  763. std::vector<cmCPackComponent*>::iterator dependIt;
  764. for (dependIt = component->Dependencies.begin();
  765. dependIt != component->Dependencies.end(); ++dependIt) {
  766. // Write NSIS code to select this dependency
  767. out << " SectionGetFlags ${" << (*dependIt)->Name << "} $0\n";
  768. out << " IntOp $0 $0 | ${SF_SELECTED}\n";
  769. out << " SectionSetFlags ${" << (*dependIt)->Name << "} $0\n";
  770. out << " IntOp $" << (*dependIt)->Name
  771. << "_selected 0 + ${SF_SELECTED}\n";
  772. // Recurse
  773. out << CreateSelectionDependenciesDescription(*dependIt, visited).c_str();
  774. }
  775. return out.str();
  776. }
  777. std::string cmCPackNSISGenerator::CreateDeselectionDependenciesDescription(
  778. cmCPackComponent* component, std::set<cmCPackComponent*>& visited)
  779. {
  780. // Don't visit a component twice
  781. if (visited.count(component)) {
  782. return std::string();
  783. }
  784. visited.insert(component);
  785. std::ostringstream out;
  786. std::vector<cmCPackComponent*>::iterator dependIt;
  787. for (dependIt = component->ReverseDependencies.begin();
  788. dependIt != component->ReverseDependencies.end(); ++dependIt) {
  789. // Write NSIS code to deselect this dependency
  790. out << " SectionGetFlags ${" << (*dependIt)->Name << "} $0\n";
  791. out << " IntOp $1 ${SF_SELECTED} ~\n";
  792. out << " IntOp $0 $0 & $1\n";
  793. out << " SectionSetFlags ${" << (*dependIt)->Name << "} $0\n";
  794. out << " IntOp $" << (*dependIt)->Name << "_selected 0 + 0\n";
  795. // Recurse
  796. out
  797. << CreateDeselectionDependenciesDescription(*dependIt, visited).c_str();
  798. }
  799. return out.str();
  800. }
  801. std::string cmCPackNSISGenerator::CreateComponentGroupDescription(
  802. cmCPackComponentGroup* group, std::ostream& macrosOut)
  803. {
  804. if (group->Components.empty() && group->Subgroups.empty()) {
  805. // Silently skip empty groups. NSIS doesn't support them.
  806. return std::string();
  807. }
  808. std::string code = "SectionGroup ";
  809. if (group->IsExpandedByDefault) {
  810. code += "/e ";
  811. }
  812. if (group->IsBold) {
  813. code += "\"!" + group->DisplayName + "\" " + group->Name + "\n";
  814. } else {
  815. code += "\"" + group->DisplayName + "\" " + group->Name + "\n";
  816. }
  817. std::vector<cmCPackComponentGroup*>::iterator groupIt;
  818. for (groupIt = group->Subgroups.begin(); groupIt != group->Subgroups.end();
  819. ++groupIt) {
  820. code += this->CreateComponentGroupDescription(*groupIt, macrosOut);
  821. }
  822. std::vector<cmCPackComponent*>::iterator comp;
  823. for (comp = group->Components.begin(); comp != group->Components.end();
  824. ++comp) {
  825. if ((*comp)->Files.empty()) {
  826. continue;
  827. }
  828. code += this->CreateComponentDescription(*comp, macrosOut);
  829. }
  830. code += "SectionGroupEnd\n";
  831. return code;
  832. }
  833. std::string cmCPackNSISGenerator::TranslateNewlines(std::string str)
  834. {
  835. cmSystemTools::ReplaceString(str, "\n", "$\\r$\\n");
  836. return str;
  837. }