cpack.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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> -P <ProjectName> -R <ReleaseVersion> [options]", 0},
  37. {0,0,0}
  38. };
  39. //----------------------------------------------------------------------------
  40. static const cmDocumentationEntry cmDocumentationDescription[] =
  41. {
  42. {0,
  43. "The \"cpack\" executable is the CMake packaging program. "
  44. "CMake-generated build trees created for projects that use "
  45. "the INSTALL_* commands have packaging support. "
  46. "This program will generate the package.", 0},
  47. CMAKE_STANDARD_INTRODUCTION,
  48. {0,0,0}
  49. };
  50. //----------------------------------------------------------------------------
  51. static const cmDocumentationEntry cmDocumentationOptions[] =
  52. {
  53. {"-G <generator>", "Use the specified generator to generate package.",
  54. "CPack may support multiple native packaging systems on certain platforms. A "
  55. "generator is responsible for generating input files for particular system "
  56. "and invoking that systems. Possible generator names are specified in the "
  57. "Generators section." },
  58. {"-P <ProjectName>", "Specify the project name.",
  59. "This option specifies the project name that will be used to generate the "
  60. "installer." },
  61. {"-C <Configuration>", "Specify the project configuration",
  62. "This option specifies the configuration that the project was build with, "
  63. "for example 'Debug', 'Release'." },
  64. {"-R <ReleaseVersion>", "Specify the release version of the project.",
  65. "This option specifies the release version of the project that will be "
  66. "used by installer." },
  67. {"-D <var>=<value>", "Set a CPack variable.", \
  68. "Set a variable that can be used by the generator."}, \
  69. {"--patch <ReleasePatch>", "Specify the patch of the project.",
  70. "This option specifies the patch of the project that will be "
  71. "used by installer." },
  72. {"--vendor <ProjectVendor>", "Specify the vendor of the project.",
  73. "This option specifies the vendor of the project that will be "
  74. "used by installer." },
  75. {0,0,0}
  76. };
  77. //----------------------------------------------------------------------------
  78. static const cmDocumentationEntry cmDocumentationSeeAlso[] =
  79. {
  80. {0, "cmake", 0},
  81. {0, "ccmake", 0},
  82. {0, 0, 0}
  83. };
  84. //----------------------------------------------------------------------------
  85. int cpackUnknownArgument(const char*, void*)
  86. {
  87. return 1;
  88. }
  89. //----------------------------------------------------------------------------
  90. struct cpackDefinitions
  91. {
  92. typedef std::map<cmStdString, cmStdString> MapType;
  93. MapType m_Map;
  94. cmCPackLog *m_Log;
  95. };
  96. //----------------------------------------------------------------------------
  97. int cpackDefinitionArgument(const char* argument, const char* cValue,
  98. void* call_data)
  99. {
  100. (void)argument;
  101. cpackDefinitions* def = static_cast<cpackDefinitions*>(call_data);
  102. std::string value = cValue;
  103. size_t pos = value.find_first_of("=");
  104. if ( pos == std::string::npos )
  105. {
  106. cmCPack_Log(def->m_Log, cmCPackLog::LOG_ERROR, "Please specify CPack definitions as: KEY=VALUE" << std::endl);
  107. return 0;
  108. }
  109. std::string key = value.substr(0, pos);
  110. value = value.c_str() + pos + 1;
  111. def->m_Map[key] = value;
  112. cmCPack_Log(def->m_Log, cmCPackLog::LOG_DEBUG, "Set CPack variable: " << key.c_str() << " to \"" << value.c_str() << "\"" << std::endl);
  113. return 1;
  114. }
  115. //----------------------------------------------------------------------------
  116. // this is CPack.
  117. int main (int argc, char *argv[])
  118. {
  119. cmCPackLog log;
  120. log.SetErrorPrefix("CPack Error: ");
  121. log.SetWarningPrefix("CPack Warning: ");
  122. log.SetOutputPrefix("CPack: ");
  123. log.SetVerbosePrefix("CPack Verbose: ");
  124. cmSystemTools::EnableMSVCDebugHook();
  125. if ( cmSystemTools::GetCurrentWorkingDirectory().size() == 0 )
  126. {
  127. cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "Current working directory cannot be established." << std::endl);
  128. }
  129. std::string generator;
  130. bool help = false;
  131. bool helpVersion = false;
  132. bool verbose = false;
  133. bool debug = false;
  134. std::string helpFull;
  135. std::string helpMAN;
  136. std::string helpHTML;
  137. std::string cpackProjectName;
  138. std::string cpackProjectDirectory = cmsys::SystemTools::GetCurrentWorkingDirectory();
  139. std::string cpackBuildConfig;
  140. std::string cpackProjectVersion;
  141. std::string cpackProjectPatch;
  142. std::string cpackProjectVendor;
  143. std::string cpackConfigFile;
  144. cpackDefinitions definitions;
  145. definitions.m_Log = &log;
  146. cpackConfigFile = "";
  147. cmDocumentation doc;
  148. cmsys::CommandLineArguments arg;
  149. arg.Initialize(argc, argv);
  150. typedef cmsys::CommandLineArguments argT;
  151. // Help arguments
  152. arg.AddArgument("--help", argT::NO_ARGUMENT, &help, "CPack help");
  153. arg.AddArgument("--help-full", argT::SPACE_ARGUMENT, &helpFull, "CPack help");
  154. arg.AddArgument("--help-html", argT::SPACE_ARGUMENT, &helpHTML, "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, "CPack configuration file");
  161. arg.AddArgument("-C", argT::SPACE_ARGUMENT, &cpackBuildConfig, "CPack build configuration");
  162. arg.AddArgument("-G", argT::SPACE_ARGUMENT, &generator, "CPack generator");
  163. arg.AddArgument("-P", argT::SPACE_ARGUMENT, &cpackProjectName, "CPack project name");
  164. arg.AddArgument("-R", argT::SPACE_ARGUMENT, &cpackProjectVersion, "CPack project version");
  165. arg.AddArgument("-B", argT::SPACE_ARGUMENT, &cpackProjectDirectory, "CPack project directory");
  166. arg.AddArgument("--patch", argT::SPACE_ARGUMENT, &cpackProjectPatch, "CPack project patch");
  167. arg.AddArgument("--vendor", argT::SPACE_ARGUMENT, &cpackProjectVendor, "CPack project vendor");
  168. arg.AddCallback("-D", argT::SPACE_ARGUMENT, cpackDefinitionArgument, &definitions, "CPack Definitions");
  169. arg.SetUnknownArgumentCallback(cpackUnknownArgument);
  170. // Parse command line
  171. int parsed = arg.Parse();
  172. // Setup logging
  173. if ( verbose )
  174. {
  175. log.SetVerbose(verbose);
  176. cmCPack_Log(&log, cmCPackLog::LOG_OUTPUT, "Enable Verbse" << std::endl);
  177. }
  178. if ( debug )
  179. {
  180. log.SetDebug(debug);
  181. cmCPack_Log(&log, cmCPackLog::LOG_OUTPUT, "Enable Debug" << std::endl);
  182. }
  183. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "Read CPack config file: " << cpackConfigFile.c_str() << std::endl);
  184. cmake cminst;
  185. cmGlobalGenerator cmgg;
  186. cmgg.SetCMakeInstance(&cminst);
  187. cmLocalGenerator* cmlg = cmgg.CreateLocalGenerator();
  188. cmMakefile* mf = cmlg->GetMakefile();
  189. bool cpackConfigFileSpecified = true;
  190. if ( cpackConfigFile.empty() )
  191. {
  192. cpackConfigFile = cmSystemTools::GetCurrentWorkingDirectory();
  193. cpackConfigFile += "/CPackConfig.cmake";
  194. cpackConfigFileSpecified = false;
  195. }
  196. cmCPackGenerators generators;
  197. generators.SetLogger(&log);
  198. cmCPackGenericGenerator* cpackGenerator = 0;
  199. if ( !helpFull.empty() || !helpMAN.empty() || !helpHTML.empty() || helpVersion )
  200. {
  201. help = true;
  202. }
  203. if ( parsed && !help )
  204. {
  205. if ( cmSystemTools::FileExists(cpackConfigFile.c_str()) )
  206. {
  207. if ( !mf->ReadListFile(0, cpackConfigFile.c_str()) )
  208. {
  209. cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "Problem reding CPack config file: \"" << cpackConfigFile.c_str() << "\"" << std::endl);
  210. return 1;
  211. }
  212. }
  213. else if ( cpackConfigFileSpecified )
  214. {
  215. cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "Cannot find CPack config file: \"" << cpackConfigFile.c_str() << "\"" << std::endl);
  216. return 1;
  217. }
  218. if ( !generator.empty() ) { mf->AddDefinition("CPACK_GENERATOR", generator.c_str()); }
  219. if ( !cpackProjectName.empty() ) { mf->AddDefinition("CPACK_PACKAGE_NAME", cpackProjectName.c_str()); }
  220. if ( !cpackProjectVersion.empty() ) { mf->AddDefinition("CPACK_PACKAGE_VERSION", cpackProjectVersion.c_str()); }
  221. if ( !cpackProjectVendor.empty() ) { mf->AddDefinition("CPACK_PACKAGE_VENDOR", cpackProjectVendor.c_str()); }
  222. if ( !cpackProjectDirectory.empty() ) { mf->AddDefinition("CPACK_PACKAGE_DIRECTORY", cpackProjectDirectory.c_str()); }
  223. if ( !cpackBuildConfig.empty() ) { mf->AddDefinition("CPACK_BUILD_CONFIG", cpackBuildConfig.c_str()); }
  224. cpackDefinitions::MapType::iterator cdit;
  225. for ( cdit = definitions.m_Map.begin(); cdit != definitions.m_Map.end(); ++cdit )
  226. {
  227. mf->AddDefinition(cdit->first.c_str(), cdit->second.c_str());
  228. }
  229. const char* gen = mf->GetDefinition("CPACK_GENERATOR");
  230. if ( !gen )
  231. {
  232. cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "CPack generator not specified" << std::endl);
  233. parsed = 0;
  234. }
  235. if ( parsed && !mf->GetDefinition("CPACK_PACKAGE_NAME") )
  236. {
  237. cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "CPack project name not specified" << std::endl);
  238. parsed = 0;
  239. }
  240. if ( parsed && !(mf->GetDefinition("CPACK_PACKAGE_VERSION")
  241. || mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR") && mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR")
  242. && mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH")) )
  243. {
  244. cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "CPack project version not specified" << std::endl
  245. << "Specify CPACK_PACKAGE_VERSION, or CPACK_PACKAGE_VERSION_MAJOR, CPACK_PACKAGE_VERSION_MINOR, and CPACK_PACKAGE_VERSION_PATCH."
  246. << std::endl);
  247. parsed = 0;
  248. }
  249. if ( parsed )
  250. {
  251. cpackGenerator = generators.NewGenerator(gen);
  252. if ( !cpackGenerator )
  253. {
  254. cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "Cannot initialize CPack generator: " << generator.c_str() << std::endl);
  255. parsed = 0;
  256. }
  257. if ( !cpackGenerator->Initialize(gen, mf) )
  258. {
  259. parsed = 0;
  260. }
  261. if ( parsed && !cpackGenerator->FindRunningCMake(argv[0]) )
  262. {
  263. cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "Cannot initialize the generator" << std::endl);
  264. parsed = 0;
  265. }
  266. if ( !mf->GetDefinition("CPACK_INSTALL_COMMANDS") && !mf->GetDefinition("CPACK_INSTALLED_DIRECTORIES") )
  267. {
  268. cmsys::SystemTools::ConvertToUnixSlashes(cpackProjectDirectory);
  269. std::string makeInstallFile = cpackProjectDirectory + "/cmake_install.cmake";
  270. if ( !cmsys::SystemTools::FileExists(makeInstallFile.c_str()) )
  271. {
  272. cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "Cannot find installation file: " << makeInstallFile.c_str() << std::endl);
  273. cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "Please specify build tree of the project that uses CMake, specify CPACK_INSTALL_COMMANDS, or specify CPACK_INSTALLED_DIRECTORIES." << std::endl);
  274. parsed = 0;
  275. }
  276. }
  277. }
  278. }
  279. if ( !parsed || help )
  280. {
  281. doc.CheckOptions(argc, argv);
  282. // Construct and print requested documentation.
  283. doc.SetName("cpack");
  284. doc.SetNameSection(cmDocumentationName);
  285. doc.SetUsageSection(cmDocumentationUsage);
  286. doc.SetDescriptionSection(cmDocumentationDescription);
  287. doc.SetOptionsSection(cmDocumentationOptions);
  288. doc.SetSeeAlsoList(cmDocumentationSeeAlso);
  289. #undef cout
  290. return doc.PrintRequestedDocumentation(std::cout)? 0:1;
  291. #define cout no_cout_use_cmCPack_Log
  292. }
  293. #ifdef _WIN32
  294. std::string comspec = "cmw9xcom.exe";
  295. cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
  296. #endif
  297. const char* projName = mf->GetDefinition("CPACK_PACKAGE_NAME");
  298. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "Use generator: " << cpackGenerator->GetNameOfClass() << std::endl);
  299. cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "For project: " << projName << std::endl);
  300. const char* projVersion = mf->GetDefinition("CPACK_PACKAGE_VERSION");
  301. if ( !projVersion )
  302. {
  303. const char* projVersionMajor = mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR");
  304. const char* projVersionMinor = mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR");
  305. const char* projVersionPatch = mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH");
  306. cmOStringStream ostr;
  307. ostr << projVersionMajor << "." << projVersionMinor << "." << projVersionPatch;
  308. mf->AddDefinition("CPACK_PACKAGE_VERSION", ostr.str().c_str());
  309. }
  310. int res = cpackGenerator->ProcessGenerator();
  311. if ( !res )
  312. {
  313. cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "Error when generating package: " << projName << std::endl);
  314. return 1;
  315. }
  316. return 0;
  317. }