cmCPackGenerator.cxx 56 KB

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