cpack.cxx 16 KB

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