cmCPackBundleGenerator.cxx 7.7 KB

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