cmCPackBundleGenerator.cxx 8.5 KB

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