cmCPackPackageMakerGenerator.cxx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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::Initialize(const char* name, cmMakefile* mf)
  125. {
  126. int res = this->Superclass::Initialize(name, mf);
  127. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  128. "cmCPackPackageMakerGenerator::Initialize()" << std::endl);
  129. std::vector<std::string> path;
  130. std::string pkgPath
  131. = "/Developer/Applications/Utilities/PackageMaker.app/Contents";
  132. std::string versionFile = pkgPath + "/version.plist";
  133. pkgPath += "/MacOS";
  134. if ( !cmSystemTools::FileExists(versionFile.c_str()) )
  135. {
  136. cmCPackLogger(cmCPackLog::LOG_ERROR,
  137. "Cannot find PackageMaker compiler version file: "
  138. << versionFile.c_str() << std::endl);
  139. return 0;
  140. }
  141. std::ifstream ifs(versionFile.c_str());
  142. if ( !ifs )
  143. {
  144. cmCPackLogger(cmCPackLog::LOG_ERROR,
  145. "Cannot open PackageMaker compiler version file" << std::endl);
  146. return 0;
  147. }
  148. // Check the PackageMaker version
  149. cmsys::RegularExpression rexKey("<key>CFBundleShortVersionString</key>");
  150. cmsys::RegularExpression rexVersion("<string>([0-9]+.[0-9.]+)</string>");
  151. std::string line;
  152. bool foundKey = false;
  153. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  154. {
  155. if ( rexKey.find(line) )
  156. {
  157. foundKey = true;
  158. break;
  159. }
  160. }
  161. if ( !foundKey )
  162. {
  163. cmCPackLogger(cmCPackLog::LOG_ERROR,
  164. "Cannot find CFBundleShortVersionString in the PackageMaker compiler "
  165. "version file" << std::endl);
  166. return 0;
  167. }
  168. if ( !cmSystemTools::GetLineFromStream(ifs, line) ||
  169. !rexVersion.find(line) )
  170. {
  171. cmCPackLogger(cmCPackLog::LOG_ERROR,
  172. "Problem reading the PackageMaker compiler version file: "
  173. << versionFile.c_str() << std::endl);
  174. return 0;
  175. }
  176. this->PackageMakerVersion = atof(rexVersion.match(1).c_str());
  177. if ( this->PackageMakerVersion < 1 )
  178. {
  179. cmCPackLogger(cmCPackLog::LOG_ERROR, "Require PackageMaker 1.0 or higher"
  180. << std::endl);
  181. return 0;
  182. }
  183. cmCPackLogger(cmCPackLog::LOG_DEBUG, "PackageMaker version is: "
  184. << this->PackageMakerVersion << std::endl);
  185. path.push_back(pkgPath);
  186. pkgPath = cmSystemTools::FindProgram("PackageMaker", path, false);
  187. if ( pkgPath.empty() )
  188. {
  189. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find PackageMaker compiler"
  190. << std::endl);
  191. return 0;
  192. }
  193. this->SetOption("CPACK_INSTALLER_PROGRAM", pkgPath.c_str());
  194. pkgPath = cmSystemTools::FindProgram("hdiutil", path, false);
  195. if ( pkgPath.empty() )
  196. {
  197. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find hdiutil compiler"
  198. << std::endl);
  199. return 0;
  200. }
  201. this->SetOption("CPACK_INSTALLER_PROGRAM_DISK_IMAGE", pkgPath.c_str());
  202. return res;
  203. }
  204. //----------------------------------------------------------------------
  205. bool cmCPackPackageMakerGenerator::CopyCreateResourceFile(const char* name)
  206. {
  207. std::string uname = cmSystemTools::UpperCase(name);
  208. std::string cpackVar = "CPACK_RESOURCE_FILE_" + uname;
  209. const char* inFileName = this->GetOption(cpackVar.c_str());
  210. if ( !inFileName )
  211. {
  212. cmCPackLogger(cmCPackLog::LOG_ERROR, "CPack option: " << cpackVar.c_str()
  213. << " not specified. It should point to " << name << ".rtf, " << name
  214. << ".html, or " << name << ".txt file" << std::endl);
  215. return false;
  216. }
  217. if ( !cmSystemTools::FileExists(inFileName) )
  218. {
  219. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find " << name
  220. << " resource file: " << inFileName << std::endl);
  221. return false;
  222. }
  223. std::string ext = cmSystemTools::GetFilenameLastExtension(inFileName);
  224. if ( ext != ".rtfd" && ext != ".rtf" && ext != ".html" && ext != ".txt" )
  225. {
  226. cmCPackLogger(cmCPackLog::LOG_ERROR, "Bad file extension specified: "
  227. << ext << ". Currently only .rtfd, .rtf, .html, and .txt files allowed."
  228. << std::endl);
  229. return false;
  230. }
  231. std::string destFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  232. destFileName += "/Resources/";
  233. destFileName += name + ext;
  234. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: " << inFileName
  235. << " to " << destFileName.c_str() << std::endl);
  236. this->ConfigureFile(inFileName, destFileName.c_str());
  237. return true;
  238. }
  239. bool cmCPackPackageMakerGenerator::CopyResourcePlistFile(const char* name)
  240. {
  241. std::string inFName = "CPack.";
  242. inFName += name;
  243. inFName += ".in";
  244. std::string inFileName = this->FindTemplate(inFName.c_str());
  245. if ( inFileName.empty() )
  246. {
  247. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find input file: "
  248. << inFName << std::endl);
  249. return false;
  250. }
  251. std::string destFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  252. destFileName += "/";
  253. destFileName += name;
  254. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Configure file: "
  255. << inFileName.c_str() << " to " << destFileName.c_str() << std::endl);
  256. this->ConfigureFile(inFileName.c_str(), destFileName.c_str());
  257. return true;
  258. }