cmCPackGenericGenerator.cxx 16 KB

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