cmCPackProductBuildGenerator.cxx 7.4 KB

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