cmCPackGenerator.cxx 56 KB

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