cmCPackGenericGenerator.cxx 34 KB

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