cmCPackGenericGenerator.cxx 14 KB

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