cpack.cxx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmSystemTools.h"
  14. // Need these for documentation support.
  15. #include "cmake.h"
  16. #include "cmDocumentation.h"
  17. #include "cmCPackGenerators.h"
  18. #include "cmCPackGenericGenerator.h"
  19. #include <cmsys/CommandLineArguments.hxx>
  20. //----------------------------------------------------------------------------
  21. static const cmDocumentationEntry cmDocumentationName[] =
  22. {
  23. {0,
  24. " cpack - Packaging driver provided by CMake.", 0},
  25. {0,0,0}
  26. };
  27. //----------------------------------------------------------------------------
  28. static const cmDocumentationEntry cmDocumentationUsage[] =
  29. {
  30. {0,
  31. " cpack -G <generator> -P <ProjectName> -R <ReleaseVersion> [options]", 0},
  32. {0,0,0}
  33. };
  34. //----------------------------------------------------------------------------
  35. static const cmDocumentationEntry cmDocumentationDescription[] =
  36. {
  37. {0,
  38. "The \"cpack\" executable is the CMake packaging program. "
  39. "CMake-generated build trees created for projects that use "
  40. "the INSTALL_* commands have packaging support. "
  41. "This program will generate the package.", 0},
  42. CMAKE_STANDARD_INTRODUCTION,
  43. {0,0,0}
  44. };
  45. //----------------------------------------------------------------------------
  46. static const cmDocumentationEntry cmDocumentationOptions[] =
  47. {
  48. {"-G <generator>", "Use the specified generator to generate package.",
  49. "CPack may support multiple native packaging systems on certain platforms. A "
  50. "generator is responsible for generating input files for particular system "
  51. "and invoking that systems. Possible generator names are specified in the "
  52. "Generators section." },
  53. {"-P <ProjectName>", "Specify the project name.",
  54. "This option specifies the project name that will be used to generate the "
  55. "installer." },
  56. {"-R <ReleaseVersion>", "Specify the release version of the project.",
  57. "This option specifies the release version of the project that will be "
  58. "used by installer." },
  59. {"-D <var>=<value>", "Set a CPack variable.", \
  60. "Set a variable that can be used by the generator."}, \
  61. {"--patch <ReleasePatch>", "Specify the patch of the project.",
  62. "This option specifies the patch of the project that will be "
  63. "used by installer." },
  64. {"--vendor <ProjectVendor>", "Specify the vendor of the project.",
  65. "This option specifies the vendor of the project that will be "
  66. "used by installer." },
  67. {0,0,0}
  68. };
  69. //----------------------------------------------------------------------------
  70. static const cmDocumentationEntry cmDocumentationSeeAlso[] =
  71. {
  72. {0, "cmake", 0},
  73. {0, "ccmake", 0},
  74. {0, 0, 0}
  75. };
  76. //----------------------------------------------------------------------------
  77. int cpackUnknownArgument(const char*, void*)
  78. {
  79. return 1;
  80. }
  81. //----------------------------------------------------------------------------
  82. typedef std::map<cmStdString, cmStdString> cpackDefinitionsMapType;
  83. //----------------------------------------------------------------------------
  84. int cpackDefinitionArgument(const char* argument, const char* cValue,
  85. void* call_data)
  86. {
  87. (void)argument;
  88. std::string value = cValue;
  89. size_t pos = value.find_first_of("=");
  90. if ( pos == std::string::npos )
  91. {
  92. std::cerr << "Please specify CPack definitions as: KEY=VALUE" << std::endl;
  93. return 0;
  94. }
  95. std::string key = value.substr(0, pos);
  96. value = value.c_str() + pos + 1;
  97. cpackDefinitionsMapType* map = static_cast<cpackDefinitionsMapType*>(call_data);
  98. (*map)[key] = value;
  99. return 1;
  100. }
  101. //----------------------------------------------------------------------------
  102. // this is CPack.
  103. int main (int argc, char *argv[])
  104. {
  105. int res = 0;
  106. cmSystemTools::EnableMSVCDebugHook();
  107. if ( cmSystemTools::GetCurrentWorkingDirectory().size() == 0 )
  108. {
  109. std::cerr << "Current working directory cannot be established." << std::endl;
  110. }
  111. std::string generator;
  112. bool help = false;
  113. bool helpVersion = false;
  114. std::string helpFull;
  115. std::string helpMAN;
  116. std::string helpHTML;
  117. std::string cpackProjectName;
  118. std::string cpackProjectDirectory = cmsys::SystemTools::GetCurrentWorkingDirectory();
  119. std::string cpackBuildConfig;
  120. std::string cpackProjectVersion;
  121. std::string cpackProjectPatch;
  122. std::string cpackProjectVendor;
  123. cpackDefinitionsMapType definitionsMap;
  124. cmDocumentation doc;
  125. cmsys::CommandLineArguments arg;
  126. arg.Initialize(argc, argv);
  127. typedef cmsys::CommandLineArguments argT;
  128. // Help arguments
  129. arg.AddArgument("--help", argT::NO_ARGUMENT, &help, "CPack help");
  130. arg.AddArgument("--help-full", argT::SPACE_ARGUMENT, &helpFull, "CPack help");
  131. arg.AddArgument("--help-html", argT::SPACE_ARGUMENT, &helpHTML, "CPack help");
  132. arg.AddArgument("--help-man", argT::SPACE_ARGUMENT, &helpMAN, "CPack help");
  133. arg.AddArgument("--version", argT::NO_ARGUMENT, &helpVersion, "CPack help");
  134. arg.AddArgument("-C", argT::SPACE_ARGUMENT, &cpackBuildConfig, "CPack build configuration");
  135. arg.AddArgument("-G", argT::SPACE_ARGUMENT, &generator, "CPack generator");
  136. arg.AddArgument("-P", argT::SPACE_ARGUMENT, &cpackProjectName, "CPack project name");
  137. arg.AddArgument("-R", argT::SPACE_ARGUMENT, &cpackProjectVersion, "CPack project version");
  138. arg.AddArgument("-B", argT::SPACE_ARGUMENT, &cpackProjectDirectory, "CPack project directory");
  139. arg.AddArgument("--patch", argT::SPACE_ARGUMENT, &cpackProjectPatch, "CPack project patch");
  140. arg.AddArgument("--vendor", argT::SPACE_ARGUMENT, &cpackProjectVendor, "CPack project vendor");
  141. arg.AddCallback("-D", argT::SPACE_ARGUMENT, cpackDefinitionArgument, &definitionsMap, "CPack Definitions");
  142. arg.SetUnknownArgumentCallback(cpackUnknownArgument);
  143. int parsed = arg.Parse();
  144. cmCPackGenerators generators;
  145. cmCPackGenericGenerator* cpackGenerator = 0;
  146. if ( !helpFull.empty() || !helpMAN.empty() || !helpHTML.empty() || helpVersion )
  147. {
  148. help = true;
  149. }
  150. if ( parsed && !help )
  151. {
  152. if ( generator.empty() )
  153. {
  154. std::cerr << "CPack generator not specified" << std::endl;
  155. parsed = 0;
  156. }
  157. if ( parsed && cpackProjectName.empty() )
  158. {
  159. std::cerr << "CPack project name not specified" << std::endl;
  160. parsed = 0;
  161. }
  162. if ( parsed && cpackProjectVersion.empty() )
  163. {
  164. std::cerr << "CPack project version not specified" << std::endl;
  165. parsed = 0;
  166. }
  167. if ( parsed )
  168. {
  169. cpackGenerator = generators.NewGenerator(generator.c_str());
  170. if ( !cpackGenerator )
  171. {
  172. std::cerr << "Cannot initialize CPack generator: " << generator.c_str() << std::endl;
  173. parsed = 0;
  174. }
  175. if ( parsed && !cpackGenerator->FindRunningCMake(argv[0]) )
  176. {
  177. std::cerr << "Cannot initialize the generator" << std::endl;
  178. parsed = 0;
  179. }
  180. cmsys::SystemTools::ConvertToUnixSlashes(cpackProjectDirectory);
  181. std::string makeInstallFile = cpackProjectDirectory + "/cmake_install.cmake";
  182. if ( !cmsys::SystemTools::FileExists(makeInstallFile.c_str()) )
  183. {
  184. std::cerr << "Cannot find installation file: " << makeInstallFile.c_str() << std::endl;
  185. parsed = 0;
  186. }
  187. }
  188. }
  189. if ( !parsed || help )
  190. {
  191. doc.CheckOptions(argc, argv);
  192. // Construct and print requested documentation.
  193. doc.SetName("cpack");
  194. doc.SetNameSection(cmDocumentationName);
  195. doc.SetUsageSection(cmDocumentationUsage);
  196. doc.SetDescriptionSection(cmDocumentationDescription);
  197. doc.SetOptionsSection(cmDocumentationOptions);
  198. doc.SetSeeAlsoList(cmDocumentationSeeAlso);
  199. return doc.PrintRequestedDocumentation(std::cout)? 0:1;
  200. }
  201. #ifdef _WIN32
  202. std::string comspec = "cmw9xcom.exe";
  203. cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
  204. #endif
  205. std::cout << "Use generator: " << cpackGenerator->GetNameOfClass() << std::endl;
  206. std::cout << "For project: " << cpackProjectName.c_str() << std::endl;
  207. cpackGenerator->SetOption("CPACK_PROJECT_NAME", cpackProjectName.c_str());
  208. cpackGenerator->SetOption("CPACK_PROJECT_VERSION", cpackProjectVersion.c_str());
  209. cpackGenerator->SetOption("CPACK_PROJECT_VERSION_PATCH", cpackProjectPatch.c_str());
  210. cpackGenerator->SetOption("CPACK_PROJECT_VENDOR", cpackProjectVendor.c_str());
  211. cpackGenerator->SetOption("CPACK_PROJECT_DIRECTORY", cpackProjectDirectory.c_str());
  212. if ( !cpackBuildConfig.empty() )
  213. {
  214. cpackGenerator->SetOption("CPACK_BUILD_CONFIG", cpackBuildConfig.c_str());
  215. }
  216. cpackDefinitionsMapType::iterator cdit;
  217. for ( cdit = definitionsMap.begin(); cdit != definitionsMap.end(); ++cdit )
  218. {
  219. cpackGenerator->SetOption(cdit->first.c_str(), cdit->second.c_str());
  220. }
  221. res = cpackGenerator->ProcessGenerator();
  222. if ( !res )
  223. {
  224. std::cerr << "Error when generating package: " << cpackProjectName.c_str() << std::endl;
  225. return 1;
  226. }
  227. return 0;
  228. }