cpack.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 "cmSystemTools.h"
  11. // Need these for documentation support.
  12. #include "cmake.h"
  13. #include "cmDocumentation.h"
  14. #include "cmCPackGeneratorFactory.h"
  15. #include "cmCPackGenerator.h"
  16. #include "cmake.h"
  17. #include "cmGlobalGenerator.h"
  18. #include "cmLocalGenerator.h"
  19. #include "cmMakefile.h"
  20. #include "cmCPackLog.h"
  21. #include <cmsys/CommandLineArguments.hxx>
  22. #include <cmsys/SystemTools.hxx>
  23. #include <cmsys/Encoding.hxx>
  24. //----------------------------------------------------------------------------
  25. static const char * cmDocumentationName[][2] =
  26. {
  27. {0,
  28. " cpack - Packaging driver provided by CMake."},
  29. {0,0}
  30. };
  31. //----------------------------------------------------------------------------
  32. static const char * cmDocumentationUsage[][2] =
  33. {
  34. {0,
  35. " cpack -G <generator> [options]"},
  36. {0,0}
  37. };
  38. //----------------------------------------------------------------------------
  39. static const char * cmDocumentationOptions[][2] =
  40. {
  41. {"-G <generator>", "Use the specified generator to generate package."},
  42. {"-C <Configuration>", "Specify the project configuration"},
  43. {"-D <var>=<value>", "Set a CPack variable."},
  44. {"--config <config file>", "Specify the config file."},
  45. {"--verbose,-V","enable verbose output"},
  46. {"--debug","enable debug output (for CPack developers)"},
  47. {"-P <package name>","override/define CPACK_PACKAGE_NAME"},
  48. {"-R <package version>","override/define CPACK_PACKAGE_VERSION"},
  49. {"-B <package directory>","override/define CPACK_PACKAGE_DIRECTORY"},
  50. {"--vendor <vendor name>","override/define CPACK_PACKAGE_VENDOR"},
  51. {0,0}
  52. };
  53. //----------------------------------------------------------------------------
  54. int cpackUnknownArgument(const char*, void*)
  55. {
  56. return 1;
  57. }
  58. //----------------------------------------------------------------------------
  59. struct cpackDefinitions
  60. {
  61. typedef std::map<std::string, std::string> MapType;
  62. MapType Map;
  63. cmCPackLog *Log;
  64. };
  65. //----------------------------------------------------------------------------
  66. int cpackDefinitionArgument(const char* argument, const char* cValue,
  67. void* call_data)
  68. {
  69. (void)argument;
  70. cpackDefinitions* def = static_cast<cpackDefinitions*>(call_data);
  71. std::string value = cValue;
  72. size_t pos = value.find_first_of("=");
  73. if ( pos == std::string::npos )
  74. {
  75. cmCPack_Log(def->Log, cmCPackLog::LOG_ERROR,
  76. "Please specify CPack definitions as: KEY=VALUE" << std::endl);
  77. return 0;
  78. }
  79. std::string key = value.substr(0, pos);
  80. value = value.c_str() + pos + 1;
  81. def->Map[key] = value;
  82. cmCPack_Log(def->Log, cmCPackLog::LOG_DEBUG, "Set CPack variable: "
  83. << key << " to \"" << value << "\"" << std::endl);
  84. return 1;
  85. }
  86. //----------------------------------------------------------------------------
  87. // this is CPack.
  88. int main (int argc, char const* const* argv)
  89. {
  90. cmsys::Encoding::CommandLineArguments args =
  91. cmsys::Encoding::CommandLineArguments::Main(argc, argv);
  92. argc = args.argc();
  93. argv = args.argv();
  94. cmSystemTools::FindCMakeResources(argv[0]);
  95. cmCPackLog log;
  96. log.SetErrorPrefix("CPack Error: ");
  97. log.SetWarningPrefix("CPack Warning: ");
  98. log.SetOutputPrefix("CPack: ");
  99. log.SetVerbosePrefix("CPack Verbose: ");
  100. cmSystemTools::EnableMSVCDebugHook();
  101. if ( cmSystemTools::GetCurrentWorkingDirectory().size() == 0 )
  102. {
  103. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  104. "Current working directory cannot be established." << std::endl);
  105. return 1;
  106. }
  107. std::string generator;
  108. bool help = false;
  109. bool helpVersion = false;
  110. bool verbose = false;
  111. bool debug = false;
  112. std::string helpFull;
  113. std::string helpMAN;
  114. std::string helpHTML;
  115. std::string cpackProjectName;
  116. std::string cpackProjectDirectory;
  117. std::string cpackBuildConfig;
  118. std::string cpackProjectVersion;
  119. std::string cpackProjectPatch;
  120. std::string cpackProjectVendor;
  121. std::string cpackConfigFile;
  122. cpackDefinitions definitions;
  123. definitions.Log = &log;
  124. cpackConfigFile = "";
  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,
  131. "CPack help");
  132. arg.AddArgument("--help-html", argT::SPACE_ARGUMENT, &helpHTML,
  133. "CPack help");
  134. arg.AddArgument("--help-man", argT::SPACE_ARGUMENT, &helpMAN, "CPack help");
  135. arg.AddArgument("--version", argT::NO_ARGUMENT, &helpVersion, "CPack help");
  136. arg.AddArgument("-V", argT::NO_ARGUMENT, &verbose, "CPack verbose");
  137. arg.AddArgument("--verbose", argT::NO_ARGUMENT, &verbose, "-V");
  138. arg.AddArgument("--debug", argT::NO_ARGUMENT, &debug, "-V");
  139. arg.AddArgument("--config", argT::SPACE_ARGUMENT, &cpackConfigFile,
  140. "CPack configuration file");
  141. arg.AddArgument("-C", argT::SPACE_ARGUMENT, &cpackBuildConfig,
  142. "CPack build configuration");
  143. arg.AddArgument("-G", argT::SPACE_ARGUMENT,
  144. &generator, "CPack generator");
  145. arg.AddArgument("-P", argT::SPACE_ARGUMENT,
  146. &cpackProjectName, "CPack project name");
  147. arg.AddArgument("-R", argT::SPACE_ARGUMENT,
  148. &cpackProjectVersion, "CPack project version");
  149. arg.AddArgument("-B", argT::SPACE_ARGUMENT,
  150. &cpackProjectDirectory, "CPack project directory");
  151. arg.AddArgument("--patch", argT::SPACE_ARGUMENT,
  152. &cpackProjectPatch, "CPack project patch");
  153. arg.AddArgument("--vendor", argT::SPACE_ARGUMENT,
  154. &cpackProjectVendor, "CPack project vendor");
  155. arg.AddCallback("-D", argT::SPACE_ARGUMENT,
  156. cpackDefinitionArgument, &definitions, "CPack Definitions");
  157. arg.SetUnknownArgumentCallback(cpackUnknownArgument);
  158. // Parse command line
  159. int parsed = arg.Parse();
  160. // Setup logging
  161. if ( verbose )
  162. {
  163. log.SetVerbose(verbose);
  164. cmCPack_Log(&log, cmCPackLog::LOG_OUTPUT, "Enable Verbose" << std::endl);
  165. }
  166. if ( debug )
  167. {
  168. log.SetDebug(debug);
  169. cmCPack_Log(&log, cmCPackLog::LOG_OUTPUT, "Enable Debug" << std::endl);
  170. }
  171. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
  172. "Read CPack config file: " << cpackConfigFile << std::endl);
  173. cmake cminst;
  174. cminst.RemoveUnscriptableCommands();
  175. cmGlobalGenerator cmgg;
  176. cmgg.SetCMakeInstance(&cminst);
  177. cmsys::auto_ptr<cmLocalGenerator> cmlg(cmgg.CreateLocalGenerator());
  178. cmMakefile* globalMF = cmlg->GetMakefile();
  179. bool cpackConfigFileSpecified = true;
  180. if ( cpackConfigFile.empty() )
  181. {
  182. cpackConfigFile = cmSystemTools::GetCurrentWorkingDirectory();
  183. cpackConfigFile += "/CPackConfig.cmake";
  184. cpackConfigFileSpecified = false;
  185. }
  186. cmCPackGeneratorFactory generators;
  187. generators.SetLogger(&log);
  188. cmCPackGenerator* cpackGenerator = 0;
  189. cmDocumentation doc;
  190. doc.addCPackStandardDocSections();
  191. /* Were we invoked to display doc or to do some work ?
  192. * Unlike cmake launching cpack with zero argument
  193. * should launch cpack using "cpackConfigFile" if it exists
  194. * in the current directory.
  195. */
  196. if((doc.CheckOptions(argc, argv,"-G")) && !(argc==1))
  197. {
  198. help = true;
  199. }
  200. else
  201. {
  202. help = false;
  203. }
  204. // This part is used for cpack documentation lookup as well.
  205. cminst.AddCMakePaths();
  206. if ( parsed && !help )
  207. {
  208. // find out which system cpack is running on, so it can setup the search
  209. // paths, so FIND_XXX() commands can be used in scripts
  210. std::string systemFile =
  211. globalMF->GetModulesFile("CMakeDetermineSystem.cmake");
  212. if (!globalMF->ReadListFile(0, systemFile.c_str()))
  213. {
  214. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  215. "Error reading CMakeDetermineSystem.cmake" << std::endl);
  216. return 1;
  217. }
  218. systemFile =
  219. globalMF->GetModulesFile("CMakeSystemSpecificInformation.cmake");
  220. if (!globalMF->ReadListFile(0, systemFile.c_str()))
  221. {
  222. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  223. "Error reading CMakeSystemSpecificInformation.cmake" << std::endl);
  224. return 1;
  225. }
  226. if ( !cpackBuildConfig.empty() )
  227. {
  228. globalMF->AddDefinition("CPACK_BUILD_CONFIG", cpackBuildConfig.c_str());
  229. }
  230. if ( cmSystemTools::FileExists(cpackConfigFile.c_str()) )
  231. {
  232. cpackConfigFile =
  233. cmSystemTools::CollapseFullPath(cpackConfigFile.c_str());
  234. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
  235. "Read CPack configuration file: " << cpackConfigFile
  236. << std::endl);
  237. if ( !globalMF->ReadListFile(0, cpackConfigFile.c_str()) )
  238. {
  239. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  240. "Problem reading CPack config file: \""
  241. << cpackConfigFile << "\"" << std::endl);
  242. return 1;
  243. }
  244. }
  245. else if ( cpackConfigFileSpecified )
  246. {
  247. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  248. "Cannot find CPack config file: \"" <<
  249. cpackConfigFile << "\"" << std::endl);
  250. return 1;
  251. }
  252. if ( !generator.empty() )
  253. {
  254. globalMF->AddDefinition("CPACK_GENERATOR", generator.c_str());
  255. }
  256. if ( !cpackProjectName.empty() )
  257. {
  258. globalMF->AddDefinition("CPACK_PACKAGE_NAME", cpackProjectName.c_str());
  259. }
  260. if ( !cpackProjectVersion.empty() )
  261. {
  262. globalMF->AddDefinition("CPACK_PACKAGE_VERSION",
  263. cpackProjectVersion.c_str());
  264. }
  265. if ( !cpackProjectVendor.empty() )
  266. {
  267. globalMF->AddDefinition("CPACK_PACKAGE_VENDOR",
  268. cpackProjectVendor.c_str());
  269. }
  270. // if this is not empty it has been set on the command line
  271. // go for it. Command line override values set in config file.
  272. if ( !cpackProjectDirectory.empty() )
  273. {
  274. globalMF->AddDefinition("CPACK_PACKAGE_DIRECTORY",
  275. cpackProjectDirectory.c_str());
  276. }
  277. // The value has not been set on the command line
  278. else
  279. {
  280. // get a default value (current working directory)
  281. cpackProjectDirectory = cmsys::SystemTools::GetCurrentWorkingDirectory();
  282. // use default value iff no value has been provided by the config file
  283. if (!globalMF->IsSet("CPACK_PACKAGE_DIRECTORY"))
  284. {
  285. globalMF->AddDefinition("CPACK_PACKAGE_DIRECTORY",
  286. cpackProjectDirectory.c_str());
  287. }
  288. }
  289. cpackDefinitions::MapType::iterator cdit;
  290. for ( cdit = definitions.Map.begin();
  291. cdit != definitions.Map.end();
  292. ++cdit )
  293. {
  294. globalMF->AddDefinition(cdit->first, cdit->second.c_str());
  295. }
  296. const char* cpackModulesPath =
  297. globalMF->GetDefinition("CPACK_MODULE_PATH");
  298. if ( cpackModulesPath )
  299. {
  300. globalMF->AddDefinition("CMAKE_MODULE_PATH", cpackModulesPath);
  301. }
  302. const char* genList = globalMF->GetDefinition("CPACK_GENERATOR");
  303. if ( !genList )
  304. {
  305. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  306. "CPack generator not specified" << std::endl);
  307. }
  308. else
  309. {
  310. std::vector<std::string> generatorsVector;
  311. cmSystemTools::ExpandListArgument(genList,
  312. generatorsVector);
  313. std::vector<std::string>::iterator it;
  314. for ( it = generatorsVector.begin();
  315. it != generatorsVector.end();
  316. ++it )
  317. {
  318. const char* gen = it->c_str();
  319. cmMakefile newMF(*globalMF);
  320. cmMakefile* mf = &newMF;
  321. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
  322. "Specified generator: " << gen << std::endl);
  323. if ( parsed && !mf->GetDefinition("CPACK_PACKAGE_NAME") )
  324. {
  325. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  326. "CPack project name not specified" << std::endl);
  327. parsed = 0;
  328. }
  329. if (parsed &&
  330. !(mf->GetDefinition("CPACK_PACKAGE_VERSION") ||
  331. (mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR") &&
  332. mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR") &&
  333. mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH"))))
  334. {
  335. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  336. "CPack project version not specified" << std::endl
  337. << "Specify CPACK_PACKAGE_VERSION, or "
  338. "CPACK_PACKAGE_VERSION_MAJOR, "
  339. "CPACK_PACKAGE_VERSION_MINOR, and CPACK_PACKAGE_VERSION_PATCH."
  340. << std::endl);
  341. parsed = 0;
  342. }
  343. if ( parsed )
  344. {
  345. cpackGenerator = generators.NewGenerator(gen);
  346. if ( !cpackGenerator )
  347. {
  348. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  349. "Cannot initialize CPack generator: "
  350. << gen << std::endl);
  351. parsed = 0;
  352. }
  353. if ( parsed && !cpackGenerator->Initialize(gen, mf) )
  354. {
  355. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  356. "Cannot initialize the generator " << gen << std::endl);
  357. parsed = 0;
  358. }
  359. if ( !mf->GetDefinition("CPACK_INSTALL_COMMANDS") &&
  360. !mf->GetDefinition("CPACK_INSTALLED_DIRECTORIES") &&
  361. !mf->GetDefinition("CPACK_INSTALL_CMAKE_PROJECTS") )
  362. {
  363. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  364. "Please specify build tree of the project that uses CMake "
  365. "using CPACK_INSTALL_CMAKE_PROJECTS, specify "
  366. "CPACK_INSTALL_COMMANDS, or specify "
  367. "CPACK_INSTALLED_DIRECTORIES."
  368. << std::endl);
  369. parsed = 0;
  370. }
  371. if ( parsed )
  372. {
  373. const char* projName = mf->GetDefinition("CPACK_PACKAGE_NAME");
  374. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "Use generator: "
  375. << cpackGenerator->GetNameOfClass() << std::endl);
  376. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "For project: "
  377. << projName << std::endl);
  378. const char* projVersion =
  379. mf->GetDefinition("CPACK_PACKAGE_VERSION");
  380. if ( !projVersion )
  381. {
  382. const char* projVersionMajor
  383. = mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR");
  384. const char* projVersionMinor
  385. = mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR");
  386. const char* projVersionPatch
  387. = mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH");
  388. cmOStringStream ostr;
  389. ostr << projVersionMajor << "." << projVersionMinor << "."
  390. << projVersionPatch;
  391. mf->AddDefinition("CPACK_PACKAGE_VERSION",
  392. ostr.str().c_str());
  393. }
  394. int res = cpackGenerator->DoPackage();
  395. if ( !res )
  396. {
  397. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  398. "Error when generating package: " << projName << std::endl);
  399. return 1;
  400. }
  401. }
  402. }
  403. }
  404. }
  405. }
  406. /* In this case we are building the documentation object
  407. * instance in order to create appropriate structure
  408. * in order to satisfy the appropriate --help-xxx request
  409. */
  410. if ( help )
  411. {
  412. // Construct and print requested documentation.
  413. doc.SetName("cpack");
  414. doc.SetSection("Name",cmDocumentationName);
  415. doc.SetSection("Usage",cmDocumentationUsage);
  416. doc.PrependSection("Options",cmDocumentationOptions);
  417. std::vector<cmDocumentationEntry> v;
  418. cmCPackGeneratorFactory::DescriptionsMap::const_iterator generatorIt;
  419. for( generatorIt = generators.GetGeneratorsList().begin();
  420. generatorIt != generators.GetGeneratorsList().end();
  421. ++ generatorIt )
  422. {
  423. cmDocumentationEntry e;
  424. e.Name = generatorIt->first.c_str();
  425. e.Brief = generatorIt->second.c_str();
  426. v.push_back(e);
  427. }
  428. doc.SetSection("Generators",v);
  429. #undef cout
  430. return doc.PrintRequestedDocumentation(std::cout)? 0:1;
  431. #define cout no_cout_use_cmCPack_Log
  432. }
  433. if (cmSystemTools::GetErrorOccuredFlag())
  434. {
  435. return 1;
  436. }
  437. return 0;
  438. }