cmCPackProductBuildGenerator.cxx 8.8 KB

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