cpack.cxx 16 KB

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