cmCPackGenerator.cxx 54 KB

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