cpack.cxx 16 KB

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