cmCPackBundleGenerator.cxx 8.1 KB

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