cmCPackGenerator.cxx 57 KB

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