cmCPackBundleGenerator.cxx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 output;
  172. std::string bundle_path;
  173. bundle_path = src_dir + "/";
  174. bundle_path += this->GetOption("CPACK_BUNDLE_NAME");
  175. bundle_path += ".app";
  176. // A list of additional files to sign, ie. frameworks and plugins.
  177. const std::string sign_parameter =
  178. this->GetOption("CPACK_BUNDLE_APPLE_CODESIGN_PARAMETER")
  179. ? this->GetOption("CPACK_BUNDLE_APPLE_CODESIGN_PARAMETER")
  180. : "--deep -f";
  181. const std::string sign_files =
  182. this->GetOption("CPACK_BUNDLE_APPLE_CODESIGN_FILES")
  183. ? this->GetOption("CPACK_BUNDLE_APPLE_CODESIGN_FILES") : "";
  184. std::vector<std::string> relFiles;
  185. cmSystemTools::ExpandListArgument(sign_files, relFiles);
  186. // sign the files supplied by the user, ie. frameworks.
  187. for(std::vector<std::string>::iterator it = relFiles.begin();
  188. it != relFiles.end(); ++it)
  189. {
  190. std::ostringstream temp_sign_file_cmd;
  191. temp_sign_file_cmd << this->GetOption("CPACK_COMMAND_CODESIGN");
  192. temp_sign_file_cmd << " " << sign_parameter << " -s \""
  193. << cpack_apple_cert_app;
  194. temp_sign_file_cmd << "\" -i ";
  195. temp_sign_file_cmd << this->GetOption("CPACK_APPLE_BUNDLE_ID");
  196. temp_sign_file_cmd << " \"";
  197. temp_sign_file_cmd << bundle_path;
  198. temp_sign_file_cmd << it->c_str() << "\"";
  199. if(!this->RunCommand(temp_sign_file_cmd, &output))
  200. {
  201. cmCPackLogger(cmCPackLog::LOG_ERROR,
  202. "Error signing file:"
  203. << bundle_path << it->c_str() << std::endl << output << std::endl);
  204. return 0;
  205. }
  206. }
  207. // sign main binary
  208. std::ostringstream temp_sign_binary_cmd;
  209. temp_sign_binary_cmd << this->GetOption("CPACK_COMMAND_CODESIGN");
  210. temp_sign_binary_cmd << " " << sign_parameter << " -s \""
  211. << cpack_apple_cert_app;
  212. temp_sign_binary_cmd << "\" \"" << bundle_path << "\"";
  213. if(!this->RunCommand(temp_sign_binary_cmd, &output))
  214. {
  215. cmCPackLogger(cmCPackLog::LOG_ERROR,
  216. "Error signing the application binary."
  217. << std::endl << output << std::endl);
  218. return 0;
  219. }
  220. // sign app bundle
  221. std::ostringstream temp_codesign_cmd;
  222. temp_codesign_cmd << this->GetOption("CPACK_COMMAND_CODESIGN");
  223. temp_codesign_cmd << " " << sign_parameter << " -s \""
  224. << cpack_apple_cert_app << "\"";
  225. if(this->GetOption("CPACK_BUNDLE_APPLE_ENTITLEMENTS"))
  226. {
  227. temp_codesign_cmd << " --entitlements ";
  228. temp_codesign_cmd << this->GetOption("CPACK_BUNDLE_APPLE_ENTITLEMENTS");
  229. }
  230. temp_codesign_cmd << " \"" << bundle_path << "\"";
  231. if(!this->RunCommand(temp_codesign_cmd, &output))
  232. {
  233. cmCPackLogger(cmCPackLog::LOG_ERROR,
  234. "Error signing the application package."
  235. << std::endl << output << std::endl);
  236. return 0;
  237. }
  238. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  239. "- Application has been codesigned"
  240. << std::endl);
  241. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  242. (this->GetOption("CPACK_BUNDLE_APPLE_ENTITLEMENTS")
  243. ? "with entitlement sandboxing" : "without entitlement sandboxing")
  244. << std::endl);
  245. }
  246. return 1;
  247. }