cmCPackGenerator.cxx 56 KB

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