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 "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 cmDocumentationEntry cmDocumentationName[] =
  27. {
  28. {0,
  29. " cpack - Packaging driver provided by CMake.", 0},
  30. {0,0,0}
  31. };
  32. //----------------------------------------------------------------------------
  33. static const cmDocumentationEntry cmDocumentationUsage[] =
  34. {
  35. {0,
  36. " cpack -G <generator> [options]",
  37. 0},
  38. {0,0,0}
  39. };
  40. //----------------------------------------------------------------------------
  41. static const cmDocumentationEntry cmDocumentationDescription[] =
  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 cmDocumentationEntry cmDocumentationOptions[] =
  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 cmDocumentationEntry cmDocumentationSeeAlso[] =
  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. cmGlobalGenerator cmgg;
  194. cmgg.SetCMakeInstance(&cminst);
  195. cmLocalGenerator* cmlg = cmgg.CreateLocalGenerator();
  196. cmMakefile* globalMF = cmlg->GetMakefile();
  197. bool cpackConfigFileSpecified = true;
  198. if ( cpackConfigFile.empty() )
  199. {
  200. cpackConfigFile = cmSystemTools::GetCurrentWorkingDirectory();
  201. cpackConfigFile += "/CPackConfig.cmake";
  202. cpackConfigFileSpecified = false;
  203. }
  204. cmCPackGenerators generators;
  205. generators.SetLogger(&log);
  206. cmCPackGenericGenerator* cpackGenerator = 0;
  207. if ( !helpFull.empty() || !helpMAN.empty() ||
  208. !helpHTML.empty() || helpVersion )
  209. {
  210. help = true;
  211. }
  212. if ( parsed && !help )
  213. {
  214. // find out which system cpack is running on, so it can setup the search
  215. // paths, so FIND_XXX() commands can be used in scripts
  216. cminst.AddCMakePaths(argv[0]);
  217. std::string systemFile =
  218. globalMF->GetModulesFile("CMakeDetermineSystem.cmake");
  219. if (!globalMF->ReadListFile(0, systemFile.c_str()))
  220. {
  221. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  222. "Error reading CMakeDetermineSystem.cmake" << std::endl);
  223. return 1;
  224. }
  225. systemFile =
  226. globalMF->GetModulesFile("CMakeSystemSpecificInformation.cmake");
  227. if (!globalMF->ReadListFile(0, systemFile.c_str()))
  228. {
  229. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  230. "Error reading CMakeSystemSpecificInformation.cmake" << std::endl);
  231. return 1;
  232. }
  233. if ( cmSystemTools::FileExists(cpackConfigFile.c_str()) )
  234. {
  235. cpackConfigFile =
  236. cmSystemTools::CollapseFullPath(cpackConfigFile.c_str());
  237. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
  238. "Read CPack configuration file: " << cpackConfigFile.c_str()
  239. << std::endl);
  240. if ( !globalMF->ReadListFile(0, cpackConfigFile.c_str()) )
  241. {
  242. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  243. "Problem reading CPack config file: \""
  244. << cpackConfigFile.c_str() << "\"" << std::endl);
  245. return 1;
  246. }
  247. }
  248. else if ( cpackConfigFileSpecified )
  249. {
  250. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  251. "Cannot find CPack config file: \"" << cpackConfigFile.c_str()
  252. << "\"" << std::endl);
  253. return 1;
  254. }
  255. if ( !generator.empty() )
  256. {
  257. globalMF->AddDefinition("CPACK_GENERATOR", generator.c_str());
  258. }
  259. if ( !cpackProjectName.empty() )
  260. {
  261. globalMF->AddDefinition("CPACK_PACKAGE_NAME", cpackProjectName.c_str());
  262. }
  263. if ( !cpackProjectVersion.empty() )
  264. {
  265. globalMF->AddDefinition("CPACK_PACKAGE_VERSION",
  266. cpackProjectVersion.c_str());
  267. }
  268. if ( !cpackProjectVendor.empty() )
  269. {
  270. globalMF->AddDefinition("CPACK_PACKAGE_VENDOR",
  271. cpackProjectVendor.c_str());
  272. }
  273. if ( !cpackProjectDirectory.empty() )
  274. {
  275. globalMF->AddDefinition("CPACK_PACKAGE_DIRECTORY",
  276. cpackProjectDirectory.c_str());
  277. }
  278. if ( !cpackBuildConfig.empty() )
  279. {
  280. globalMF->AddDefinition("CPACK_BUILD_CONFIG", cpackBuildConfig.c_str());
  281. }
  282. cpackDefinitions::MapType::iterator cdit;
  283. for ( cdit = definitions.Map.begin();
  284. cdit != definitions.Map.end();
  285. ++cdit )
  286. {
  287. globalMF->AddDefinition(cdit->first.c_str(), cdit->second.c_str());
  288. }
  289. const char* cpackModulesPath =
  290. globalMF->GetDefinition("CPACK_MODULE_PATH");
  291. if ( cpackModulesPath )
  292. {
  293. globalMF->AddDefinition("CMAKE_MODULE_PATH", cpackModulesPath);
  294. }
  295. const char* genList = globalMF->GetDefinition("CPACK_GENERATOR");
  296. if ( !genList )
  297. {
  298. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  299. "CPack generator not specified" << std::endl);
  300. parsed = 0;
  301. }
  302. else
  303. {
  304. std::vector<std::string> generatorsVector;
  305. cmSystemTools::ExpandListArgument(genList,
  306. generatorsVector);
  307. std::vector<std::string>::iterator it;
  308. for ( it = generatorsVector.begin();
  309. it != generatorsVector.end();
  310. ++it )
  311. {
  312. const char* gen = it->c_str();
  313. cmMakefile newMF(*globalMF);
  314. cmMakefile* mf = &newMF;
  315. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
  316. "Specified generator: " << gen << std::endl);
  317. if ( parsed && !mf->GetDefinition("CPACK_PACKAGE_NAME") )
  318. {
  319. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  320. "CPack project name not specified" << std::endl);
  321. parsed = 0;
  322. }
  323. if ( parsed && !(mf->GetDefinition("CPACK_PACKAGE_VERSION")
  324. || mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR") &&
  325. mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR")
  326. && mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH")) )
  327. {
  328. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  329. "CPack project version not specified" << std::endl
  330. << "Specify CPACK_PACKAGE_VERSION, or "
  331. "CPACK_PACKAGE_VERSION_MAJOR, "
  332. "CPACK_PACKAGE_VERSION_MINOR, and CPACK_PACKAGE_VERSION_PATCH."
  333. << std::endl);
  334. parsed = 0;
  335. }
  336. if ( parsed )
  337. {
  338. cpackGenerator = generators.NewGenerator(gen);
  339. if ( !cpackGenerator )
  340. {
  341. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  342. "Cannot initialize CPack generator: "
  343. << generator.c_str() << std::endl);
  344. parsed = 0;
  345. }
  346. if ( parsed && !cpackGenerator->Initialize(gen, mf, argv[0]) )
  347. {
  348. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  349. "Cannot initialize the generator" << std::endl);
  350. parsed = 0;
  351. }
  352. if ( !mf->GetDefinition("CPACK_INSTALL_COMMANDS") &&
  353. !mf->GetDefinition("CPACK_INSTALLED_DIRECTORIES") &&
  354. !mf->GetDefinition("CPACK_INSTALL_CMAKE_PROJECTS") )
  355. {
  356. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  357. "Please specify build tree of the project that uses CMake "
  358. "using CPACK_INSTALL_CMAKE_PROJECTS, specify "
  359. "CPACK_INSTALL_COMMANDS, or specify "
  360. "CPACK_INSTALLED_DIRECTORIES."
  361. << std::endl);
  362. parsed = 0;
  363. }
  364. if ( parsed )
  365. {
  366. #ifdef _WIN32
  367. std::string comspec = "cmw9xcom.exe";
  368. cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
  369. #endif
  370. const char* projName = mf->GetDefinition("CPACK_PACKAGE_NAME");
  371. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "Use generator: "
  372. << cpackGenerator->GetNameOfClass() << std::endl);
  373. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "For project: "
  374. << projName << std::endl);
  375. const char* projVersion =
  376. mf->GetDefinition("CPACK_PACKAGE_VERSION");
  377. if ( !projVersion )
  378. {
  379. const char* projVersionMajor
  380. = mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR");
  381. const char* projVersionMinor
  382. = mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR");
  383. const char* projVersionPatch
  384. = mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH");
  385. cmOStringStream ostr;
  386. ostr << projVersionMajor << "." << projVersionMinor << "."
  387. << projVersionPatch;
  388. mf->AddDefinition("CPACK_PACKAGE_VERSION",
  389. ostr.str().c_str());
  390. }
  391. int res = cpackGenerator->ProcessGenerator();
  392. if ( !res )
  393. {
  394. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  395. "Error when generating package: " << projName << std::endl);
  396. return 1;
  397. }
  398. }
  399. }
  400. }
  401. }
  402. }
  403. if ( !parsed || help )
  404. {
  405. doc.CheckOptions(argc, argv);
  406. // Construct and print requested documentation.
  407. doc.SetName("cpack");
  408. doc.SetNameSection(cmDocumentationName);
  409. doc.SetUsageSection(cmDocumentationUsage);
  410. doc.SetDescriptionSection(cmDocumentationDescription);
  411. doc.SetOptionsSection(cmDocumentationOptions);
  412. std::vector<cmDocumentationEntry> v;
  413. cmCPackGenerators::DescriptionsMap::const_iterator generatorIt;
  414. for( generatorIt = generators.GetGeneratorsList().begin();
  415. generatorIt != generators.GetGeneratorsList().end();
  416. ++ generatorIt )
  417. {
  418. cmDocumentationEntry e;
  419. e.name = generatorIt->first.c_str();
  420. e.brief = generatorIt->second.c_str();
  421. e.full = "";
  422. v.push_back(e);
  423. }
  424. cmDocumentationEntry empty = {0,0,0};
  425. v.push_back(empty);
  426. doc.SetGeneratorsSection(&v[0]);
  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. }