cmCPackProductBuildGenerator.cxx 8.7 KB

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