cpack.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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().empty())
  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.SetHomeDirectory("");
  175. cminst.SetHomeOutputDirectory("");
  176. cminst.GetState()->RemoveUnscriptableCommands();
  177. cmGlobalGenerator cmgg;
  178. cmgg.SetCMakeInstance(&cminst);
  179. cmsys::auto_ptr<cmLocalGenerator> cmlg(cmgg.CreateLocalGenerator());
  180. cmMakefile* globalMF = cmlg->GetMakefile();
  181. #if defined(__CYGWIN__)
  182. globalMF->AddDefinition("CMAKE_LEGACY_CYGWIN_WIN32", "0");
  183. #endif
  184. bool cpackConfigFileSpecified = true;
  185. if ( cpackConfigFile.empty() )
  186. {
  187. cpackConfigFile = cmSystemTools::GetCurrentWorkingDirectory();
  188. cpackConfigFile += "/CPackConfig.cmake";
  189. cpackConfigFileSpecified = false;
  190. }
  191. cmCPackGeneratorFactory generators;
  192. generators.SetLogger(&log);
  193. cmCPackGenerator* cpackGenerator = 0;
  194. cmDocumentation doc;
  195. doc.addCPackStandardDocSections();
  196. /* Were we invoked to display doc or to do some work ?
  197. * Unlike cmake launching cpack with zero argument
  198. * should launch cpack using "cpackConfigFile" if it exists
  199. * in the current directory.
  200. */
  201. if((doc.CheckOptions(argc, argv,"-G")) && !(argc==1))
  202. {
  203. help = true;
  204. }
  205. else
  206. {
  207. help = false;
  208. }
  209. // This part is used for cpack documentation lookup as well.
  210. cminst.AddCMakePaths();
  211. if ( parsed && !help )
  212. {
  213. // find out which system cpack is running on, so it can setup the search
  214. // paths, so FIND_XXX() commands can be used in scripts
  215. std::string systemFile =
  216. globalMF->GetModulesFile("CMakeDetermineSystem.cmake");
  217. if (!globalMF->ReadListFile(systemFile.c_str()))
  218. {
  219. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  220. "Error reading CMakeDetermineSystem.cmake" << std::endl);
  221. return 1;
  222. }
  223. systemFile =
  224. globalMF->GetModulesFile("CMakeSystemSpecificInformation.cmake");
  225. if (!globalMF->ReadListFile(systemFile.c_str()))
  226. {
  227. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  228. "Error reading CMakeSystemSpecificInformation.cmake" << std::endl);
  229. return 1;
  230. }
  231. if ( !cpackBuildConfig.empty() )
  232. {
  233. globalMF->AddDefinition("CPACK_BUILD_CONFIG", cpackBuildConfig.c_str());
  234. }
  235. if ( cmSystemTools::FileExists(cpackConfigFile.c_str()) )
  236. {
  237. cpackConfigFile =
  238. cmSystemTools::CollapseFullPath(cpackConfigFile);
  239. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
  240. "Read CPack configuration file: " << cpackConfigFile
  241. << std::endl);
  242. if ( !globalMF->ReadListFile(cpackConfigFile.c_str()) )
  243. {
  244. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  245. "Problem reading CPack config file: \""
  246. << cpackConfigFile << "\"" << std::endl);
  247. return 1;
  248. }
  249. }
  250. else if ( cpackConfigFileSpecified )
  251. {
  252. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  253. "Cannot find CPack config file: \"" <<
  254. cpackConfigFile << "\"" << std::endl);
  255. return 1;
  256. }
  257. if ( !generator.empty() )
  258. {
  259. globalMF->AddDefinition("CPACK_GENERATOR", generator.c_str());
  260. }
  261. if ( !cpackProjectName.empty() )
  262. {
  263. globalMF->AddDefinition("CPACK_PACKAGE_NAME", cpackProjectName.c_str());
  264. }
  265. if ( !cpackProjectVersion.empty() )
  266. {
  267. globalMF->AddDefinition("CPACK_PACKAGE_VERSION",
  268. cpackProjectVersion.c_str());
  269. }
  270. if ( !cpackProjectVendor.empty() )
  271. {
  272. globalMF->AddDefinition("CPACK_PACKAGE_VENDOR",
  273. cpackProjectVendor.c_str());
  274. }
  275. // if this is not empty it has been set on the command line
  276. // go for it. Command line override values set in config file.
  277. if ( !cpackProjectDirectory.empty() )
  278. {
  279. globalMF->AddDefinition("CPACK_PACKAGE_DIRECTORY",
  280. cpackProjectDirectory.c_str());
  281. }
  282. // The value has not been set on the command line
  283. else
  284. {
  285. // get a default value (current working directory)
  286. cpackProjectDirectory = cmsys::SystemTools::GetCurrentWorkingDirectory();
  287. // use default value iff no value has been provided by the config file
  288. if (!globalMF->IsSet("CPACK_PACKAGE_DIRECTORY"))
  289. {
  290. globalMF->AddDefinition("CPACK_PACKAGE_DIRECTORY",
  291. cpackProjectDirectory.c_str());
  292. }
  293. }
  294. cpackDefinitions::MapType::iterator cdit;
  295. for ( cdit = definitions.Map.begin();
  296. cdit != definitions.Map.end();
  297. ++cdit )
  298. {
  299. globalMF->AddDefinition(cdit->first, cdit->second.c_str());
  300. }
  301. const char* cpackModulesPath =
  302. globalMF->GetDefinition("CPACK_MODULE_PATH");
  303. if ( cpackModulesPath )
  304. {
  305. globalMF->AddDefinition("CMAKE_MODULE_PATH", cpackModulesPath);
  306. }
  307. const char* genList = globalMF->GetDefinition("CPACK_GENERATOR");
  308. if ( !genList )
  309. {
  310. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  311. "CPack generator not specified" << std::endl);
  312. }
  313. else
  314. {
  315. std::vector<std::string> generatorsVector;
  316. cmSystemTools::ExpandListArgument(genList,
  317. generatorsVector);
  318. std::vector<std::string>::iterator it;
  319. for ( it = generatorsVector.begin();
  320. it != generatorsVector.end();
  321. ++it )
  322. {
  323. const char* gen = it->c_str();
  324. cmMakefile::ScopePushPop raii(globalMF);
  325. cmMakefile* mf = globalMF;
  326. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
  327. "Specified generator: " << gen << std::endl);
  328. if ( parsed && !mf->GetDefinition("CPACK_PACKAGE_NAME") )
  329. {
  330. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  331. "CPack project name not specified" << std::endl);
  332. parsed = 0;
  333. }
  334. if (parsed &&
  335. !(mf->GetDefinition("CPACK_PACKAGE_VERSION") ||
  336. (mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR") &&
  337. mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR") &&
  338. mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH"))))
  339. {
  340. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  341. "CPack project version not specified" << std::endl
  342. << "Specify CPACK_PACKAGE_VERSION, or "
  343. "CPACK_PACKAGE_VERSION_MAJOR, "
  344. "CPACK_PACKAGE_VERSION_MINOR, and CPACK_PACKAGE_VERSION_PATCH."
  345. << std::endl);
  346. parsed = 0;
  347. }
  348. if ( parsed )
  349. {
  350. cpackGenerator = generators.NewGenerator(gen);
  351. if ( !cpackGenerator )
  352. {
  353. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  354. "Cannot initialize CPack generator: "
  355. << gen << std::endl);
  356. parsed = 0;
  357. }
  358. if ( parsed && !cpackGenerator->Initialize(gen, mf) )
  359. {
  360. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  361. "Cannot initialize the generator " << gen << std::endl);
  362. parsed = 0;
  363. }
  364. if ( !mf->GetDefinition("CPACK_INSTALL_COMMANDS") &&
  365. !mf->GetDefinition("CPACK_INSTALLED_DIRECTORIES") &&
  366. !mf->GetDefinition("CPACK_INSTALL_CMAKE_PROJECTS") )
  367. {
  368. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  369. "Please specify build tree of the project that uses CMake "
  370. "using CPACK_INSTALL_CMAKE_PROJECTS, specify "
  371. "CPACK_INSTALL_COMMANDS, or specify "
  372. "CPACK_INSTALLED_DIRECTORIES."
  373. << std::endl);
  374. parsed = 0;
  375. }
  376. if ( parsed )
  377. {
  378. const char* projName = mf->GetDefinition("CPACK_PACKAGE_NAME");
  379. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "Use generator: "
  380. << cpackGenerator->GetNameOfClass() << std::endl);
  381. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "For project: "
  382. << projName << std::endl);
  383. const char* projVersion =
  384. mf->GetDefinition("CPACK_PACKAGE_VERSION");
  385. if ( !projVersion )
  386. {
  387. const char* projVersionMajor
  388. = mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR");
  389. const char* projVersionMinor
  390. = mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR");
  391. const char* projVersionPatch
  392. = mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH");
  393. std::ostringstream ostr;
  394. ostr << projVersionMajor << "." << projVersionMinor << "."
  395. << projVersionPatch;
  396. mf->AddDefinition("CPACK_PACKAGE_VERSION",
  397. ostr.str().c_str());
  398. }
  399. int res = cpackGenerator->DoPackage();
  400. if ( !res )
  401. {
  402. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  403. "Error when generating package: " << projName << std::endl);
  404. return 1;
  405. }
  406. }
  407. }
  408. }
  409. }
  410. }
  411. /* In this case we are building the documentation object
  412. * instance in order to create appropriate structure
  413. * in order to satisfy the appropriate --help-xxx request
  414. */
  415. if ( help )
  416. {
  417. // Construct and print requested documentation.
  418. doc.SetName("cpack");
  419. doc.SetSection("Name",cmDocumentationName);
  420. doc.SetSection("Usage",cmDocumentationUsage);
  421. doc.PrependSection("Options",cmDocumentationOptions);
  422. std::vector<cmDocumentationEntry> v;
  423. cmCPackGeneratorFactory::DescriptionsMap::const_iterator generatorIt;
  424. for( generatorIt = generators.GetGeneratorsList().begin();
  425. generatorIt != generators.GetGeneratorsList().end();
  426. ++ generatorIt )
  427. {
  428. cmDocumentationEntry e;
  429. e.Name = generatorIt->first.c_str();
  430. e.Brief = generatorIt->second.c_str();
  431. v.push_back(e);
  432. }
  433. doc.SetSection("Generators",v);
  434. #undef cout
  435. return doc.PrintRequestedDocumentation(std::cout)? 0:1;
  436. #define cout no_cout_use_cmCPack_Log
  437. }
  438. if (cmSystemTools::GetErrorOccuredFlag())
  439. {
  440. return 1;
  441. }
  442. return 0;
  443. }