cpack.cxx 16 KB

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