cmCPackProductBuildGenerator.cxx 8.8 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 "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, nullptr)) {
  50. return 0;
  51. }
  52. }
  53. std::string resDir = packageDirFileName + "/Contents";
  54. if (this->IsSet("CPACK_PRODUCTBUILD_RESOURCES_DIR")) {
  55. std::string userResDir =
  56. this->GetOption("CPACK_PRODUCTBUILD_RESOURCES_DIR");
  57. if (!cmSystemTools::CopyADirectory(userResDir, resDir)) {
  58. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem copying the resource files"
  59. << std::endl);
  60. return 0;
  61. }
  62. }
  63. // Copy or create all of the resource files we need.
  64. if (!this->CopyCreateResourceFile("License", resDir) ||
  65. !this->CopyCreateResourceFile("ReadMe", resDir) ||
  66. !this->CopyCreateResourceFile("Welcome", resDir)) {
  67. cmCPackLogger(cmCPackLog::LOG_ERROR,
  68. "Problem copying the License, ReadMe and Welcome files"
  69. << std::endl);
  70. return 0;
  71. }
  72. // combine package(s) into a distribution
  73. WriteDistributionFile(packageDirFileName.c_str());
  74. std::ostringstream pkgCmd;
  75. std::string version = this->GetOption("CPACK_PACKAGE_VERSION");
  76. std::string productbuild = this->GetOption("CPACK_COMMAND_PRODUCTBUILD");
  77. std::string identityName;
  78. if (const char* n = this->GetOption("CPACK_PRODUCTBUILD_IDENTITY_NAME")) {
  79. identityName = n;
  80. }
  81. std::string keychainPath;
  82. if (const char* p = this->GetOption("CPACK_PRODUCTBUILD_KEYCHAIN_PATH")) {
  83. keychainPath = p;
  84. }
  85. pkgCmd << productbuild << " --distribution \"" << packageDirFileName
  86. << "/Contents/distribution.dist\""
  87. << " --package-path \"" << packageDirFileName << "/Contents/Packages"
  88. << "\""
  89. << " --resources \"" << resDir << "\""
  90. << " --version \"" << version << "\""
  91. << (identityName.empty() ? "" : " --sign \"" + identityName + "\"")
  92. << (keychainPath.empty() ? ""
  93. : " --keychain \"" + keychainPath + "\"")
  94. << " \"" << packageFileNames[0] << "\"";
  95. // Run ProductBuild
  96. return RunProductBuild(pkgCmd.str());
  97. }
  98. int cmCPackProductBuildGenerator::InitializeInternal()
  99. {
  100. this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/Applications");
  101. std::vector<std::string> no_paths;
  102. std::string program =
  103. cmSystemTools::FindProgram("pkgbuild", no_paths, false);
  104. if (program.empty()) {
  105. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find pkgbuild executable"
  106. << std::endl);
  107. return 0;
  108. }
  109. this->SetOptionIfNotSet("CPACK_COMMAND_PKGBUILD", program.c_str());
  110. program = cmSystemTools::FindProgram("productbuild", no_paths, false);
  111. if (program.empty()) {
  112. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find productbuild executable"
  113. << std::endl);
  114. return 0;
  115. }
  116. this->SetOptionIfNotSet("CPACK_COMMAND_PRODUCTBUILD", program.c_str());
  117. return this->Superclass::InitializeInternal();
  118. }
  119. bool cmCPackProductBuildGenerator::RunProductBuild(const std::string& command)
  120. {
  121. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  122. tmpFile += "/ProductBuildOutput.log";
  123. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Execute: " << command << std::endl);
  124. std::string output, error_output;
  125. int retVal = 1;
  126. bool res = cmSystemTools::RunSingleCommand(command.c_str(), &output,
  127. &error_output, &retVal, nullptr,
  128. this->GeneratorVerbose, 0);
  129. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Done running command" << std::endl);
  130. if (!res || retVal) {
  131. cmGeneratedFileStream ofs(tmpFile.c_str());
  132. ofs << "# Run command: " << command << std::endl
  133. << "# Output:" << std::endl
  134. << output << std::endl;
  135. cmCPackLogger(cmCPackLog::LOG_ERROR,
  136. "Problem running command: " << command << std::endl
  137. << "Please check " << tmpFile
  138. << " for errors" << std::endl);
  139. return false;
  140. }
  141. return true;
  142. }
  143. bool cmCPackProductBuildGenerator::GenerateComponentPackage(
  144. const std::string& packageFileDir, const std::string& packageFileName,
  145. const std::string& packageDir, const cmCPackComponent* component)
  146. {
  147. std::string packageFile = packageFileDir;
  148. packageFile += '/';
  149. packageFile += packageFileName;
  150. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "- Building component package: "
  151. << packageFile << 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. }