cmCPackBundleGenerator.cxx 8.1 KB

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