cmCPackBundleGenerator.cxx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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::PackageFiles()
  45. {
  46. // Get required arguments ...
  47. const std::string cpack_bundle_name = this->GetOption("CPACK_BUNDLE_NAME")
  48. ? this->GetOption("CPACK_BUNDLE_NAME") : "";
  49. if(cpack_bundle_name.empty())
  50. {
  51. cmCPackLogger(cmCPackLog::LOG_ERROR,
  52. "CPACK_BUNDLE_NAME must be set."
  53. << std::endl);
  54. return 0;
  55. }
  56. const std::string cpack_bundle_plist = this->GetOption("CPACK_BUNDLE_PLIST")
  57. ? this->GetOption("CPACK_BUNDLE_PLIST") : "";
  58. if(cpack_bundle_plist.empty())
  59. {
  60. cmCPackLogger(cmCPackLog::LOG_ERROR,
  61. "CPACK_BUNDLE_PLIST must be set."
  62. << std::endl);
  63. return 0;
  64. }
  65. const std::string cpack_bundle_icon = this->GetOption("CPACK_BUNDLE_ICON")
  66. ? this->GetOption("CPACK_BUNDLE_ICON") : "";
  67. if(cpack_bundle_icon.empty())
  68. {
  69. cmCPackLogger(cmCPackLog::LOG_ERROR,
  70. "CPACK_BUNDLE_ICON must be set."
  71. << std::endl);
  72. return 0;
  73. }
  74. // Get optional arguments ...
  75. const std::string cpack_bundle_startup_command =
  76. this->GetOption("CPACK_BUNDLE_STARTUP_COMMAND")
  77. ? this->GetOption("CPACK_BUNDLE_STARTUP_COMMAND") : "";
  78. // The staging directory contains everything that will end-up inside the
  79. // final disk image ...
  80. cmOStringStream staging;
  81. staging << toplevel;
  82. cmOStringStream contents;
  83. contents << staging.str() << "/" << cpack_bundle_name
  84. << ".app/" << "Contents";
  85. cmOStringStream application;
  86. application << contents.str() << "/" << "MacOS";
  87. cmOStringStream resources;
  88. resources << contents.str() << "/" << "Resources";
  89. // Install a required, user-provided bundle metadata file ...
  90. cmOStringStream plist_source;
  91. plist_source << cpack_bundle_plist;
  92. cmOStringStream plist_target;
  93. plist_target << contents.str() << "/" << "Info.plist";
  94. if(!this->CopyFile(plist_source, plist_target))
  95. {
  96. cmCPackLogger(cmCPackLog::LOG_ERROR,
  97. "Error copying plist. Check the value of CPACK_BUNDLE_PLIST."
  98. << std::endl);
  99. return 0;
  100. }
  101. // Install a user-provided bundle icon ...
  102. cmOStringStream icon_source;
  103. icon_source << cpack_bundle_icon;
  104. cmOStringStream icon_target;
  105. icon_target << resources.str() << "/" << cpack_bundle_name << ".icns";
  106. if(!this->CopyFile(icon_source, icon_target))
  107. {
  108. cmCPackLogger(cmCPackLog::LOG_ERROR,
  109. "Error copying bundle icon. Check the value of CPACK_BUNDLE_ICON."
  110. << std::endl);
  111. return 0;
  112. }
  113. // Optionally a user-provided startup command (could be an
  114. // executable or a script) ...
  115. if(!cpack_bundle_startup_command.empty())
  116. {
  117. cmOStringStream command_source;
  118. command_source << cpack_bundle_startup_command;
  119. cmOStringStream command_target;
  120. command_target << application.str() << "/" << cpack_bundle_name;
  121. if(!this->CopyFile(command_source, command_target))
  122. {
  123. cmCPackLogger(cmCPackLog::LOG_ERROR,
  124. "Error copying startup command. "
  125. " Check the value of CPACK_BUNDLE_STARTUP_COMMAND."
  126. << std::endl);
  127. return 0;
  128. }
  129. cmSystemTools::SetPermissions(command_target.str().c_str(), 0777);
  130. }
  131. return this->CreateDMG();
  132. }