cmCPackBundleGenerator.cxx 7.8 KB

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