cmCPackProductBuildGenerator.cxx 9.3 KB

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