cmCPackBundleGenerator.cxx 8.5 KB

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