cmCPackGenerator.cxx 56 KB

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