cpack.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 "cmake.h"
  20. #include "cmGlobalGenerator.h"
  21. #include "cmLocalGenerator.h"
  22. #include "cmMakefile.h"
  23. #include "cmCPackLog.h"
  24. #include <cmsys/CommandLineArguments.hxx>
  25. //----------------------------------------------------------------------------
  26. static const cmDocumentationEntry cmDocumentationName[] =
  27. {
  28. {0,
  29. " cpack - Packaging driver provided by CMake.", 0},
  30. {0,0,0}
  31. };
  32. //----------------------------------------------------------------------------
  33. static const cmDocumentationEntry cmDocumentationUsage[] =
  34. {
  35. {0,
  36. " cpack -G <generator> -P <ProjectName> -R <ReleaseVersion> [options]", 0},
  37. {0,0,0}
  38. };
  39. //----------------------------------------------------------------------------
  40. static const cmDocumentationEntry cmDocumentationDescription[] =
  41. {
  42. {0,
  43. "The \"cpack\" executable is the CMake packaging program. "
  44. "CMake-generated build trees created for projects that use "
  45. "the INSTALL_* commands have packaging support. "
  46. "This program will generate the package.", 0},
  47. CMAKE_STANDARD_INTRODUCTION,
  48. {0,0,0}
  49. };
  50. //----------------------------------------------------------------------------
  51. static const cmDocumentationEntry cmDocumentationOptions[] =
  52. {
  53. {"-G <generator>", "Use the specified generator to generate package.",
  54. "CPack may support multiple native packaging systems on certain platforms. A "
  55. "generator is responsible for generating input files for particular system "
  56. "and invoking that systems. Possible generator names are specified in the "
  57. "Generators section." },
  58. {"-P <ProjectName>", "Specify the project name.",
  59. "This option specifies the project name that will be used to generate the "
  60. "installer." },
  61. {"-R <ReleaseVersion>", "Specify the release version of the project.",
  62. "This option specifies the release version of the project that will be "
  63. "used by installer." },
  64. {"-D <var>=<value>", "Set a CPack variable.", \
  65. "Set a variable that can be used by the generator."}, \
  66. {"--patch <ReleasePatch>", "Specify the patch of the project.",
  67. "This option specifies the patch of the project that will be "
  68. "used by installer." },
  69. {"--vendor <ProjectVendor>", "Specify the vendor of the project.",
  70. "This option specifies the vendor of the project that will be "
  71. "used by installer." },
  72. {0,0,0}
  73. };
  74. //----------------------------------------------------------------------------
  75. static const cmDocumentationEntry cmDocumentationSeeAlso[] =
  76. {
  77. {0, "cmake", 0},
  78. {0, "ccmake", 0},
  79. {0, 0, 0}
  80. };
  81. //----------------------------------------------------------------------------
  82. int cpackUnknownArgument(const char*, void*)
  83. {
  84. return 1;
  85. }
  86. //----------------------------------------------------------------------------
  87. struct cpackDefinitions
  88. {
  89. typedef std::map<cmStdString, cmStdString> MapType;
  90. MapType m_Map;
  91. cmCPackLog *m_Log;
  92. };
  93. //----------------------------------------------------------------------------
  94. int cpackDefinitionArgument(const char* argument, const char* cValue,
  95. void* call_data)
  96. {
  97. (void)argument;
  98. cpackDefinitions* def = static_cast<cpackDefinitions*>(call_data);
  99. std::string value = cValue;
  100. size_t pos = value.find_first_of("=");
  101. if ( pos == std::string::npos )
  102. {
  103. cmCPack_Log(def->m_Log, cmCPackLog::LOG_ERROR, "Please specify CPack definitions as: KEY=VALUE" << std::endl);
  104. return 0;
  105. }
  106. std::string key = value.substr(0, pos);
  107. value = value.c_str() + pos + 1;
  108. def->m_Map[key] = value;
  109. cmCPack_Log(def->m_Log, cmCPackLog::LOG_DEBUG, "Set CPack variable: " << key.c_str() << " to \"" << value.c_str() << "\"" << std::endl);
  110. return 1;
  111. }
  112. //----------------------------------------------------------------------------
  113. // this is CPack.
  114. int main (int argc, char *argv[])
  115. {
  116. cmCPackLog log;
  117. log.SetErrorPrefix("CPack Error: ");
  118. log.SetWarningPrefix("CPack Warning: ");
  119. log.SetOutputPrefix("CPack: ");
  120. log.SetVerbosePrefix("CPack Verbose: ");
  121. int res = 0;
  122. cmSystemTools::EnableMSVCDebugHook();
  123. if ( cmSystemTools::GetCurrentWorkingDirectory().size() == 0 )
  124. {
  125. cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "Current working directory cannot be established." << std::endl);
  126. }
  127. std::string generator;
  128. bool help = false;
  129. bool helpVersion = false;
  130. bool verbose = false;
  131. bool debug = false;
  132. std::string helpFull;
  133. std::string helpMAN;
  134. std::string helpHTML;
  135. std::string cpackProjectName;
  136. std::string cpackProjectDirectory = cmsys::SystemTools::GetCurrentWorkingDirectory();
  137. std::string cpackBuildConfig;
  138. std::string cpackProjectVersion;
  139. std::string cpackProjectPatch;
  140. std::string cpackProjectVendor;
  141. std::string cpackConfigFile;
  142. cpackDefinitions definitions;
  143. definitions.m_Log = &log;
  144. cpackConfigFile = "";
  145. cmDocumentation doc;
  146. cmsys::CommandLineArguments arg;
  147. arg.Initialize(argc, argv);
  148. typedef cmsys::CommandLineArguments argT;
  149. // Help arguments
  150. arg.AddArgument("--help", argT::NO_ARGUMENT, &help, "CPack help");
  151. arg.AddArgument("--help-full", argT::SPACE_ARGUMENT, &helpFull, "CPack help");
  152. arg.AddArgument("--help-html", argT::SPACE_ARGUMENT, &helpHTML, "CPack help");
  153. arg.AddArgument("--help-man", argT::SPACE_ARGUMENT, &helpMAN, "CPack help");
  154. arg.AddArgument("--version", argT::NO_ARGUMENT, &helpVersion, "CPack help");
  155. arg.AddArgument("-V", argT::NO_ARGUMENT, &verbose, "CPack verbose");
  156. arg.AddArgument("--verbose", argT::NO_ARGUMENT, &verbose, "-V");
  157. arg.AddArgument("--debug", argT::NO_ARGUMENT, &debug, "-V");
  158. arg.AddArgument("--config", argT::SPACE_ARGUMENT, &cpackConfigFile, "CPack configuration file");
  159. arg.AddArgument("-C", argT::SPACE_ARGUMENT, &cpackBuildConfig, "CPack build configuration");
  160. arg.AddArgument("-G", argT::SPACE_ARGUMENT, &generator, "CPack generator");
  161. arg.AddArgument("-P", argT::SPACE_ARGUMENT, &cpackProjectName, "CPack project name");
  162. arg.AddArgument("-R", argT::SPACE_ARGUMENT, &cpackProjectVersion, "CPack project version");
  163. arg.AddArgument("-B", argT::SPACE_ARGUMENT, &cpackProjectDirectory, "CPack project directory");
  164. arg.AddArgument("--patch", argT::SPACE_ARGUMENT, &cpackProjectPatch, "CPack project patch");
  165. arg.AddArgument("--vendor", argT::SPACE_ARGUMENT, &cpackProjectVendor, "CPack project vendor");
  166. arg.AddCallback("-D", argT::SPACE_ARGUMENT, cpackDefinitionArgument, &definitions, "CPack Definitions");
  167. arg.SetUnknownArgumentCallback(cpackUnknownArgument);
  168. // Parse command line
  169. int parsed = arg.Parse();
  170. // Setup logging
  171. if ( verbose )
  172. {
  173. log.SetVerbose(verbose);
  174. cmCPack_Log(&log, cmCPackLog::LOG_OUTPUT, "Enable Verbse" << std::endl);
  175. }
  176. if ( debug )
  177. {
  178. log.SetDebug(debug);
  179. cmCPack_Log(&log, cmCPackLog::LOG_OUTPUT, "Enable Debug" << std::endl);
  180. }
  181. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "Read CPack config file: " << cpackConfigFile.c_str() << std::endl);
  182. cmake cminst;
  183. cmGlobalGenerator cmgg;
  184. cmgg.SetCMakeInstance(&cminst);
  185. cmLocalGenerator* cmlg = cmgg.CreateLocalGenerator();
  186. cmMakefile* mf = cmlg->GetMakefile();
  187. bool cpackConfigFileSpecified = true;
  188. if ( cpackConfigFile.empty() )
  189. {
  190. cpackConfigFile = cmSystemTools::GetCurrentWorkingDirectory();
  191. cpackConfigFile += "/CPack.cmake";
  192. cpackConfigFileSpecified = false;
  193. }
  194. cmCPackGenerators generators;
  195. generators.SetLogger(&log);
  196. cmCPackGenericGenerator* cpackGenerator = 0;
  197. if ( !helpFull.empty() || !helpMAN.empty() || !helpHTML.empty() || helpVersion )
  198. {
  199. help = true;
  200. }
  201. if ( parsed && !help )
  202. {
  203. if ( cmSystemTools::FileExists(cpackConfigFile.c_str()) && !mf->ReadListFile(0, cpackConfigFile.c_str()) )
  204. {
  205. cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "Problem reding CPack config file: \"" << cpackConfigFile.c_str() << "\"" << std::endl);
  206. return 1;
  207. }
  208. else if ( cpackConfigFileSpecified )
  209. {
  210. cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "Cannot find CPack config file: \"" << cpackConfigFile.c_str() << "\"" << std::endl);
  211. return 1;
  212. }
  213. if ( !generator.empty() ) { mf->AddDefinition("CPACK_GENERATOR", generator.c_str()); }
  214. if ( !cpackProjectName.empty() ) { mf->AddDefinition("CPACK_PROJECT_NAME", cpackProjectName.c_str()); }
  215. if ( !cpackProjectVersion.empty() ) { mf->AddDefinition("CPACK_PROJECT_VERSION", cpackProjectVersion.c_str()); }
  216. if ( !cpackProjectPatch.empty() ) { mf->AddDefinition("CPACK_PROJECT_VERSION_PATCH", cpackProjectPatch.c_str()); }
  217. if ( !cpackProjectVendor.empty() ) { mf->AddDefinition("CPACK_PROJECT_VENDOR", cpackProjectVendor.c_str()); }
  218. if ( !cpackProjectDirectory.empty() ) { mf->AddDefinition("CPACK_PROJECT_DIRECTORY", cpackProjectDirectory.c_str()); }
  219. if ( !cpackBuildConfig.empty() ) { mf->AddDefinition("CPACK_BUILD_CONFIG", cpackBuildConfig.c_str()); }
  220. cpackDefinitions::MapType::iterator cdit;
  221. for ( cdit = definitions.m_Map.begin(); cdit != definitions.m_Map.end(); ++cdit )
  222. {
  223. cpackGenerator->SetOption(cdit->first.c_str(), cdit->second.c_str());
  224. }
  225. const char* gen = mf->GetDefinition("CPACK_GENERATOR");
  226. if ( !gen )
  227. {
  228. cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "CPack generator not specified" << std::endl);
  229. parsed = 0;
  230. }
  231. if ( parsed && !mf->GetDefinition("CPACK_PROJECT_NAME") )
  232. {
  233. cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "CPack project name not specified" << std::endl);
  234. parsed = 0;
  235. }
  236. if ( parsed && !mf->GetDefinition("CPACK_PROJECT_VERSION"))
  237. {
  238. cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "CPack project version not specified" << std::endl);
  239. parsed = 0;
  240. }
  241. if ( parsed )
  242. {
  243. cpackGenerator = generators.NewGenerator(gen);
  244. if ( !cpackGenerator )
  245. {
  246. cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "Cannot initialize CPack generator: " << generator.c_str() << std::endl);
  247. parsed = 0;
  248. }
  249. cpackGenerator->Initialize(gen, mf);
  250. if ( parsed && !cpackGenerator->FindRunningCMake(argv[0]) )
  251. {
  252. cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "Cannot initialize the generator" << std::endl);
  253. parsed = 0;
  254. }
  255. cmsys::SystemTools::ConvertToUnixSlashes(cpackProjectDirectory);
  256. std::string makeInstallFile = cpackProjectDirectory + "/cmake_install.cmake";
  257. if ( !cmsys::SystemTools::FileExists(makeInstallFile.c_str()) )
  258. {
  259. cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "Cannot find installation file: " << makeInstallFile.c_str() << std::endl);
  260. parsed = 0;
  261. }
  262. }
  263. }
  264. if ( !parsed || help )
  265. {
  266. doc.CheckOptions(argc, argv);
  267. // Construct and print requested documentation.
  268. doc.SetName("cpack");
  269. doc.SetNameSection(cmDocumentationName);
  270. doc.SetUsageSection(cmDocumentationUsage);
  271. doc.SetDescriptionSection(cmDocumentationDescription);
  272. doc.SetOptionsSection(cmDocumentationOptions);
  273. doc.SetSeeAlsoList(cmDocumentationSeeAlso);
  274. #undef cout
  275. return doc.PrintRequestedDocumentation(std::cout)? 0:1;
  276. #define cout no_cout_use_cmCPack_Log
  277. }
  278. #ifdef _WIN32
  279. std::string comspec = "cmw9xcom.exe";
  280. cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
  281. #endif
  282. const char* projName = mf->GetDefinition("CPACK_PROJECT_NAME");
  283. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "Use generator: " << cpackGenerator->GetNameOfClass() << std::endl);
  284. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "For project: " << projName << std::endl);
  285. res = cpackGenerator->ProcessGenerator();
  286. if ( !res )
  287. {
  288. cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "Error when generating package: " << cpackProjectName.c_str() << std::endl);
  289. return 1;
  290. }
  291. return 0;
  292. }