cmCPackBundleGenerator.cxx 8.6 KB

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