cmCPackBundleGenerator.cxx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. cmCPackBundleGenerator::cmCPackBundleGenerator()
  15. {
  16. }
  17. cmCPackBundleGenerator::~cmCPackBundleGenerator()
  18. {
  19. }
  20. int cmCPackBundleGenerator::InitializeInternal()
  21. {
  22. const char* name = this->GetOption("CPACK_BUNDLE_NAME");
  23. if (0 == name) {
  24. cmCPackLogger(cmCPackLog::LOG_ERROR,
  25. "CPACK_BUNDLE_NAME must be set to use the Bundle generator."
  26. << std::endl);
  27. return 0;
  28. }
  29. if (this->GetOption("CPACK_BUNDLE_APPLE_CERT_APP")) {
  30. const std::string codesign_path = cmSystemTools::FindProgram(
  31. "codesign", std::vector<std::string>(), false);
  32. if (codesign_path.empty()) {
  33. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot locate codesign command"
  34. << std::endl);
  35. return 0;
  36. }
  37. this->SetOptionIfNotSet("CPACK_COMMAND_CODESIGN", codesign_path.c_str());
  38. }
  39. return this->Superclass::InitializeInternal();
  40. }
  41. const char* cmCPackBundleGenerator::GetPackagingInstallPrefix()
  42. {
  43. this->InstallPrefix = "/";
  44. this->InstallPrefix += this->GetOption("CPACK_BUNDLE_NAME");
  45. this->InstallPrefix += ".app/Contents/Resources";
  46. return this->InstallPrefix.c_str();
  47. }
  48. int cmCPackBundleGenerator::ConstructBundle()
  49. {
  50. // Get required arguments ...
  51. const std::string cpack_bundle_name = this->GetOption("CPACK_BUNDLE_NAME")
  52. ? this->GetOption("CPACK_BUNDLE_NAME")
  53. : "";
  54. if (cpack_bundle_name.empty()) {
  55. cmCPackLogger(cmCPackLog::LOG_ERROR, "CPACK_BUNDLE_NAME must be set."
  56. << std::endl);
  57. return 0;
  58. }
  59. const std::string cpack_bundle_plist = this->GetOption("CPACK_BUNDLE_PLIST")
  60. ? this->GetOption("CPACK_BUNDLE_PLIST")
  61. : "";
  62. if (cpack_bundle_plist.empty()) {
  63. cmCPackLogger(cmCPackLog::LOG_ERROR, "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. : "";
  70. if (cpack_bundle_icon.empty()) {
  71. cmCPackLogger(cmCPackLog::LOG_ERROR, "CPACK_BUNDLE_ICON must be set."
  72. << std::endl);
  73. return 0;
  74. }
  75. // Get optional arguments ...
  76. const std::string cpack_bundle_startup_command =
  77. this->GetOption("CPACK_BUNDLE_STARTUP_COMMAND")
  78. ? this->GetOption("CPACK_BUNDLE_STARTUP_COMMAND")
  79. : "";
  80. // The staging directory contains everything that will end-up inside the
  81. // final disk image ...
  82. std::ostringstream staging;
  83. staging << toplevel;
  84. std::ostringstream contents;
  85. contents << staging.str() << "/" << cpack_bundle_name << ".app/"
  86. << "Contents";
  87. std::ostringstream application;
  88. application << contents.str() << "/"
  89. << "MacOS";
  90. std::ostringstream resources;
  91. resources << contents.str() << "/"
  92. << "Resources";
  93. // Install a required, user-provided bundle metadata file ...
  94. std::ostringstream plist_source;
  95. plist_source << cpack_bundle_plist;
  96. std::ostringstream plist_target;
  97. plist_target << contents.str() << "/"
  98. << "Info.plist";
  99. if (!this->CopyFile(plist_source, plist_target)) {
  100. cmCPackLogger(
  101. cmCPackLog::LOG_ERROR,
  102. "Error copying plist. Check the value of CPACK_BUNDLE_PLIST."
  103. << std::endl);
  104. return 0;
  105. }
  106. // Install a user-provided bundle icon ...
  107. std::ostringstream icon_source;
  108. icon_source << cpack_bundle_icon;
  109. std::ostringstream icon_target;
  110. icon_target << resources.str() << "/" << cpack_bundle_name << ".icns";
  111. if (!this->CopyFile(icon_source, icon_target)) {
  112. cmCPackLogger(
  113. cmCPackLog::LOG_ERROR,
  114. "Error copying bundle icon. Check the value of CPACK_BUNDLE_ICON."
  115. << std::endl);
  116. return 0;
  117. }
  118. // Optionally a user-provided startup command (could be an
  119. // executable or a script) ...
  120. if (!cpack_bundle_startup_command.empty()) {
  121. std::ostringstream command_source;
  122. command_source << cpack_bundle_startup_command;
  123. std::ostringstream command_target;
  124. command_target << application.str() << "/" << cpack_bundle_name;
  125. if (!this->CopyFile(command_source, command_target)) {
  126. cmCPackLogger(cmCPackLog::LOG_ERROR,
  127. "Error copying startup command. "
  128. " Check the value of CPACK_BUNDLE_STARTUP_COMMAND."
  129. << std::endl);
  130. return 0;
  131. }
  132. cmSystemTools::SetPermissions(command_target.str().c_str(), 0777);
  133. }
  134. return 1;
  135. }
  136. int cmCPackBundleGenerator::PackageFiles()
  137. {
  138. if (!this->ConstructBundle()) {
  139. return 0;
  140. }
  141. if (!this->SignBundle(toplevel)) {
  142. return 0;
  143. }
  144. return this->CreateDMG(toplevel, packageFileNames[0]);
  145. }
  146. bool cmCPackBundleGenerator::SupportsComponentInstallation() const
  147. {
  148. return false;
  149. }
  150. int cmCPackBundleGenerator::SignBundle(const std::string& src_dir)
  151. {
  152. const std::string cpack_apple_cert_app =
  153. this->GetOption("CPACK_BUNDLE_APPLE_CERT_APP")
  154. ? this->GetOption("CPACK_BUNDLE_APPLE_CERT_APP")
  155. : "";
  156. // codesign the application.
  157. if (!cpack_apple_cert_app.empty()) {
  158. std::string output;
  159. std::string bundle_path;
  160. bundle_path = src_dir + "/";
  161. bundle_path += this->GetOption("CPACK_BUNDLE_NAME");
  162. bundle_path += ".app";
  163. // A list of additional files to sign, ie. frameworks and plugins.
  164. const std::string sign_parameter =
  165. this->GetOption("CPACK_BUNDLE_APPLE_CODESIGN_PARAMETER")
  166. ? this->GetOption("CPACK_BUNDLE_APPLE_CODESIGN_PARAMETER")
  167. : "--deep -f";
  168. const std::string sign_files =
  169. this->GetOption("CPACK_BUNDLE_APPLE_CODESIGN_FILES")
  170. ? this->GetOption("CPACK_BUNDLE_APPLE_CODESIGN_FILES")
  171. : "";
  172. std::vector<std::string> relFiles;
  173. cmSystemTools::ExpandListArgument(sign_files, relFiles);
  174. // sign the files supplied by the user, ie. frameworks.
  175. for (std::vector<std::string>::iterator it = relFiles.begin();
  176. it != relFiles.end(); ++it) {
  177. std::ostringstream temp_sign_file_cmd;
  178. temp_sign_file_cmd << this->GetOption("CPACK_COMMAND_CODESIGN");
  179. temp_sign_file_cmd << " " << sign_parameter << " -s \""
  180. << cpack_apple_cert_app;
  181. temp_sign_file_cmd << "\" -i ";
  182. temp_sign_file_cmd << this->GetOption("CPACK_APPLE_BUNDLE_ID");
  183. temp_sign_file_cmd << " \"";
  184. temp_sign_file_cmd << bundle_path;
  185. temp_sign_file_cmd << *it << "\"";
  186. if (!this->RunCommand(temp_sign_file_cmd, &output)) {
  187. cmCPackLogger(cmCPackLog::LOG_ERROR,
  188. "Error signing file:" << bundle_path << *it << std::endl
  189. << output << std::endl);
  190. return 0;
  191. }
  192. }
  193. // sign main binary
  194. std::ostringstream temp_sign_binary_cmd;
  195. temp_sign_binary_cmd << this->GetOption("CPACK_COMMAND_CODESIGN");
  196. temp_sign_binary_cmd << " " << sign_parameter << " -s \""
  197. << cpack_apple_cert_app;
  198. temp_sign_binary_cmd << "\" \"" << bundle_path << "\"";
  199. if (!this->RunCommand(temp_sign_binary_cmd, &output)) {
  200. cmCPackLogger(cmCPackLog::LOG_ERROR,
  201. "Error signing the application binary." << std::endl
  202. << output
  203. << std::endl);
  204. return 0;
  205. }
  206. // sign app bundle
  207. std::ostringstream temp_codesign_cmd;
  208. temp_codesign_cmd << this->GetOption("CPACK_COMMAND_CODESIGN");
  209. temp_codesign_cmd << " " << sign_parameter << " -s \""
  210. << cpack_apple_cert_app << "\"";
  211. if (this->GetOption("CPACK_BUNDLE_APPLE_ENTITLEMENTS")) {
  212. temp_codesign_cmd << " --entitlements ";
  213. temp_codesign_cmd << this->GetOption("CPACK_BUNDLE_APPLE_ENTITLEMENTS");
  214. }
  215. temp_codesign_cmd << " \"" << bundle_path << "\"";
  216. if (!this->RunCommand(temp_codesign_cmd, &output)) {
  217. cmCPackLogger(cmCPackLog::LOG_ERROR,
  218. "Error signing the application package." << std::endl
  219. << output
  220. << std::endl);
  221. return 0;
  222. }
  223. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "- Application has been codesigned"
  224. << std::endl);
  225. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  226. (this->GetOption("CPACK_BUNDLE_APPLE_ENTITLEMENTS")
  227. ? "with entitlement sandboxing"
  228. : "without entitlement sandboxing")
  229. << std::endl);
  230. }
  231. return 1;
  232. }