cpack.cxx 16 KB

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