cmCPackGenericGenerator.cxx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. this->GeneratorVerbose = false;
  27. this->MakefileMap = 0;
  28. this->Logger = 0;
  29. }
  30. //----------------------------------------------------------------------
  31. cmCPackGenericGenerator::~cmCPackGenericGenerator()
  32. {
  33. this->MakefileMap = 0;
  34. }
  35. //----------------------------------------------------------------------
  36. int cmCPackGenericGenerator::PrepareNames()
  37. {
  38. this->SetOption("CPACK_GENERATOR", this->Name.c_str());
  39. std::string tempDirectory = this->GetOption("CPACK_PACKAGE_DIRECTORY");
  40. tempDirectory += "/_CPack_Packages/";
  41. const char* toplevelTag = this->GetOption("CPACK_TOPLEVEL_TAG");
  42. if ( toplevelTag )
  43. {
  44. tempDirectory += toplevelTag;
  45. tempDirectory += "/";
  46. }
  47. tempDirectory += this->GetOption("CPACK_GENERATOR");
  48. std::string topDirectory = tempDirectory;
  49. std::string outName = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  50. tempDirectory += "/" + outName;
  51. outName += ".";
  52. outName += this->GetOutputExtension();
  53. std::string destFile = this->GetOption("CPACK_PACKAGE_DIRECTORY");
  54. destFile += "/" + outName;
  55. std::string outFile = topDirectory + "/" + outName;
  56. std::string installPrefix = tempDirectory + this->GetInstallPrefix();
  57. this->SetOptionIfNotSet("CPACK_TOPLEVEL_DIRECTORY", topDirectory.c_str());
  58. this->SetOptionIfNotSet("CPACK_TEMPORARY_DIRECTORY", tempDirectory.c_str());
  59. this->SetOptionIfNotSet("CPACK_OUTPUT_FILE_NAME", outName.c_str());
  60. this->SetOptionIfNotSet("CPACK_OUTPUT_FILE_PATH", destFile.c_str());
  61. this->SetOptionIfNotSet("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  62. outFile.c_str());
  63. this->SetOptionIfNotSet("CPACK_INSTALL_DIRECTORY", this->GetInstallPath());
  64. this->SetOptionIfNotSet("CPACK_NATIVE_INSTALL_DIRECTORY",
  65. cmsys::SystemTools::ConvertToOutputPath(this->GetInstallPath()).c_str());
  66. this->SetOptionIfNotSet("CPACK_TEMPORARY_INSTALL_DIRECTORY",
  67. installPrefix.c_str());
  68. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  69. "Look for: CPACK_PACKAGE_DESCRIPTION_FILE" << std::endl);
  70. const char* descFileName
  71. = this->GetOption("CPACK_PACKAGE_DESCRIPTION_FILE");
  72. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  73. "Look for: " << descFileName << std::endl);
  74. if ( descFileName )
  75. {
  76. if ( !cmSystemTools::FileExists(descFileName) )
  77. {
  78. cmCPackLogger(cmCPackLog::LOG_ERROR,
  79. "Cannot find description file name: " << descFileName << std::endl);
  80. return 0;
  81. }
  82. std::ifstream ifs(descFileName);
  83. if ( !ifs )
  84. {
  85. cmCPackLogger(cmCPackLog::LOG_ERROR,
  86. "Cannot open description file name: " << descFileName << std::endl);
  87. return 0;
  88. }
  89. cmOStringStream ostr;
  90. std::string line;
  91. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  92. "Read description file: " << descFileName << std::endl);
  93. while ( ifs && cmSystemTools::GetLineFromStream(ifs, line) )
  94. {
  95. ostr << cmSystemTools::MakeXMLSafe(line.c_str()) << std::endl;
  96. }
  97. this->SetOptionIfNotSet("CPACK_PACKAGE_DESCRIPTION", ostr.str().c_str());
  98. }
  99. if ( !this->GetOption("CPACK_PACKAGE_DESCRIPTION") )
  100. {
  101. cmCPackLogger(cmCPackLog::LOG_ERROR,
  102. "Project description not specified. Please specify "
  103. "CPACK_PACKAGE_DESCRIPTION or CPACK_PACKAGE_DESCRIPTION_FILE."
  104. << std::endl);
  105. return 0;
  106. }
  107. std::vector<std::string> path;
  108. std::string pkgPath = cmSystemTools::FindProgram("strip", path, false);
  109. if ( !pkgPath.empty() )
  110. {
  111. this->SetOptionIfNotSet("CPACK_STRIP_COMMAND", pkgPath.c_str());
  112. }
  113. return 1;
  114. }
  115. //----------------------------------------------------------------------
  116. int cmCPackGenericGenerator::InstallProject()
  117. {
  118. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Install projects" << std::endl);
  119. std::vector<cmsys::RegularExpression> ignoreFilesRegex;
  120. const char* cpackIgnoreFiles = this->GetOption("CPACK_IGNORE_FILES");
  121. if ( cpackIgnoreFiles )
  122. {
  123. std::vector<std::string> ignoreFilesRegexString;
  124. cmSystemTools::ExpandListArgument(cpackIgnoreFiles,
  125. ignoreFilesRegexString);
  126. std::vector<std::string>::iterator it;
  127. for ( it = ignoreFilesRegexString.begin();
  128. it != ignoreFilesRegexString.end();
  129. ++it )
  130. {
  131. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  132. "Create ignore files regex for: " << it->c_str() << std::endl);
  133. ignoreFilesRegex.push_back(it->c_str());
  134. }
  135. }
  136. const char* tempInstallDirectory
  137. = this->GetOption("CPACK_TEMPORARY_INSTALL_DIRECTORY");
  138. int res = 1;
  139. if ( !cmsys::SystemTools::MakeDirectory(tempInstallDirectory))
  140. {
  141. cmCPackLogger(cmCPackLog::LOG_ERROR,
  142. "Problem creating temporary directory: " << tempInstallDirectory
  143. << std::endl);
  144. return 0;
  145. }
  146. bool movable = true;
  147. if ( movable )
  148. {
  149. // Make sure there is no destdir
  150. cmSystemTools::PutEnv("DESTDIR=");
  151. }
  152. else
  153. {
  154. std::string destDir = "DESTDIR=";
  155. destDir += tempInstallDirectory;
  156. cmSystemTools::PutEnv(destDir.c_str());
  157. }
  158. const char* installCommands = this->GetOption("CPACK_INSTALL_COMMANDS");
  159. if ( installCommands && *installCommands )
  160. {
  161. std::vector<std::string> installCommandsVector;
  162. cmSystemTools::ExpandListArgument(installCommands,installCommandsVector);
  163. std::vector<std::string>::iterator it;
  164. for ( it = installCommandsVector.begin();
  165. it != installCommandsVector.end();
  166. ++it )
  167. {
  168. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Execute: " << it->c_str()
  169. << std::endl);
  170. std::string output;
  171. int retVal = 1;
  172. bool resB = cmSystemTools::RunSingleCommand(it->c_str(), &output,
  173. &retVal, 0, this->GeneratorVerbose, 0);
  174. if ( !resB || retVal )
  175. {
  176. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  177. tmpFile += "/InstallOutput.log";
  178. cmGeneratedFileStream ofs(tmpFile.c_str());
  179. ofs << "# Run command: " << it->c_str() << std::endl
  180. << "# Output:" << std::endl
  181. << output.c_str() << std::endl;
  182. cmCPackLogger(cmCPackLog::LOG_ERROR,
  183. "Problem running install command: " << it->c_str() << std::endl
  184. << "Please check " << tmpFile.c_str() << " for errors"
  185. << std::endl);
  186. res = 0;
  187. break;
  188. }
  189. }
  190. }
  191. const char* installDirectories
  192. = this->GetOption("CPACK_INSTALLED_DIRECTORIES");
  193. if ( installDirectories && *installDirectories )
  194. {
  195. std::vector<std::string> installDirectoriesVector;
  196. cmSystemTools::ExpandListArgument(installDirectories,
  197. installDirectoriesVector);
  198. if ( installDirectoriesVector.size() % 2 != 0 )
  199. {
  200. cmCPackLogger(cmCPackLog::LOG_ERROR,
  201. "CPACK_INSTALLED_DIRECTORIES should contain pairs of <directory> and "
  202. "<subdirectory>. The <subdirectory> can be '.' to be installed in "
  203. "the toplevel directory of installation." << std::endl);
  204. return 0;
  205. }
  206. std::vector<std::string>::iterator it;
  207. const char* tempDir = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  208. for ( it = installDirectoriesVector.begin();
  209. it != installDirectoriesVector.end();
  210. ++it )
  211. {
  212. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Find files" << std::endl);
  213. cmsys::Glob gl;
  214. std::string toplevel = it->c_str();
  215. it ++;
  216. std::string subdir = it->c_str();
  217. std::string findExpr = toplevel;
  218. findExpr += "/*";
  219. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  220. "- Install directory: " << toplevel << std::endl);
  221. gl.RecurseOn();
  222. if ( !gl.FindFiles(findExpr) )
  223. {
  224. cmCPackLogger(cmCPackLog::LOG_ERROR,
  225. "Cannot find any files in the installed directory" << std::endl);
  226. return 0;
  227. }
  228. std::vector<std::string>& files = gl.GetFiles();
  229. std::vector<std::string>::iterator gfit;
  230. std::vector<cmsys::RegularExpression>::iterator regIt;
  231. for ( gfit = files.begin(); gfit != files.end(); ++ gfit )
  232. {
  233. bool skip = false;
  234. std::string &inFile = *gfit;
  235. for ( regIt= ignoreFilesRegex.begin();
  236. regIt!= ignoreFilesRegex.end();
  237. ++ regIt)
  238. {
  239. if ( regIt->find(inFile.c_str()) )
  240. {
  241. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Ignore file: "
  242. << inFile.c_str() << std::endl);
  243. skip = true;
  244. }
  245. }
  246. if ( skip )
  247. {
  248. continue;
  249. }
  250. std::string filePath = tempDir;
  251. filePath += "/" + subdir + "/"
  252. + cmSystemTools::RelativePath(toplevel.c_str(), gfit->c_str());
  253. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Copy file: "
  254. << inFile.c_str() << " -> " << filePath.c_str() << std::endl);
  255. if ( !cmSystemTools::CopyFileIfDifferent(inFile.c_str(),
  256. filePath.c_str()) )
  257. {
  258. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem copying file: "
  259. << inFile.c_str() << " -> " << filePath.c_str() << std::endl);
  260. return 0;
  261. }
  262. }
  263. }
  264. }
  265. const char* cmakeProjects
  266. = this->GetOption("CPACK_INSTALL_CMAKE_PROJECTS");
  267. const char* cmakeGenerator
  268. = this->GetOption("CPACK_CMAKE_GENERATOR");
  269. std::string currentWorkingDirectory =
  270. cmSystemTools::GetCurrentWorkingDirectory();
  271. if ( cmakeProjects && *cmakeProjects )
  272. {
  273. if ( !cmakeGenerator )
  274. {
  275. cmCPackLogger(cmCPackLog::LOG_ERROR,
  276. "CPACK_INSTALL_CMAKE_PROJECTS is specified, but "
  277. "CPACK_CMAKE_GENERATOR is not. CPACK_CMAKE_GENERATOR "
  278. "is required to install the project."
  279. << std::endl);
  280. return 0;
  281. }
  282. std::vector<std::string> cmakeProjectsVector;
  283. cmSystemTools::ExpandListArgument(cmakeProjects,
  284. cmakeProjectsVector);
  285. std::vector<std::string>::iterator it;
  286. for ( it = cmakeProjectsVector.begin();
  287. it != cmakeProjectsVector.end();
  288. ++it )
  289. {
  290. if ( it+1 == cmakeProjectsVector.end() ||
  291. it+2 == cmakeProjectsVector.end() ||
  292. it+3 == cmakeProjectsVector.end() )
  293. {
  294. cmCPackLogger(cmCPackLog::LOG_ERROR,
  295. "Not enough items on list: CPACK_INSTALL_CMAKE_PROJECTS. "
  296. "CPACK_INSTALL_CMAKE_PROJECTS should hold quadruplet of install "
  297. "directory, install project name, install component, and install "
  298. "subdirectory."
  299. << std::endl);
  300. return 0;
  301. }
  302. std::string installDirectory = it->c_str();
  303. ++it;
  304. std::string installProjectName = it->c_str();
  305. ++it;
  306. std::string installComponent = it->c_str();
  307. ++it;
  308. std::string installSubDirectory = it->c_str();
  309. std::string installFile = installDirectory + "/cmake_install.cmake";
  310. const char* buildConfig = this->GetOption("CPACK_BUILD_CONFIG");
  311. cmGlobalGenerator* globalGenerator
  312. = this->MakefileMap->GetCMakeInstance()->CreateGlobalGenerator(
  313. cmakeGenerator);
  314. // set the global flag for unix style paths on cmSystemTools as
  315. // soon as the generator is set. This allows gmake to be used
  316. // on windows.
  317. cmSystemTools::SetForceUnixPaths(globalGenerator->GetForceUnixPaths());
  318. // Does this generator require pre-install?
  319. if ( globalGenerator->GetPreinstallTargetName() )
  320. {
  321. globalGenerator->FindMakeProgram(this->MakefileMap);
  322. const char* cmakeMakeProgram
  323. = this->MakefileMap->GetDefinition("CMAKE_MAKE_PROGRAM");
  324. std::string buildCommand
  325. = globalGenerator->GenerateBuildCommand(cmakeMakeProgram,
  326. installProjectName.c_str(), 0,
  327. globalGenerator->GetPreinstallTargetName(),
  328. buildConfig, false);
  329. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  330. "- Install command: " << buildCommand << std::endl);
  331. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  332. "- Run preinstall target for: " << installProjectName << std::endl);
  333. std::string output;
  334. int retVal = 1;
  335. bool resB =
  336. cmSystemTools::RunSingleCommand(buildCommand.c_str(),
  337. &output,
  338. &retVal,
  339. installDirectory.c_str(),
  340. this->GeneratorVerbose, 0);
  341. if ( !resB || retVal )
  342. {
  343. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  344. tmpFile += "/PreinstallOutput.log";
  345. cmGeneratedFileStream ofs(tmpFile.c_str());
  346. ofs << "# Run command: " << buildCommand.c_str() << std::endl
  347. << "# Directory: " << installDirectory.c_str() << std::endl
  348. << "# Output:" << std::endl
  349. << output.c_str() << std::endl;
  350. cmCPackLogger(cmCPackLog::LOG_ERROR,
  351. "Problem running install command: " << buildCommand.c_str()
  352. << std::endl
  353. << "Please check " << tmpFile.c_str() << " for errors"
  354. << std::endl);
  355. return 0;
  356. }
  357. }
  358. delete globalGenerator;
  359. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  360. "- Install project: " << installProjectName << std::endl);
  361. cmake cm;
  362. cmGlobalGenerator gg;
  363. gg.SetCMakeInstance(&cm);
  364. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  365. lg->SetGlobalGenerator(&gg);
  366. cmMakefile *mf = lg->GetMakefile();
  367. std::string realInstallDirectory = tempInstallDirectory;
  368. if ( !installSubDirectory.empty() && installSubDirectory != "/" )
  369. {
  370. realInstallDirectory += installSubDirectory;
  371. }
  372. if ( movable )
  373. {
  374. mf->AddDefinition("CMAKE_INSTALL_PREFIX", tempInstallDirectory);
  375. }
  376. if ( buildConfig && *buildConfig )
  377. {
  378. mf->AddDefinition("BUILD_TYPE", buildConfig);
  379. }
  380. std::string installComponentLowerCase
  381. = cmSystemTools::LowerCase(installComponent);
  382. if ( installComponentLowerCase != "all" )
  383. {
  384. mf->AddDefinition("CMAKE_INSTALL_COMPONENT",
  385. installComponent.c_str());
  386. }
  387. res = mf->ReadListFile(0, installFile.c_str());
  388. if ( cmSystemTools::GetErrorOccuredFlag() )
  389. {
  390. res = 0;
  391. }
  392. }
  393. }
  394. const char* binaryDirectories = this->GetOption("CPACK_BINARY_DIR");
  395. if ( binaryDirectories && !cmakeProjects )
  396. {
  397. std::vector<std::string> binaryDirectoriesVector;
  398. cmSystemTools::ExpandListArgument(binaryDirectories,
  399. binaryDirectoriesVector);
  400. std::vector<std::string>::iterator it;
  401. for ( it = binaryDirectoriesVector.begin();
  402. it != binaryDirectoriesVector.end();
  403. ++it )
  404. {
  405. std::string installFile = it->c_str();
  406. installFile += "/cmake_install.cmake";
  407. cmake cm;
  408. cmGlobalGenerator gg;
  409. gg.SetCMakeInstance(&cm);
  410. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  411. lg->SetGlobalGenerator(&gg);
  412. cmMakefile *mf = lg->GetMakefile();
  413. if ( movable )
  414. {
  415. mf->AddDefinition("CMAKE_INSTALL_PREFIX", tempInstallDirectory);
  416. }
  417. const char* buildConfig = this->GetOption("CPACK_BUILD_CONFIG");
  418. if ( buildConfig && *buildConfig )
  419. {
  420. mf->AddDefinition("BUILD_TYPE", buildConfig);
  421. }
  422. res = mf->ReadListFile(0, installFile.c_str());
  423. if ( cmSystemTools::GetErrorOccuredFlag() )
  424. {
  425. res = 0;
  426. }
  427. }
  428. }
  429. if ( !movable )
  430. {
  431. cmSystemTools::PutEnv("DESTDIR=");
  432. }
  433. const char* stripExecutable = this->GetOption("CPACK_STRIP_COMMAND");
  434. const char* stripFiles
  435. = this->GetOption("CPACK_STRIP_FILES");
  436. if ( stripFiles && *stripFiles && stripExecutable && *stripExecutable )
  437. {
  438. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "- Strip files" << std::endl);
  439. std::vector<std::string> stripFilesVector;
  440. cmSystemTools::ExpandListArgument(stripFiles,
  441. stripFilesVector);
  442. std::vector<std::string>::iterator it;
  443. for ( it = stripFilesVector.begin();
  444. it != stripFilesVector.end();
  445. ++it )
  446. {
  447. std::string fileName = tempInstallDirectory;
  448. fileName += "/" + *it;
  449. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  450. " Strip file: " << fileName.c_str()
  451. << std::endl);
  452. std::string stripCommand = stripExecutable;
  453. stripCommand += " \"";
  454. stripCommand += fileName + "\"";
  455. int retVal = 1;
  456. std::string output;
  457. bool resB =
  458. cmSystemTools::RunSingleCommand(stripCommand.c_str(), &output,
  459. &retVal, 0,
  460. this->GeneratorVerbose, 0);
  461. if ( !resB || retVal )
  462. {
  463. cmCPackLogger(cmCPackLog::LOG_ERROR,
  464. "Problem running install command: " << stripCommand.c_str()
  465. << std::endl
  466. << "Error was: \"" << output.c_str() << "\""
  467. << std::endl);
  468. return 0;
  469. }
  470. }
  471. }
  472. return res;
  473. }
  474. //----------------------------------------------------------------------
  475. void cmCPackGenericGenerator::SetOptionIfNotSet(const char* op,
  476. const char* value)
  477. {
  478. if ( this->MakefileMap->GetDefinition(op) )
  479. {
  480. return;
  481. }
  482. this->SetOption(op, value);
  483. }
  484. //----------------------------------------------------------------------
  485. void cmCPackGenericGenerator::SetOption(const char* op, const char* value)
  486. {
  487. if ( !op )
  488. {
  489. return;
  490. }
  491. if ( !value )
  492. {
  493. this->MakefileMap->RemoveDefinition(op);
  494. return;
  495. }
  496. cmCPackLogger(cmCPackLog::LOG_DEBUG, this->GetNameOfClass()
  497. << "::SetOption(" << op << ", " << value << ")" << std::endl);
  498. this->MakefileMap->AddDefinition(op, value);
  499. }
  500. //----------------------------------------------------------------------
  501. int cmCPackGenericGenerator::ProcessGenerator()
  502. {
  503. if ( !this->PrepareNames() )
  504. {
  505. return 0;
  506. }
  507. if ( !this->InstallProject() )
  508. {
  509. return 0;
  510. }
  511. const char* tempPackageFileName = this->GetOption(
  512. "CPACK_TEMPORARY_PACKAGE_FILE_NAME");
  513. const char* packageFileName = this->GetOption("CPACK_OUTPUT_FILE_PATH");
  514. const char* tempDirectory = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  515. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Find files" << std::endl);
  516. cmsys::Glob gl;
  517. std::string findExpr = tempDirectory;
  518. findExpr += "/*";
  519. gl.RecurseOn();
  520. if ( !gl.FindFiles(findExpr) )
  521. {
  522. cmCPackLogger(cmCPackLog::LOG_ERROR,
  523. "Cannot find any files in the packaging tree" << std::endl);
  524. return 0;
  525. }
  526. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Compress package" << std::endl);
  527. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Compress files to: "
  528. << tempPackageFileName << std::endl);
  529. if ( cmSystemTools::FileExists(tempPackageFileName) )
  530. {
  531. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Remove old package file"
  532. << std::endl);
  533. cmSystemTools::RemoveFile(tempPackageFileName);
  534. }
  535. if ( cmSystemTools::IsOn(this->GetOption(
  536. "CPACK_INCLUDE_TOPLEVEL_DIRECTORY")) )
  537. {
  538. tempDirectory = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  539. }
  540. if ( !this->CompressFiles(tempPackageFileName,
  541. tempDirectory, gl.GetFiles()) )
  542. {
  543. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem compressing the directory"
  544. << std::endl);
  545. return 0;
  546. }
  547. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Finalize package" << std::endl);
  548. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Copy final package: "
  549. << tempPackageFileName << " to " << packageFileName << std::endl);
  550. if ( !cmSystemTools::CopyFileIfDifferent(tempPackageFileName,
  551. packageFileName) )
  552. {
  553. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem copying the package: "
  554. << tempPackageFileName << " to " << packageFileName << std::endl);
  555. return 0;
  556. }
  557. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Package " << packageFileName
  558. << " generated." << std::endl);
  559. return 1;
  560. }
  561. //----------------------------------------------------------------------
  562. int cmCPackGenericGenerator::Initialize(const char* name, cmMakefile* mf,
  563. const char* argv0)
  564. {
  565. this->MakefileMap = mf;
  566. this->Name = name;
  567. if ( !this->FindRunningCMake(argv0) )
  568. {
  569. cmCPackLogger(cmCPackLog::LOG_ERROR,
  570. "Cannot initialize the generator" << std::endl);
  571. return 0;
  572. }
  573. return this->InitializeInternal();
  574. }
  575. //----------------------------------------------------------------------
  576. int cmCPackGenericGenerator::InitializeInternal()
  577. {
  578. return 1;
  579. }
  580. //----------------------------------------------------------------------
  581. const char* cmCPackGenericGenerator::GetOption(const char* op)
  582. {
  583. return this->MakefileMap->GetDefinition(op);
  584. }
  585. //----------------------------------------------------------------------
  586. int cmCPackGenericGenerator::FindRunningCMake(const char* arg0)
  587. {
  588. int found = 0;
  589. // Find our own executable.
  590. std::vector<cmStdString> failures;
  591. this->CPackSelf = arg0;
  592. cmSystemTools::ConvertToUnixSlashes(this->CPackSelf);
  593. failures.push_back(this->CPackSelf);
  594. this->CPackSelf = cmSystemTools::FindProgram(this->CPackSelf.c_str());
  595. if(!cmSystemTools::FileExists(this->CPackSelf.c_str()))
  596. {
  597. failures.push_back(this->CPackSelf);
  598. this->CPackSelf = "/usr/local/bin/ctest";
  599. }
  600. if(!cmSystemTools::FileExists(this->CPackSelf.c_str()))
  601. {
  602. failures.push_back(this->CPackSelf);
  603. cmOStringStream msg;
  604. msg << "CTEST can not find the command line program ctest.\n";
  605. msg << " argv[0] = \"" << arg0 << "\"\n";
  606. msg << " Attempted paths:\n";
  607. std::vector<cmStdString>::iterator i;
  608. for(i=failures.begin(); i != failures.end(); ++i)
  609. {
  610. msg << " \"" << i->c_str() << "\"\n";
  611. }
  612. cmSystemTools::Error(msg.str().c_str());
  613. }
  614. std::string dir;
  615. std::string file;
  616. if(cmSystemTools::SplitProgramPath(this->CPackSelf.c_str(),
  617. dir, file, true))
  618. {
  619. this->CMakeSelf = dir += "/cmake";
  620. this->CMakeSelf += cmSystemTools::GetExecutableExtension();
  621. if(cmSystemTools::FileExists(this->CMakeSelf.c_str()))
  622. {
  623. found = 1;
  624. }
  625. }
  626. if ( !found )
  627. {
  628. failures.push_back(this->CMakeSelf);
  629. #ifdef CMAKE_BUILD_DIR
  630. std::string intdir = ".";
  631. #ifdef CMAKE_INTDIR
  632. intdir = CMAKE_INTDIR;
  633. #endif
  634. this->CMakeSelf = CMAKE_BUILD_DIR;
  635. this->CMakeSelf += "/bin/";
  636. this->CMakeSelf += intdir;
  637. this->CMakeSelf += "/cmake";
  638. this->CMakeSelf += cmSystemTools::GetExecutableExtension();
  639. #endif
  640. if(!cmSystemTools::FileExists(this->CMakeSelf.c_str()))
  641. {
  642. failures.push_back(this->CMakeSelf);
  643. cmOStringStream msg;
  644. msg << "CTEST can not find the command line program cmake.\n";
  645. msg << " argv[0] = \"" << arg0 << "\"\n";
  646. msg << " Attempted paths:\n";
  647. std::vector<cmStdString>::iterator i;
  648. for(i=failures.begin(); i != failures.end(); ++i)
  649. {
  650. msg << " \"" << i->c_str() << "\"\n";
  651. }
  652. cmSystemTools::Error(msg.str().c_str());
  653. }
  654. }
  655. // do CMAKE_ROOT, look for the environment variable first
  656. std::string cMakeRoot;
  657. std::string modules;
  658. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT" << std::endl);
  659. if (getenv("CMAKE_ROOT"))
  660. {
  661. cMakeRoot = getenv("CMAKE_ROOT");
  662. modules = cMakeRoot + "/Modules/CMake.cmake";
  663. }
  664. if(modules.empty() || !cmSystemTools::FileExists(modules.c_str()))
  665. {
  666. // next try exe/..
  667. cMakeRoot = cmSystemTools::GetProgramPath(this->CMakeSelf.c_str());
  668. std::string::size_type slashPos = cMakeRoot.rfind("/");
  669. if(slashPos != std::string::npos)
  670. {
  671. cMakeRoot = cMakeRoot.substr(0, slashPos);
  672. }
  673. // is there no Modules direcory there?
  674. modules = cMakeRoot + "/Modules/CMake.cmake";
  675. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT: "
  676. << modules.c_str() << std::endl);
  677. }
  678. if (!cmSystemTools::FileExists(modules.c_str()))
  679. {
  680. // try exe/../share/cmake
  681. cMakeRoot += CMAKE_DATA_DIR;
  682. modules = cMakeRoot + "/Modules/CMake.cmake";
  683. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT: "
  684. << modules.c_str() << std::endl);
  685. }
  686. #ifdef CMAKE_ROOT_DIR
  687. if (!cmSystemTools::FileExists(modules.c_str()))
  688. {
  689. // try compiled in root directory
  690. cMakeRoot = CMAKE_ROOT_DIR;
  691. modules = cMakeRoot + "/Modules/CMake.cmake";
  692. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT: "
  693. << modules.c_str() << std::endl);
  694. }
  695. #endif
  696. #ifdef CMAKE_PREFIX
  697. if (!cmSystemTools::FileExists(modules.c_str()))
  698. {
  699. // try compiled in install prefix
  700. cMakeRoot = CMAKE_PREFIX CMAKE_DATA_DIR;
  701. modules = cMakeRoot + "/Modules/CMake.cmake";
  702. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT: "
  703. << modules.c_str() << std::endl);
  704. }
  705. #endif
  706. if (!cmSystemTools::FileExists(modules.c_str()))
  707. {
  708. // try
  709. cMakeRoot = cmSystemTools::GetProgramPath(this->CMakeSelf.c_str());
  710. cMakeRoot += CMAKE_DATA_DIR;
  711. modules = cMakeRoot + "/Modules/CMake.cmake";
  712. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT: "
  713. << modules.c_str() << std::endl);
  714. }
  715. if(!cmSystemTools::FileExists(modules.c_str()))
  716. {
  717. // next try exe
  718. cMakeRoot = cmSystemTools::GetProgramPath(this->CMakeSelf.c_str());
  719. // is there no Modules direcory there?
  720. modules = cMakeRoot + "/Modules/CMake.cmake";
  721. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT: "
  722. << modules.c_str() << std::endl);
  723. }
  724. if (!cmSystemTools::FileExists(modules.c_str()))
  725. {
  726. // couldn't find modules
  727. cmSystemTools::Error("Could not find CMAKE_ROOT !!!\n"
  728. "CMake has most likely not been installed correctly.\n"
  729. "Modules directory not found in\n",
  730. cMakeRoot.c_str());
  731. return 0;
  732. }
  733. this->CMakeRoot = cMakeRoot;
  734. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT: "
  735. << this->CMakeRoot.c_str() << std::endl);
  736. this->SetOption("CMAKE_ROOT", this->CMakeRoot.c_str());
  737. return 1;
  738. }
  739. //----------------------------------------------------------------------
  740. int cmCPackGenericGenerator::CompressFiles(const char* outFileName,
  741. const char* toplevel, const std::vector<std::string>& files)
  742. {
  743. (void)outFileName;
  744. (void)toplevel;
  745. (void)files;
  746. return 0;
  747. }
  748. //----------------------------------------------------------------------
  749. const char* cmCPackGenericGenerator::GetInstallPath()
  750. {
  751. if ( !this->InstallPath.empty() )
  752. {
  753. return this->InstallPath.c_str();
  754. }
  755. #if defined(_WIN32) && !defined(__CYGWIN__)
  756. const char* prgfiles = cmsys::SystemTools::GetEnv("ProgramFiles");
  757. const char* sysDrive = cmsys::SystemTools::GetEnv("SystemDrive");
  758. if ( prgfiles )
  759. {
  760. this->InstallPath = prgfiles;
  761. }
  762. else if ( sysDrive )
  763. {
  764. this->InstallPath = sysDrive;
  765. this->InstallPath += "/Program Files";
  766. }
  767. else
  768. {
  769. this->InstallPath = "c:/Program Files";
  770. }
  771. this->InstallPath += "/";
  772. this->InstallPath += this->GetOption("CPACK_PACKAGE_NAME");
  773. this->InstallPath += "-";
  774. this->InstallPath += this->GetOption("CPACK_PACKAGE_VERSION");
  775. #else
  776. this->InstallPath = "/usr/local/";
  777. #endif
  778. return this->InstallPath.c_str();
  779. }
  780. //----------------------------------------------------------------------
  781. std::string cmCPackGenericGenerator::FindTemplate(const char* name)
  782. {
  783. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Look for template: "
  784. << name << std::endl);
  785. std::string ffile = this->MakefileMap->GetModulesFile(name);
  786. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Found template: "
  787. << ffile.c_str() << std::endl);
  788. return ffile;
  789. }
  790. //----------------------------------------------------------------------
  791. bool cmCPackGenericGenerator::ConfigureString(const std::string& inString,
  792. std::string& outString)
  793. {
  794. this->MakefileMap->ConfigureString(inString,
  795. outString, true, false);
  796. return true;
  797. }
  798. //----------------------------------------------------------------------
  799. bool cmCPackGenericGenerator::ConfigureFile(const char* inName,
  800. const char* outName)
  801. {
  802. return this->MakefileMap->ConfigureFile(inName, outName,
  803. false, true, false) == 1;
  804. }