cpack.cxx 17 KB

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