cmCPackBundleGenerator.cxx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCPackBundleGenerator.h"
  11. #include "cmCPackLog.h"
  12. #include "cmSystemTools.h"
  13. #include <cmsys/RegularExpression.hxx>
  14. //----------------------------------------------------------------------
  15. cmCPackBundleGenerator::cmCPackBundleGenerator()
  16. {
  17. }
  18. //----------------------------------------------------------------------
  19. cmCPackBundleGenerator::~cmCPackBundleGenerator()
  20. {
  21. }
  22. //----------------------------------------------------------------------
  23. int cmCPackBundleGenerator::InitializeInternal()
  24. {
  25. const char* name = this->GetOption("CPACK_BUNDLE_NAME");
  26. if(0 == name)
  27. {
  28. cmCPackLogger(cmCPackLog::LOG_ERROR,
  29. "CPACK_BUNDLE_NAME must be set to use the Bundle generator."
  30. << std::endl);
  31. return 0;
  32. }
  33. return this->Superclass::InitializeInternal();
  34. }
  35. //----------------------------------------------------------------------
  36. const char* cmCPackBundleGenerator::GetPackagingInstallPrefix()
  37. {
  38. this->InstallPrefix = "/";
  39. this->InstallPrefix += this->GetOption("CPACK_BUNDLE_NAME");
  40. this->InstallPrefix += ".app/Contents/Resources";
  41. return this->InstallPrefix.c_str();
  42. }
  43. //----------------------------------------------------------------------
  44. int cmCPackBundleGenerator::CompressFiles(const char* outFileName,
  45. const char* toplevel, const std::vector<std::string>& files)
  46. {
  47. (void) files;
  48. // Get required arguments ...
  49. const std::string cpack_bundle_name = this->GetOption("CPACK_BUNDLE_NAME")
  50. ? this->GetOption("CPACK_BUNDLE_NAME") : "";
  51. if(cpack_bundle_name.empty())
  52. {
  53. cmCPackLogger(cmCPackLog::LOG_ERROR,
  54. "CPACK_BUNDLE_NAME must be set."
  55. << std::endl);
  56. return 0;
  57. }
  58. const std::string cpack_bundle_plist = this->GetOption("CPACK_BUNDLE_PLIST")
  59. ? this->GetOption("CPACK_BUNDLE_PLIST") : "";
  60. if(cpack_bundle_plist.empty())
  61. {
  62. cmCPackLogger(cmCPackLog::LOG_ERROR,
  63. "CPACK_BUNDLE_PLIST must be set."
  64. << std::endl);
  65. return 0;
  66. }
  67. const std::string cpack_bundle_icon = this->GetOption("CPACK_BUNDLE_ICON")
  68. ? this->GetOption("CPACK_BUNDLE_ICON") : "";
  69. if(cpack_bundle_icon.empty())
  70. {
  71. cmCPackLogger(cmCPackLog::LOG_ERROR,
  72. "CPACK_BUNDLE_ICON must be set."
  73. << std::endl);
  74. return 0;
  75. }
  76. // Get optional arguments ...
  77. const std::string cpack_bundle_startup_command =
  78. this->GetOption("CPACK_BUNDLE_STARTUP_COMMAND")
  79. ? this->GetOption("CPACK_BUNDLE_STARTUP_COMMAND") : "";
  80. // The staging directory contains everything that will end-up inside the
  81. // final disk image ...
  82. cmOStringStream staging;
  83. staging << toplevel;
  84. cmOStringStream contents;
  85. contents << staging.str() << "/" << cpack_bundle_name
  86. << ".app/" << "Contents";
  87. cmOStringStream application;
  88. application << contents.str() << "/" << "MacOS";
  89. cmOStringStream resources;
  90. resources << contents.str() << "/" << "Resources";
  91. // Install a required, user-provided bundle metadata file ...
  92. cmOStringStream plist_source;
  93. plist_source << cpack_bundle_plist;
  94. cmOStringStream plist_target;
  95. plist_target << contents.str() << "/" << "Info.plist";
  96. if(!this->CopyFile(plist_source, plist_target))
  97. {
  98. cmCPackLogger(cmCPackLog::LOG_ERROR,
  99. "Error copying plist. Check the value of CPACK_BUNDLE_PLIST."
  100. << std::endl);
  101. return 0;
  102. }
  103. // Install a user-provided bundle icon ...
  104. cmOStringStream icon_source;
  105. icon_source << cpack_bundle_icon;
  106. cmOStringStream icon_target;
  107. icon_target << resources.str() << "/" << cpack_bundle_name << ".icns";
  108. if(!this->CopyFile(icon_source, icon_target))
  109. {
  110. cmCPackLogger(cmCPackLog::LOG_ERROR,
  111. "Error copying bundle icon. Check the value of CPACK_BUNDLE_ICON."
  112. << std::endl);
  113. return 0;
  114. }
  115. // Optionally a user-provided startup command (could be an
  116. // executable or a script) ...
  117. if(!cpack_bundle_startup_command.empty())
  118. {
  119. cmOStringStream command_source;
  120. command_source << cpack_bundle_startup_command;
  121. cmOStringStream command_target;
  122. command_target << application.str() << "/" << cpack_bundle_name;
  123. if(!this->CopyFile(command_source, command_target))
  124. {
  125. cmCPackLogger(cmCPackLog::LOG_ERROR,
  126. "Error copying startup command. "
  127. " Check the value of CPACK_BUNDLE_STARTUP_COMMAND."
  128. << std::endl);
  129. return 0;
  130. }
  131. cmSystemTools::SetPermissions(command_target.str().c_str(), 0777);
  132. }
  133. return this->CreateDMG(toplevel, outFileName);
  134. }