cmCPackPackageMakerGenerator.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 "cmCPackPackageMakerGenerator.h"
  14. #include "cmake.h"
  15. #include "cmGlobalGenerator.h"
  16. #include "cmLocalGenerator.h"
  17. #include "cmSystemTools.h"
  18. #include "cmMakefile.h"
  19. #include "cmGeneratedFileStream.h"
  20. #include "cmCPackLog.h"
  21. #include <cmsys/SystemTools.hxx>
  22. #include <cmsys/Glob.hxx>
  23. //----------------------------------------------------------------------
  24. cmCPackPackageMakerGenerator::cmCPackPackageMakerGenerator()
  25. {
  26. this->PackageMakerVersion = 0;
  27. }
  28. //----------------------------------------------------------------------
  29. cmCPackPackageMakerGenerator::~cmCPackPackageMakerGenerator()
  30. {
  31. }
  32. //----------------------------------------------------------------------
  33. int cmCPackPackageMakerGenerator::CompressFiles(const char* outFileName,
  34. const char* toplevel,
  35. const std::vector<std::string>& files)
  36. {
  37. (void) files; // TODO: Fix api to not need files.
  38. (void) toplevel; // TODO: Use toplevel
  39. // Create directory structure
  40. std::string resDir = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  41. resDir += "/Resources";
  42. std::string preflightDirName = resDir + "/PreFlight";
  43. std::string postflightDirName = resDir + "/PostFlight";
  44. if ( !cmsys::SystemTools::MakeDirectory(preflightDirName.c_str())
  45. || !cmsys::SystemTools::MakeDirectory(postflightDirName.c_str()) )
  46. {
  47. cmCPackLogger(cmCPackLog::LOG_ERROR,
  48. "Problem creating installer directories: "
  49. << preflightDirName.c_str() << " and "
  50. << postflightDirName.c_str() << std::endl);
  51. return 0;
  52. }
  53. if ( !this->CopyCreateResourceFile("License")
  54. || !this->CopyCreateResourceFile("ReadMe")
  55. || !this->CopyCreateResourceFile("Welcome")
  56. || !this->CopyResourcePlistFile("Info.plist")
  57. || !this->CopyResourcePlistFile("Description.plist") )
  58. {
  59. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem copying the resource files"
  60. << std::endl);
  61. return 0;
  62. }
  63. std::string packageDirFileName
  64. = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  65. packageDirFileName += ".pkg";
  66. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  67. tmpFile += "/PackageMakerOutput.log";
  68. cmOStringStream pkgCmd;
  69. pkgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
  70. << "\" -build -p \"" << packageDirFileName << "\" -f \""
  71. << this->GetOption("CPACK_TEMPORARY_DIRECTORY")
  72. << "\" -r \"" << this->GetOption("CPACK_TOPLEVEL_DIRECTORY")
  73. << "/Resources\" -i \""
  74. << this->GetOption("CPACK_TOPLEVEL_DIRECTORY") << "/Info.plist\" -d \""
  75. << this->GetOption("CPACK_TOPLEVEL_DIRECTORY") << "/Description.plist\"";
  76. if ( this->PackageMakerVersion > 2.0 )
  77. {
  78. pkgCmd << " -v";
  79. }
  80. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Execute: " << pkgCmd.str().c_str()
  81. << std::endl);
  82. std::string output;
  83. int retVal = 1;
  84. //bool res = cmSystemTools::RunSingleCommand(pkgCmd.str().c_str(), &output,
  85. //&retVal, 0, this->GeneratorVerbose, 0);
  86. bool res = true;
  87. retVal = system(pkgCmd.str().c_str());
  88. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Done running package maker"
  89. << std::endl);
  90. if ( !res || retVal )
  91. {
  92. cmGeneratedFileStream ofs(tmpFile.c_str());
  93. ofs << "# Run command: " << pkgCmd.str().c_str() << std::endl
  94. << "# Output:" << std::endl
  95. << output.c_str() << std::endl;
  96. cmCPackLogger(cmCPackLog::LOG_ERROR,
  97. "Problem running PackageMaker command: " << pkgCmd.str().c_str()
  98. << std::endl << "Please check " << tmpFile.c_str() << " for errors"
  99. << std::endl);
  100. return 0;
  101. }
  102. tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  103. tmpFile += "/hdiutilOutput.log";
  104. cmOStringStream dmgCmd;
  105. dmgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM_DISK_IMAGE")
  106. << "\" create -ov -format UDZO -srcfolder \"" << packageDirFileName
  107. << "\" \"" << outFileName << "\"";
  108. res = cmSystemTools::RunSingleCommand(dmgCmd.str().c_str(), &output,
  109. &retVal, 0, this->GeneratorVerbose, 0);
  110. if ( !res || retVal )
  111. {
  112. cmGeneratedFileStream ofs(tmpFile.c_str());
  113. ofs << "# Run command: " << dmgCmd.str().c_str() << std::endl
  114. << "# Output:" << std::endl
  115. << output.c_str() << std::endl;
  116. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running hdiutil command: "
  117. << dmgCmd.str().c_str() << std::endl
  118. << "Please check " << tmpFile.c_str() << " for errors" << std::endl);
  119. return 0;
  120. }
  121. return 1;
  122. }
  123. //----------------------------------------------------------------------
  124. int cmCPackPackageMakerGenerator::InitializeInternal()
  125. {
  126. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  127. "cmCPackPackageMakerGenerator::Initialize()" << std::endl);
  128. std::vector<std::string> path;
  129. std::string pkgPath
  130. = "/Developer/Applications/Utilities/PackageMaker.app/Contents";
  131. std::string versionFile = pkgPath + "/version.plist";
  132. if ( !cmSystemTools::FileExists(versionFile.c_str()) )
  133. {
  134. pkgPath = "/Developer/Applications/PackageMaker.app/Contents";
  135. std::string newVersionFile = pkgPath + "/version.plist";
  136. if ( !cmSystemTools::FileExists(newVersionFile.c_str()) )
  137. {
  138. cmCPackLogger(cmCPackLog::LOG_ERROR,
  139. "Cannot find PackageMaker compiler version file: "
  140. << versionFile.c_str() << " or " << newVersionFile.c_str()
  141. << std::endl);
  142. return 0;
  143. }
  144. versionFile = newVersionFile;
  145. }
  146. std::ifstream ifs(versionFile.c_str());
  147. if ( !ifs )
  148. {
  149. cmCPackLogger(cmCPackLog::LOG_ERROR,
  150. "Cannot open PackageMaker compiler version file" << std::endl);
  151. return 0;
  152. }
  153. // Check the PackageMaker version
  154. cmsys::RegularExpression rexKey("<key>CFBundleShortVersionString</key>");
  155. cmsys::RegularExpression rexVersion("<string>([0-9]+.[0-9.]+)</string>");
  156. std::string line;
  157. bool foundKey = false;
  158. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  159. {
  160. if ( rexKey.find(line) )
  161. {
  162. foundKey = true;
  163. break;
  164. }
  165. }
  166. if ( !foundKey )
  167. {
  168. cmCPackLogger(cmCPackLog::LOG_ERROR,
  169. "Cannot find CFBundleShortVersionString in the PackageMaker compiler "
  170. "version file" << std::endl);
  171. return 0;
  172. }
  173. if ( !cmSystemTools::GetLineFromStream(ifs, line) ||
  174. !rexVersion.find(line) )
  175. {
  176. cmCPackLogger(cmCPackLog::LOG_ERROR,
  177. "Problem reading the PackageMaker compiler version file: "
  178. << versionFile.c_str() << std::endl);
  179. return 0;
  180. }
  181. this->PackageMakerVersion = atof(rexVersion.match(1).c_str());
  182. if ( this->PackageMakerVersion < 1 )
  183. {
  184. cmCPackLogger(cmCPackLog::LOG_ERROR, "Require PackageMaker 1.0 or higher"
  185. << std::endl);
  186. return 0;
  187. }
  188. cmCPackLogger(cmCPackLog::LOG_DEBUG, "PackageMaker version is: "
  189. << this->PackageMakerVersion << std::endl);
  190. pkgPath += "/MacOS";
  191. path.push_back(pkgPath);
  192. pkgPath = cmSystemTools::FindProgram("PackageMaker", path, false);
  193. if ( pkgPath.empty() )
  194. {
  195. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find PackageMaker compiler"
  196. << std::endl);
  197. return 0;
  198. }
  199. this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM", pkgPath.c_str());
  200. pkgPath = cmSystemTools::FindProgram("hdiutil", path, false);
  201. if ( pkgPath.empty() )
  202. {
  203. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find hdiutil compiler"
  204. << std::endl);
  205. return 0;
  206. }
  207. this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM_DISK_IMAGE",
  208. pkgPath.c_str());
  209. return this->Superclass::InitializeInternal();
  210. }
  211. //----------------------------------------------------------------------
  212. bool cmCPackPackageMakerGenerator::CopyCreateResourceFile(const char* name)
  213. {
  214. std::string uname = cmSystemTools::UpperCase(name);
  215. std::string cpackVar = "CPACK_RESOURCE_FILE_" + uname;
  216. const char* inFileName = this->GetOption(cpackVar.c_str());
  217. if ( !inFileName )
  218. {
  219. cmCPackLogger(cmCPackLog::LOG_ERROR, "CPack option: " << cpackVar.c_str()
  220. << " not specified. It should point to "
  221. << (name ? name : "(NULL)")
  222. << ".rtf, " << name
  223. << ".html, or " << name << ".txt file" << std::endl);
  224. return false;
  225. }
  226. if ( !cmSystemTools::FileExists(inFileName) )
  227. {
  228. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find "
  229. << (name ? name : "(NULL)")
  230. << " resource file: " << inFileName << std::endl);
  231. return false;
  232. }
  233. std::string ext = cmSystemTools::GetFilenameLastExtension(inFileName);
  234. if ( ext != ".rtfd" && ext != ".rtf" && ext != ".html" && ext != ".txt" )
  235. {
  236. cmCPackLogger(cmCPackLog::LOG_ERROR, "Bad file extension specified: "
  237. << ext << ". Currently only .rtfd, .rtf, .html, and .txt files allowed."
  238. << std::endl);
  239. return false;
  240. }
  241. std::string destFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  242. destFileName += "/Resources/";
  243. destFileName += name + ext;
  244. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: "
  245. << (inFileName ? inFileName : "(NULL)")
  246. << " to " << destFileName.c_str() << std::endl);
  247. this->ConfigureFile(inFileName, destFileName.c_str());
  248. return true;
  249. }
  250. bool cmCPackPackageMakerGenerator::CopyResourcePlistFile(const char* name)
  251. {
  252. std::string inFName = "CPack.";
  253. inFName += name;
  254. inFName += ".in";
  255. std::string inFileName = this->FindTemplate(inFName.c_str());
  256. if ( inFileName.empty() )
  257. {
  258. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find input file: "
  259. << inFName << std::endl);
  260. return false;
  261. }
  262. std::string destFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  263. destFileName += "/";
  264. destFileName += name;
  265. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: "
  266. << inFileName.c_str() << " to " << destFileName.c_str() << std::endl);
  267. this->ConfigureFile(inFileName.c_str(), destFileName.c_str());
  268. return true;
  269. }