cmCPackGenerator.cxx 36 KB

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