cmCPackGenerator.cxx 64 KB

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