cpack.cxx 16 KB

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