cmCPackNSISGenerator.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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_UNIICON");
  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.net"
  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. const char* cpackPackageDeskTopLinks
  219. = this->GetOption("CPACK_CREATE_DESKTOP_LINKS");
  220. std::vector<std::string> cpackPackageDesktopLinksVector;
  221. if(cpackPackageDeskTopLinks)
  222. {
  223. cmCPackLogger(cmCPackLog::LOG_DEBUG, "CPACK_CREATE_DESKTOP_LINKS: "
  224. << cpackPackageDeskTopLinks << std::endl);
  225. cmSystemTools::
  226. ExpandListArgument(cpackPackageDeskTopLinks,
  227. cpackPackageDesktopLinksVector);
  228. for(std::vector<std::string>::iterator i =
  229. cpackPackageDesktopLinksVector.begin(); i !=
  230. cpackPackageDesktopLinksVector.end(); ++i)
  231. {
  232. cmCPackLogger(cmCPackLog::LOG_DEBUG, "CPACK_CREATE_DESKTOP_LINKS: "
  233. << *i << std::endl);
  234. }
  235. }
  236. else
  237. {
  238. cmCPackLogger(cmCPackLog::LOG_DEBUG, "CPACK_CREATE_DESKTOP_LINKS: "
  239. << "not set" << std::endl);
  240. }
  241. if ( cpackPackageExecutables )
  242. {
  243. cmCPackLogger(cmCPackLog::LOG_DEBUG, "The cpackPackageExecutables: "
  244. << cpackPackageExecutables << "." << std::endl);
  245. cmOStringStream str;
  246. cmOStringStream deleteStr;
  247. std::vector<std::string> cpackPackageExecutablesVector;
  248. cmSystemTools::ExpandListArgument(cpackPackageExecutables,
  249. cpackPackageExecutablesVector);
  250. if ( cpackPackageExecutablesVector.size() % 2 != 0 )
  251. {
  252. cmCPackLogger(cmCPackLog::LOG_ERROR,
  253. "CPACK_PACKAGE_EXECUTABLES should contain pairs of <executable> and "
  254. "<icon name>." << std::endl);
  255. return 0;
  256. }
  257. std::vector<std::string>::iterator it;
  258. for ( it = cpackPackageExecutablesVector.begin();
  259. it != cpackPackageExecutablesVector.end();
  260. ++it )
  261. {
  262. std::string execName = *it;
  263. ++ it;
  264. std::string linkName = *it;
  265. str << " CreateShortCut \"$SMPROGRAMS\\$STARTMENU_FOLDER\\"
  266. << linkName << ".lnk\" \"$INSTDIR\\bin\\" << execName << ".exe\""
  267. << std::endl;
  268. deleteStr << " Delete \"$SMPROGRAMS\\$MUI_TEMP\\" << linkName
  269. << ".lnk\"" << std::endl;
  270. // see if CPACK_CREATE_DESKTOP_LINK_ExeName is on
  271. // if so add a desktop link
  272. if(cpackPackageDesktopLinksVector.size() &&
  273. std::find(cpackPackageDesktopLinksVector.begin(),
  274. cpackPackageDesktopLinksVector.end(),
  275. execName)
  276. != cpackPackageDesktopLinksVector.end())
  277. {
  278. str << " StrCmp \"$INSTALL_DESKTOP\" \"1\" 0 +2\n";
  279. str << " CreateShortCut \"$DESKTOP\\"
  280. << linkName << ".lnk\" \"$INSTDIR\\bin\\" << execName << ".exe\""
  281. << std::endl;
  282. deleteStr << " StrCmp \"$INSTALL_DESKTOP\" \"1\" 0 +2\n";
  283. deleteStr << " Delete \"$DESKTOP\\" << linkName
  284. << ".lnk\"" << std::endl;
  285. }
  286. }
  287. this->CreateMenuLinks(str, deleteStr);
  288. this->SetOptionIfNotSet("CPACK_NSIS_CREATE_ICONS", str.str().c_str());
  289. this->SetOptionIfNotSet("CPACK_NSIS_DELETE_ICONS",
  290. deleteStr.str().c_str());
  291. }
  292. this->SetOptionIfNotSet("CPACK_NSIS_COMPRESSOR", "lzma");
  293. return this->Superclass::InitializeInternal();
  294. }
  295. //----------------------------------------------------------------------
  296. void cmCPackNSISGenerator::CreateMenuLinks( cmOStringStream& str,
  297. cmOStringStream& deleteStr)
  298. {
  299. const char* cpackMenuLinks
  300. = this->GetOption("CPACK_NSIS_MENU_LINKS");
  301. if(!cpackMenuLinks)
  302. {
  303. return;
  304. }
  305. cmCPackLogger(cmCPackLog::LOG_DEBUG, "The cpackMenuLinks: "
  306. << cpackMenuLinks << "." << std::endl);
  307. std::vector<std::string> cpackMenuLinksVector;
  308. cmSystemTools::ExpandListArgument(cpackMenuLinks,
  309. cpackMenuLinksVector);
  310. if ( cpackMenuLinksVector.size() % 2 != 0 )
  311. {
  312. cmCPackLogger(
  313. cmCPackLog::LOG_ERROR,
  314. "CPACK_PACKAGE_EXECUTABLES should contain pairs of <executable> and "
  315. "<icon name>." << std::endl);
  316. return;
  317. }
  318. std::vector<std::string>::iterator it;
  319. for ( it = cpackMenuLinksVector.begin();
  320. it != cpackMenuLinksVector.end();
  321. ++it )
  322. {
  323. std::string sourceName = *it;
  324. bool url = false;
  325. if(sourceName.find("http:") == 0)
  326. {
  327. url = true;
  328. }
  329. /* convert / to \\ */
  330. if(!url)
  331. {
  332. cmSystemTools::ReplaceString(sourceName, "/", "\\");
  333. }
  334. ++ it;
  335. std::string linkName = *it;
  336. if(!url)
  337. {
  338. str << " CreateShortCut \"$SMPROGRAMS\\$STARTMENU_FOLDER\\"
  339. << linkName << ".lnk\" \"$INSTDIR\\" << sourceName << "\""
  340. << std::endl;
  341. deleteStr << " Delete \"$SMPROGRAMS\\$MUI_TEMP\\" << linkName
  342. << ".lnk\"" << std::endl;
  343. }
  344. else
  345. {
  346. str << " WriteINIStr \"$SMPROGRAMS\\$STARTMENU_FOLDER\\"
  347. << linkName << ".url\" \"InternetShortcut\" \"URL\" \""
  348. << sourceName << "\""
  349. << std::endl;
  350. deleteStr << " Delete \"$SMPROGRAMS\\$MUI_TEMP\\" << linkName
  351. << ".url\"" << std::endl;
  352. }
  353. // see if CPACK_CREATE_DESKTOP_LINK_ExeName is on
  354. // if so add a desktop link
  355. std::string desktop = "CPACK_CREATE_DESKTOP_LINK_";
  356. desktop += linkName;
  357. if(this->IsSet(desktop.c_str()))
  358. {
  359. str << " StrCmp \"$INSTALL_DESKTOP\" \"1\" 0 +2\n";
  360. str << " CreateShortCut \"$DESKTOP\\"
  361. << linkName << ".lnk\" \"$INSTDIR\\" << sourceName << "\""
  362. << std::endl;
  363. deleteStr << " StrCmp \"$INSTALL_DESKTOP\" \"1\" 0 +2\n";
  364. deleteStr << " Delete \"$DESKTOP\\" << linkName
  365. << ".lnk\"" << std::endl;
  366. }
  367. }
  368. }
  369. //----------------------------------------------------------------------
  370. bool cmCPackNSISGenerator::GetListOfSubdirectories(const char* topdir,
  371. std::vector<std::string>& dirs)
  372. {
  373. cmsys::Directory dir;
  374. dir.Load(topdir);
  375. size_t fileNum;
  376. for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum)
  377. {
  378. if (strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".") &&
  379. strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".."))
  380. {
  381. cmsys_stl::string fullPath = topdir;
  382. fullPath += "/";
  383. fullPath += dir.GetFile(static_cast<unsigned long>(fileNum));
  384. if(cmsys::SystemTools::FileIsDirectory(fullPath.c_str()) &&
  385. !cmsys::SystemTools::FileIsSymlink(fullPath.c_str()))
  386. {
  387. if (!this->GetListOfSubdirectories(fullPath.c_str(), dirs))
  388. {
  389. return false;
  390. }
  391. }
  392. }
  393. }
  394. dirs.push_back(topdir);
  395. return true;
  396. }