cmCPackBundleGenerator.cxx 8.5 KB

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