cmCPackGenerator.cxx 56 KB

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