cpack.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmSystemTools.h"
  14. // Need these for documentation support.
  15. #include "cmake.h"
  16. #include "cmDocumentation.h"
  17. #include "cmCPackGenerators.h"
  18. #include "cmCPackGenericGenerator.h"
  19. #include "cmake.h"
  20. #include "cmGlobalGenerator.h"
  21. #include "cmLocalGenerator.h"
  22. #include "cmMakefile.h"
  23. #include "cmCPackLog.h"
  24. #include <cmsys/CommandLineArguments.hxx>
  25. //----------------------------------------------------------------------------
  26. static const char * cmDocumentationName[][3] =
  27. {
  28. {0,
  29. " cpack - Packaging driver provided by CMake.", 0},
  30. {0,0,0}
  31. };
  32. //----------------------------------------------------------------------------
  33. static const char * cmDocumentationUsage[][3] =
  34. {
  35. {0,
  36. " cpack -G <generator> [options]",
  37. 0},
  38. {0,0,0}
  39. };
  40. //----------------------------------------------------------------------------
  41. static const char * cmDocumentationDescription[][3] =
  42. {
  43. {0,
  44. "The \"cpack\" executable is the CMake packaging program. "
  45. "CMake-generated build trees created for projects that use "
  46. "the INSTALL_* commands have packaging support. "
  47. "This program will generate the package.", 0},
  48. CMAKE_STANDARD_INTRODUCTION,
  49. {0,0,0}
  50. };
  51. //----------------------------------------------------------------------------
  52. static const char * cmDocumentationOptions[][3] =
  53. {
  54. {"-G <generator>", "Use the specified generator to generate package.",
  55. "CPack may support multiple native packaging systems on certain "
  56. "platforms. A generator is responsible for generating input files for "
  57. "particular system and invoking that systems. Possible generator names "
  58. "are specified in the Generators section." },
  59. {"-C <Configuration>", "Specify the project configuration",
  60. "This option specifies the configuration that the project was build "
  61. "with, for example 'Debug', 'Release'." },
  62. {"-D <var>=<value>", "Set a CPack variable.", \
  63. "Set a variable that can be used by the generator."}, \
  64. {"--config <config file>", "Specify the config file.",
  65. "Specify the config file to use to create the package. By default "
  66. "CPackConfig.cmake in the current directory will be used." },
  67. {0,0,0}
  68. };
  69. //----------------------------------------------------------------------------
  70. static const char * cmDocumentationSeeAlso[][3] =
  71. {
  72. {0, "cmake", 0},
  73. {0, "ccmake", 0},
  74. {0, 0, 0}
  75. };
  76. //----------------------------------------------------------------------------
  77. int cpackUnknownArgument(const char*, void*)
  78. {
  79. return 1;
  80. }
  81. //----------------------------------------------------------------------------
  82. struct cpackDefinitions
  83. {
  84. typedef std::map<cmStdString, cmStdString> MapType;
  85. MapType Map;
  86. cmCPackLog *Log;
  87. };
  88. //----------------------------------------------------------------------------
  89. int cpackDefinitionArgument(const char* argument, const char* cValue,
  90. void* call_data)
  91. {
  92. (void)argument;
  93. cpackDefinitions* def = static_cast<cpackDefinitions*>(call_data);
  94. std::string value = cValue;
  95. size_t pos = value.find_first_of("=");
  96. if ( pos == std::string::npos )
  97. {
  98. cmCPack_Log(def->Log, cmCPackLog::LOG_ERROR,
  99. "Please specify CPack definitions as: KEY=VALUE" << std::endl);
  100. return 0;
  101. }
  102. std::string key = value.substr(0, pos);
  103. value = value.c_str() + pos + 1;
  104. def->Map[key] = value;
  105. cmCPack_Log(def->Log, cmCPackLog::LOG_DEBUG, "Set CPack variable: "
  106. << key.c_str() << " to \"" << value.c_str() << "\"" << std::endl);
  107. return 1;
  108. }
  109. //----------------------------------------------------------------------------
  110. // this is CPack.
  111. int main (int argc, char *argv[])
  112. {
  113. cmCPackLog log;
  114. log.SetErrorPrefix("CPack Error: ");
  115. log.SetWarningPrefix("CPack Warning: ");
  116. log.SetOutputPrefix("CPack: ");
  117. log.SetVerbosePrefix("CPack Verbose: ");
  118. cmSystemTools::EnableMSVCDebugHook();
  119. if ( cmSystemTools::GetCurrentWorkingDirectory().size() == 0 )
  120. {
  121. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  122. "Current working directory cannot be established." << std::endl);
  123. }
  124. std::string generator;
  125. bool help = false;
  126. bool helpVersion = false;
  127. bool verbose = false;
  128. bool debug = false;
  129. std::string helpFull;
  130. std::string helpMAN;
  131. std::string helpHTML;
  132. std::string cpackProjectName;
  133. std::string cpackProjectDirectory
  134. = cmsys::SystemTools::GetCurrentWorkingDirectory();
  135. std::string cpackBuildConfig;
  136. std::string cpackProjectVersion;
  137. std::string cpackProjectPatch;
  138. std::string cpackProjectVendor;
  139. std::string cpackConfigFile;
  140. cpackDefinitions definitions;
  141. definitions.Log = &log;
  142. cpackConfigFile = "";
  143. cmDocumentation doc;
  144. cmsys::CommandLineArguments arg;
  145. arg.Initialize(argc, argv);
  146. typedef cmsys::CommandLineArguments argT;
  147. // Help arguments
  148. arg.AddArgument("--help", argT::NO_ARGUMENT, &help, "CPack help");
  149. arg.AddArgument("--help-full", argT::SPACE_ARGUMENT, &helpFull,
  150. "CPack help");
  151. arg.AddArgument("--help-html", argT::SPACE_ARGUMENT, &helpHTML,
  152. "CPack help");
  153. arg.AddArgument("--help-man", argT::SPACE_ARGUMENT, &helpMAN, "CPack help");
  154. arg.AddArgument("--version", argT::NO_ARGUMENT, &helpVersion, "CPack help");
  155. arg.AddArgument("-V", argT::NO_ARGUMENT, &verbose, "CPack verbose");
  156. arg.AddArgument("--verbose", argT::NO_ARGUMENT, &verbose, "-V");
  157. arg.AddArgument("--debug", argT::NO_ARGUMENT, &debug, "-V");
  158. arg.AddArgument("--config", argT::SPACE_ARGUMENT, &cpackConfigFile,
  159. "CPack configuration file");
  160. arg.AddArgument("-C", argT::SPACE_ARGUMENT, &cpackBuildConfig,
  161. "CPack build configuration");
  162. arg.AddArgument("-G", argT::SPACE_ARGUMENT,
  163. &generator, "CPack generator");
  164. arg.AddArgument("-P", argT::SPACE_ARGUMENT,
  165. &cpackProjectName, "CPack project name");
  166. arg.AddArgument("-R", argT::SPACE_ARGUMENT,
  167. &cpackProjectVersion, "CPack project version");
  168. arg.AddArgument("-B", argT::SPACE_ARGUMENT,
  169. &cpackProjectDirectory, "CPack project directory");
  170. arg.AddArgument("--patch", argT::SPACE_ARGUMENT,
  171. &cpackProjectPatch, "CPack project patch");
  172. arg.AddArgument("--vendor", argT::SPACE_ARGUMENT,
  173. &cpackProjectVendor, "CPack project vendor");
  174. arg.AddCallback("-D", argT::SPACE_ARGUMENT,
  175. cpackDefinitionArgument, &definitions, "CPack Definitions");
  176. arg.SetUnknownArgumentCallback(cpackUnknownArgument);
  177. // Parse command line
  178. int parsed = arg.Parse();
  179. // Setup logging
  180. if ( verbose )
  181. {
  182. log.SetVerbose(verbose);
  183. cmCPack_Log(&log, cmCPackLog::LOG_OUTPUT, "Enable Verbse" << std::endl);
  184. }
  185. if ( debug )
  186. {
  187. log.SetDebug(debug);
  188. cmCPack_Log(&log, cmCPackLog::LOG_OUTPUT, "Enable Debug" << std::endl);
  189. }
  190. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
  191. "Read CPack config file: " << cpackConfigFile.c_str() << std::endl);
  192. cmake cminst;
  193. cminst.RemoveUnscriptableCommands();
  194. cmGlobalGenerator cmgg;
  195. cmgg.SetCMakeInstance(&cminst);
  196. cmLocalGenerator* cmlg = cmgg.CreateLocalGenerator();
  197. cmMakefile* globalMF = cmlg->GetMakefile();
  198. bool cpackConfigFileSpecified = true;
  199. if ( cpackConfigFile.empty() )
  200. {
  201. cpackConfigFile = cmSystemTools::GetCurrentWorkingDirectory();
  202. cpackConfigFile += "/CPackConfig.cmake";
  203. cpackConfigFileSpecified = false;
  204. }
  205. cmCPackGenerators generators;
  206. generators.SetLogger(&log);
  207. cmCPackGenericGenerator* cpackGenerator = 0;
  208. if ( !helpFull.empty() || !helpMAN.empty() ||
  209. !helpHTML.empty() || helpVersion )
  210. {
  211. help = true;
  212. }
  213. if ( parsed && !help )
  214. {
  215. // find out which system cpack is running on, so it can setup the search
  216. // paths, so FIND_XXX() commands can be used in scripts
  217. cminst.AddCMakePaths(argv[0]);
  218. std::string systemFile =
  219. globalMF->GetModulesFile("CMakeDetermineSystem.cmake");
  220. if (!globalMF->ReadListFile(0, systemFile.c_str()))
  221. {
  222. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  223. "Error reading CMakeDetermineSystem.cmake" << std::endl);
  224. return 1;
  225. }
  226. systemFile =
  227. globalMF->GetModulesFile("CMakeSystemSpecificInformation.cmake");
  228. if (!globalMF->ReadListFile(0, systemFile.c_str()))
  229. {
  230. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  231. "Error reading CMakeSystemSpecificInformation.cmake" << std::endl);
  232. return 1;
  233. }
  234. if ( cmSystemTools::FileExists(cpackConfigFile.c_str()) )
  235. {
  236. cpackConfigFile =
  237. cmSystemTools::CollapseFullPath(cpackConfigFile.c_str());
  238. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
  239. "Read CPack configuration file: " << cpackConfigFile.c_str()
  240. << std::endl);
  241. if ( !globalMF->ReadListFile(0, cpackConfigFile.c_str()) )
  242. {
  243. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  244. "Problem reading CPack config file: \""
  245. << cpackConfigFile.c_str() << "\"" << std::endl);
  246. return 1;
  247. }
  248. }
  249. else if ( cpackConfigFileSpecified )
  250. {
  251. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  252. "Cannot find CPack config file: \"" << cpackConfigFile.c_str()
  253. << "\"" << std::endl);
  254. return 1;
  255. }
  256. if ( !generator.empty() )
  257. {
  258. globalMF->AddDefinition("CPACK_GENERATOR", generator.c_str());
  259. }
  260. if ( !cpackProjectName.empty() )
  261. {
  262. globalMF->AddDefinition("CPACK_PACKAGE_NAME", cpackProjectName.c_str());
  263. }
  264. if ( !cpackProjectVersion.empty() )
  265. {
  266. globalMF->AddDefinition("CPACK_PACKAGE_VERSION",
  267. cpackProjectVersion.c_str());
  268. }
  269. if ( !cpackProjectVendor.empty() )
  270. {
  271. globalMF->AddDefinition("CPACK_PACKAGE_VENDOR",
  272. cpackProjectVendor.c_str());
  273. }
  274. if ( !cpackProjectDirectory.empty() )
  275. {
  276. globalMF->AddDefinition("CPACK_PACKAGE_DIRECTORY",
  277. cpackProjectDirectory.c_str());
  278. }
  279. if ( !cpackBuildConfig.empty() )
  280. {
  281. globalMF->AddDefinition("CPACK_BUILD_CONFIG", cpackBuildConfig.c_str());
  282. }
  283. cpackDefinitions::MapType::iterator cdit;
  284. for ( cdit = definitions.Map.begin();
  285. cdit != definitions.Map.end();
  286. ++cdit )
  287. {
  288. globalMF->AddDefinition(cdit->first.c_str(), cdit->second.c_str());
  289. }
  290. const char* cpackModulesPath =
  291. globalMF->GetDefinition("CPACK_MODULE_PATH");
  292. if ( cpackModulesPath )
  293. {
  294. globalMF->AddDefinition("CMAKE_MODULE_PATH", cpackModulesPath);
  295. }
  296. const char* genList = globalMF->GetDefinition("CPACK_GENERATOR");
  297. if ( !genList )
  298. {
  299. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  300. "CPack generator not specified" << std::endl);
  301. parsed = 0;
  302. }
  303. else
  304. {
  305. std::vector<std::string> generatorsVector;
  306. cmSystemTools::ExpandListArgument(genList,
  307. generatorsVector);
  308. std::vector<std::string>::iterator it;
  309. for ( it = generatorsVector.begin();
  310. it != generatorsVector.end();
  311. ++it )
  312. {
  313. const char* gen = it->c_str();
  314. cmMakefile newMF(*globalMF);
  315. cmMakefile* mf = &newMF;
  316. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
  317. "Specified generator: " << gen << std::endl);
  318. if ( parsed && !mf->GetDefinition("CPACK_PACKAGE_NAME") )
  319. {
  320. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  321. "CPack project name not specified" << std::endl);
  322. parsed = 0;
  323. }
  324. if ( parsed && !(mf->GetDefinition("CPACK_PACKAGE_VERSION")
  325. || mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR") &&
  326. mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR")
  327. && mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH")) )
  328. {
  329. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  330. "CPack project version not specified" << std::endl
  331. << "Specify CPACK_PACKAGE_VERSION, or "
  332. "CPACK_PACKAGE_VERSION_MAJOR, "
  333. "CPACK_PACKAGE_VERSION_MINOR, and CPACK_PACKAGE_VERSION_PATCH."
  334. << std::endl);
  335. parsed = 0;
  336. }
  337. if ( parsed )
  338. {
  339. cpackGenerator = generators.NewGenerator(gen);
  340. if ( !cpackGenerator )
  341. {
  342. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  343. "Cannot initialize CPack generator: "
  344. << gen << std::endl);
  345. parsed = 0;
  346. }
  347. if ( parsed && !cpackGenerator->Initialize(gen, mf, argv[0]) )
  348. {
  349. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  350. "Cannot initialize the generator " << gen << std::endl);
  351. parsed = 0;
  352. }
  353. if ( !mf->GetDefinition("CPACK_INSTALL_COMMANDS") &&
  354. !mf->GetDefinition("CPACK_INSTALLED_DIRECTORIES") &&
  355. !mf->GetDefinition("CPACK_INSTALL_CMAKE_PROJECTS") )
  356. {
  357. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  358. "Please specify build tree of the project that uses CMake "
  359. "using CPACK_INSTALL_CMAKE_PROJECTS, specify "
  360. "CPACK_INSTALL_COMMANDS, or specify "
  361. "CPACK_INSTALLED_DIRECTORIES."
  362. << std::endl);
  363. parsed = 0;
  364. }
  365. if ( parsed )
  366. {
  367. #ifdef _WIN32
  368. std::string comspec = "cmw9xcom.exe";
  369. cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
  370. #endif
  371. const char* projName = mf->GetDefinition("CPACK_PACKAGE_NAME");
  372. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "Use generator: "
  373. << cpackGenerator->GetNameOfClass() << std::endl);
  374. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "For project: "
  375. << projName << std::endl);
  376. const char* projVersion =
  377. mf->GetDefinition("CPACK_PACKAGE_VERSION");
  378. if ( !projVersion )
  379. {
  380. const char* projVersionMajor
  381. = mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR");
  382. const char* projVersionMinor
  383. = mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR");
  384. const char* projVersionPatch
  385. = mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH");
  386. cmOStringStream ostr;
  387. ostr << projVersionMajor << "." << projVersionMinor << "."
  388. << projVersionPatch;
  389. mf->AddDefinition("CPACK_PACKAGE_VERSION",
  390. ostr.str().c_str());
  391. }
  392. int res = cpackGenerator->ProcessGenerator();
  393. if ( !res )
  394. {
  395. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  396. "Error when generating package: " << projName << std::endl);
  397. return 1;
  398. }
  399. }
  400. }
  401. }
  402. }
  403. }
  404. if ( help )
  405. {
  406. doc.CheckOptions(argc, argv);
  407. // Construct and print requested documentation.
  408. doc.SetName("cpack");
  409. doc.SetSection("Name",cmDocumentationName);
  410. doc.SetSection("Usage",cmDocumentationUsage);
  411. doc.SetSection("Description",cmDocumentationDescription);
  412. doc.SetSection("Options",cmDocumentationOptions);
  413. std::vector<cmDocumentationEntry> v;
  414. cmCPackGenerators::DescriptionsMap::const_iterator generatorIt;
  415. for( generatorIt = generators.GetGeneratorsList().begin();
  416. generatorIt != generators.GetGeneratorsList().end();
  417. ++ generatorIt )
  418. {
  419. cmDocumentationEntry e;
  420. e.Name = generatorIt->first.c_str();
  421. e.Brief = generatorIt->second.c_str();
  422. e.Full = "";
  423. v.push_back(e);
  424. }
  425. doc.SetSection("Generators",v);
  426. doc.SetSeeAlsoList(cmDocumentationSeeAlso);
  427. #undef cout
  428. return doc.PrintRequestedDocumentation(std::cout)? 0:1;
  429. #define cout no_cout_use_cmCPack_Log
  430. }
  431. return 0;
  432. }