cmCPackProductBuildGenerator.cxx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 = 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() ? "" : " --identifier \"" + identifier + "\"")
  95. << (identityName.empty() ? "" : " --sign \"" + identityName + "\"")
  96. << (keychainPath.empty() ? ""
  97. : " --keychain \"" + keychainPath + "\"")
  98. << " \"" << packageFileNames[0] << "\"";
  99. // Run ProductBuild
  100. return RunProductBuild(pkgCmd.str());
  101. }
  102. int cmCPackProductBuildGenerator::InitializeInternal()
  103. {
  104. this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/Applications");
  105. std::vector<std::string> no_paths;
  106. std::string program =
  107. cmSystemTools::FindProgram("pkgbuild", no_paths, false);
  108. if (program.empty()) {
  109. cmCPackLogger(cmCPackLog::LOG_ERROR,
  110. "Cannot find pkgbuild executable" << std::endl);
  111. return 0;
  112. }
  113. this->SetOptionIfNotSet("CPACK_COMMAND_PKGBUILD", program);
  114. program = cmSystemTools::FindProgram("productbuild", no_paths, false);
  115. if (program.empty()) {
  116. cmCPackLogger(cmCPackLog::LOG_ERROR,
  117. "Cannot find productbuild executable" << std::endl);
  118. return 0;
  119. }
  120. this->SetOptionIfNotSet("CPACK_COMMAND_PRODUCTBUILD", program);
  121. return this->Superclass::InitializeInternal();
  122. }
  123. bool cmCPackProductBuildGenerator::RunProductBuild(const std::string& command)
  124. {
  125. std::string tmpFile = cmStrCat(this->GetOption("CPACK_TOPLEVEL_DIRECTORY"),
  126. "/ProductBuildOutput.log");
  127. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Execute: " << command << std::endl);
  128. std::string output;
  129. int retVal = 1;
  130. bool res = cmSystemTools::RunSingleCommand(
  131. command, &output, &output, &retVal, nullptr, this->GeneratorVerbose,
  132. cmDuration::zero());
  133. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Done running command" << std::endl);
  134. if (!res || retVal) {
  135. cmGeneratedFileStream ofs(tmpFile);
  136. ofs << "# Run command: " << command << std::endl
  137. << "# Output:" << std::endl
  138. << output << std::endl;
  139. cmCPackLogger(cmCPackLog::LOG_ERROR,
  140. "Problem running command: " << command << std::endl
  141. << "Please check " << tmpFile
  142. << " for errors" << std::endl);
  143. return false;
  144. }
  145. return true;
  146. }
  147. bool cmCPackProductBuildGenerator::GenerateComponentPackage(
  148. const std::string& packageFileDir, const std::string& packageFileName,
  149. const std::string& packageDir, const cmCPackComponent* component)
  150. {
  151. std::string packageFile = cmStrCat(packageFileDir, '/', packageFileName);
  152. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  153. "- Building component package: " << packageFile
  154. << std::endl);
  155. const char* comp_name = component ? component->Name.c_str() : nullptr;
  156. cmValue preflight = this->GetComponentScript("PREFLIGHT", comp_name);
  157. cmValue postflight = this->GetComponentScript("POSTFLIGHT", comp_name);
  158. std::string resDir = packageFileDir;
  159. if (component) {
  160. resDir += "/";
  161. resDir += component->Name;
  162. }
  163. std::string scriptDir = resDir + "/scripts";
  164. if (!cmsys::SystemTools::MakeDirectory(scriptDir.c_str())) {
  165. cmCPackLogger(cmCPackLog::LOG_ERROR,
  166. "Problem creating installer directory: " << scriptDir
  167. << std::endl);
  168. return false;
  169. }
  170. // if preflight, postflight, or postupgrade are set
  171. // then copy them into the script directory and make
  172. // them executable
  173. if (preflight) {
  174. this->CopyInstallScript(scriptDir, preflight, "preinstall");
  175. }
  176. if (postflight) {
  177. this->CopyInstallScript(scriptDir, postflight, "postinstall");
  178. }
  179. // The command that will be used to run ProductBuild
  180. std::ostringstream pkgCmd;
  181. std::string pkgId;
  182. if (cmValue n = this->GetOption("CPACK_PRODUCTBUILD_IDENTIFIER")) {
  183. pkgId = n;
  184. } else {
  185. pkgId = cmStrCat("com.", this->GetOption("CPACK_PACKAGE_VENDOR"), '.',
  186. this->GetOption("CPACK_PACKAGE_NAME"));
  187. }
  188. if (component) {
  189. pkgId += '.';
  190. pkgId += component->Name;
  191. }
  192. std::string version = this->GetOption("CPACK_PACKAGE_VERSION");
  193. std::string pkgbuild = this->GetOption("CPACK_COMMAND_PKGBUILD");
  194. std::string identityName;
  195. if (cmValue n = this->GetOption("CPACK_PKGBUILD_IDENTITY_NAME")) {
  196. identityName = n;
  197. }
  198. std::string keychainPath;
  199. if (cmValue p = this->GetOption("CPACK_PKGBUILD_KEYCHAIN_PATH")) {
  200. keychainPath = p;
  201. }
  202. pkgCmd << pkgbuild << " --root \"" << packageDir << "\""
  203. << " --identifier \"" << pkgId << "\""
  204. << " --scripts \"" << scriptDir << "\""
  205. << " --version \"" << version << "\""
  206. << " --install-location \"/\""
  207. << (identityName.empty() ? "" : " --sign \"" + identityName + "\"")
  208. << (keychainPath.empty() ? ""
  209. : " --keychain \"" + keychainPath + "\"")
  210. << " \"" << packageFile << "\"";
  211. if (component && !component->Plist.empty()) {
  212. pkgCmd << " --component-plist \"" << component->Plist << "\"";
  213. }
  214. // Run ProductBuild
  215. return RunProductBuild(pkgCmd.str());
  216. }
  217. cmValue cmCPackProductBuildGenerator::GetComponentScript(
  218. const char* script, const char* component_name)
  219. {
  220. std::string scriptname = std::string("CPACK_") + script + "_";
  221. if (component_name) {
  222. scriptname += cmSystemTools::UpperCase(component_name);
  223. scriptname += "_";
  224. }
  225. scriptname += "SCRIPT";
  226. return this->GetOption(scriptname);
  227. }