cmCPackGenericGenerator.cxx 30 KB

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