cpack.cxx 16 KB

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