cpack.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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* globalMF = 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. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
  219. "Read CPack configuration file: " << cpackConfigFile.c_str()
  220. << std::endl);
  221. if ( !globalMF->ReadListFile(0, cpackConfigFile.c_str()) )
  222. {
  223. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  224. "Problem reding CPack config file: \""
  225. << cpackConfigFile.c_str() << "\"" << std::endl);
  226. return 1;
  227. }
  228. }
  229. else if ( cpackConfigFileSpecified )
  230. {
  231. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  232. "Cannot find CPack config file: \"" << cpackConfigFile.c_str()
  233. << "\"" << std::endl);
  234. return 1;
  235. }
  236. if ( !generator.empty() )
  237. {
  238. globalMF->AddDefinition("CPACK_GENERATOR", generator.c_str());
  239. }
  240. if ( !cpackProjectName.empty() )
  241. {
  242. globalMF->AddDefinition("CPACK_PACKAGE_NAME", cpackProjectName.c_str());
  243. }
  244. if ( !cpackProjectVersion.empty() )
  245. {
  246. globalMF->AddDefinition("CPACK_PACKAGE_VERSION",
  247. cpackProjectVersion.c_str());
  248. }
  249. if ( !cpackProjectVendor.empty() )
  250. {
  251. globalMF->AddDefinition("CPACK_PACKAGE_VENDOR",
  252. cpackProjectVendor.c_str());
  253. }
  254. if ( !cpackProjectDirectory.empty() )
  255. {
  256. globalMF->AddDefinition("CPACK_PACKAGE_DIRECTORY",
  257. cpackProjectDirectory.c_str());
  258. }
  259. if ( !cpackBuildConfig.empty() )
  260. {
  261. globalMF->AddDefinition("CPACK_BUILD_CONFIG", cpackBuildConfig.c_str());
  262. }
  263. cpackDefinitions::MapType::iterator cdit;
  264. for ( cdit = definitions.Map.begin();
  265. cdit != definitions.Map.end();
  266. ++cdit )
  267. {
  268. globalMF->AddDefinition(cdit->first.c_str(), cdit->second.c_str());
  269. }
  270. const char* cpackModulesPath =
  271. globalMF->GetDefinition("CPACK_MODULE_PATH");
  272. if ( cpackModulesPath )
  273. {
  274. globalMF->AddDefinition("CMAKE_MODULE_PATH", cpackModulesPath);
  275. }
  276. const char* genList = globalMF->GetDefinition("CPACK_GENERATOR");
  277. if ( !genList )
  278. {
  279. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  280. "CPack generator not specified" << std::endl);
  281. parsed = 0;
  282. }
  283. else
  284. {
  285. std::vector<std::string> generatorsVector;
  286. cmSystemTools::ExpandListArgument(genList,
  287. generatorsVector);
  288. std::vector<std::string>::iterator it;
  289. for ( it = generatorsVector.begin();
  290. it != generatorsVector.end();
  291. ++it )
  292. {
  293. const char* gen = it->c_str();
  294. cmMakefile newMF(*globalMF);
  295. cmMakefile* mf = &newMF;
  296. {
  297. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
  298. "Specified generator: " << gen << std::endl);
  299. if ( parsed && !mf->GetDefinition("CPACK_PACKAGE_NAME") )
  300. {
  301. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  302. "CPack project name not specified" << std::endl);
  303. parsed = 0;
  304. }
  305. if ( parsed && !(mf->GetDefinition("CPACK_PACKAGE_VERSION")
  306. || mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR") &&
  307. mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR")
  308. && mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH")) )
  309. {
  310. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  311. "CPack project version not specified" << std::endl
  312. << "Specify CPACK_PACKAGE_VERSION, or "
  313. "CPACK_PACKAGE_VERSION_MAJOR, "
  314. "CPACK_PACKAGE_VERSION_MINOR, and CPACK_PACKAGE_VERSION_PATCH."
  315. << std::endl);
  316. parsed = 0;
  317. }
  318. if ( parsed )
  319. {
  320. cpackGenerator = generators.NewGenerator(gen);
  321. if ( !cpackGenerator )
  322. {
  323. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  324. "Cannot initialize CPack generator: "
  325. << generator.c_str() << std::endl);
  326. parsed = 0;
  327. }
  328. if ( parsed && !cpackGenerator->Initialize(gen, mf, argv[0]) )
  329. {
  330. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  331. "Cannot initialize the generator" << std::endl);
  332. parsed = 0;
  333. }
  334. if ( !mf->GetDefinition("CPACK_INSTALL_COMMANDS") &&
  335. !mf->GetDefinition("CPACK_INSTALLED_DIRECTORIES") &&
  336. !mf->GetDefinition("CPACK_INSTALL_CMAKE_PROJECTS") )
  337. {
  338. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  339. "Please specify build tree of the project that uses CMake "
  340. "using CPACK_INSTALL_CMAKE_PROJECTS, specify "
  341. "CPACK_INSTALL_COMMANDS, or specify "
  342. "CPACK_INSTALLED_DIRECTORIES."
  343. << std::endl);
  344. parsed = 0;
  345. }
  346. if ( parsed )
  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 =
  358. mf->GetDefinition("CPACK_PACKAGE_VERSION");
  359. if ( !projVersion )
  360. {
  361. const char* projVersionMajor
  362. = mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR");
  363. const char* projVersionMinor
  364. = mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR");
  365. const char* projVersionPatch
  366. = mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH");
  367. cmOStringStream ostr;
  368. ostr << projVersionMajor << "." << projVersionMinor << "."
  369. << projVersionPatch;
  370. mf->AddDefinition("CPACK_PACKAGE_VERSION",
  371. ostr.str().c_str());
  372. }
  373. int res = cpackGenerator->ProcessGenerator();
  374. if ( !res )
  375. {
  376. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  377. "Error when generating package: " << projName << std::endl);
  378. return 1;
  379. }
  380. }
  381. }
  382. }
  383. }
  384. }
  385. }
  386. if ( !parsed || help )
  387. {
  388. doc.CheckOptions(argc, argv);
  389. // Construct and print requested documentation.
  390. doc.SetName("cpack");
  391. doc.SetNameSection(cmDocumentationName);
  392. doc.SetUsageSection(cmDocumentationUsage);
  393. doc.SetDescriptionSection(cmDocumentationDescription);
  394. doc.SetOptionsSection(cmDocumentationOptions);
  395. std::vector<cmDocumentationEntry> v;
  396. cmCPackGenerators::DescriptionsMap::const_iterator generatorIt;
  397. for( generatorIt = generators.GetGeneratorsList().begin();
  398. generatorIt != generators.GetGeneratorsList().end();
  399. ++ generatorIt )
  400. {
  401. cmDocumentationEntry e;
  402. e.name = generatorIt->first.c_str();
  403. e.brief = generatorIt->second.c_str();
  404. e.full = "";
  405. v.push_back(e);
  406. }
  407. cmDocumentationEntry empty = {0,0,0};
  408. v.push_back(empty);
  409. doc.SetGeneratorsSection(&v[0]);
  410. doc.SetSeeAlsoList(cmDocumentationSeeAlso);
  411. #undef cout
  412. return doc.PrintRequestedDocumentation(std::cout)? 0:1;
  413. #define cout no_cout_use_cmCPack_Log
  414. }
  415. return 0;
  416. }