cmCPackProductBuildGenerator.cxx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 "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 (this->IsSet("CPACK_PRODUCTBUILD_RESOURCES_DIR")) {
  54. std::string userResDir =
  55. this->GetOption("CPACK_PRODUCTBUILD_RESOURCES_DIR");
  56. if (!cmSystemTools::CopyADirectory(userResDir, resDir)) {
  57. cmCPackLogger(cmCPackLog::LOG_ERROR,
  58. "Problem copying the resource files" << std::endl);
  59. return 0;
  60. }
  61. }
  62. // Copy or create all of the resource files we need.
  63. if (!this->CopyCreateResourceFile("License", resDir) ||
  64. !this->CopyCreateResourceFile("ReadMe", resDir) ||
  65. !this->CopyCreateResourceFile("Welcome", resDir)) {
  66. cmCPackLogger(cmCPackLog::LOG_ERROR,
  67. "Problem copying the License, ReadMe and Welcome files"
  68. << std::endl);
  69. return 0;
  70. }
  71. // combine package(s) into a distribution
  72. WriteDistributionFile(packageDirFileName.c_str(), "PRODUCTBUILD");
  73. std::ostringstream pkgCmd;
  74. std::string version = this->GetOption("CPACK_PACKAGE_VERSION");
  75. std::string productbuild = this->GetOption("CPACK_COMMAND_PRODUCTBUILD");
  76. std::string identityName;
  77. if (cmValue n = this->GetOption("CPACK_PRODUCTBUILD_IDENTITY_NAME")) {
  78. identityName = n;
  79. }
  80. std::string keychainPath;
  81. if (cmValue p = this->GetOption("CPACK_PRODUCTBUILD_KEYCHAIN_PATH")) {
  82. keychainPath = p;
  83. }
  84. std::string identifier;
  85. if (cmValue i = this->GetOption("CPACK_PRODUCTBUILD_IDENTIFIER")) {
  86. identifier = i;
  87. }
  88. pkgCmd << productbuild << " --distribution \"" << packageDirFileName
  89. << "/Contents/distribution.dist\""
  90. << " --package-path \"" << packageDirFileName << "/Contents/Packages"
  91. << "\""
  92. << " --resources \"" << resDir << "\""
  93. << " --version \"" << version << "\""
  94. << (identifier.empty()
  95. ? std::string{}
  96. : cmStrCat(" --identifier \"", identifier, '"'))
  97. << (identityName.empty() ? std::string{}
  98. : cmStrCat(" --sign \"", identityName, '"'))
  99. << (keychainPath.empty()
  100. ? std::string{}
  101. : cmStrCat(" --keychain \"", keychainPath, '"'))
  102. << " \"" << packageFileNames[0] << "\"";
  103. // Run ProductBuild
  104. return RunProductBuild(pkgCmd.str());
  105. }
  106. int cmCPackProductBuildGenerator::InitializeInternal()
  107. {
  108. this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/Applications");
  109. std::vector<std::string> no_paths;
  110. std::string program =
  111. cmSystemTools::FindProgram("pkgbuild", no_paths, false);
  112. if (program.empty()) {
  113. cmCPackLogger(cmCPackLog::LOG_ERROR,
  114. "Cannot find pkgbuild executable" << std::endl);
  115. return 0;
  116. }
  117. this->SetOptionIfNotSet("CPACK_COMMAND_PKGBUILD", program);
  118. program = cmSystemTools::FindProgram("productbuild", no_paths, false);
  119. if (program.empty()) {
  120. cmCPackLogger(cmCPackLog::LOG_ERROR,
  121. "Cannot find productbuild executable" << std::endl);
  122. return 0;
  123. }
  124. this->SetOptionIfNotSet("CPACK_COMMAND_PRODUCTBUILD", program);
  125. return this->Superclass::InitializeInternal();
  126. }
  127. bool cmCPackProductBuildGenerator::RunProductBuild(const std::string& command)
  128. {
  129. std::string tmpFile = cmStrCat(this->GetOption("CPACK_TOPLEVEL_DIRECTORY"),
  130. "/ProductBuildOutput.log");
  131. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Execute: " << command << std::endl);
  132. std::string output;
  133. int retVal = 1;
  134. bool res = cmSystemTools::RunSingleCommand(
  135. command, &output, &output, &retVal, nullptr, this->GeneratorVerbose,
  136. cmDuration::zero());
  137. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Done running command" << std::endl);
  138. if (!res || retVal) {
  139. cmGeneratedFileStream ofs(tmpFile);
  140. ofs << "# Run command: " << command << std::endl
  141. << "# Output:" << std::endl
  142. << output << std::endl;
  143. cmCPackLogger(cmCPackLog::LOG_ERROR,
  144. "Problem running command: " << command << std::endl
  145. << "Please check " << tmpFile
  146. << " for errors" << std::endl);
  147. return false;
  148. }
  149. return true;
  150. }
  151. bool cmCPackProductBuildGenerator::GenerateComponentPackage(
  152. const std::string& packageFileDir, const std::string& packageFileName,
  153. const std::string& packageDir, const cmCPackComponent* component)
  154. {
  155. std::string packageFile = cmStrCat(packageFileDir, '/', packageFileName);
  156. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  157. "- Building component package: " << packageFile
  158. << std::endl);
  159. const char* comp_name = component ? component->Name.c_str() : nullptr;
  160. cmValue preflight = this->GetComponentScript("PREFLIGHT", comp_name);
  161. cmValue postflight = this->GetComponentScript("POSTFLIGHT", comp_name);
  162. std::string resDir = packageFileDir;
  163. if (component) {
  164. resDir += "/";
  165. resDir += component->Name;
  166. }
  167. std::string scriptDir = cmStrCat(resDir, "/scripts");
  168. if (!cmsys::SystemTools::MakeDirectory(scriptDir.c_str())) {
  169. cmCPackLogger(cmCPackLog::LOG_ERROR,
  170. "Problem creating installer directory: " << scriptDir
  171. << std::endl);
  172. return false;
  173. }
  174. // if preflight, postflight, or postupgrade are set
  175. // then copy them into the script directory and make
  176. // them executable
  177. if (preflight) {
  178. this->CopyInstallScript(scriptDir, preflight, "preinstall");
  179. }
  180. if (postflight) {
  181. this->CopyInstallScript(scriptDir, postflight, "postinstall");
  182. }
  183. // The command that will be used to run ProductBuild
  184. std::ostringstream pkgCmd;
  185. std::string pkgId;
  186. if (cmValue n = this->GetOption("CPACK_PRODUCTBUILD_IDENTIFIER")) {
  187. pkgId = n;
  188. } else {
  189. pkgId = cmStrCat("com.", this->GetOption("CPACK_PACKAGE_VENDOR"), '.',
  190. this->GetOption("CPACK_PACKAGE_NAME"));
  191. }
  192. if (component) {
  193. pkgId += '.';
  194. pkgId += component->Name;
  195. }
  196. std::string version = this->GetOption("CPACK_PACKAGE_VERSION");
  197. std::string pkgbuild = this->GetOption("CPACK_COMMAND_PKGBUILD");
  198. std::string identityName;
  199. if (cmValue n = this->GetOption("CPACK_PKGBUILD_IDENTITY_NAME")) {
  200. identityName = n;
  201. }
  202. std::string keychainPath;
  203. if (cmValue p = this->GetOption("CPACK_PKGBUILD_KEYCHAIN_PATH")) {
  204. keychainPath = p;
  205. }
  206. pkgCmd << pkgbuild << " --root \"" << packageDir << "\""
  207. << " --identifier \"" << pkgId << "\""
  208. << " --scripts \"" << scriptDir << "\""
  209. << " --version \"" << version << "\""
  210. << " --install-location \"/\""
  211. << (identityName.empty() ? std::string{}
  212. : cmStrCat(" --sign \"", identityName, "\""))
  213. << (keychainPath.empty()
  214. ? std::string{}
  215. : cmStrCat(" --keychain \"", keychainPath, "\""))
  216. << " \"" << packageFile << "\"";
  217. if (component && !component->Plist.empty()) {
  218. pkgCmd << " --component-plist \"" << component->Plist << "\"";
  219. }
  220. // Run ProductBuild
  221. return RunProductBuild(pkgCmd.str());
  222. }
  223. cmValue cmCPackProductBuildGenerator::GetComponentScript(
  224. const char* script, const char* component_name)
  225. {
  226. std::string scriptname = cmStrCat("CPACK_", script, "_");
  227. if (component_name) {
  228. scriptname += cmSystemTools::UpperCase(component_name);
  229. scriptname += "_";
  230. }
  231. scriptname += "SCRIPT";
  232. return this->GetOption(scriptname);
  233. }