cpack.cxx 13 KB

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