cmCPackGenerator.cxx 55 KB

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