cpack.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 "cmCPackGeneratorFactory.h"
  18. #include "cmCPackGenerator.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. cmSystemTools::FindExecutableDirectory(argv[0]);
  114. cmCPackLog log;
  115. log.SetErrorPrefix("CPack Error: ");
  116. log.SetWarningPrefix("CPack Warning: ");
  117. log.SetOutputPrefix("CPack: ");
  118. log.SetVerbosePrefix("CPack Verbose: ");
  119. cmSystemTools::EnableMSVCDebugHook();
  120. if ( cmSystemTools::GetCurrentWorkingDirectory().size() == 0 )
  121. {
  122. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  123. "Current working directory cannot be established." << std::endl);
  124. }
  125. std::string generator;
  126. bool help = false;
  127. bool helpVersion = false;
  128. bool verbose = false;
  129. bool debug = false;
  130. std::string helpFull;
  131. std::string helpMAN;
  132. std::string helpHTML;
  133. std::string cpackProjectName;
  134. std::string cpackProjectDirectory
  135. = cmsys::SystemTools::GetCurrentWorkingDirectory();
  136. std::string cpackBuildConfig;
  137. std::string cpackProjectVersion;
  138. std::string cpackProjectPatch;
  139. std::string cpackProjectVendor;
  140. std::string cpackConfigFile;
  141. cpackDefinitions definitions;
  142. definitions.Log = &log;
  143. cpackConfigFile = "";
  144. cmDocumentation doc;
  145. cmsys::CommandLineArguments arg;
  146. arg.Initialize(argc, argv);
  147. typedef cmsys::CommandLineArguments argT;
  148. // Help arguments
  149. arg.AddArgument("--help", argT::NO_ARGUMENT, &help, "CPack help");
  150. arg.AddArgument("--help-full", argT::SPACE_ARGUMENT, &helpFull,
  151. "CPack help");
  152. arg.AddArgument("--help-html", argT::SPACE_ARGUMENT, &helpHTML,
  153. "CPack help");
  154. arg.AddArgument("--help-man", argT::SPACE_ARGUMENT, &helpMAN, "CPack help");
  155. arg.AddArgument("--version", argT::NO_ARGUMENT, &helpVersion, "CPack help");
  156. arg.AddArgument("-V", argT::NO_ARGUMENT, &verbose, "CPack verbose");
  157. arg.AddArgument("--verbose", argT::NO_ARGUMENT, &verbose, "-V");
  158. arg.AddArgument("--debug", argT::NO_ARGUMENT, &debug, "-V");
  159. arg.AddArgument("--config", argT::SPACE_ARGUMENT, &cpackConfigFile,
  160. "CPack configuration file");
  161. arg.AddArgument("-C", argT::SPACE_ARGUMENT, &cpackBuildConfig,
  162. "CPack build configuration");
  163. arg.AddArgument("-G", argT::SPACE_ARGUMENT,
  164. &generator, "CPack generator");
  165. arg.AddArgument("-P", argT::SPACE_ARGUMENT,
  166. &cpackProjectName, "CPack project name");
  167. arg.AddArgument("-R", argT::SPACE_ARGUMENT,
  168. &cpackProjectVersion, "CPack project version");
  169. arg.AddArgument("-B", argT::SPACE_ARGUMENT,
  170. &cpackProjectDirectory, "CPack project directory");
  171. arg.AddArgument("--patch", argT::SPACE_ARGUMENT,
  172. &cpackProjectPatch, "CPack project patch");
  173. arg.AddArgument("--vendor", argT::SPACE_ARGUMENT,
  174. &cpackProjectVendor, "CPack project vendor");
  175. arg.AddCallback("-D", argT::SPACE_ARGUMENT,
  176. cpackDefinitionArgument, &definitions, "CPack Definitions");
  177. arg.SetUnknownArgumentCallback(cpackUnknownArgument);
  178. // Parse command line
  179. int parsed = arg.Parse();
  180. // Setup logging
  181. if ( verbose )
  182. {
  183. log.SetVerbose(verbose);
  184. cmCPack_Log(&log, cmCPackLog::LOG_OUTPUT, "Enable Verbse" << std::endl);
  185. }
  186. if ( debug )
  187. {
  188. log.SetDebug(debug);
  189. cmCPack_Log(&log, cmCPackLog::LOG_OUTPUT, "Enable Debug" << std::endl);
  190. }
  191. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
  192. "Read CPack config file: " << cpackConfigFile.c_str() << std::endl);
  193. cmake cminst;
  194. cminst.RemoveUnscriptableCommands();
  195. cmGlobalGenerator cmgg;
  196. cmgg.SetCMakeInstance(&cminst);
  197. cmLocalGenerator* cmlg = cmgg.CreateLocalGenerator();
  198. cmMakefile* globalMF = cmlg->GetMakefile();
  199. bool cpackConfigFileSpecified = true;
  200. if ( cpackConfigFile.empty() )
  201. {
  202. cpackConfigFile = cmSystemTools::GetCurrentWorkingDirectory();
  203. cpackConfigFile += "/CPackConfig.cmake";
  204. cpackConfigFileSpecified = false;
  205. }
  206. cmCPackGeneratorFactory generators;
  207. generators.SetLogger(&log);
  208. cmCPackGenerator* cpackGenerator = 0;
  209. if ( !helpFull.empty() || !helpMAN.empty() ||
  210. !helpHTML.empty() || helpVersion )
  211. {
  212. help = true;
  213. }
  214. if ( parsed && !help )
  215. {
  216. // find out which system cpack is running on, so it can setup the search
  217. // paths, so FIND_XXX() commands can be used in scripts
  218. cminst.AddCMakePaths();
  219. std::string systemFile =
  220. globalMF->GetModulesFile("CMakeDetermineSystem.cmake");
  221. if (!globalMF->ReadListFile(0, systemFile.c_str()))
  222. {
  223. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  224. "Error reading CMakeDetermineSystem.cmake" << std::endl);
  225. return 1;
  226. }
  227. systemFile =
  228. globalMF->GetModulesFile("CMakeSystemSpecificInformation.cmake");
  229. if (!globalMF->ReadListFile(0, systemFile.c_str()))
  230. {
  231. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  232. "Error reading CMakeSystemSpecificInformation.cmake" << std::endl);
  233. return 1;
  234. }
  235. if ( cmSystemTools::FileExists(cpackConfigFile.c_str()) )
  236. {
  237. cpackConfigFile =
  238. cmSystemTools::CollapseFullPath(cpackConfigFile.c_str());
  239. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
  240. "Read CPack configuration file: " << cpackConfigFile.c_str()
  241. << std::endl);
  242. if ( !globalMF->ReadListFile(0, cpackConfigFile.c_str()) )
  243. {
  244. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  245. "Problem reading CPack config file: \""
  246. << cpackConfigFile.c_str() << "\"" << std::endl);
  247. return 1;
  248. }
  249. }
  250. else if ( cpackConfigFileSpecified )
  251. {
  252. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  253. "Cannot find CPack config file: \"" << cpackConfigFile.c_str()
  254. << "\"" << std::endl);
  255. return 1;
  256. }
  257. if ( !generator.empty() )
  258. {
  259. globalMF->AddDefinition("CPACK_GENERATOR", generator.c_str());
  260. }
  261. if ( !cpackProjectName.empty() )
  262. {
  263. globalMF->AddDefinition("CPACK_PACKAGE_NAME", cpackProjectName.c_str());
  264. }
  265. if ( !cpackProjectVersion.empty() )
  266. {
  267. globalMF->AddDefinition("CPACK_PACKAGE_VERSION",
  268. cpackProjectVersion.c_str());
  269. }
  270. if ( !cpackProjectVendor.empty() )
  271. {
  272. globalMF->AddDefinition("CPACK_PACKAGE_VENDOR",
  273. cpackProjectVendor.c_str());
  274. }
  275. if ( !cpackProjectDirectory.empty() )
  276. {
  277. globalMF->AddDefinition("CPACK_PACKAGE_DIRECTORY",
  278. cpackProjectDirectory.c_str());
  279. }
  280. if ( !cpackBuildConfig.empty() )
  281. {
  282. globalMF->AddDefinition("CPACK_BUILD_CONFIG", cpackBuildConfig.c_str());
  283. }
  284. cpackDefinitions::MapType::iterator cdit;
  285. for ( cdit = definitions.Map.begin();
  286. cdit != definitions.Map.end();
  287. ++cdit )
  288. {
  289. globalMF->AddDefinition(cdit->first.c_str(), cdit->second.c_str());
  290. }
  291. const char* cpackModulesPath =
  292. globalMF->GetDefinition("CPACK_MODULE_PATH");
  293. if ( cpackModulesPath )
  294. {
  295. globalMF->AddDefinition("CMAKE_MODULE_PATH", cpackModulesPath);
  296. }
  297. const char* genList = globalMF->GetDefinition("CPACK_GENERATOR");
  298. if ( !genList )
  299. {
  300. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  301. "CPack generator not specified" << std::endl);
  302. parsed = 0;
  303. }
  304. else
  305. {
  306. std::vector<std::string> generatorsVector;
  307. cmSystemTools::ExpandListArgument(genList,
  308. generatorsVector);
  309. std::vector<std::string>::iterator it;
  310. for ( it = generatorsVector.begin();
  311. it != generatorsVector.end();
  312. ++it )
  313. {
  314. const char* gen = it->c_str();
  315. cmMakefile newMF(*globalMF);
  316. cmMakefile* mf = &newMF;
  317. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
  318. "Specified generator: " << gen << std::endl);
  319. if ( parsed && !mf->GetDefinition("CPACK_PACKAGE_NAME") )
  320. {
  321. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  322. "CPack project name not specified" << std::endl);
  323. parsed = 0;
  324. }
  325. if ( parsed && !(mf->GetDefinition("CPACK_PACKAGE_VERSION")
  326. || mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR") &&
  327. mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR")
  328. && mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH")) )
  329. {
  330. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  331. "CPack project version not specified" << std::endl
  332. << "Specify CPACK_PACKAGE_VERSION, or "
  333. "CPACK_PACKAGE_VERSION_MAJOR, "
  334. "CPACK_PACKAGE_VERSION_MINOR, and CPACK_PACKAGE_VERSION_PATCH."
  335. << std::endl);
  336. parsed = 0;
  337. }
  338. if ( parsed )
  339. {
  340. cpackGenerator = generators.NewGenerator(gen);
  341. if ( !cpackGenerator )
  342. {
  343. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  344. "Cannot initialize CPack generator: "
  345. << gen << std::endl);
  346. parsed = 0;
  347. }
  348. if ( parsed && !cpackGenerator->Initialize(gen, mf, argv[0]) )
  349. {
  350. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  351. "Cannot initialize the generator " << gen << std::endl);
  352. parsed = 0;
  353. }
  354. if ( !mf->GetDefinition("CPACK_INSTALL_COMMANDS") &&
  355. !mf->GetDefinition("CPACK_INSTALLED_DIRECTORIES") &&
  356. !mf->GetDefinition("CPACK_INSTALL_CMAKE_PROJECTS") )
  357. {
  358. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  359. "Please specify build tree of the project that uses CMake "
  360. "using CPACK_INSTALL_CMAKE_PROJECTS, specify "
  361. "CPACK_INSTALL_COMMANDS, or specify "
  362. "CPACK_INSTALLED_DIRECTORIES."
  363. << std::endl);
  364. parsed = 0;
  365. }
  366. if ( parsed )
  367. {
  368. #ifdef _WIN32
  369. std::string comspec = "cmw9xcom.exe";
  370. cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
  371. #endif
  372. const char* projName = mf->GetDefinition("CPACK_PACKAGE_NAME");
  373. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "Use generator: "
  374. << cpackGenerator->GetNameOfClass() << std::endl);
  375. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "For project: "
  376. << projName << std::endl);
  377. const char* projVersion =
  378. mf->GetDefinition("CPACK_PACKAGE_VERSION");
  379. if ( !projVersion )
  380. {
  381. const char* projVersionMajor
  382. = mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR");
  383. const char* projVersionMinor
  384. = mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR");
  385. const char* projVersionPatch
  386. = mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH");
  387. cmOStringStream ostr;
  388. ostr << projVersionMajor << "." << projVersionMinor << "."
  389. << projVersionPatch;
  390. mf->AddDefinition("CPACK_PACKAGE_VERSION",
  391. ostr.str().c_str());
  392. }
  393. int res = cpackGenerator->DoPackage();
  394. if ( !res )
  395. {
  396. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  397. "Error when generating package: " << projName << std::endl);
  398. return 1;
  399. }
  400. }
  401. }
  402. }
  403. }
  404. }
  405. if ( help )
  406. {
  407. doc.CheckOptions(argc, argv);
  408. // Construct and print requested documentation.
  409. doc.SetName("cpack");
  410. doc.SetSection("Name",cmDocumentationName);
  411. doc.SetSection("Usage",cmDocumentationUsage);
  412. doc.SetSection("Description",cmDocumentationDescription);
  413. doc.SetSection("Options",cmDocumentationOptions);
  414. std::vector<cmDocumentationEntry> v;
  415. cmCPackGeneratorFactory::DescriptionsMap::const_iterator generatorIt;
  416. for( generatorIt = generators.GetGeneratorsList().begin();
  417. generatorIt != generators.GetGeneratorsList().end();
  418. ++ generatorIt )
  419. {
  420. cmDocumentationEntry e;
  421. e.Name = generatorIt->first.c_str();
  422. e.Brief = generatorIt->second.c_str();
  423. e.Full = "";
  424. v.push_back(e);
  425. }
  426. doc.SetSection("Generators",v);
  427. doc.SetSeeAlsoList(cmDocumentationSeeAlso);
  428. #undef cout
  429. return doc.PrintRequestedDocumentation(std::cout)? 0:1;
  430. #define cout no_cout_use_cmCPack_Log
  431. }
  432. return 0;
  433. }