cmCPackGenerator.cxx 57 KB

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