cmCPackProductBuildGenerator.cxx 8.0 KB

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