cmCPackBundleGenerator.cxx 8.5 KB

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