cpack.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmSystemTools.h"
  11. // Need these for documentation support.
  12. #include "cmake.h"
  13. #include "cmDocumentation.h"
  14. #include "cmCPackGeneratorFactory.h"
  15. #include "cmCPackGenerator.h"
  16. #include "cmake.h"
  17. #include "cmGlobalGenerator.h"
  18. #include "cmLocalGenerator.h"
  19. #include "cmMakefile.h"
  20. #include "cmCPackLog.h"
  21. #include <cmsys/CommandLineArguments.hxx>
  22. #include <memory> // auto_ptr
  23. //----------------------------------------------------------------------------
  24. static const char * cmDocumentationName[][3] =
  25. {
  26. {0,
  27. " cpack - Packaging driver provided by CMake.", 0},
  28. {0,0,0}
  29. };
  30. //----------------------------------------------------------------------------
  31. static const char * cmDocumentationUsage[][3] =
  32. {
  33. {0,
  34. " cpack -G <generator> [options]",
  35. 0},
  36. {0,0,0}
  37. };
  38. //----------------------------------------------------------------------------
  39. static const char * cmDocumentationDescription[][3] =
  40. {
  41. {0,
  42. "The \"cpack\" executable is the CMake packaging program. "
  43. "CMake-generated build trees created for projects that use "
  44. "the INSTALL_* commands have packaging support. "
  45. "This program will generate the package.", 0},
  46. CMAKE_STANDARD_INTRODUCTION,
  47. {0,0,0}
  48. };
  49. //----------------------------------------------------------------------------
  50. static const char * cmDocumentationOptions[][3] =
  51. {
  52. {"-G <generator>", "Use the specified generator to generate package.",
  53. "CPack may support multiple native packaging systems on certain "
  54. "platforms. A generator is responsible for generating input files for "
  55. "particular system and invoking that systems. Possible generator names "
  56. "are specified in the Generators section." },
  57. {"-C <Configuration>", "Specify the project configuration",
  58. "This option specifies the configuration that the project was build "
  59. "with, for example 'Debug', 'Release'." },
  60. {"-D <var>=<value>", "Set a CPack variable.", \
  61. "Set a variable that can be used by the generator."}, \
  62. {"--config <config file>", "Specify the config file.",
  63. "Specify the config file to use to create the package. By default "
  64. "CPackConfig.cmake in the current directory will be used." },
  65. {0,0,0}
  66. };
  67. //----------------------------------------------------------------------------
  68. static const char * cmDocumentationSeeAlso[][3] =
  69. {
  70. {0, "cmake", 0},
  71. {0, "ccmake", 0},
  72. {0, 0, 0}
  73. };
  74. //----------------------------------------------------------------------------
  75. int cpackUnknownArgument(const char*, void*)
  76. {
  77. return 1;
  78. }
  79. //----------------------------------------------------------------------------
  80. struct cpackDefinitions
  81. {
  82. typedef std::map<cmStdString, cmStdString> MapType;
  83. MapType Map;
  84. cmCPackLog *Log;
  85. };
  86. //----------------------------------------------------------------------------
  87. int cpackDefinitionArgument(const char* argument, const char* cValue,
  88. void* call_data)
  89. {
  90. (void)argument;
  91. cpackDefinitions* def = static_cast<cpackDefinitions*>(call_data);
  92. std::string value = cValue;
  93. size_t pos = value.find_first_of("=");
  94. if ( pos == std::string::npos )
  95. {
  96. cmCPack_Log(def->Log, cmCPackLog::LOG_ERROR,
  97. "Please specify CPack definitions as: KEY=VALUE" << std::endl);
  98. return 0;
  99. }
  100. std::string key = value.substr(0, pos);
  101. value = value.c_str() + pos + 1;
  102. def->Map[key] = value;
  103. cmCPack_Log(def->Log, cmCPackLog::LOG_DEBUG, "Set CPack variable: "
  104. << key.c_str() << " to \"" << value.c_str() << "\"" << std::endl);
  105. return 1;
  106. }
  107. //----------------------------------------------------------------------------
  108. // this is CPack.
  109. int main (int argc, char *argv[])
  110. {
  111. cmSystemTools::FindExecutableDirectory(argv[0]);
  112. cmCPackLog log;
  113. log.SetErrorPrefix("CPack Error: ");
  114. log.SetWarningPrefix("CPack Warning: ");
  115. log.SetOutputPrefix("CPack: ");
  116. log.SetVerbosePrefix("CPack Verbose: ");
  117. cmSystemTools::EnableMSVCDebugHook();
  118. if ( cmSystemTools::GetCurrentWorkingDirectory().size() == 0 )
  119. {
  120. cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
  121. "Current working directory cannot be established." << std::endl);
  122. }
  123. std::string generator;
  124. bool help = false;
  125. bool helpVersion = false;
  126. bool verbose = false;
  127. bool debug = false;
  128. std::string helpFull;
  129. std::string helpMAN;
  130. std::string helpHTML;
  131. std::string cpackProjectName;
  132. std::string cpackProjectDirectory
  133. = cmsys::SystemTools::GetCurrentWorkingDirectory();
  134. std::string cpackBuildConfig;
  135. std::string cpackProjectVersion;
  136. std::string cpackProjectPatch;
  137. std::string cpackProjectVendor;
  138. std::string cpackConfigFile;
  139. cpackDefinitions definitions;
  140. definitions.Log = &log;
  141. cpackConfigFile = "";
  142. cmDocumentation doc;
  143. cmsys::CommandLineArguments arg;
  144. arg.Initialize(argc, argv);
  145. typedef cmsys::CommandLineArguments argT;
  146. // Help arguments
  147. arg.AddArgument("--help", argT::NO_ARGUMENT, &help, "CPack help");
  148. arg.AddArgument("--help-full", argT::SPACE_ARGUMENT, &helpFull,
  149. "CPack help");
  150. arg.AddArgument("--help-html", argT::SPACE_ARGUMENT, &helpHTML,
  151. "CPack help");
  152. arg.AddArgument("--help-man", argT::SPACE_ARGUMENT, &helpMAN, "CPack help");
  153. arg.AddArgument("--version", argT::NO_ARGUMENT, &helpVersion, "CPack help");
  154. arg.AddArgument("-V", argT::NO_ARGUMENT, &verbose, "CPack verbose");
  155. arg.AddArgument("--verbose", argT::NO_ARGUMENT, &verbose, "-V");
  156. arg.AddArgument("--debug", argT::NO_ARGUMENT, &debug, "-V");
  157. arg.AddArgument("--config", argT::SPACE_ARGUMENT, &cpackConfigFile,
  158. "CPack configuration file");
  159. arg.AddArgument("-C", argT::SPACE_ARGUMENT, &cpackBuildConfig,
  160. "CPack build configuration");
  161. arg.AddArgument("-G", argT::SPACE_ARGUMENT,
  162. &generator, "CPack generator");
  163. arg.AddArgument("-P", argT::SPACE_ARGUMENT,
  164. &cpackProjectName, "CPack project name");
  165. arg.AddArgument("-R", argT::SPACE_ARGUMENT,
  166. &cpackProjectVersion, "CPack project version");
  167. arg.AddArgument("-B", argT::SPACE_ARGUMENT,
  168. &cpackProjectDirectory, "CPack project directory");
  169. arg.AddArgument("--patch", argT::SPACE_ARGUMENT,
  170. &cpackProjectPatch, "CPack project patch");
  171. arg.AddArgument("--vendor", argT::SPACE_ARGUMENT,
  172. &cpackProjectVendor, "CPack project vendor");
  173. arg.AddCallback("-D", argT::SPACE_ARGUMENT,
  174. cpackDefinitionArgument, &definitions, "CPack Definitions");
  175. arg.SetUnknownArgumentCallback(cpackUnknownArgument);
  176. // Parse command line
  177. int parsed = arg.Parse();
  178. // Setup logging
  179. if ( verbose )
  180. {
  181. log.SetVerbose(verbose);
  182. cmCPack_Log(&log, cmCPackLog::LOG_OUTPUT, "Enable Verbse" << std::endl);
  183. }
  184. if ( debug )
  185. {
  186. log.SetDebug(debug);
  187. cmCPack_Log(&log, cmCPackLog::LOG_OUTPUT, "Enable Debug" << std::endl);
  188. }
  189. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
  190. "Read CPack config file: " << cpackConfigFile.c_str() << std::endl);
  191. cmake cminst;
  192. cminst.RemoveUnscriptableCommands();
  193. cmGlobalGenerator cmgg;
  194. cmgg.SetCMakeInstance(&cminst);
  195. std::auto_ptr<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. cmCPackGeneratorFactory generators;
  205. generators.SetLogger(&log);
  206. cmCPackGenerator* 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();
  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 &&
  324. !(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) )
  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->DoPackage();
  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. cmCPackGeneratorFactory::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. if (cmSystemTools::GetErrorOccuredFlag())
  432. {
  433. return 1;
  434. }
  435. return 0;
  436. }