cpack.cxx 16 KB

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