cmCPackGenerator.cxx 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCPackGenerator.h"
  11. #include "cmMakefile.h"
  12. #include "cmCPackLog.h"
  13. #include "cmake.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmGeneratedFileStream.h"
  17. #include "cmCPackComponentGroup.h"
  18. #include "cmXMLSafe.h"
  19. #include <cmsys/SystemTools.hxx>
  20. #include <cmsys/Glob.hxx>
  21. #include <cmsys/FStream.hxx>
  22. #include <algorithm>
  23. #include <list>
  24. #if defined(__HAIKU__)
  25. #include <FindDirectory.h>
  26. #include <StorageDefs.h>
  27. #endif
  28. //----------------------------------------------------------------------
  29. cmCPackGenerator::cmCPackGenerator()
  30. {
  31. this->GeneratorVerbose = cmSystemTools::OUTPUT_NONE;
  32. this->MakefileMap = 0;
  33. this->Logger = 0;
  34. this->componentPackageMethod = ONE_PACKAGE_PER_GROUP;
  35. }
  36. //----------------------------------------------------------------------
  37. cmCPackGenerator::~cmCPackGenerator()
  38. {
  39. this->MakefileMap = 0;
  40. }
  41. //----------------------------------------------------------------------
  42. void cmCPackGeneratorProgress(const char *msg, float prog, void* ptr)
  43. {
  44. cmCPackGenerator* self = static_cast<cmCPackGenerator*>(ptr);
  45. self->DisplayVerboseOutput(msg, prog);
  46. }
  47. //----------------------------------------------------------------------
  48. void cmCPackGenerator::DisplayVerboseOutput(const char* msg,
  49. float progress)
  50. {
  51. (void)progress;
  52. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "" << msg << std::endl);
  53. }
  54. //----------------------------------------------------------------------
  55. int cmCPackGenerator::PrepareNames()
  56. {
  57. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  58. "Create temp directory." << std::endl);
  59. // checks CPACK_SET_DESTDIR support
  60. if (IsOn("CPACK_SET_DESTDIR"))
  61. {
  62. if (SETDESTDIR_UNSUPPORTED==SupportsSetDestdir())
  63. {
  64. cmCPackLogger(cmCPackLog::LOG_ERROR,
  65. "CPACK_SET_DESTDIR is set to ON but the '"
  66. << Name << "' generator does NOT support it."
  67. << std::endl);
  68. return 0;
  69. }
  70. else if (SETDESTDIR_SHOULD_NOT_BE_USED==SupportsSetDestdir())
  71. {
  72. cmCPackLogger(cmCPackLog::LOG_WARNING,
  73. "CPACK_SET_DESTDIR is set to ON but it is "
  74. << "usually a bad idea to do that with '"
  75. << Name << "' generator. Use at your own risk."
  76. << std::endl);
  77. }
  78. }
  79. std::string tempDirectory = this->GetOption("CPACK_PACKAGE_DIRECTORY");
  80. tempDirectory += "/_CPack_Packages/";
  81. const char* toplevelTag = this->GetOption("CPACK_TOPLEVEL_TAG");
  82. if ( toplevelTag )
  83. {
  84. tempDirectory += toplevelTag;
  85. tempDirectory += "/";
  86. }
  87. tempDirectory += this->GetOption("CPACK_GENERATOR");
  88. std::string topDirectory = tempDirectory;
  89. const char* pfname = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  90. if(!pfname)
  91. {
  92. cmCPackLogger(cmCPackLog::LOG_ERROR,
  93. "CPACK_PACKAGE_FILE_NAME not specified" << std::endl);
  94. return 0;
  95. }
  96. std::string outName = pfname;
  97. tempDirectory += "/" + outName;
  98. if(!this->GetOutputExtension())
  99. {
  100. cmCPackLogger(cmCPackLog::LOG_ERROR,
  101. "No output extension specified" << std::endl);
  102. return 0;
  103. }
  104. outName += this->GetOutputExtension();
  105. const char* pdir = this->GetOption("CPACK_PACKAGE_DIRECTORY");
  106. if(!pdir)
  107. {
  108. cmCPackLogger(cmCPackLog::LOG_ERROR,
  109. "CPACK_PACKAGE_DIRECTORY not specified" << std::endl);
  110. return 0;
  111. }
  112. std::string destFile = pdir;
  113. this->SetOptionIfNotSet("CPACK_OUTPUT_FILE_PREFIX", destFile.c_str());
  114. destFile += "/" + outName;
  115. std::string outFile = topDirectory + "/" + outName;
  116. this->SetOptionIfNotSet("CPACK_TOPLEVEL_DIRECTORY", topDirectory.c_str());
  117. this->SetOptionIfNotSet("CPACK_TEMPORARY_DIRECTORY", tempDirectory.c_str());
  118. this->SetOptionIfNotSet("CPACK_OUTPUT_FILE_NAME", outName.c_str());
  119. this->SetOptionIfNotSet("CPACK_OUTPUT_FILE_PATH", destFile.c_str());
  120. this->SetOptionIfNotSet("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  121. outFile.c_str());
  122. this->SetOptionIfNotSet("CPACK_INSTALL_DIRECTORY", this->GetInstallPath());
  123. this->SetOptionIfNotSet("CPACK_NATIVE_INSTALL_DIRECTORY",
  124. cmsys::SystemTools::ConvertToOutputPath(this->GetInstallPath()).c_str());
  125. this->SetOptionIfNotSet("CPACK_TEMPORARY_INSTALL_DIRECTORY",
  126. tempDirectory.c_str());
  127. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  128. "Look for: CPACK_PACKAGE_DESCRIPTION_FILE" << std::endl);
  129. const char* descFileName
  130. = this->GetOption("CPACK_PACKAGE_DESCRIPTION_FILE");
  131. if ( descFileName )
  132. {
  133. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  134. "Look for: " << descFileName << std::endl);
  135. if ( !cmSystemTools::FileExists(descFileName) )
  136. {
  137. cmCPackLogger(cmCPackLog::LOG_ERROR,
  138. "Cannot find description file name: ["
  139. << descFileName << "]" << std::endl);
  140. return 0;
  141. }
  142. cmsys::ifstream ifs(descFileName);
  143. if ( !ifs )
  144. {
  145. cmCPackLogger(cmCPackLog::LOG_ERROR,
  146. "Cannot open description file name: " << descFileName << std::endl);
  147. return 0;
  148. }
  149. std::ostringstream ostr;
  150. std::string line;
  151. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  152. "Read description file: " << descFileName << std::endl);
  153. while ( ifs && cmSystemTools::GetLineFromStream(ifs, line) )
  154. {
  155. ostr << cmXMLSafe(line) << std::endl;
  156. }
  157. this->SetOptionIfNotSet("CPACK_PACKAGE_DESCRIPTION", ostr.str().c_str());
  158. }
  159. if ( !this->GetOption("CPACK_PACKAGE_DESCRIPTION") )
  160. {
  161. cmCPackLogger(cmCPackLog::LOG_ERROR,
  162. "Project description not specified. Please specify "
  163. "CPACK_PACKAGE_DESCRIPTION or CPACK_PACKAGE_DESCRIPTION_FILE."
  164. << std::endl);
  165. return 0;
  166. }
  167. this->SetOptionIfNotSet("CPACK_REMOVE_TOPLEVEL_DIRECTORY", "1");
  168. return 1;
  169. }
  170. //----------------------------------------------------------------------
  171. int cmCPackGenerator::InstallProject()
  172. {
  173. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Install projects" << std::endl);
  174. this->CleanTemporaryDirectory();
  175. std::string bareTempInstallDirectory
  176. = this->GetOption("CPACK_TEMPORARY_INSTALL_DIRECTORY");
  177. std::string tempInstallDirectoryStr = bareTempInstallDirectory;
  178. bool setDestDir = cmSystemTools::IsOn(this->GetOption("CPACK_SET_DESTDIR"))
  179. | cmSystemTools::IsInternallyOn(
  180. this->GetOption("CPACK_SET_DESTDIR"));
  181. if (!setDestDir)
  182. {
  183. tempInstallDirectoryStr += this->GetPackagingInstallPrefix();
  184. }
  185. const char* tempInstallDirectory = tempInstallDirectoryStr.c_str();
  186. int res = 1;
  187. if ( !cmsys::SystemTools::MakeDirectory(bareTempInstallDirectory.c_str()))
  188. {
  189. cmCPackLogger(cmCPackLog::LOG_ERROR,
  190. "Problem creating temporary directory: "
  191. << (tempInstallDirectory ? tempInstallDirectory : "(NULL)")
  192. << std::endl);
  193. return 0;
  194. }
  195. if ( setDestDir )
  196. {
  197. std::string destDir = "DESTDIR=";
  198. destDir += tempInstallDirectory;
  199. cmSystemTools::PutEnv(destDir);
  200. }
  201. else
  202. {
  203. // Make sure there is no destdir
  204. cmSystemTools::PutEnv("DESTDIR=");
  205. }
  206. // If the CPackConfig file sets CPACK_INSTALL_COMMANDS then run them
  207. // as listed
  208. if ( !this->InstallProjectViaInstallCommands(
  209. setDestDir, tempInstallDirectory) )
  210. {
  211. return 0;
  212. }
  213. // If the CPackConfig file sets CPACK_INSTALL_SCRIPT then run them
  214. // as listed
  215. if ( !this->InstallProjectViaInstallScript(
  216. setDestDir, tempInstallDirectory) )
  217. {
  218. return 0;
  219. }
  220. // If the CPackConfig file sets CPACK_INSTALLED_DIRECTORIES
  221. // then glob it and copy it to CPACK_TEMPORARY_DIRECTORY
  222. // This is used in Source packaging
  223. if ( !this->InstallProjectViaInstalledDirectories(
  224. setDestDir, tempInstallDirectory) )
  225. {
  226. return 0;
  227. }
  228. // If the project is a CMAKE project then run pre-install
  229. // and then read the cmake_install script to run it
  230. if ( !this->InstallProjectViaInstallCMakeProjects(
  231. setDestDir, bareTempInstallDirectory) )
  232. {
  233. return 0;
  234. }
  235. if ( setDestDir )
  236. {
  237. cmSystemTools::PutEnv("DESTDIR=");
  238. }
  239. return res;
  240. }
  241. //----------------------------------------------------------------------
  242. int cmCPackGenerator::InstallProjectViaInstallCommands(
  243. bool setDestDir, const std::string& tempInstallDirectory)
  244. {
  245. (void) setDestDir;
  246. const char* installCommands = this->GetOption("CPACK_INSTALL_COMMANDS");
  247. if ( installCommands && *installCommands )
  248. {
  249. std::string tempInstallDirectoryEnv = "CMAKE_INSTALL_PREFIX=";
  250. tempInstallDirectoryEnv += tempInstallDirectory;
  251. cmSystemTools::PutEnv(tempInstallDirectoryEnv);
  252. std::vector<std::string> installCommandsVector;
  253. cmSystemTools::ExpandListArgument(installCommands,installCommandsVector);
  254. std::vector<std::string>::iterator it;
  255. for ( it = installCommandsVector.begin();
  256. it != installCommandsVector.end();
  257. ++it )
  258. {
  259. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Execute: " << *it
  260. << std::endl);
  261. std::string output;
  262. int retVal = 1;
  263. bool resB = cmSystemTools::RunSingleCommand(
  264. it->c_str(), &output, &output,
  265. &retVal, 0, this->GeneratorVerbose, 0);
  266. if ( !resB || retVal )
  267. {
  268. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  269. tmpFile += "/InstallOutput.log";
  270. cmGeneratedFileStream ofs(tmpFile.c_str());
  271. ofs << "# Run command: " << *it << std::endl
  272. << "# Output:" << std::endl
  273. << output << std::endl;
  274. cmCPackLogger(cmCPackLog::LOG_ERROR,
  275. "Problem running install command: " << *it << std::endl
  276. << "Please check " << tmpFile << " for errors"
  277. << std::endl);
  278. return 0;
  279. }
  280. }
  281. }
  282. return 1;
  283. }
  284. //----------------------------------------------------------------------
  285. int cmCPackGenerator::InstallProjectViaInstalledDirectories(
  286. bool setDestDir, const std::string& tempInstallDirectory)
  287. {
  288. (void)setDestDir;
  289. (void)tempInstallDirectory;
  290. std::vector<cmsys::RegularExpression> ignoreFilesRegex;
  291. const char* cpackIgnoreFiles = this->GetOption("CPACK_IGNORE_FILES");
  292. if ( cpackIgnoreFiles )
  293. {
  294. std::vector<std::string> ignoreFilesRegexString;
  295. cmSystemTools::ExpandListArgument(cpackIgnoreFiles,
  296. ignoreFilesRegexString);
  297. std::vector<std::string>::iterator it;
  298. for ( it = ignoreFilesRegexString.begin();
  299. it != ignoreFilesRegexString.end();
  300. ++it )
  301. {
  302. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  303. "Create ignore files regex for: " << *it << std::endl);
  304. ignoreFilesRegex.push_back(it->c_str());
  305. }
  306. }
  307. const char* installDirectories
  308. = this->GetOption("CPACK_INSTALLED_DIRECTORIES");
  309. if ( installDirectories && *installDirectories )
  310. {
  311. std::vector<std::string> installDirectoriesVector;
  312. cmSystemTools::ExpandListArgument(installDirectories,
  313. installDirectoriesVector);
  314. if ( installDirectoriesVector.size() % 2 != 0 )
  315. {
  316. cmCPackLogger(cmCPackLog::LOG_ERROR,
  317. "CPACK_INSTALLED_DIRECTORIES should contain pairs of <directory> and "
  318. "<subdirectory>. The <subdirectory> can be '.' to be installed in "
  319. "the toplevel directory of installation." << std::endl);
  320. return 0;
  321. }
  322. std::vector<std::string>::iterator it;
  323. const std::string& tempDir = tempInstallDirectory;
  324. for ( it = installDirectoriesVector.begin();
  325. it != installDirectoriesVector.end();
  326. ++it )
  327. {
  328. std::list<std::pair<std::string,std::string> > symlinkedFiles;
  329. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Find files" << std::endl);
  330. cmsys::Glob gl;
  331. std::string top = *it;
  332. it ++;
  333. std::string subdir = *it;
  334. std::string findExpr = top;
  335. findExpr += "/*";
  336. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  337. "- Install directory: " << top << std::endl);
  338. gl.RecurseOn();
  339. if ( !gl.FindFiles(findExpr) )
  340. {
  341. cmCPackLogger(cmCPackLog::LOG_ERROR,
  342. "Cannot find any files in the installed directory" << std::endl);
  343. return 0;
  344. }
  345. files = gl.GetFiles();
  346. std::vector<std::string>::iterator gfit;
  347. std::vector<cmsys::RegularExpression>::iterator regIt;
  348. for ( gfit = files.begin(); gfit != files.end(); ++ gfit )
  349. {
  350. bool skip = false;
  351. std::string &inFile = *gfit;
  352. for ( regIt= ignoreFilesRegex.begin();
  353. regIt!= ignoreFilesRegex.end();
  354. ++ regIt)
  355. {
  356. if ( regIt->find(inFile.c_str()) )
  357. {
  358. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Ignore file: "
  359. << inFile << std::endl);
  360. skip = true;
  361. }
  362. }
  363. if ( skip )
  364. {
  365. continue;
  366. }
  367. std::string filePath = tempDir;
  368. filePath += "/" + subdir + "/"
  369. + cmSystemTools::RelativePath(top.c_str(), gfit->c_str());
  370. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Copy file: "
  371. << inFile << " -> " << filePath << std::endl);
  372. /* If the file is a symlink we will have to re-create it */
  373. if ( cmSystemTools::FileIsSymlink(inFile))
  374. {
  375. std::string targetFile;
  376. std::string inFileRelative =
  377. cmSystemTools::RelativePath(top.c_str(),inFile.c_str());
  378. cmSystemTools::ReadSymlink(inFile,targetFile);
  379. symlinkedFiles.push_back(std::pair<std::string,
  380. std::string>(targetFile,inFileRelative));
  381. }
  382. /* If it is not a symlink then do a plain copy */
  383. else if (!(
  384. cmSystemTools::CopyFileIfDifferent(inFile.c_str(),filePath.c_str())
  385. &&
  386. cmSystemTools::CopyFileTime(inFile.c_str(),filePath.c_str())
  387. ) )
  388. {
  389. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem copying file: "
  390. << inFile << " -> " << filePath << std::endl);
  391. return 0;
  392. }
  393. }
  394. /* rebuild symlinks in the installed tree */
  395. if (!symlinkedFiles.empty())
  396. {
  397. std::list< std::pair<std::string,std::string> >::iterator symlinkedIt;
  398. std::string curDir = cmSystemTools::GetCurrentWorkingDirectory();
  399. std::string goToDir = tempDir;
  400. goToDir += "/"+subdir;
  401. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  402. "Change dir to: " << goToDir <<std::endl);
  403. cmSystemTools::ChangeDirectory(goToDir);
  404. for (symlinkedIt=symlinkedFiles.begin();
  405. symlinkedIt != symlinkedFiles.end();
  406. ++symlinkedIt)
  407. {
  408. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Will create a symlink: "
  409. << symlinkedIt->second << "--> "
  410. << symlinkedIt->first << std::endl);
  411. // make sure directory exists for symlink
  412. std::string destDir =
  413. cmSystemTools::GetFilenamePath(symlinkedIt->second);
  414. if(!destDir.empty() && !cmSystemTools::MakeDirectory(destDir))
  415. {
  416. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot create dir: "
  417. << destDir
  418. << "\nTrying to create symlink: "
  419. << symlinkedIt->second << "--> "
  420. << symlinkedIt->first
  421. << std::endl);
  422. }
  423. if (!cmSystemTools::CreateSymlink(symlinkedIt->first,
  424. symlinkedIt->second))
  425. {
  426. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot create symlink: "
  427. << symlinkedIt->second << "--> "
  428. << symlinkedIt->first << std::endl);
  429. return 0;
  430. }
  431. }
  432. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Going back to: "
  433. << curDir <<std::endl);
  434. cmSystemTools::ChangeDirectory(curDir);
  435. }
  436. }
  437. }
  438. return 1;
  439. }
  440. //----------------------------------------------------------------------
  441. int cmCPackGenerator::InstallProjectViaInstallScript(
  442. bool setDestDir, const std::string& tempInstallDirectory)
  443. {
  444. const char* cmakeScripts
  445. = this->GetOption("CPACK_INSTALL_SCRIPT");
  446. if ( cmakeScripts && *cmakeScripts )
  447. {
  448. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  449. "- Install scripts: " << cmakeScripts << std::endl);
  450. std::vector<std::string> cmakeScriptsVector;
  451. cmSystemTools::ExpandListArgument(cmakeScripts,
  452. cmakeScriptsVector);
  453. std::vector<std::string>::iterator it;
  454. for ( it = cmakeScriptsVector.begin();
  455. it != cmakeScriptsVector.end();
  456. ++it )
  457. {
  458. std::string installScript = *it;
  459. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  460. "- Install script: " << installScript << std::endl);
  461. if ( setDestDir )
  462. {
  463. // For DESTDIR based packaging, use the *project* CMAKE_INSTALL_PREFIX
  464. // underneath the tempInstallDirectory. The value of the project's
  465. // CMAKE_INSTALL_PREFIX is sent in here as the value of the
  466. // CPACK_INSTALL_PREFIX variable.
  467. std::string dir;
  468. if (this->GetOption("CPACK_INSTALL_PREFIX"))
  469. {
  470. dir += this->GetOption("CPACK_INSTALL_PREFIX");
  471. }
  472. this->SetOption("CMAKE_INSTALL_PREFIX", dir.c_str());
  473. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  474. "- Using DESTDIR + CPACK_INSTALL_PREFIX... (this->SetOption)"
  475. << std::endl);
  476. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  477. "- Setting CMAKE_INSTALL_PREFIX to '" << dir << "'" << std::endl);
  478. }
  479. else
  480. {
  481. this->SetOption("CMAKE_INSTALL_PREFIX", tempInstallDirectory.c_str());
  482. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  483. "- Using non-DESTDIR install... (this->SetOption)" << std::endl);
  484. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  485. "- Setting CMAKE_INSTALL_PREFIX to '" << tempInstallDirectory
  486. << "'" << std::endl);
  487. }
  488. this->SetOptionIfNotSet("CMAKE_CURRENT_BINARY_DIR",
  489. tempInstallDirectory.c_str());
  490. this->SetOptionIfNotSet("CMAKE_CURRENT_SOURCE_DIR",
  491. tempInstallDirectory.c_str());
  492. int res = this->MakefileMap->ReadListFile(installScript.c_str());
  493. if ( cmSystemTools::GetErrorOccuredFlag() || !res )
  494. {
  495. return 0;
  496. }
  497. }
  498. }
  499. return 1;
  500. }
  501. //----------------------------------------------------------------------
  502. int cmCPackGenerator::InstallProjectViaInstallCMakeProjects(
  503. bool setDestDir, const std::string& baseTempInstallDirectory)
  504. {
  505. const char* cmakeProjects
  506. = this->GetOption("CPACK_INSTALL_CMAKE_PROJECTS");
  507. const char* cmakeGenerator
  508. = this->GetOption("CPACK_CMAKE_GENERATOR");
  509. std::string absoluteDestFiles;
  510. if ( cmakeProjects && *cmakeProjects )
  511. {
  512. if ( !cmakeGenerator )
  513. {
  514. cmCPackLogger(cmCPackLog::LOG_ERROR,
  515. "CPACK_INSTALL_CMAKE_PROJECTS is specified, but "
  516. "CPACK_CMAKE_GENERATOR is not. CPACK_CMAKE_GENERATOR "
  517. "is required to install the project."
  518. << std::endl);
  519. return 0;
  520. }
  521. std::vector<std::string> cmakeProjectsVector;
  522. cmSystemTools::ExpandListArgument(cmakeProjects,
  523. cmakeProjectsVector);
  524. std::vector<std::string>::iterator it;
  525. for ( it = cmakeProjectsVector.begin();
  526. it != cmakeProjectsVector.end();
  527. ++it )
  528. {
  529. if ( it+1 == cmakeProjectsVector.end() ||
  530. it+2 == cmakeProjectsVector.end() ||
  531. it+3 == cmakeProjectsVector.end() )
  532. {
  533. cmCPackLogger(cmCPackLog::LOG_ERROR,
  534. "Not enough items on list: CPACK_INSTALL_CMAKE_PROJECTS. "
  535. "CPACK_INSTALL_CMAKE_PROJECTS should hold quadruplet of install "
  536. "directory, install project name, install component, and install "
  537. "subdirectory."
  538. << std::endl);
  539. return 0;
  540. }
  541. std::string installDirectory = *it;
  542. ++it;
  543. std::string installProjectName = *it;
  544. ++it;
  545. std::string installComponent = *it;
  546. ++it;
  547. std::string installSubDirectory = *it;
  548. std::string installFile = installDirectory + "/cmake_install.cmake";
  549. std::vector<std::string> componentsVector;
  550. bool componentInstall = false;
  551. /*
  552. * We do a component install iff
  553. * - the CPack generator support component
  554. * - the user did not request Monolithic install
  555. * (this works at CPack time too)
  556. */
  557. if (this->SupportsComponentInstallation() &
  558. !(this->IsOn("CPACK_MONOLITHIC_INSTALL")))
  559. {
  560. // Determine the installation types for this project (if provided).
  561. std::string installTypesVar = "CPACK_"
  562. + cmSystemTools::UpperCase(installComponent) + "_INSTALL_TYPES";
  563. const char *installTypes = this->GetOption(installTypesVar);
  564. if (installTypes && *installTypes)
  565. {
  566. std::vector<std::string> installTypesVector;
  567. cmSystemTools::ExpandListArgument(installTypes, installTypesVector);
  568. std::vector<std::string>::iterator installTypeIt;
  569. for (installTypeIt = installTypesVector.begin();
  570. installTypeIt != installTypesVector.end();
  571. ++installTypeIt)
  572. {
  573. this->GetInstallationType(installProjectName,
  574. *installTypeIt);
  575. }
  576. }
  577. // Determine the set of components that will be used in this project
  578. std::string componentsVar
  579. = "CPACK_COMPONENTS_" + cmSystemTools::UpperCase(installComponent);
  580. const char *components = this->GetOption(componentsVar);
  581. if (components && *components)
  582. {
  583. cmSystemTools::ExpandListArgument(components, componentsVector);
  584. std::vector<std::string>::iterator compIt;
  585. for (compIt = componentsVector.begin();
  586. compIt != componentsVector.end();
  587. ++compIt)
  588. {
  589. GetComponent(installProjectName, *compIt);
  590. }
  591. componentInstall = true;
  592. }
  593. }
  594. if (componentsVector.empty())
  595. {
  596. componentsVector.push_back(installComponent);
  597. }
  598. const char* buildConfigCstr = this->GetOption("CPACK_BUILD_CONFIG");
  599. std::string buildConfig = buildConfigCstr ? buildConfigCstr : "";
  600. cmGlobalGenerator* globalGenerator
  601. = this->MakefileMap->GetCMakeInstance()->CreateGlobalGenerator(
  602. cmakeGenerator);
  603. if ( !globalGenerator )
  604. {
  605. cmCPackLogger(cmCPackLog::LOG_ERROR,
  606. "Specified package generator not found. "
  607. "CPACK_CMAKE_GENERATOR value is invalid."
  608. << std::endl);
  609. return 0;
  610. }
  611. // set the global flag for unix style paths on cmSystemTools as
  612. // soon as the generator is set. This allows gmake to be used
  613. // on windows.
  614. cmSystemTools::SetForceUnixPaths(globalGenerator->GetForceUnixPaths());
  615. // Does this generator require pre-install?
  616. if (const char* preinstall = globalGenerator->GetPreinstallTargetName())
  617. {
  618. std::string buildCommand =
  619. globalGenerator->GenerateCMakeBuildCommand(
  620. preinstall, buildConfig, "", false);
  621. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  622. "- Install command: " << buildCommand << std::endl);
  623. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  624. "- Run preinstall target for: " << installProjectName << std::endl);
  625. std::string output;
  626. int retVal = 1;
  627. bool resB =
  628. cmSystemTools::RunSingleCommand(buildCommand.c_str(),
  629. &output, &output,
  630. &retVal,
  631. installDirectory.c_str(),
  632. this->GeneratorVerbose, 0);
  633. if ( !resB || retVal )
  634. {
  635. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  636. tmpFile += "/PreinstallOutput.log";
  637. cmGeneratedFileStream ofs(tmpFile.c_str());
  638. ofs << "# Run command: " << buildCommand << std::endl
  639. << "# Directory: " << installDirectory << std::endl
  640. << "# Output:" << std::endl
  641. << output << std::endl;
  642. cmCPackLogger(cmCPackLog::LOG_ERROR,
  643. "Problem running install command: " << buildCommand
  644. << std::endl
  645. << "Please check " << tmpFile << " for errors"
  646. << std::endl);
  647. return 0;
  648. }
  649. }
  650. delete globalGenerator;
  651. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  652. "- Install project: " << installProjectName << std::endl);
  653. // Run the installation for each component
  654. std::vector<std::string>::iterator componentIt;
  655. for (componentIt = componentsVector.begin();
  656. componentIt != componentsVector.end();
  657. ++componentIt)
  658. {
  659. std::string tempInstallDirectory = baseTempInstallDirectory;
  660. installComponent = *componentIt;
  661. if (componentInstall)
  662. {
  663. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  664. "- Install component: " << installComponent
  665. << std::endl);
  666. }
  667. cmake cm;
  668. cm.SetHomeDirectory("");
  669. cm.SetHomeOutputDirectory("");
  670. cm.AddCMakePaths();
  671. cm.SetProgressCallback(cmCPackGeneratorProgress, this);
  672. cmGlobalGenerator gg(&cm);
  673. cmsys::auto_ptr<cmMakefile> mf(
  674. new cmMakefile(&gg, cm.GetCurrentSnapshot()));
  675. cmsys::auto_ptr<cmLocalGenerator> lg(
  676. gg.CreateLocalGenerator(mf.get()));
  677. std::string realInstallDirectory = tempInstallDirectory;
  678. if ( !installSubDirectory.empty() && installSubDirectory != "/" )
  679. {
  680. realInstallDirectory += installSubDirectory;
  681. }
  682. if (componentInstall)
  683. {
  684. tempInstallDirectory += "/";
  685. // Some CPack generators would rather chose
  686. // the local installation directory suffix.
  687. // Some (e.g. RPM) use
  688. // one install directory for each component **GROUP**
  689. // instead of the default
  690. // one install directory for each component.
  691. tempInstallDirectory +=
  692. GetComponentInstallDirNameSuffix(installComponent);
  693. if (this->IsOn("CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY"))
  694. {
  695. tempInstallDirectory += "/";
  696. tempInstallDirectory += this->GetOption("CPACK_PACKAGE_FILE_NAME");
  697. }
  698. }
  699. if (!setDestDir)
  700. {
  701. tempInstallDirectory += this->GetPackagingInstallPrefix();
  702. }
  703. if ( setDestDir )
  704. {
  705. // For DESTDIR based packaging, use the *project*
  706. // CMAKE_INSTALL_PREFIX underneath the tempInstallDirectory. The
  707. // value of the project's CMAKE_INSTALL_PREFIX is sent in here as
  708. // the value of the CPACK_INSTALL_PREFIX variable.
  709. //
  710. // If DESTDIR has been 'internally set ON' this means that
  711. // the underlying CPack specific generator did ask for that
  712. // In this case we may override CPACK_INSTALL_PREFIX with
  713. // CPACK_PACKAGING_INSTALL_PREFIX
  714. // I know this is tricky and awkward but it's the price for
  715. // CPACK_SET_DESTDIR backward compatibility.
  716. if (cmSystemTools::IsInternallyOn(
  717. this->GetOption("CPACK_SET_DESTDIR")))
  718. {
  719. this->SetOption("CPACK_INSTALL_PREFIX",
  720. this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX"));
  721. }
  722. std::string dir;
  723. if (this->GetOption("CPACK_INSTALL_PREFIX"))
  724. {
  725. dir += this->GetOption("CPACK_INSTALL_PREFIX");
  726. }
  727. mf->AddDefinition("CMAKE_INSTALL_PREFIX", dir.c_str());
  728. cmCPackLogger(
  729. cmCPackLog::LOG_DEBUG,
  730. "- Using DESTDIR + CPACK_INSTALL_PREFIX... (mf->AddDefinition)"
  731. << std::endl);
  732. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  733. "- Setting CMAKE_INSTALL_PREFIX to '" << dir << "'"
  734. << std::endl);
  735. // Make sure that DESTDIR + CPACK_INSTALL_PREFIX directory
  736. // exists:
  737. //
  738. if (cmSystemTools::StringStartsWith(dir.c_str(), "/"))
  739. {
  740. dir = tempInstallDirectory + dir;
  741. }
  742. else
  743. {
  744. dir = tempInstallDirectory + "/" + dir;
  745. }
  746. /*
  747. * We must re-set DESTDIR for each component
  748. * We must not add the CPACK_INSTALL_PREFIX part because
  749. * it will be added using the override of CMAKE_INSTALL_PREFIX
  750. * The main reason for this awkward trick is that
  751. * are using DESTDIR for 2 different reasons:
  752. * - Because it was asked by the CPack Generator or the user
  753. * using CPACK_SET_DESTDIR
  754. * - Because it was already used for component install
  755. * in order to put things in subdirs...
  756. */
  757. cmSystemTools::PutEnv(
  758. std::string("DESTDIR=")+tempInstallDirectory
  759. );
  760. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  761. "- Creating directory: '" << dir << "'" << std::endl);
  762. if ( !cmsys::SystemTools::MakeDirectory(dir.c_str()))
  763. {
  764. cmCPackLogger(cmCPackLog::LOG_ERROR,
  765. "Problem creating temporary directory: "
  766. << dir << std::endl);
  767. return 0;
  768. }
  769. }
  770. else
  771. {
  772. mf->AddDefinition("CMAKE_INSTALL_PREFIX",
  773. tempInstallDirectory.c_str());
  774. if ( !cmsys::SystemTools::MakeDirectory(
  775. tempInstallDirectory.c_str()))
  776. {
  777. cmCPackLogger(cmCPackLog::LOG_ERROR,
  778. "Problem creating temporary directory: "
  779. << tempInstallDirectory << std::endl);
  780. return 0;
  781. }
  782. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  783. "- Using non-DESTDIR install... (mf->AddDefinition)"
  784. << std::endl);
  785. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  786. "- Setting CMAKE_INSTALL_PREFIX to '"
  787. << tempInstallDirectory
  788. << "'" << std::endl);
  789. }
  790. if (!buildConfig.empty())
  791. {
  792. mf->AddDefinition("BUILD_TYPE", buildConfig.c_str());
  793. }
  794. std::string installComponentLowerCase
  795. = cmSystemTools::LowerCase(installComponent);
  796. if ( installComponentLowerCase != "all" )
  797. {
  798. mf->AddDefinition("CMAKE_INSTALL_COMPONENT",
  799. installComponent.c_str());
  800. }
  801. // strip on TRUE, ON, 1, one or several file names, but not on
  802. // FALSE, OFF, 0 and an empty string
  803. if (!cmSystemTools::IsOff(this->GetOption("CPACK_STRIP_FILES")))
  804. {
  805. mf->AddDefinition("CMAKE_INSTALL_DO_STRIP", "1");
  806. }
  807. // Remember the list of files before installation
  808. // of the current component (if we are in component install)
  809. const char* InstallPrefix = tempInstallDirectory.c_str();
  810. std::vector<std::string> filesBefore;
  811. std::string findExpr(InstallPrefix);
  812. if (componentInstall)
  813. {
  814. cmsys::Glob glB;
  815. findExpr += "/*";
  816. glB.RecurseOn();
  817. glB.FindFiles(findExpr);
  818. filesBefore = glB.GetFiles();
  819. std::sort(filesBefore.begin(),filesBefore.end());
  820. }
  821. // If CPack was asked to warn on ABSOLUTE INSTALL DESTINATION
  822. // then forward request to cmake_install.cmake script
  823. if (this->IsOn("CPACK_WARN_ON_ABSOLUTE_INSTALL_DESTINATION"))
  824. {
  825. mf->AddDefinition("CMAKE_WARN_ON_ABSOLUTE_INSTALL_DESTINATION",
  826. "1");
  827. }
  828. // If current CPack generator does support
  829. // ABSOLUTE INSTALL DESTINATION or CPack has been asked for
  830. // then ask cmake_install.cmake script to error out
  831. // as soon as it occurs (before installing file)
  832. if (!SupportsAbsoluteDestination() ||
  833. this->IsOn("CPACK_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION"))
  834. {
  835. mf->AddDefinition("CMAKE_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION",
  836. "1");
  837. }
  838. // do installation
  839. int res = mf->ReadListFile(installFile.c_str());
  840. // forward definition of CMAKE_ABSOLUTE_DESTINATION_FILES
  841. // to CPack (may be used by generators like CPack RPM or DEB)
  842. // in order to transparently handle ABSOLUTE PATH
  843. if (mf->GetDefinition("CMAKE_ABSOLUTE_DESTINATION_FILES"))
  844. {
  845. mf->AddDefinition("CPACK_ABSOLUTE_DESTINATION_FILES",
  846. mf->GetDefinition("CMAKE_ABSOLUTE_DESTINATION_FILES"));
  847. }
  848. // Now rebuild the list of files after installation
  849. // of the current component (if we are in component install)
  850. if (componentInstall)
  851. {
  852. cmsys::Glob glA;
  853. glA.RecurseOn();
  854. glA.FindFiles(findExpr);
  855. std::vector<std::string> filesAfter = glA.GetFiles();
  856. std::sort(filesAfter.begin(),filesAfter.end());
  857. std::vector<std::string>::iterator diff;
  858. std::vector<std::string> result(filesAfter.size());
  859. diff = std::set_difference (
  860. filesAfter.begin(),filesAfter.end(),
  861. filesBefore.begin(),filesBefore.end(),
  862. result.begin());
  863. std::vector<std::string>::iterator fit;
  864. std::string localFileName;
  865. // Populate the File field of each component
  866. for (fit=result.begin();fit!=diff;++fit)
  867. {
  868. localFileName =
  869. cmSystemTools::RelativePath(InstallPrefix, fit->c_str());
  870. localFileName =
  871. localFileName.substr(localFileName.find_first_not_of('/'),
  872. std::string::npos);
  873. Components[installComponent].Files.push_back(localFileName);
  874. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Adding file <"
  875. <<localFileName<<"> to component <"
  876. <<installComponent<<">"<<std::endl);
  877. }
  878. }
  879. if (NULL !=mf->GetDefinition("CPACK_ABSOLUTE_DESTINATION_FILES")) {
  880. if (!absoluteDestFiles.empty()) {
  881. absoluteDestFiles +=";";
  882. }
  883. absoluteDestFiles +=
  884. mf->GetDefinition("CPACK_ABSOLUTE_DESTINATION_FILES");
  885. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  886. "Got some ABSOLUTE DESTINATION FILES: "
  887. << absoluteDestFiles << std::endl);
  888. // define component specific var
  889. if (componentInstall)
  890. {
  891. std::string absoluteDestFileComponent =
  892. std::string("CPACK_ABSOLUTE_DESTINATION_FILES")
  893. + "_" + GetComponentInstallDirNameSuffix(installComponent);
  894. if (NULL != this->GetOption(absoluteDestFileComponent))
  895. {
  896. std::string absoluteDestFilesListComponent =
  897. this->GetOption(absoluteDestFileComponent);
  898. absoluteDestFilesListComponent +=";";
  899. absoluteDestFilesListComponent +=
  900. mf->GetDefinition("CPACK_ABSOLUTE_DESTINATION_FILES");
  901. this->SetOption(absoluteDestFileComponent,
  902. absoluteDestFilesListComponent.c_str());
  903. }
  904. else
  905. {
  906. this->SetOption(absoluteDestFileComponent,
  907. mf->GetDefinition("CPACK_ABSOLUTE_DESTINATION_FILES"));
  908. }
  909. }
  910. }
  911. if ( cmSystemTools::GetErrorOccuredFlag() || !res )
  912. {
  913. return 0;
  914. }
  915. }
  916. }
  917. }
  918. this->SetOption("CPACK_ABSOLUTE_DESTINATION_FILES",
  919. absoluteDestFiles.c_str());
  920. return 1;
  921. }
  922. //----------------------------------------------------------------------
  923. bool cmCPackGenerator::ReadListFile(const char* moduleName)
  924. {
  925. bool retval;
  926. std::string fullPath = this->MakefileMap->GetModulesFile(moduleName);
  927. retval = this->MakefileMap->ReadListFile(fullPath.c_str());
  928. // include FATAL_ERROR and ERROR in the return status
  929. retval = retval && (! cmSystemTools::GetErrorOccuredFlag());
  930. return retval;
  931. }
  932. //----------------------------------------------------------------------
  933. void cmCPackGenerator::SetOptionIfNotSet(const std::string& op,
  934. const char* value)
  935. {
  936. const char* def = this->MakefileMap->GetDefinition(op);
  937. if ( def && *def )
  938. {
  939. return;
  940. }
  941. this->SetOption(op, value);
  942. }
  943. //----------------------------------------------------------------------
  944. void cmCPackGenerator::SetOption(const std::string& op, const char* value)
  945. {
  946. if ( !value )
  947. {
  948. this->MakefileMap->RemoveDefinition(op);
  949. return;
  950. }
  951. cmCPackLogger(cmCPackLog::LOG_DEBUG, this->GetNameOfClass()
  952. << "::SetOption(" << op << ", " << value << ")" << std::endl);
  953. this->MakefileMap->AddDefinition(op, value);
  954. }
  955. //----------------------------------------------------------------------
  956. int cmCPackGenerator::DoPackage()
  957. {
  958. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  959. "Create package using " << this->Name << std::endl);
  960. // Prepare CPack internal name and check
  961. // values for many CPACK_xxx vars
  962. if ( !this->PrepareNames() )
  963. {
  964. return 0;
  965. }
  966. // Digest Component grouping specification
  967. if ( !this->PrepareGroupingKind() )
  968. {
  969. return 0;
  970. }
  971. if ( cmSystemTools::IsOn(
  972. this->GetOption("CPACK_REMOVE_TOPLEVEL_DIRECTORY")) )
  973. {
  974. const char* toplevelDirectory
  975. = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  976. if ( cmSystemTools::FileExists(toplevelDirectory) )
  977. {
  978. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  979. "Remove toplevel directory: "
  980. << toplevelDirectory << std::endl);
  981. if ( !cmSystemTools::RepeatedRemoveDirectory(toplevelDirectory) )
  982. {
  983. cmCPackLogger(cmCPackLog::LOG_ERROR,
  984. "Problem removing toplevel directory: "
  985. << toplevelDirectory
  986. << std::endl);
  987. return 0;
  988. }
  989. }
  990. }
  991. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  992. "About to install project " << std::endl);
  993. if ( !this->InstallProject() )
  994. {
  995. return 0;
  996. }
  997. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  998. "Done install project " << std::endl);
  999. const char* tempPackageFileName = this->GetOption(
  1000. "CPACK_TEMPORARY_PACKAGE_FILE_NAME");
  1001. const char* tempDirectory = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  1002. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Find files" << std::endl);
  1003. cmsys::Glob gl;
  1004. std::string findExpr = tempDirectory;
  1005. findExpr += "/*";
  1006. gl.RecurseOn();
  1007. gl.SetRecurseThroughSymlinks(false);
  1008. if ( !gl.FindFiles(findExpr) )
  1009. {
  1010. cmCPackLogger(cmCPackLog::LOG_ERROR,
  1011. "Cannot find any files in the packaging tree" << std::endl);
  1012. return 0;
  1013. }
  1014. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Create package" << std::endl);
  1015. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Package files to: "
  1016. << (tempPackageFileName ? tempPackageFileName : "(NULL)") << std::endl);
  1017. if ( cmSystemTools::FileExists(tempPackageFileName) )
  1018. {
  1019. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Remove old package file"
  1020. << std::endl);
  1021. cmSystemTools::RemoveFile(tempPackageFileName);
  1022. }
  1023. if ( cmSystemTools::IsOn(this->GetOption(
  1024. "CPACK_INCLUDE_TOPLEVEL_DIRECTORY")) )
  1025. {
  1026. tempDirectory = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  1027. }
  1028. // The files to be installed
  1029. files = gl.GetFiles();
  1030. packageFileNames.clear();
  1031. /* Put at least one file name into the list of
  1032. * wanted packageFileNames. The specific generator
  1033. * may update this during PackageFiles.
  1034. * (either putting several names or updating the provided one)
  1035. */
  1036. packageFileNames.push_back(tempPackageFileName ? tempPackageFileName : "");
  1037. toplevel = tempDirectory;
  1038. if ( !this->PackageFiles() || cmSystemTools::GetErrorOccuredFlag())
  1039. {
  1040. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem compressing the directory"
  1041. << std::endl);
  1042. return 0;
  1043. }
  1044. /*
  1045. * Copy the generated packages to final destination
  1046. * - there may be several of them
  1047. * - the initially provided name may have changed
  1048. * (because the specific generator did 'normalize' it)
  1049. */
  1050. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Copying final package(s) ["
  1051. <<packageFileNames.size()
  1052. <<"]:"<<std::endl);
  1053. std::vector<std::string>::iterator it;
  1054. /* now copy package one by one */
  1055. for (it=packageFileNames.begin();it!=packageFileNames.end();++it)
  1056. {
  1057. std::string tmpPF(this->GetOption("CPACK_OUTPUT_FILE_PREFIX"));
  1058. tempPackageFileName = it->c_str();
  1059. tmpPF += "/"+cmSystemTools::GetFilenameName(*it);
  1060. const char* packageFileName = tmpPF.c_str();
  1061. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Copy final package(s): "
  1062. << (tempPackageFileName ? tempPackageFileName : "(NULL)" )
  1063. << " to "
  1064. << (packageFileName ? packageFileName : "(NULL)")
  1065. << std::endl);
  1066. if ( !cmSystemTools::CopyFileIfDifferent(tempPackageFileName,
  1067. packageFileName) )
  1068. {
  1069. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem copying the package: "
  1070. << (tempPackageFileName ? tempPackageFileName : "(NULL)" )
  1071. << " to "
  1072. << (packageFileName ? packageFileName : "(NULL)")
  1073. << std::endl);
  1074. return 0;
  1075. }
  1076. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "- package: "
  1077. << packageFileName
  1078. << " generated." << std::endl);
  1079. }
  1080. return 1;
  1081. }
  1082. //----------------------------------------------------------------------
  1083. int cmCPackGenerator::Initialize(const std::string& name, cmMakefile* mf)
  1084. {
  1085. this->MakefileMap = mf;
  1086. this->Name = name;
  1087. // set the running generator name
  1088. this->SetOption("CPACK_GENERATOR", this->Name.c_str());
  1089. // Load the project specific config file
  1090. const char* config =
  1091. this->GetOption("CPACK_PROJECT_CONFIG_FILE");
  1092. if(config)
  1093. {
  1094. mf->ReadListFile(config);
  1095. }
  1096. int result = this->InitializeInternal();
  1097. if (cmSystemTools::GetErrorOccuredFlag())
  1098. {
  1099. return 0;
  1100. }
  1101. // If a generator subclass did not already set this option in its
  1102. // InitializeInternal implementation, and the project did not already set
  1103. // it, the default value should be:
  1104. this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/");
  1105. return result;
  1106. }
  1107. //----------------------------------------------------------------------
  1108. int cmCPackGenerator::InitializeInternal()
  1109. {
  1110. return 1;
  1111. }
  1112. //----------------------------------------------------------------------
  1113. bool cmCPackGenerator::IsSet(const std::string& name) const
  1114. {
  1115. return this->MakefileMap->IsSet(name);
  1116. }
  1117. //----------------------------------------------------------------------
  1118. bool cmCPackGenerator::IsOn(const std::string& name) const
  1119. {
  1120. return cmSystemTools::IsOn(GetOption(name));
  1121. }
  1122. //----------------------------------------------------------------------
  1123. const char* cmCPackGenerator::GetOption(const std::string& op) const
  1124. {
  1125. const char* ret = this->MakefileMap->GetDefinition(op);
  1126. if(!ret)
  1127. {
  1128. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  1129. "Warning, GetOption return NULL for: "
  1130. << op
  1131. << std::endl);
  1132. }
  1133. return ret;
  1134. }
  1135. //----------------------------------------------------------------------
  1136. std::vector<std::string> cmCPackGenerator::GetOptions() const
  1137. {
  1138. return this->MakefileMap->GetDefinitions();
  1139. }
  1140. //----------------------------------------------------------------------
  1141. int cmCPackGenerator::PackageFiles()
  1142. {
  1143. return 0;
  1144. }
  1145. //----------------------------------------------------------------------
  1146. const char* cmCPackGenerator::GetInstallPath()
  1147. {
  1148. if ( !this->InstallPath.empty() )
  1149. {
  1150. return this->InstallPath.c_str();
  1151. }
  1152. #if defined(_WIN32) && !defined(__CYGWIN__)
  1153. const char* prgfiles = cmsys::SystemTools::GetEnv("ProgramFiles");
  1154. const char* sysDrive = cmsys::SystemTools::GetEnv("SystemDrive");
  1155. if ( prgfiles )
  1156. {
  1157. this->InstallPath = prgfiles;
  1158. }
  1159. else if ( sysDrive )
  1160. {
  1161. this->InstallPath = sysDrive;
  1162. this->InstallPath += "/Program Files";
  1163. }
  1164. else
  1165. {
  1166. this->InstallPath = "c:/Program Files";
  1167. }
  1168. this->InstallPath += "/";
  1169. this->InstallPath += this->GetOption("CPACK_PACKAGE_NAME");
  1170. this->InstallPath += "-";
  1171. this->InstallPath += this->GetOption("CPACK_PACKAGE_VERSION");
  1172. #elif defined(__HAIKU__)
  1173. char dir[B_PATH_NAME_LENGTH];
  1174. if (find_directory(B_SYSTEM_DIRECTORY, -1, false, dir, sizeof(dir)) == B_OK)
  1175. {
  1176. this->InstallPath = dir;
  1177. }
  1178. else
  1179. {
  1180. this->InstallPath = "/boot/system";
  1181. }
  1182. #else
  1183. this->InstallPath = "/usr/local/";
  1184. #endif
  1185. return this->InstallPath.c_str();
  1186. }
  1187. //----------------------------------------------------------------------
  1188. const char* cmCPackGenerator::GetPackagingInstallPrefix()
  1189. {
  1190. cmCPackLogger(cmCPackLog::LOG_DEBUG, "GetPackagingInstallPrefix: '"
  1191. << this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX") << "'" << std::endl);
  1192. return this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX");
  1193. }
  1194. //----------------------------------------------------------------------
  1195. std::string cmCPackGenerator::FindTemplate(const char* name)
  1196. {
  1197. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Look for template: "
  1198. << (name ? name : "(NULL)") << std::endl);
  1199. std::string ffile = this->MakefileMap->GetModulesFile(name);
  1200. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Found template: "
  1201. << ffile << std::endl);
  1202. return ffile;
  1203. }
  1204. //----------------------------------------------------------------------
  1205. bool cmCPackGenerator::ConfigureString(const std::string& inString,
  1206. std::string& outString)
  1207. {
  1208. this->MakefileMap->ConfigureString(inString,
  1209. outString, true, false);
  1210. return true;
  1211. }
  1212. //----------------------------------------------------------------------
  1213. bool cmCPackGenerator::ConfigureFile(const char* inName,
  1214. const char* outName, bool copyOnly /* = false */)
  1215. {
  1216. return this->MakefileMap->ConfigureFile(inName, outName,
  1217. copyOnly, true, false) == 1;
  1218. }
  1219. //----------------------------------------------------------------------
  1220. int cmCPackGenerator::CleanTemporaryDirectory()
  1221. {
  1222. std::string tempInstallDirectoryWithPostfix
  1223. = this->GetOption("CPACK_TEMPORARY_INSTALL_DIRECTORY");
  1224. const char* tempInstallDirectory = tempInstallDirectoryWithPostfix.c_str();
  1225. if(cmsys::SystemTools::FileExists(tempInstallDirectory))
  1226. {
  1227. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  1228. "- Clean temporary : "
  1229. << tempInstallDirectory << std::endl);
  1230. if(!cmSystemTools::RepeatedRemoveDirectory(tempInstallDirectory))
  1231. {
  1232. cmCPackLogger(cmCPackLog::LOG_ERROR,
  1233. "Problem removing temporary directory: " <<
  1234. tempInstallDirectory
  1235. << std::endl);
  1236. return 0;
  1237. }
  1238. }
  1239. return 1;
  1240. }
  1241. //----------------------------------------------------------------------
  1242. cmInstalledFile const* cmCPackGenerator::GetInstalledFile(
  1243. std::string const& name) const
  1244. {
  1245. cmake const* cm = this->MakefileMap->GetCMakeInstance();
  1246. return cm->GetInstalledFile(name);
  1247. }
  1248. //----------------------------------------------------------------------
  1249. int cmCPackGenerator::PrepareGroupingKind()
  1250. {
  1251. // find a component package method specified by the user
  1252. ComponentPackageMethod method = UNKNOWN_COMPONENT_PACKAGE_METHOD;
  1253. if(this->GetOption("CPACK_COMPONENTS_ALL_IN_ONE_PACKAGE"))
  1254. {
  1255. method = ONE_PACKAGE;
  1256. }
  1257. if(this->GetOption("CPACK_COMPONENTS_IGNORE_GROUPS"))
  1258. {
  1259. method = ONE_PACKAGE_PER_COMPONENT;
  1260. }
  1261. if(this->GetOption("CPACK_COMPONENTS_ONE_PACKAGE_PER_GROUP"))
  1262. {
  1263. method = ONE_PACKAGE_PER_GROUP;
  1264. }
  1265. std::string groupingType;
  1266. // Second way to specify grouping
  1267. if (NULL != this->GetOption("CPACK_COMPONENTS_GROUPING")) {
  1268. groupingType = this->GetOption("CPACK_COMPONENTS_GROUPING");
  1269. }
  1270. if (!groupingType.empty())
  1271. {
  1272. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "["
  1273. << this->Name << "]"
  1274. << " requested component grouping = "<< groupingType <<std::endl);
  1275. if (groupingType == "ALL_COMPONENTS_IN_ONE")
  1276. {
  1277. method = ONE_PACKAGE;
  1278. }
  1279. else if (groupingType == "IGNORE")
  1280. {
  1281. method = ONE_PACKAGE_PER_COMPONENT;
  1282. }
  1283. else if (groupingType == "ONE_PER_GROUP")
  1284. {
  1285. method = ONE_PACKAGE_PER_GROUP;
  1286. }
  1287. else
  1288. {
  1289. cmCPackLogger(cmCPackLog::LOG_WARNING, "["
  1290. << this->Name << "]"
  1291. << " requested component grouping type <"<< groupingType
  1292. << "> UNKNOWN not in (ALL_COMPONENTS_IN_ONE,IGNORE,ONE_PER_GROUP)"
  1293. << std::endl);
  1294. }
  1295. }
  1296. // Some components were defined but NO group
  1297. // fallback to default if not group based
  1298. if(method == ONE_PACKAGE_PER_GROUP &&
  1299. this->ComponentGroups.empty() && !this->Components.empty())
  1300. {
  1301. if(componentPackageMethod == ONE_PACKAGE)
  1302. {
  1303. method = ONE_PACKAGE;
  1304. }
  1305. else
  1306. {
  1307. method = ONE_PACKAGE_PER_COMPONENT;
  1308. }
  1309. cmCPackLogger(cmCPackLog::LOG_WARNING, "["
  1310. << this->Name << "]"
  1311. << " One package per component group requested, "
  1312. << "but NO component groups exist: Ignoring component group."
  1313. << std::endl);
  1314. }
  1315. // if user specified packaging method, override the default packaging method
  1316. if(method != UNKNOWN_COMPONENT_PACKAGE_METHOD)
  1317. {
  1318. componentPackageMethod = method;
  1319. }
  1320. const char* method_names[] =
  1321. {
  1322. "ALL_COMPONENTS_IN_ONE",
  1323. "IGNORE_GROUPS",
  1324. "ONE_PER_GROUP"
  1325. };
  1326. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "["
  1327. << this->Name << "]"
  1328. << " requested component grouping = "
  1329. << method_names[componentPackageMethod]
  1330. << std::endl);
  1331. return 1;
  1332. }
  1333. //----------------------------------------------------------------------
  1334. std::string cmCPackGenerator::GetComponentInstallDirNameSuffix(
  1335. const std::string& componentName) {
  1336. return componentName;
  1337. }
  1338. //----------------------------------------------------------------------
  1339. std::string cmCPackGenerator::GetComponentPackageFileName(
  1340. const std::string& initialPackageFileName,
  1341. const std::string& groupOrComponentName,
  1342. bool isGroupName) {
  1343. /*
  1344. * the default behavior is to use the
  1345. * component [group] name as a suffix
  1346. */
  1347. std::string suffix="-"+groupOrComponentName;
  1348. /* check if we should use DISPLAY name */
  1349. std::string dispNameVar = "CPACK_"+Name+"_USE_DISPLAY_NAME_IN_FILENAME";
  1350. if (IsOn(dispNameVar))
  1351. {
  1352. /* the component Group case */
  1353. if (isGroupName)
  1354. {
  1355. std::string groupDispVar = "CPACK_COMPONENT_GROUP_"
  1356. + cmSystemTools::UpperCase(groupOrComponentName) + "_DISPLAY_NAME";
  1357. const char* groupDispName = GetOption(groupDispVar);
  1358. if (groupDispName)
  1359. {
  1360. suffix = "-"+std::string(groupDispName);
  1361. }
  1362. }
  1363. /* the [single] component case */
  1364. else
  1365. {
  1366. std::string dispVar = "CPACK_COMPONENT_"
  1367. + cmSystemTools::UpperCase(groupOrComponentName) + "_DISPLAY_NAME";
  1368. const char* dispName = GetOption(dispVar);
  1369. if(dispName)
  1370. {
  1371. suffix = "-"+std::string(dispName);
  1372. }
  1373. }
  1374. }
  1375. return initialPackageFileName + suffix;
  1376. }
  1377. //----------------------------------------------------------------------
  1378. enum cmCPackGenerator::CPackSetDestdirSupport
  1379. cmCPackGenerator::SupportsSetDestdir() const
  1380. {
  1381. return cmCPackGenerator::SETDESTDIR_SUPPORTED;
  1382. }
  1383. //----------------------------------------------------------------------
  1384. bool cmCPackGenerator::SupportsAbsoluteDestination() const
  1385. {
  1386. return true;
  1387. }
  1388. //----------------------------------------------------------------------
  1389. bool cmCPackGenerator::SupportsComponentInstallation() const
  1390. {
  1391. return false;
  1392. }
  1393. //----------------------------------------------------------------------
  1394. bool cmCPackGenerator::WantsComponentInstallation() const
  1395. {
  1396. return (!IsOn("CPACK_MONOLITHIC_INSTALL")
  1397. && SupportsComponentInstallation()
  1398. // check that we have at least one group or component
  1399. && (!this->ComponentGroups.empty() || !this->Components.empty()));
  1400. }
  1401. //----------------------------------------------------------------------
  1402. cmCPackInstallationType*
  1403. cmCPackGenerator::GetInstallationType(const std::string& projectName,
  1404. const std::string& name)
  1405. {
  1406. (void) projectName;
  1407. bool hasInstallationType = this->InstallationTypes.count(name) != 0;
  1408. cmCPackInstallationType *installType = &this->InstallationTypes[name];
  1409. if (!hasInstallationType)
  1410. {
  1411. // Define the installation type
  1412. std::string macroPrefix = "CPACK_INSTALL_TYPE_"
  1413. + cmsys::SystemTools::UpperCase(name);
  1414. installType->Name = name;
  1415. const char* displayName
  1416. = this->GetOption(macroPrefix + "_DISPLAY_NAME");
  1417. if (displayName && *displayName)
  1418. {
  1419. installType->DisplayName = displayName;
  1420. }
  1421. else
  1422. {
  1423. installType->DisplayName = installType->Name;
  1424. }
  1425. installType->Index = static_cast<unsigned>(
  1426. this->InstallationTypes.size());
  1427. }
  1428. return installType;
  1429. }
  1430. //----------------------------------------------------------------------
  1431. cmCPackComponent*
  1432. cmCPackGenerator::GetComponent(const std::string& projectName,
  1433. const std::string& name)
  1434. {
  1435. bool hasComponent = this->Components.count(name) != 0;
  1436. cmCPackComponent *component = &this->Components[name];
  1437. if (!hasComponent)
  1438. {
  1439. // Define the component
  1440. std::string macroPrefix = "CPACK_COMPONENT_"
  1441. + cmsys::SystemTools::UpperCase(name);
  1442. component->Name = name;
  1443. const char* displayName
  1444. = this->GetOption(macroPrefix + "_DISPLAY_NAME");
  1445. if (displayName && *displayName)
  1446. {
  1447. component->DisplayName = displayName;
  1448. }
  1449. else
  1450. {
  1451. component->DisplayName = component->Name;
  1452. }
  1453. component->IsHidden
  1454. = this->IsOn(macroPrefix + "_HIDDEN");
  1455. component->IsRequired
  1456. = this->IsOn(macroPrefix + "_REQUIRED");
  1457. component->IsDisabledByDefault
  1458. = this->IsOn(macroPrefix + "_DISABLED");
  1459. component->IsDownloaded
  1460. = this->IsOn(macroPrefix + "_DOWNLOADED")
  1461. || cmSystemTools::IsOn(this->GetOption("CPACK_DOWNLOAD_ALL"));
  1462. const char* archiveFile = this->GetOption(macroPrefix +
  1463. "_ARCHIVE_FILE");
  1464. if (archiveFile && *archiveFile)
  1465. {
  1466. component->ArchiveFile = archiveFile;
  1467. }
  1468. const char* groupName = this->GetOption(macroPrefix + "_GROUP");
  1469. if (groupName && *groupName)
  1470. {
  1471. component->Group = GetComponentGroup(projectName, groupName);
  1472. component->Group->Components.push_back(component);
  1473. }
  1474. else
  1475. {
  1476. component->Group = 0;
  1477. }
  1478. const char* description
  1479. = this->GetOption(macroPrefix + "_DESCRIPTION");
  1480. if (description && *description)
  1481. {
  1482. component->Description = description;
  1483. }
  1484. // Determine the installation types.
  1485. const char *installTypes
  1486. = this->GetOption(macroPrefix + "_INSTALL_TYPES");
  1487. if (installTypes && *installTypes)
  1488. {
  1489. std::vector<std::string> installTypesVector;
  1490. cmSystemTools::ExpandListArgument(installTypes, installTypesVector);
  1491. std::vector<std::string>::iterator installTypesIt;
  1492. for (installTypesIt = installTypesVector.begin();
  1493. installTypesIt != installTypesVector.end();
  1494. ++installTypesIt)
  1495. {
  1496. component->InstallationTypes.push_back(
  1497. this->GetInstallationType(projectName, *installTypesIt));
  1498. }
  1499. }
  1500. // Determine the component dependencies.
  1501. const char *depends = this->GetOption(macroPrefix + "_DEPENDS");
  1502. if (depends && *depends)
  1503. {
  1504. std::vector<std::string> dependsVector;
  1505. cmSystemTools::ExpandListArgument(depends, dependsVector);
  1506. std::vector<std::string>::iterator dependIt;
  1507. for (dependIt = dependsVector.begin();
  1508. dependIt != dependsVector.end();
  1509. ++dependIt)
  1510. {
  1511. cmCPackComponent *child = GetComponent(projectName,
  1512. *dependIt);
  1513. component->Dependencies.push_back(child);
  1514. child->ReverseDependencies.push_back(component);
  1515. }
  1516. }
  1517. }
  1518. return component;
  1519. }
  1520. //----------------------------------------------------------------------
  1521. cmCPackComponentGroup*
  1522. cmCPackGenerator::GetComponentGroup(const std::string& projectName,
  1523. const std::string& name)
  1524. {
  1525. (void) projectName;
  1526. std::string macroPrefix = "CPACK_COMPONENT_GROUP_"
  1527. + cmsys::SystemTools::UpperCase(name);
  1528. bool hasGroup = this->ComponentGroups.count(name) != 0;
  1529. cmCPackComponentGroup *group = &this->ComponentGroups[name];
  1530. if (!hasGroup)
  1531. {
  1532. // Define the group
  1533. group->Name = name;
  1534. const char* displayName
  1535. = this->GetOption(macroPrefix + "_DISPLAY_NAME");
  1536. if (displayName && *displayName)
  1537. {
  1538. group->DisplayName = displayName;
  1539. }
  1540. else
  1541. {
  1542. group->DisplayName = group->Name;
  1543. }
  1544. const char* description
  1545. = this->GetOption(macroPrefix + "_DESCRIPTION");
  1546. if (description && *description)
  1547. {
  1548. group->Description = description;
  1549. }
  1550. group->IsBold
  1551. = this->IsOn(macroPrefix + "_BOLD_TITLE");
  1552. group->IsExpandedByDefault
  1553. = this->IsOn(macroPrefix + "_EXPANDED");
  1554. const char* parentGroupName
  1555. = this->GetOption(macroPrefix + "_PARENT_GROUP");
  1556. if (parentGroupName && *parentGroupName)
  1557. {
  1558. group->ParentGroup = GetComponentGroup(projectName, parentGroupName);
  1559. group->ParentGroup->Subgroups.push_back(group);
  1560. }
  1561. else
  1562. {
  1563. group->ParentGroup = 0;
  1564. }
  1565. }
  1566. return group;
  1567. }