cmCPackGenericGenerator.cxx 16 KB

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