cmCPackProductBuildGenerator.cxx 8.2 KB

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