cpack.cxx 16 KB

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