cmCPackGenericGenerator.cxx 14 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 "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::GenerateHeader(std::ostream* os)
  244. {
  245. (void)os;
  246. return 1;
  247. }
  248. //----------------------------------------------------------------------
  249. int cmCPackGenericGenerator::FindRunningCMake(const char* arg0)
  250. {
  251. int found = 0;
  252. // Find our own executable.
  253. std::vector<cmStdString> failures;
  254. m_CPackSelf = arg0;
  255. cmSystemTools::ConvertToUnixSlashes(m_CPackSelf);
  256. failures.push_back(m_CPackSelf);
  257. m_CPackSelf = cmSystemTools::FindProgram(m_CPackSelf.c_str());
  258. if(!cmSystemTools::FileExists(m_CPackSelf.c_str()))
  259. {
  260. failures.push_back(m_CPackSelf);
  261. m_CPackSelf = "/usr/local/bin/ctest";
  262. }
  263. if(!cmSystemTools::FileExists(m_CPackSelf.c_str()))
  264. {
  265. failures.push_back(m_CPackSelf);
  266. cmOStringStream msg;
  267. msg << "CTEST can not find the command line program ctest.\n";
  268. msg << " argv[0] = \"" << arg0 << "\"\n";
  269. msg << " Attempted paths:\n";
  270. std::vector<cmStdString>::iterator i;
  271. for(i=failures.begin(); i != failures.end(); ++i)
  272. {
  273. msg << " \"" << i->c_str() << "\"\n";
  274. }
  275. cmSystemTools::Error(msg.str().c_str());
  276. }
  277. std::string dir;
  278. std::string file;
  279. if(cmSystemTools::SplitProgramPath(m_CPackSelf.c_str(),
  280. dir, file, true))
  281. {
  282. m_CMakeSelf = dir += "/cmake";
  283. m_CMakeSelf += cmSystemTools::GetExecutableExtension();
  284. if(cmSystemTools::FileExists(m_CMakeSelf.c_str()))
  285. {
  286. found = 1;
  287. }
  288. }
  289. if ( !found )
  290. {
  291. failures.push_back(m_CMakeSelf);
  292. #ifdef CMAKE_BUILD_DIR
  293. std::string intdir = ".";
  294. #ifdef CMAKE_INTDIR
  295. intdir = CMAKE_INTDIR;
  296. #endif
  297. m_CMakeSelf = CMAKE_BUILD_DIR;
  298. m_CMakeSelf += "/bin/";
  299. m_CMakeSelf += intdir;
  300. m_CMakeSelf += "/cmake";
  301. m_CMakeSelf += cmSystemTools::GetExecutableExtension();
  302. #endif
  303. if(!cmSystemTools::FileExists(m_CMakeSelf.c_str()))
  304. {
  305. failures.push_back(m_CMakeSelf);
  306. cmOStringStream msg;
  307. msg << "CTEST can not find the command line program cmake.\n";
  308. msg << " argv[0] = \"" << arg0 << "\"\n";
  309. msg << " Attempted paths:\n";
  310. std::vector<cmStdString>::iterator i;
  311. for(i=failures.begin(); i != failures.end(); ++i)
  312. {
  313. msg << " \"" << i->c_str() << "\"\n";
  314. }
  315. cmSystemTools::Error(msg.str().c_str());
  316. }
  317. }
  318. // do CMAKE_ROOT, look for the environment variable first
  319. std::string cMakeRoot;
  320. std::string modules;
  321. if (getenv("CMAKE_ROOT"))
  322. {
  323. cMakeRoot = getenv("CMAKE_ROOT");
  324. modules = cMakeRoot + "/Modules/CMake.cmake";
  325. }
  326. if(!cmSystemTools::FileExists(modules.c_str()))
  327. {
  328. // next try exe/..
  329. cMakeRoot = cmSystemTools::GetProgramPath(m_CMakeSelf.c_str());
  330. std::string::size_type slashPos = cMakeRoot.rfind("/");
  331. if(slashPos != std::string::npos)
  332. {
  333. cMakeRoot = cMakeRoot.substr(0, slashPos);
  334. }
  335. // is there no Modules direcory there?
  336. modules = cMakeRoot + "/Modules/CMake.cmake";
  337. }
  338. if (!cmSystemTools::FileExists(modules.c_str()))
  339. {
  340. // try exe/../share/cmake
  341. cMakeRoot += CMAKE_DATA_DIR;
  342. modules = cMakeRoot + "/Modules/CMake.cmake";
  343. }
  344. #ifdef CMAKE_ROOT_DIR
  345. if (!cmSystemTools::FileExists(modules.c_str()))
  346. {
  347. // try compiled in root directory
  348. cMakeRoot = CMAKE_ROOT_DIR;
  349. modules = cMakeRoot + "/Modules/CMake.cmake";
  350. }
  351. #endif
  352. #ifdef CMAKE_PREFIX
  353. if (!cmSystemTools::FileExists(modules.c_str()))
  354. {
  355. // try compiled in install prefix
  356. cMakeRoot = CMAKE_PREFIX CMAKE_DATA_DIR;
  357. modules = cMakeRoot + "/Modules/CMake.cmake";
  358. }
  359. #endif
  360. if (!cmSystemTools::FileExists(modules.c_str()))
  361. {
  362. // try
  363. cMakeRoot = cmSystemTools::GetProgramPath(m_CMakeSelf.c_str());
  364. cMakeRoot += CMAKE_DATA_DIR;
  365. modules = cMakeRoot + "/Modules/CMake.cmake";
  366. }
  367. if(!cmSystemTools::FileExists(modules.c_str()))
  368. {
  369. // next try exe
  370. cMakeRoot = cmSystemTools::GetProgramPath(m_CMakeSelf.c_str());
  371. // is there no Modules direcory there?
  372. modules = cMakeRoot + "/Modules/CMake.cmake";
  373. }
  374. if (!cmSystemTools::FileExists(modules.c_str()))
  375. {
  376. // couldn't find modules
  377. cmSystemTools::Error("Could not find CMAKE_ROOT !!!\n"
  378. "CMake has most likely not been installed correctly.\n"
  379. "Modules directory not found in\n",
  380. cMakeRoot.c_str());
  381. return 0;
  382. }
  383. m_CMakeRoot = cMakeRoot;
  384. return 1;
  385. }
  386. //----------------------------------------------------------------------
  387. int cmCPackGenericGenerator::CompressFiles(const char* outFileName, const char* toplevel,
  388. const std::vector<std::string>& files)
  389. {
  390. (void)outFileName;
  391. (void)toplevel;
  392. (void)files;
  393. return 0;
  394. }
  395. //----------------------------------------------------------------------
  396. const char* cmCPackGenericGenerator::GetInstallPath()
  397. {
  398. if ( !m_InstallPath.empty() )
  399. {
  400. return m_InstallPath.c_str();
  401. }
  402. #if defined(_WIN32) && !defined(__CYGWIN__)
  403. const char* prgfiles = cmsys::SystemTools::GetEnv("ProgramFiles");
  404. const char* sysDrive = cmsys::SystemTools::GetEnv("SystemDrive");
  405. if ( prgfiles )
  406. {
  407. m_InstallPath = prgfiles;
  408. }
  409. else if ( sysDrive )
  410. {
  411. m_InstallPath = sysDrive;
  412. m_InstallPath += "/Program Files";
  413. }
  414. else
  415. {
  416. m_InstallPath = "c:/Program Files";
  417. }
  418. m_InstallPath += "/";
  419. m_InstallPath += this->GetOption("CPACK_PROJECT_NAME");
  420. m_InstallPath += "-";
  421. m_InstallPath += this->GetOption("CPACK_PROJECT_VERSION");
  422. #else
  423. m_InstallPath = "/usr/local/";
  424. #endif
  425. return m_InstallPath.c_str();
  426. }
  427. //----------------------------------------------------------------------
  428. std::string cmCPackGenericGenerator::FindTemplate(const char* name)
  429. {
  430. return m_MakefileMap->GetModulesFile(name);
  431. }
  432. //----------------------------------------------------------------------
  433. bool cmCPackGenericGenerator::ConfigureFile(const char* inName, const char* outName)
  434. {
  435. return m_MakefileMap->ConfigureFile(inName, outName, false, true, false);
  436. }