cpack.cxx 20 KB

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