cpack.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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> [options]",
  37. 0},
  38. {0,0,0}
  39. };
  40. //----------------------------------------------------------------------------
  41. static const cmDocumentationEntry cmDocumentationDescription[] =
  42. {
  43. {0,
  44. "The \"cpack\" executable is the CMake packaging program. "
  45. "CMake-generated build trees created for projects that use "
  46. "the INSTALL_* commands have packaging support. "
  47. "This program will generate the package.", 0},
  48. CMAKE_STANDARD_INTRODUCTION,
  49. {0,0,0}
  50. };
  51. //----------------------------------------------------------------------------
  52. static const cmDocumentationEntry cmDocumentationOptions[] =
  53. {
  54. {"-G <generator>", "Use the specified generator to generate package.",
  55. "CPack may support multiple native packaging systems on certain "
  56. "platforms. A generator is responsible for generating input files for "
  57. "particular system and invoking that systems. Possible generator names "
  58. "are specified in the Generators section." },
  59. {"-C <Configuration>", "Specify the project configuration",
  60. "This option specifies the configuration that the project was build "
  61. "with, for example 'Debug', 'Release'." },
  62. {"-D <var>=<value>", "Set a CPack variable.", \
  63. "Set a variable that can be used by the generator."}, \
  64. {"--config <config file>", "Specify the config file.",
  65. "Specify the config file to use to create the package. By default "
  66. "CPackConfig.cmake in the current directory will be used." },
  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. struct cpackDefinitions
  83. {
  84. typedef std::map<cmStdString, cmStdString> MapType;
  85. MapType Map;
  86. cmCPackLog *Log;
  87. };
  88. //----------------------------------------------------------------------------
  89. int cpackDefinitionArgument(const char* argument, const char* cValue,
  90. void* call_data)
  91. {
  92. (void)argument;
  93. cpackDefinitions* def = static_cast<cpackDefinitions*>(call_data);
  94. std::string value = cValue;
  95. size_t pos = value.find_first_of("=");
  96. if ( pos == std::string::npos )
  97. {
  98. cmCPack_Log(def->Log, cmCPackLog::LOG_ERROR,
  99. "Please specify CPack definitions as: KEY=VALUE" << std::endl);
  100. return 0;
  101. }
  102. std::string key = value.substr(0, pos);
  103. value = value.c_str() + pos + 1;
  104. def->Map[key] = value;
  105. cmCPack_Log(def->Log, cmCPackLog::LOG_DEBUG, "Set CPack variable: "
  106. << key.c_str() << " to \"" << value.c_str() << "\"" << std::endl);
  107. return 1;
  108. }
  109. //----------------------------------------------------------------------------
  110. // this is CPack.
  111. int main (int argc, char *argv[])
  112. {
  113. cmCPackLog log;
  114. log.SetErrorPrefix("CPack Error: ");
  115. log.SetWarningPrefix("CPack Warning: ");
  116. log.SetOutputPrefix("CPack: ");
  117. log.SetVerbosePrefix("CPack Verbose: ");
  118. cmSystemTools::EnableMSVCDebugHook();
  119. if ( cmSystemTools::GetCurrentWorkingDirectory().size() == 0 )
  120. {
  121. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  122. "Current working directory cannot be established." << std::endl);
  123. }
  124. std::string generator;
  125. bool help = false;
  126. bool helpVersion = false;
  127. bool verbose = false;
  128. bool debug = false;
  129. std::string helpFull;
  130. std::string helpMAN;
  131. std::string helpHTML;
  132. std::string cpackProjectName;
  133. std::string cpackProjectDirectory
  134. = cmsys::SystemTools::GetCurrentWorkingDirectory();
  135. std::string cpackBuildConfig;
  136. std::string cpackProjectVersion;
  137. std::string cpackProjectPatch;
  138. std::string cpackProjectVendor;
  139. std::string cpackConfigFile;
  140. cpackDefinitions definitions;
  141. definitions.Log = &log;
  142. cpackConfigFile = "";
  143. cmDocumentation doc;
  144. cmsys::CommandLineArguments arg;
  145. arg.Initialize(argc, argv);
  146. typedef cmsys::CommandLineArguments argT;
  147. // Help arguments
  148. arg.AddArgument("--help", argT::NO_ARGUMENT, &help, "CPack help");
  149. arg.AddArgument("--help-full", argT::SPACE_ARGUMENT, &helpFull,
  150. "CPack help");
  151. arg.AddArgument("--help-html", argT::SPACE_ARGUMENT, &helpHTML,
  152. "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,
  159. "CPack configuration file");
  160. arg.AddArgument("-C", argT::SPACE_ARGUMENT, &cpackBuildConfig,
  161. "CPack build configuration");
  162. arg.AddArgument("-G", argT::SPACE_ARGUMENT,
  163. &generator, "CPack generator");
  164. arg.AddArgument("-P", argT::SPACE_ARGUMENT,
  165. &cpackProjectName, "CPack project name");
  166. arg.AddArgument("-R", argT::SPACE_ARGUMENT,
  167. &cpackProjectVersion, "CPack project version");
  168. arg.AddArgument("-B", argT::SPACE_ARGUMENT,
  169. &cpackProjectDirectory, "CPack project directory");
  170. arg.AddArgument("--patch", argT::SPACE_ARGUMENT,
  171. &cpackProjectPatch, "CPack project patch");
  172. arg.AddArgument("--vendor", argT::SPACE_ARGUMENT,
  173. &cpackProjectVendor, "CPack project vendor");
  174. arg.AddCallback("-D", argT::SPACE_ARGUMENT,
  175. cpackDefinitionArgument, &definitions, "CPack Definitions");
  176. arg.SetUnknownArgumentCallback(cpackUnknownArgument);
  177. // Parse command line
  178. int parsed = arg.Parse();
  179. // Setup logging
  180. if ( verbose )
  181. {
  182. log.SetVerbose(verbose);
  183. cmCPack_Log(&log, cmCPackLog::LOG_OUTPUT, "Enable Verbse" << std::endl);
  184. }
  185. if ( debug )
  186. {
  187. log.SetDebug(debug);
  188. cmCPack_Log(&log, cmCPackLog::LOG_OUTPUT, "Enable Debug" << std::endl);
  189. }
  190. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
  191. "Read CPack config file: " << cpackConfigFile.c_str() << std::endl);
  192. cmake cminst;
  193. cmGlobalGenerator cmgg;
  194. cmgg.SetCMakeInstance(&cminst);
  195. cmLocalGenerator* cmlg = cmgg.CreateLocalGenerator();
  196. cmMakefile* mf = cmlg->GetMakefile();
  197. bool cpackConfigFileSpecified = true;
  198. if ( cpackConfigFile.empty() )
  199. {
  200. cpackConfigFile = cmSystemTools::GetCurrentWorkingDirectory();
  201. cpackConfigFile += "/CPackConfig.cmake";
  202. cpackConfigFileSpecified = false;
  203. }
  204. cmCPackGenerators generators;
  205. generators.SetLogger(&log);
  206. cmCPackGenericGenerator* cpackGenerator = 0;
  207. if ( !helpFull.empty() || !helpMAN.empty() ||
  208. !helpHTML.empty() || helpVersion )
  209. {
  210. help = true;
  211. }
  212. if ( parsed && !help )
  213. {
  214. if ( cmSystemTools::FileExists(cpackConfigFile.c_str()) )
  215. {
  216. cpackConfigFile =
  217. cmSystemTools::CollapseFullPath(cpackConfigFile.c_str());
  218. if ( !mf->ReadListFile(0, cpackConfigFile.c_str()) )
  219. {
  220. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  221. "Problem reding CPack config file: \""
  222. << cpackConfigFile.c_str() << "\"" << std::endl);
  223. return 1;
  224. }
  225. }
  226. else if ( cpackConfigFileSpecified )
  227. {
  228. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  229. "Cannot find CPack config file: \"" << cpackConfigFile.c_str()
  230. << "\"" << std::endl);
  231. return 1;
  232. }
  233. if ( !generator.empty() )
  234. {
  235. mf->AddDefinition("CPACK_GENERATOR", generator.c_str());
  236. }
  237. if ( !cpackProjectName.empty() )
  238. {
  239. mf->AddDefinition("CPACK_PACKAGE_NAME", cpackProjectName.c_str());
  240. }
  241. if ( !cpackProjectVersion.empty() )
  242. {
  243. mf->AddDefinition("CPACK_PACKAGE_VERSION", cpackProjectVersion.c_str());
  244. }
  245. if ( !cpackProjectVendor.empty() )
  246. {
  247. mf->AddDefinition("CPACK_PACKAGE_VENDOR", cpackProjectVendor.c_str());
  248. }
  249. if ( !cpackProjectDirectory.empty() )
  250. {
  251. mf->AddDefinition("CPACK_PACKAGE_DIRECTORY",
  252. cpackProjectDirectory.c_str());
  253. }
  254. if ( !cpackBuildConfig.empty() )
  255. {
  256. mf->AddDefinition("CPACK_BUILD_CONFIG", cpackBuildConfig.c_str());
  257. }
  258. cpackDefinitions::MapType::iterator cdit;
  259. for ( cdit = definitions.Map.begin();
  260. cdit != definitions.Map.end();
  261. ++cdit )
  262. {
  263. mf->AddDefinition(cdit->first.c_str(), cdit->second.c_str());
  264. }
  265. const char* gen = mf->GetDefinition("CPACK_GENERATOR");
  266. if ( !gen )
  267. {
  268. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  269. "CPack generator not specified" << std::endl);
  270. parsed = 0;
  271. }
  272. if ( parsed && !mf->GetDefinition("CPACK_PACKAGE_NAME") )
  273. {
  274. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  275. "CPack project name not specified" << std::endl);
  276. parsed = 0;
  277. }
  278. if ( parsed && !(mf->GetDefinition("CPACK_PACKAGE_VERSION")
  279. || mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR") &&
  280. mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR")
  281. && mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH")) )
  282. {
  283. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  284. "CPack project version not specified" << std::endl
  285. << "Specify CPACK_PACKAGE_VERSION, or CPACK_PACKAGE_VERSION_MAJOR, "
  286. "CPACK_PACKAGE_VERSION_MINOR, and CPACK_PACKAGE_VERSION_PATCH."
  287. << std::endl);
  288. parsed = 0;
  289. }
  290. if ( parsed )
  291. {
  292. cpackGenerator = generators.NewGenerator(gen);
  293. if ( !cpackGenerator )
  294. {
  295. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  296. "Cannot initialize CPack generator: "
  297. << generator.c_str() << std::endl);
  298. parsed = 0;
  299. }
  300. if ( parsed && !cpackGenerator->Initialize(gen, mf, argv[0]) )
  301. {
  302. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  303. "Cannot initialize the generator" << std::endl);
  304. parsed = 0;
  305. }
  306. if ( !mf->GetDefinition("CPACK_INSTALL_COMMANDS") &&
  307. !mf->GetDefinition("CPACK_INSTALLED_DIRECTORIES") &&
  308. !mf->GetDefinition("CPACK_INSTALL_CMAKE_PROJECTS") )
  309. {
  310. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  311. "Please specify build tree of the project that uses CMake using "
  312. " CPACK_INSTALL_CMAKE_PROJECTS, specify CPACK_INSTALL_COMMANDS, or "
  313. "specify CPACK_INSTALLED_DIRECTORIES."
  314. << std::endl);
  315. parsed = 0;
  316. }
  317. }
  318. }
  319. if ( !parsed || help )
  320. {
  321. doc.CheckOptions(argc, argv);
  322. // Construct and print requested documentation.
  323. doc.SetName("cpack");
  324. doc.SetNameSection(cmDocumentationName);
  325. doc.SetUsageSection(cmDocumentationUsage);
  326. doc.SetDescriptionSection(cmDocumentationDescription);
  327. doc.SetOptionsSection(cmDocumentationOptions);
  328. std::vector<cmDocumentationEntry> v;
  329. cmCPackGenerators::DescriptionsMap::const_iterator generatorIt;
  330. for( generatorIt = generators.GetGeneratorsList().begin();
  331. generatorIt != generators.GetGeneratorsList().end();
  332. ++ generatorIt )
  333. {
  334. cmDocumentationEntry e;
  335. e.name = generatorIt->first.c_str();
  336. e.brief = generatorIt->second.c_str();
  337. e.full = "";
  338. v.push_back(e);
  339. }
  340. cmDocumentationEntry empty = {0,0,0};
  341. v.push_back(empty);
  342. doc.SetGeneratorsSection(&v[0]);
  343. doc.SetSeeAlsoList(cmDocumentationSeeAlso);
  344. #undef cout
  345. return doc.PrintRequestedDocumentation(std::cout)? 0:1;
  346. #define cout no_cout_use_cmCPack_Log
  347. }
  348. #ifdef _WIN32
  349. std::string comspec = "cmw9xcom.exe";
  350. cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
  351. #endif
  352. const char* projName = mf->GetDefinition("CPACK_PACKAGE_NAME");
  353. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "Use generator: "
  354. << cpackGenerator->GetNameOfClass() << std::endl);
  355. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "For project: "
  356. << projName << std::endl);
  357. const char* projVersion = mf->GetDefinition("CPACK_PACKAGE_VERSION");
  358. if ( !projVersion )
  359. {
  360. const char* projVersionMajor
  361. = mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR");
  362. const char* projVersionMinor
  363. = mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR");
  364. const char* projVersionPatch
  365. = mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH");
  366. cmOStringStream ostr;
  367. ostr << projVersionMajor << "." << projVersionMinor << "."
  368. << projVersionPatch;
  369. mf->AddDefinition("CPACK_PACKAGE_VERSION", ostr.str().c_str());
  370. }
  371. int res = cpackGenerator->ProcessGenerator();
  372. if ( !res )
  373. {
  374. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  375. "Error when generating package: " << projName << std::endl);
  376. return 1;
  377. }
  378. return 0;
  379. }