cmCPackProductBuildGenerator.cxx 8.7 KB

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