cmCPackNSISGenerator.cxx 33 KB

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