cmCPackNSISGenerator.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCPackNSISGenerator.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmSystemTools.h"
  17. #include "cmMakefile.h"
  18. #include "cmGeneratedFileStream.h"
  19. #include "cmCPackLog.h"
  20. #include <cmsys/SystemTools.hxx>
  21. #include <cmsys/Glob.hxx>
  22. #include <cmsys/Directory.hxx>
  23. #include <cmsys/RegularExpression.hxx>
  24. /* NSIS uses different command line syntax on Windows and others */
  25. #ifdef _WIN32
  26. # define NSIS_OPT "/"
  27. #else
  28. # define NSIS_OPT "-"
  29. #endif
  30. //----------------------------------------------------------------------
  31. cmCPackNSISGenerator::cmCPackNSISGenerator()
  32. {
  33. }
  34. //----------------------------------------------------------------------
  35. cmCPackNSISGenerator::~cmCPackNSISGenerator()
  36. {
  37. }
  38. //----------------------------------------------------------------------
  39. int cmCPackNSISGenerator::CompressFiles(const char* outFileName,
  40. const char* toplevel, const std::vector<std::string>& files)
  41. {
  42. (void)outFileName; // TODO: Fix nsis to force out file name
  43. (void)toplevel;
  44. std::string nsisInFileName = this->FindTemplate("NSIS.template.in");
  45. if ( nsisInFileName.size() == 0 )
  46. {
  47. cmCPackLogger(cmCPackLog::LOG_ERROR,
  48. "CPack error: Could not find NSIS installer template file."
  49. << std::endl);
  50. return false;
  51. }
  52. std::string nsisInInstallOptions
  53. = this->FindTemplate("NSIS.InstallOptions.ini.in");
  54. if ( nsisInInstallOptions.size() == 0 )
  55. {
  56. cmCPackLogger(cmCPackLog::LOG_ERROR,
  57. "CPack error: Could not find NSIS installer options file."
  58. << std::endl);
  59. return false;
  60. }
  61. std::string nsisFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  62. std::string tmpFile = nsisFileName;
  63. tmpFile += "/NSISOutput.log";
  64. std::string nsisInstallOptions = nsisFileName + "/NSIS.InstallOptions.ini";
  65. nsisFileName += "/project.nsi";
  66. cmOStringStream str;
  67. std::vector<std::string>::const_iterator it;
  68. for ( it = files.begin(); it != files.end(); ++ it )
  69. {
  70. std::string fileN = cmSystemTools::RelativePath(toplevel,
  71. it->c_str());
  72. cmSystemTools::ReplaceString(fileN, "/", "\\");
  73. str << " Delete \"$INSTDIR\\" << fileN.c_str() << "\"" << std::endl;
  74. }
  75. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Uninstall Files: "
  76. << str.str().c_str() << std::endl);
  77. this->SetOptionIfNotSet("CPACK_NSIS_DELETE_FILES", str.str().c_str());
  78. std::vector<std::string> dirs;
  79. this->GetListOfSubdirectories(toplevel, dirs);
  80. std::vector<std::string>::const_iterator sit;
  81. cmOStringStream dstr;
  82. for ( sit = dirs.begin(); sit != dirs.end(); ++ sit )
  83. {
  84. std::string fileN = cmSystemTools::RelativePath(toplevel,
  85. sit->c_str());
  86. if ( fileN.empty() )
  87. {
  88. continue;
  89. }
  90. cmSystemTools::ReplaceString(fileN, "/", "\\");
  91. dstr << " RMDir \"$INSTDIR\\" << fileN.c_str() << "\"" << std::endl;
  92. }
  93. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Uninstall Dirs: "
  94. << dstr.str().c_str() << std::endl);
  95. this->SetOptionIfNotSet("CPACK_NSIS_DELETE_DIRECTORIES",
  96. dstr.str().c_str());
  97. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: " << nsisInFileName
  98. << " to " << nsisFileName << std::endl);
  99. if(this->IsSet("CPACK_NSIS_MUI_ICON")
  100. && this->IsSet("CPACK_NSIS_MUI_UNIICON"))
  101. {
  102. std::string installerIconCode="!define MUI_ICON \"";
  103. installerIconCode += this->GetOption("CPACK_NSIS_MUI_ICON");
  104. installerIconCode += "\"\n";
  105. installerIconCode += "!define MUI_UNICON \"";
  106. installerIconCode += this->GetOption("CPACK_NSIS_MUI_ICON");
  107. installerIconCode += "\"\n";
  108. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_MUI_ICON_CODE",
  109. installerIconCode.c_str());
  110. }
  111. if(this->IsSet("CPACK_PACKAGE_ICON"))
  112. {
  113. std::string installerIconCode = "!define MUI_HEADERIMAGE_BITMAP \"";
  114. installerIconCode += this->GetOption("CPACK_PACKAGE_ICON");
  115. installerIconCode += "\"\n";
  116. this->SetOptionIfNotSet("CPACK_NSIS_INSTALLER_ICON_CODE",
  117. installerIconCode.c_str());
  118. }
  119. this->ConfigureFile(nsisInInstallOptions.c_str(),
  120. nsisInstallOptions.c_str());
  121. this->ConfigureFile(nsisInFileName.c_str(), nsisFileName.c_str());
  122. std::string nsisCmd = "\"";
  123. nsisCmd += this->GetOption("CPACK_INSTALLER_PROGRAM");
  124. nsisCmd += "\" \"" + nsisFileName + "\"";
  125. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Execute: " << nsisCmd.c_str()
  126. << std::endl);
  127. std::string output;
  128. int retVal = 1;
  129. bool res = cmSystemTools::RunSingleCommand(nsisCmd.c_str(), &output,
  130. &retVal, 0, this->GeneratorVerbose, 0);
  131. if ( !res || retVal )
  132. {
  133. cmGeneratedFileStream ofs(tmpFile.c_str());
  134. ofs << "# Run command: " << nsisCmd.c_str() << std::endl
  135. << "# Output:" << std::endl
  136. << output.c_str() << std::endl;
  137. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running NSIS command: "
  138. << nsisCmd.c_str() << std::endl
  139. << "Please check " << tmpFile.c_str() << " for errors" << std::endl);
  140. return 0;
  141. }
  142. return 1;
  143. }
  144. //----------------------------------------------------------------------
  145. int cmCPackNSISGenerator::InitializeInternal()
  146. {
  147. if ( cmSystemTools::IsOn(this->GetOption(
  148. "CPACK_INCLUDE_TOPLEVEL_DIRECTORY")) )
  149. {
  150. cmCPackLogger(cmCPackLog::LOG_ERROR,
  151. "NSIS Generator cannot work with CPACK_INCLUDE_TOPLEVEL_DIRECTORY. "
  152. "This option will be ignored."
  153. << std::endl);
  154. this->SetOption("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", 0);
  155. }
  156. cmCPackLogger(cmCPackLog::LOG_DEBUG, "cmCPackNSISGenerator::Initialize()"
  157. << std::endl);
  158. std::vector<std::string> path;
  159. std::string nsisPath;
  160. #ifdef _WIN32
  161. if ( !cmsys::SystemTools::ReadRegistryValue(
  162. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS", nsisPath) )
  163. {
  164. cmCPackLogger
  165. (cmCPackLog::LOG_ERROR,
  166. "Cannot find NSIS registry value. This is usually caused by NSIS "
  167. "not being installed. Please install NSIS from "
  168. "http://nsis.sourceforge.org"
  169. << std::endl);
  170. return 0;
  171. }
  172. path.push_back(nsisPath);
  173. #endif
  174. nsisPath = cmSystemTools::FindProgram("makensis", path, false);
  175. if ( nsisPath.empty() )
  176. {
  177. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find NSIS compiler"
  178. << std::endl);
  179. return 0;
  180. }
  181. std::string nsisCmd = "\"" + nsisPath + "\" " NSIS_OPT "VERSION";
  182. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Test NSIS version: "
  183. << nsisCmd.c_str() << std::endl);
  184. std::string output;
  185. int retVal = 1;
  186. bool resS = cmSystemTools::RunSingleCommand(nsisCmd.c_str(),
  187. &output, &retVal, 0, this->GeneratorVerbose, 0);
  188. cmsys::RegularExpression versionRex("v([0-9]+.[0-9]+)");
  189. if ( !resS || retVal || !versionRex.find(output))
  190. {
  191. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  192. tmpFile += "/NSISOutput.log";
  193. cmGeneratedFileStream ofs(tmpFile.c_str());
  194. ofs << "# Run command: " << nsisCmd.c_str() << std::endl
  195. << "# Output:" << std::endl
  196. << output.c_str() << std::endl;
  197. cmCPackLogger(cmCPackLog::LOG_ERROR,
  198. "Problem checking NSIS version with command: "
  199. << nsisCmd.c_str() << std::endl
  200. << "Please check " << tmpFile.c_str() << " for errors" << std::endl);
  201. return 0;
  202. }
  203. double nsisVersion = atof(versionRex.match(1).c_str());
  204. double minNSISVersion = 2.09;
  205. cmCPackLogger(cmCPackLog::LOG_DEBUG, "NSIS Version: "
  206. << nsisVersion << std::endl);
  207. if ( nsisVersion < minNSISVersion )
  208. {
  209. cmCPackLogger(cmCPackLog::LOG_ERROR,
  210. "CPack requires NSIS Version 2.09 or greater. NSIS found on the system "
  211. "was: "
  212. << nsisVersion << std::endl);
  213. return 0;
  214. }
  215. this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM", nsisPath.c_str());
  216. const char* cpackPackageExecutables
  217. = this->GetOption("CPACK_PACKAGE_EXECUTABLES");
  218. if ( cpackPackageExecutables )
  219. {
  220. cmCPackLogger(cmCPackLog::LOG_DEBUG, "The cpackPackageExecutables: "
  221. << cpackPackageExecutables << "." << std::endl);
  222. cmOStringStream str;
  223. cmOStringStream deleteStr;
  224. std::vector<std::string> cpackPackageExecutablesVector;
  225. cmSystemTools::ExpandListArgument(cpackPackageExecutables,
  226. cpackPackageExecutablesVector);
  227. if ( cpackPackageExecutablesVector.size() % 2 != 0 )
  228. {
  229. cmCPackLogger(cmCPackLog::LOG_ERROR,
  230. "CPACK_PACKAGE_EXECUTABLES should contain pairs of <executable> and "
  231. "<icon name>." << std::endl);
  232. return 0;
  233. }
  234. std::vector<std::string>::iterator it;
  235. for ( it = cpackPackageExecutablesVector.begin();
  236. it != cpackPackageExecutablesVector.end();
  237. ++it )
  238. {
  239. std::string execName = *it;
  240. ++ it;
  241. std::string linkName = *it;
  242. str << " CreateShortCut \"$SMPROGRAMS\\$STARTMENU_FOLDER\\"
  243. << linkName << ".lnk\" \"$INSTDIR\\bin\\" << execName << ".exe\""
  244. << std::endl;
  245. deleteStr << " Delete \"$SMPROGRAMS\\$MUI_TEMP\\" << linkName
  246. << ".lnk\"" << std::endl;
  247. // see if CPACK_CREATE_DESKTOP_LINK_ExeName is on
  248. // if so add a desktop link
  249. std::string desktop = "CPACK_CREATE_DESKTOP_LINK_";
  250. desktop += execName;
  251. if(this->IsSet(desktop.c_str()))
  252. {
  253. str << " StrCmp \"$INSTALL_DESKTOP\" \"1\" 0 +2\n";
  254. str << " CreateShortCut \"$DESKTOP\\"
  255. << linkName << ".lnk\" \"$INSTDIR\\bin\\" << execName << ".exe\""
  256. << std::endl;
  257. deleteStr << " StrCmp \"$INSTALL_DESKTOP\" \"1\" 0 +2\n";
  258. deleteStr << " Delete \"$DESKTOP\\" << linkName
  259. << ".lnk\"" << std::endl;
  260. }
  261. }
  262. this->CreateMenuLinks(str, deleteStr);
  263. this->SetOptionIfNotSet("CPACK_NSIS_CREATE_ICONS", str.str().c_str());
  264. this->SetOptionIfNotSet("CPACK_NSIS_DELETE_ICONS",
  265. deleteStr.str().c_str());
  266. }
  267. this->SetOptionIfNotSet("CPACK_NSIS_COMPRESSOR", "lzma");
  268. return this->Superclass::InitializeInternal();
  269. }
  270. //----------------------------------------------------------------------
  271. void cmCPackNSISGenerator::CreateMenuLinks( cmOStringStream& str,
  272. cmOStringStream& deleteStr)
  273. {
  274. const char* cpackMenuLinks
  275. = this->GetOption("CPACK_NSIS_MENU_LINKS");
  276. if(!cpackMenuLinks)
  277. {
  278. return;
  279. }
  280. cmCPackLogger(cmCPackLog::LOG_DEBUG, "The cpackMenuLinks: "
  281. << cpackMenuLinks << "." << std::endl);
  282. std::vector<std::string> cpackMenuLinksVector;
  283. cmSystemTools::ExpandListArgument(cpackMenuLinks,
  284. cpackMenuLinksVector);
  285. if ( cpackMenuLinksVector.size() % 2 != 0 )
  286. {
  287. cmCPackLogger(
  288. cmCPackLog::LOG_ERROR,
  289. "CPACK_PACKAGE_EXECUTABLES should contain pairs of <executable> and "
  290. "<icon name>." << std::endl);
  291. return;
  292. }
  293. std::vector<std::string>::iterator it;
  294. for ( it = cpackMenuLinksVector.begin();
  295. it != cpackMenuLinksVector.end();
  296. ++it )
  297. {
  298. std::string sourceName = *it;
  299. bool url = false;
  300. if(sourceName.find("http:") == 0)
  301. {
  302. url = true;
  303. }
  304. /* convert / to \\ */
  305. if(!url)
  306. {
  307. cmSystemTools::ReplaceString(sourceName, "/", "\\");
  308. }
  309. ++ it;
  310. std::string linkName = *it;
  311. if(!url)
  312. {
  313. str << " CreateShortCut \"$SMPROGRAMS\\$STARTMENU_FOLDER\\"
  314. << linkName << ".lnk\" \"$INSTDIR\\" << sourceName << "\""
  315. << std::endl;
  316. deleteStr << " Delete \"$SMPROGRAMS\\$MUI_TEMP\\" << linkName
  317. << ".lnk\"" << std::endl;
  318. }
  319. else
  320. {
  321. str << " WriteINIStr \"$SMPROGRAMS\\$STARTMENU_FOLDER\\"
  322. << linkName << ".url\" \"InternetShortcut\" \"URL\" \""
  323. << sourceName << "\""
  324. << std::endl;
  325. deleteStr << " Delete \"$SMPROGRAMS\\$MUI_TEMP\\" << linkName
  326. << ".url\"" << std::endl;
  327. }
  328. // see if CPACK_CREATE_DESKTOP_LINK_ExeName is on
  329. // if so add a desktop link
  330. std::string desktop = "CPACK_CREATE_DESKTOP_LINK_";
  331. desktop += linkName;
  332. if(this->IsSet(desktop.c_str()))
  333. {
  334. str << " StrCmp \"$INSTALL_DESKTOP\" \"1\" 0 +2\n";
  335. str << " CreateShortCut \"$DESKTOP\\"
  336. << linkName << ".lnk\" \"$INSTDIR\\" << sourceName << "\""
  337. << std::endl;
  338. deleteStr << " StrCmp \"$INSTALL_DESKTOP\" \"1\" 0 +2\n";
  339. deleteStr << " Delete \"$DESKTOP\\" << linkName
  340. << ".lnk\"" << std::endl;
  341. }
  342. }
  343. }
  344. //----------------------------------------------------------------------
  345. bool cmCPackNSISGenerator::GetListOfSubdirectories(const char* topdir,
  346. std::vector<std::string>& dirs)
  347. {
  348. cmsys::Directory dir;
  349. dir.Load(topdir);
  350. size_t fileNum;
  351. for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum)
  352. {
  353. if (strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".") &&
  354. strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".."))
  355. {
  356. cmsys_stl::string fullPath = topdir;
  357. fullPath += "/";
  358. fullPath += dir.GetFile(static_cast<unsigned long>(fileNum));
  359. if(cmsys::SystemTools::FileIsDirectory(fullPath.c_str()) &&
  360. !cmsys::SystemTools::FileIsSymlink(fullPath.c_str()))
  361. {
  362. if (!this->GetListOfSubdirectories(fullPath.c_str(), dirs))
  363. {
  364. return false;
  365. }
  366. }
  367. }
  368. }
  369. dirs.push_back(topdir);
  370. return true;
  371. }