cmCPackProductBuildGenerator.cxx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 "cmCPackProductBuildGenerator.h"
  4. #include <cstddef>
  5. #include <map>
  6. #include <sstream>
  7. #include "cmCPackComponentGroup.h"
  8. #include "cmCPackLog.h"
  9. #include "cmDuration.h"
  10. #include "cmGeneratedFileStream.h"
  11. #include "cmStringAlgorithms.h"
  12. #include "cmSystemTools.h"
  13. #include "cmValue.h"
  14. cmCPackProductBuildGenerator::cmCPackProductBuildGenerator()
  15. {
  16. this->componentPackageMethod = ONE_PACKAGE;
  17. }
  18. cmCPackProductBuildGenerator::~cmCPackProductBuildGenerator() = default;
  19. int cmCPackProductBuildGenerator::PackageFiles()
  20. {
  21. // TODO: Use toplevel
  22. // It is used! Is this an obsolete comment?
  23. std::string packageDirFileName =
  24. this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  25. // Create the directory where component packages will be built.
  26. std::string basePackageDir =
  27. cmStrCat(packageDirFileName, "/Contents/Packages");
  28. if (!cmsys::SystemTools::MakeDirectory(basePackageDir.c_str())) {
  29. cmCPackLogger(cmCPackLog::LOG_ERROR,
  30. "Problem creating component packages directory: "
  31. << basePackageDir << std::endl);
  32. return 0;
  33. }
  34. if (!this->Components.empty()) {
  35. std::map<std::string, cmCPackComponent>::iterator compIt;
  36. for (compIt = this->Components.begin(); compIt != this->Components.end();
  37. ++compIt) {
  38. std::string packageDir = cmStrCat(toplevel, '/', compIt->first);
  39. if (!this->GenerateComponentPackage(basePackageDir,
  40. GetPackageName(compIt->second),
  41. packageDir, &compIt->second)) {
  42. return 0;
  43. }
  44. }
  45. } else {
  46. if (!this->GenerateComponentPackage(basePackageDir,
  47. this->GetOption("CPACK_PACKAGE_NAME"),
  48. toplevel, nullptr)) {
  49. return 0;
  50. }
  51. }
  52. std::string resDir = cmStrCat(packageDirFileName, "/Contents");
  53. if (cmValue v = this->GetOptionIfSet("CPACK_PRODUCTBUILD_RESOURCES_DIR")) {
  54. std::string userResDir = *v;
  55. if (!cmSystemTools::CopyADirectory(userResDir, resDir)) {
  56. cmCPackLogger(cmCPackLog::LOG_ERROR,
  57. "Problem copying the resource files" << std::endl);
  58. return 0;
  59. }
  60. }
  61. // Copy or create all of the resource files we need.
  62. if (!this->CopyCreateResourceFile("License", resDir) ||
  63. !this->CopyCreateResourceFile("ReadMe", resDir) ||
  64. !this->CopyCreateResourceFile("Welcome", resDir)) {
  65. cmCPackLogger(cmCPackLog::LOG_ERROR,
  66. "Problem copying the License, ReadMe and Welcome files"
  67. << std::endl);
  68. return 0;
  69. }
  70. // combine package(s) into a distribution
  71. WriteDistributionFile(packageDirFileName.c_str(), "PRODUCTBUILD");
  72. std::ostringstream pkgCmd;
  73. std::string version = this->GetOption("CPACK_PACKAGE_VERSION");
  74. std::string productbuild = this->GetOption("CPACK_COMMAND_PRODUCTBUILD");
  75. std::string identityName;
  76. if (cmValue n = this->GetOption("CPACK_PRODUCTBUILD_IDENTITY_NAME")) {
  77. identityName = n;
  78. }
  79. std::string keychainPath;
  80. if (cmValue p = this->GetOption("CPACK_PRODUCTBUILD_KEYCHAIN_PATH")) {
  81. keychainPath = p;
  82. }
  83. std::string identifier;
  84. if (cmValue i = this->GetOption("CPACK_PRODUCTBUILD_IDENTIFIER")) {
  85. identifier = i;
  86. }
  87. pkgCmd << productbuild << " --distribution \"" << packageDirFileName
  88. << "/Contents/distribution.dist\""
  89. " --package-path \""
  90. << packageDirFileName
  91. << "/Contents/Packages"
  92. "\""
  93. " --resources \""
  94. << resDir
  95. << "\""
  96. " --version \""
  97. << version << "\""
  98. << (identifier.empty()
  99. ? std::string{}
  100. : cmStrCat(" --identifier \"", identifier, '"'))
  101. << (identityName.empty() ? std::string{}
  102. : cmStrCat(" --sign \"", identityName, '"'))
  103. << (keychainPath.empty()
  104. ? std::string{}
  105. : cmStrCat(" --keychain \"", keychainPath, '"'))
  106. << " \"" << packageFileNames[0] << '"';
  107. // Run ProductBuild
  108. return RunProductBuild(pkgCmd.str());
  109. }
  110. int cmCPackProductBuildGenerator::InitializeInternal()
  111. {
  112. this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/Applications");
  113. std::string program = cmSystemTools::FindProgram("pkgbuild");
  114. if (program.empty()) {
  115. cmCPackLogger(cmCPackLog::LOG_ERROR,
  116. "Cannot find pkgbuild executable" << std::endl);
  117. return 0;
  118. }
  119. this->SetOptionIfNotSet("CPACK_COMMAND_PKGBUILD", program);
  120. program = cmSystemTools::FindProgram("productbuild");
  121. if (program.empty()) {
  122. cmCPackLogger(cmCPackLog::LOG_ERROR,
  123. "Cannot find productbuild executable" << std::endl);
  124. return 0;
  125. }
  126. this->SetOptionIfNotSet("CPACK_COMMAND_PRODUCTBUILD", program);
  127. return this->Superclass::InitializeInternal();
  128. }
  129. bool cmCPackProductBuildGenerator::RunProductBuild(std::string const& command)
  130. {
  131. std::string tmpFile = cmStrCat(this->GetOption("CPACK_TOPLEVEL_DIRECTORY"),
  132. "/ProductBuildOutput.log");
  133. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Execute: " << command << std::endl);
  134. std::string output;
  135. int retVal = 1;
  136. bool res = cmSystemTools::RunSingleCommand(
  137. command, &output, &output, &retVal, nullptr, this->GeneratorVerbose,
  138. cmDuration::zero());
  139. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Done running command" << std::endl);
  140. if (!res || retVal) {
  141. cmGeneratedFileStream ofs(tmpFile);
  142. ofs << "# Run command: " << command << std::endl
  143. << "# Output:" << std::endl
  144. << output << std::endl;
  145. cmCPackLogger(cmCPackLog::LOG_ERROR,
  146. "Problem running command: " << command << std::endl
  147. << "Please check " << tmpFile
  148. << " for errors" << std::endl);
  149. return false;
  150. }
  151. return true;
  152. }
  153. bool cmCPackProductBuildGenerator::GenerateComponentPackage(
  154. std::string const& packageFileDir, std::string const& packageFileName,
  155. std::string const& packageDir, cmCPackComponent const* component)
  156. {
  157. std::string packageFile = cmStrCat(packageFileDir, '/', packageFileName);
  158. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  159. "- Building component package: " << packageFile
  160. << std::endl);
  161. char const* comp_name = component ? component->Name.c_str() : nullptr;
  162. cmValue preflight = this->GetComponentScript("PREFLIGHT", comp_name);
  163. cmValue postflight = this->GetComponentScript("POSTFLIGHT", comp_name);
  164. std::string resDir = packageFileDir;
  165. if (component) {
  166. resDir += '/';
  167. resDir += component->Name;
  168. }
  169. std::string scriptDir = cmStrCat(resDir, "/scripts");
  170. if (!cmsys::SystemTools::MakeDirectory(scriptDir.c_str())) {
  171. cmCPackLogger(cmCPackLog::LOG_ERROR,
  172. "Problem creating installer directory: " << scriptDir
  173. << std::endl);
  174. return false;
  175. }
  176. // if preflight, postflight, or postupgrade are set
  177. // then copy them into the script directory and make
  178. // them executable
  179. if (preflight) {
  180. this->CopyInstallScript(scriptDir, preflight, "preinstall");
  181. }
  182. if (postflight) {
  183. this->CopyInstallScript(scriptDir, postflight, "postinstall");
  184. }
  185. // The command that will be used to run ProductBuild
  186. std::ostringstream pkgCmd;
  187. std::string pkgId;
  188. if (cmValue n = this->GetOption("CPACK_PRODUCTBUILD_IDENTIFIER")) {
  189. pkgId = n;
  190. } else {
  191. pkgId = cmStrCat("com.", this->GetOption("CPACK_PACKAGE_VENDOR"), '.',
  192. this->GetOption("CPACK_PACKAGE_NAME"));
  193. }
  194. if (component) {
  195. pkgId += '.';
  196. pkgId += component->Name;
  197. }
  198. std::string version = this->GetOption("CPACK_PACKAGE_VERSION");
  199. std::string pkgbuild = this->GetOption("CPACK_COMMAND_PKGBUILD");
  200. std::string identityName;
  201. if (cmValue n = this->GetOption("CPACK_PKGBUILD_IDENTITY_NAME")) {
  202. identityName = n;
  203. }
  204. std::string keychainPath;
  205. if (cmValue p = this->GetOption("CPACK_PKGBUILD_KEYCHAIN_PATH")) {
  206. keychainPath = p;
  207. }
  208. pkgCmd << pkgbuild << " --root \"" << packageDir
  209. << "\""
  210. " --identifier \""
  211. << pkgId
  212. << "\""
  213. " --scripts \""
  214. << scriptDir
  215. << "\""
  216. " --version \""
  217. << version
  218. << "\""
  219. " --install-location \"/\""
  220. << (identityName.empty() ? std::string{}
  221. : cmStrCat(" --sign \"", identityName, '"'))
  222. << (keychainPath.empty()
  223. ? std::string{}
  224. : cmStrCat(" --keychain \"", keychainPath, '"'))
  225. << " \"" << packageFile << '"';
  226. if (component && !component->Plist.empty()) {
  227. pkgCmd << " --component-plist \"" << component->Plist << "\"";
  228. }
  229. // Run ProductBuild
  230. return RunProductBuild(pkgCmd.str());
  231. }
  232. cmValue cmCPackProductBuildGenerator::GetComponentScript(
  233. char const* script, char const* component_name)
  234. {
  235. std::string scriptname = cmStrCat("CPACK_", script, '_');
  236. if (component_name) {
  237. scriptname += cmSystemTools::UpperCase(component_name);
  238. scriptname += '_';
  239. }
  240. scriptname += "SCRIPT";
  241. return this->GetOption(scriptname);
  242. }