cmCPackGenericGenerator.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 "cmCPackGenericGenerator.h"
  14. #include "cmMakefile.h"
  15. #include "cmCPackLog.h"
  16. #include "cmake.h"
  17. #include "cmGlobalGenerator.h"
  18. #include "cmLocalGenerator.h"
  19. #include <cmsys/SystemTools.hxx>
  20. #include <cmsys/Glob.hxx>
  21. #include <memory> // auto_ptr
  22. //----------------------------------------------------------------------
  23. cmCPackGenericGenerator::cmCPackGenericGenerator()
  24. {
  25. m_GeneratorVerbose = false;
  26. m_MakefileMap = 0;
  27. m_Logger = 0;
  28. }
  29. //----------------------------------------------------------------------
  30. cmCPackGenericGenerator::~cmCPackGenericGenerator()
  31. {
  32. m_MakefileMap = 0;
  33. }
  34. //----------------------------------------------------------------------
  35. int cmCPackGenericGenerator::PrepareNames()
  36. {
  37. this->SetOption("CPACK_GENERATOR", m_Name.c_str());
  38. std::string tempDirectory = this->GetOption("CPACK_PACKAGE_DIRECTORY");
  39. tempDirectory += "/_CPack_Packages/";
  40. tempDirectory += this->GetOption("CPACK_GENERATOR");
  41. std::string topDirectory = tempDirectory;
  42. std::string outName = this->GetOption("CPACK_PACKAGE_NAME");
  43. outName += "-";
  44. outName += this->GetOption("CPACK_PACKAGE_VERSION");
  45. const char* patch = this->GetOption("CPACK_PACKAGE_VERSION_PATCH");
  46. if ( patch && *patch )
  47. {
  48. outName += "-";
  49. outName += patch;
  50. }
  51. const char* postfix = this->GetOutputPostfix();
  52. if ( postfix && *postfix )
  53. {
  54. outName += "-";
  55. outName += postfix;
  56. }
  57. tempDirectory += "/" + outName;
  58. outName += ".";
  59. outName += this->GetOutputExtension();
  60. std::string installFile = this->GetOption("CPACK_PACKAGE_DIRECTORY");
  61. installFile += "/cmake_install.cmake";
  62. std::string destFile = this->GetOption("CPACK_PACKAGE_DIRECTORY");
  63. destFile += "/" + outName;
  64. std::string outFile = topDirectory + "/" + outName;
  65. std::string installPrefix = tempDirectory + this->GetInstallPrefix();
  66. this->SetOption("CPACK_TOPLEVEL_DIRECTORY", topDirectory.c_str());
  67. this->SetOption("CPACK_TEMPORARY_DIRECTORY", tempDirectory.c_str());
  68. this->SetOption("CPACK_INSTALL_FILE_NAME", installFile.c_str());
  69. this->SetOption("CPACK_OUTPUT_FILE_NAME", outName.c_str());
  70. this->SetOption("CPACK_PACKAGE_FILE_NAME", destFile.c_str());
  71. this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME", outFile.c_str());
  72. this->SetOption("CPACK_INSTALL_DIRECTORY", this->GetInstallPath());
  73. this->SetOption("CPACK_NATIVE_INSTALL_DIRECTORY",
  74. cmsys::SystemTools::ConvertToOutputPath(this->GetInstallPath()).c_str());
  75. this->SetOption("CPACK_TEMPORARY_INSTALL_DIRECTORY", installPrefix.c_str());
  76. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Look for: CPACK_PACKAGE_DESCRIPTION_FILE" << std::endl);
  77. const char* descFileName = this->GetOption("CPACK_PACKAGE_DESCRIPTION_FILE");
  78. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Look for: " << descFileName << std::endl);
  79. if ( descFileName )
  80. {
  81. if ( !cmSystemTools::FileExists(descFileName) )
  82. {
  83. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find description file name: " << descFileName << std::endl);
  84. return 0;
  85. }
  86. std::ifstream ifs(descFileName);
  87. if ( !ifs )
  88. {
  89. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot open description file name: " << descFileName << std::endl);
  90. return 0;
  91. }
  92. cmOStringStream ostr;
  93. std::string line;
  94. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Read description file: " << descFileName << std::endl);
  95. while ( ifs && cmSystemTools::GetLineFromStream(ifs, line) )
  96. {
  97. ostr << cmSystemTools::MakeXMLSafe(line.c_str()) << std::endl;
  98. }
  99. this->SetOption("CPACK_PACKAGE_DESCRIPTION", ostr.str().c_str());
  100. }
  101. if ( !this->GetOption("CPACK_PACKAGE_DESCRIPTION") )
  102. {
  103. cmCPackLogger(cmCPackLog::LOG_ERROR,
  104. "Project description not specified. Please specify CPACK_PACKAGE_DESCRIPTION or CPACK_PACKAGE_DESCRIPTION_FILE_NAME."
  105. << std::endl);
  106. return 0;
  107. }
  108. return 1;
  109. }
  110. //----------------------------------------------------------------------
  111. int cmCPackGenericGenerator::InstallProject()
  112. {
  113. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Install project" << std::endl);
  114. const char* tempInstallDirectory = this->GetOption("CPACK_TEMPORARY_INSTALL_DIRECTORY");
  115. const char* installFile = this->GetOption("CPACK_INSTALL_FILE_NAME");
  116. if ( !cmsys::SystemTools::MakeDirectory(tempInstallDirectory))
  117. {
  118. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem creating temporary directory: " << tempInstallDirectory << std::endl);
  119. return 0;
  120. }
  121. cmake cm;
  122. cmGlobalGenerator gg;
  123. gg.SetCMakeInstance(&cm);
  124. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  125. lg->SetGlobalGenerator(&gg);
  126. cmMakefile *mf = lg->GetMakefile();
  127. bool movable = true;
  128. if ( movable )
  129. {
  130. mf->AddDefinition("CMAKE_INSTALL_PREFIX", tempInstallDirectory);
  131. }
  132. const char* buildConfig = this->GetOption("CPACK_BUILD_CONFIG");
  133. if ( buildConfig && *buildConfig )
  134. {
  135. mf->AddDefinition("BUILD_TYPE", buildConfig);
  136. }
  137. if ( movable )
  138. {
  139. // Make sure there is no destdir
  140. cmSystemTools::PutEnv("DESTDIR=");
  141. }
  142. else
  143. {
  144. std::string destDir = "DESTDIR=";
  145. destDir += tempInstallDirectory;
  146. cmSystemTools::PutEnv(destDir.c_str());
  147. }
  148. int res = mf->ReadListFile(0, installFile);
  149. if ( !movable )
  150. {
  151. cmSystemTools::PutEnv("DESTDIR=");
  152. }
  153. return res;
  154. }
  155. //----------------------------------------------------------------------
  156. void cmCPackGenericGenerator::SetOption(const char* op, const char* value)
  157. {
  158. if ( !op )
  159. {
  160. return;
  161. }
  162. if ( !value )
  163. {
  164. m_MakefileMap->RemoveDefinition(op);
  165. return;
  166. }
  167. cmCPackLogger(cmCPackLog::LOG_DEBUG, this->GetNameOfClass() << "::SetOption(" << op << ", " << value << ")" << std::endl);
  168. m_MakefileMap->AddDefinition(op, value);
  169. }
  170. //----------------------------------------------------------------------
  171. int cmCPackGenericGenerator::ProcessGenerator()
  172. {
  173. if ( !this->PrepareNames() )
  174. {
  175. return 0;
  176. }
  177. if ( !this->InstallProject() )
  178. {
  179. return 0;
  180. }
  181. const char* tempPackageFileName = this->GetOption(
  182. "CPACK_TEMPORARY_PACKAGE_FILE_NAME");
  183. const char* packageFileName = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  184. const char* tempDirectory = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  185. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Find files" << std::endl);
  186. cmsys::Glob gl;
  187. std::string findExpr = tempDirectory;
  188. findExpr += "/*";
  189. gl.RecurseOn();
  190. if ( !gl.FindFiles(findExpr) )
  191. {
  192. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find any files in the packaging tree" << std::endl);
  193. return 0;
  194. }
  195. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Compress package" << std::endl);
  196. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Compress files to: " << tempPackageFileName << std::endl);
  197. if ( cmSystemTools::FileExists(tempPackageFileName) )
  198. {
  199. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Remove old package file" << std::endl);
  200. cmSystemTools::RemoveFile(tempPackageFileName);
  201. }
  202. if ( !this->CompressFiles(tempPackageFileName,
  203. tempDirectory, gl.GetFiles()) )
  204. {
  205. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem compressing the directory" << std::endl);
  206. return 0;
  207. }
  208. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Finalize package" << std::endl);
  209. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Copy final package: " << tempPackageFileName << " to " << packageFileName << std::endl);
  210. if ( !cmSystemTools::CopyFileIfDifferent(tempPackageFileName, packageFileName) )
  211. {
  212. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem copying the package: " << tempPackageFileName << " to " << packageFileName << std::endl);
  213. return 0;
  214. }
  215. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Package " << packageFileName << " generated." << std::endl);
  216. return 1;
  217. }
  218. //----------------------------------------------------------------------
  219. int cmCPackGenericGenerator::Initialize(const char* name, cmMakefile* mf)
  220. {
  221. m_MakefileMap = mf;
  222. m_Name = name;
  223. return 1;
  224. }
  225. //----------------------------------------------------------------------
  226. const char* cmCPackGenericGenerator::GetOption(const char* op)
  227. {
  228. return m_MakefileMap->GetDefinition(op);
  229. }
  230. //----------------------------------------------------------------------
  231. int cmCPackGenericGenerator::FindRunningCMake(const char* arg0)
  232. {
  233. int found = 0;
  234. // Find our own executable.
  235. std::vector<cmStdString> failures;
  236. m_CPackSelf = arg0;
  237. cmSystemTools::ConvertToUnixSlashes(m_CPackSelf);
  238. failures.push_back(m_CPackSelf);
  239. m_CPackSelf = cmSystemTools::FindProgram(m_CPackSelf.c_str());
  240. if(!cmSystemTools::FileExists(m_CPackSelf.c_str()))
  241. {
  242. failures.push_back(m_CPackSelf);
  243. m_CPackSelf = "/usr/local/bin/ctest";
  244. }
  245. if(!cmSystemTools::FileExists(m_CPackSelf.c_str()))
  246. {
  247. failures.push_back(m_CPackSelf);
  248. cmOStringStream msg;
  249. msg << "CTEST can not find the command line program ctest.\n";
  250. msg << " argv[0] = \"" << arg0 << "\"\n";
  251. msg << " Attempted paths:\n";
  252. std::vector<cmStdString>::iterator i;
  253. for(i=failures.begin(); i != failures.end(); ++i)
  254. {
  255. msg << " \"" << i->c_str() << "\"\n";
  256. }
  257. cmSystemTools::Error(msg.str().c_str());
  258. }
  259. std::string dir;
  260. std::string file;
  261. if(cmSystemTools::SplitProgramPath(m_CPackSelf.c_str(),
  262. dir, file, true))
  263. {
  264. m_CMakeSelf = dir += "/cmake";
  265. m_CMakeSelf += cmSystemTools::GetExecutableExtension();
  266. if(cmSystemTools::FileExists(m_CMakeSelf.c_str()))
  267. {
  268. found = 1;
  269. }
  270. }
  271. if ( !found )
  272. {
  273. failures.push_back(m_CMakeSelf);
  274. #ifdef CMAKE_BUILD_DIR
  275. std::string intdir = ".";
  276. #ifdef CMAKE_INTDIR
  277. intdir = CMAKE_INTDIR;
  278. #endif
  279. m_CMakeSelf = CMAKE_BUILD_DIR;
  280. m_CMakeSelf += "/bin/";
  281. m_CMakeSelf += intdir;
  282. m_CMakeSelf += "/cmake";
  283. m_CMakeSelf += cmSystemTools::GetExecutableExtension();
  284. #endif
  285. if(!cmSystemTools::FileExists(m_CMakeSelf.c_str()))
  286. {
  287. failures.push_back(m_CMakeSelf);
  288. cmOStringStream msg;
  289. msg << "CTEST can not find the command line program cmake.\n";
  290. msg << " argv[0] = \"" << arg0 << "\"\n";
  291. msg << " Attempted paths:\n";
  292. std::vector<cmStdString>::iterator i;
  293. for(i=failures.begin(); i != failures.end(); ++i)
  294. {
  295. msg << " \"" << i->c_str() << "\"\n";
  296. }
  297. cmSystemTools::Error(msg.str().c_str());
  298. }
  299. }
  300. // do CMAKE_ROOT, look for the environment variable first
  301. std::string cMakeRoot;
  302. std::string modules;
  303. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT" << std::endl);
  304. if (getenv("CMAKE_ROOT"))
  305. {
  306. cMakeRoot = getenv("CMAKE_ROOT");
  307. modules = cMakeRoot + "/Modules/CMake.cmake";
  308. }
  309. if(modules.empty() || !cmSystemTools::FileExists(modules.c_str()))
  310. {
  311. // next try exe/..
  312. cMakeRoot = cmSystemTools::GetProgramPath(m_CMakeSelf.c_str());
  313. std::string::size_type slashPos = cMakeRoot.rfind("/");
  314. if(slashPos != std::string::npos)
  315. {
  316. cMakeRoot = cMakeRoot.substr(0, slashPos);
  317. }
  318. // is there no Modules direcory there?
  319. modules = cMakeRoot + "/Modules/CMake.cmake";
  320. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT: " << modules.c_str() << std::endl);
  321. }
  322. if (!cmSystemTools::FileExists(modules.c_str()))
  323. {
  324. // try exe/../share/cmake
  325. cMakeRoot += CMAKE_DATA_DIR;
  326. modules = cMakeRoot + "/Modules/CMake.cmake";
  327. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT: " << modules.c_str() << std::endl);
  328. }
  329. #ifdef CMAKE_ROOT_DIR
  330. if (!cmSystemTools::FileExists(modules.c_str()))
  331. {
  332. // try compiled in root directory
  333. cMakeRoot = CMAKE_ROOT_DIR;
  334. modules = cMakeRoot + "/Modules/CMake.cmake";
  335. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT: " << modules.c_str() << std::endl);
  336. }
  337. #endif
  338. #ifdef CMAKE_PREFIX
  339. if (!cmSystemTools::FileExists(modules.c_str()))
  340. {
  341. // try compiled in install prefix
  342. cMakeRoot = CMAKE_PREFIX CMAKE_DATA_DIR;
  343. modules = cMakeRoot + "/Modules/CMake.cmake";
  344. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT: " << modules.c_str() << std::endl);
  345. }
  346. #endif
  347. if (!cmSystemTools::FileExists(modules.c_str()))
  348. {
  349. // try
  350. cMakeRoot = cmSystemTools::GetProgramPath(m_CMakeSelf.c_str());
  351. cMakeRoot += CMAKE_DATA_DIR;
  352. modules = cMakeRoot + "/Modules/CMake.cmake";
  353. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT: " << modules.c_str() << std::endl);
  354. }
  355. if(!cmSystemTools::FileExists(modules.c_str()))
  356. {
  357. // next try exe
  358. cMakeRoot = cmSystemTools::GetProgramPath(m_CMakeSelf.c_str());
  359. // is there no Modules direcory there?
  360. modules = cMakeRoot + "/Modules/CMake.cmake";
  361. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT: " << modules.c_str() << std::endl);
  362. }
  363. if (!cmSystemTools::FileExists(modules.c_str()))
  364. {
  365. // couldn't find modules
  366. cmSystemTools::Error("Could not find CMAKE_ROOT !!!\n"
  367. "CMake has most likely not been installed correctly.\n"
  368. "Modules directory not found in\n",
  369. cMakeRoot.c_str());
  370. return 0;
  371. }
  372. m_CMakeRoot = cMakeRoot;
  373. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT: " << m_CMakeRoot.c_str() << std::endl);
  374. this->SetOption("CMAKE_ROOT", m_CMakeRoot.c_str());
  375. return 1;
  376. }
  377. //----------------------------------------------------------------------
  378. int cmCPackGenericGenerator::CompressFiles(const char* outFileName, const char* toplevel,
  379. const std::vector<std::string>& files)
  380. {
  381. (void)outFileName;
  382. (void)toplevel;
  383. (void)files;
  384. return 0;
  385. }
  386. //----------------------------------------------------------------------
  387. const char* cmCPackGenericGenerator::GetInstallPath()
  388. {
  389. if ( !m_InstallPath.empty() )
  390. {
  391. return m_InstallPath.c_str();
  392. }
  393. #if defined(_WIN32) && !defined(__CYGWIN__)
  394. const char* prgfiles = cmsys::SystemTools::GetEnv("ProgramFiles");
  395. const char* sysDrive = cmsys::SystemTools::GetEnv("SystemDrive");
  396. if ( prgfiles )
  397. {
  398. m_InstallPath = prgfiles;
  399. }
  400. else if ( sysDrive )
  401. {
  402. m_InstallPath = sysDrive;
  403. m_InstallPath += "/Program Files";
  404. }
  405. else
  406. {
  407. m_InstallPath = "c:/Program Files";
  408. }
  409. m_InstallPath += "/";
  410. m_InstallPath += this->GetOption("CPACK_PACKAGE_NAME");
  411. m_InstallPath += "-";
  412. m_InstallPath += this->GetOption("CPACK_PACKAGE_VERSION");
  413. #else
  414. m_InstallPath = "/usr/local/";
  415. #endif
  416. return m_InstallPath.c_str();
  417. }
  418. //----------------------------------------------------------------------
  419. std::string cmCPackGenericGenerator::FindTemplate(const char* name)
  420. {
  421. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Look for template: " << name << std::endl);
  422. std::string ffile = m_MakefileMap->GetModulesFile(name);
  423. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Found template: " << ffile.c_str() << std::endl);
  424. return ffile;
  425. }
  426. //----------------------------------------------------------------------
  427. bool cmCPackGenericGenerator::ConfigureFile(const char* inName, const char* outName)
  428. {
  429. return m_MakefileMap->ConfigureFile(inName, outName, false, true, false) == 1;
  430. }