cmCPackProductBuildGenerator.cxx 9.3 KB

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