cmCPackGenerator.cxx 56 KB

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