cmCPackGenerator.cxx 63 KB

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