cmCPackGenerator.cxx 56 KB

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