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 "cmFSPermissions.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmGlobalGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmStateSnapshot.h"
  18. #include "cmVersion.h"
  19. #include "cmWorkingDirectory.h"
  20. #include "cmXMLSafe.h"
  21. #include "cmake.h"
  22. #if defined(__HAIKU__)
  23. #include <FindDirectory.h>
  24. #include <StorageDefs.h>
  25. #endif
  26. cmCPackGenerator::cmCPackGenerator()
  27. {
  28. this->GeneratorVerbose = cmSystemTools::OUTPUT_NONE;
  29. this->MakefileMap = nullptr;
  30. this->Logger = nullptr;
  31. this->componentPackageMethod = ONE_PACKAGE_PER_GROUP;
  32. }
  33. cmCPackGenerator::~cmCPackGenerator()
  34. {
  35. this->MakefileMap = nullptr;
  36. }
  37. void cmCPackGeneratorProgress(const char* msg, float prog, void* ptr)
  38. {
  39. cmCPackGenerator* self = static_cast<cmCPackGenerator*>(ptr);
  40. self->DisplayVerboseOutput(msg, prog);
  41. }
  42. void cmCPackGenerator::DisplayVerboseOutput(const char* msg, float progress)
  43. {
  44. (void)progress;
  45. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "" << msg << std::endl);
  46. }
  47. int cmCPackGenerator::PrepareNames()
  48. {
  49. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Create temp directory." << std::endl);
  50. // checks CPACK_SET_DESTDIR support
  51. if (IsOn("CPACK_SET_DESTDIR")) {
  52. if (SETDESTDIR_UNSUPPORTED == SupportsSetDestdir()) {
  53. cmCPackLogger(
  54. cmCPackLog::LOG_ERROR, "CPACK_SET_DESTDIR is set to ON but the '"
  55. << Name << "' generator does NOT support it." << std::endl);
  56. return 0;
  57. }
  58. if (SETDESTDIR_SHOULD_NOT_BE_USED == SupportsSetDestdir()) {
  59. cmCPackLogger(cmCPackLog::LOG_WARNING,
  60. "CPACK_SET_DESTDIR is set to ON but it is "
  61. << "usually a bad idea to do that with '" << Name
  62. << "' generator. Use at your own risk." << std::endl);
  63. }
  64. }
  65. std::string tempDirectory = this->GetOption("CPACK_PACKAGE_DIRECTORY");
  66. tempDirectory += "/_CPack_Packages/";
  67. const char* toplevelTag = this->GetOption("CPACK_TOPLEVEL_TAG");
  68. if (toplevelTag) {
  69. tempDirectory += toplevelTag;
  70. tempDirectory += "/";
  71. }
  72. tempDirectory += this->GetOption("CPACK_GENERATOR");
  73. std::string topDirectory = tempDirectory;
  74. const char* pfname = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  75. if (!pfname) {
  76. cmCPackLogger(cmCPackLog::LOG_ERROR,
  77. "CPACK_PACKAGE_FILE_NAME not specified" << std::endl);
  78. return 0;
  79. }
  80. std::string outName = pfname;
  81. tempDirectory += "/" + outName;
  82. if (!this->GetOutputExtension()) {
  83. cmCPackLogger(cmCPackLog::LOG_ERROR, "No output extension specified"
  84. << std::endl);
  85. return 0;
  86. }
  87. outName += this->GetOutputExtension();
  88. const char* pdir = this->GetOption("CPACK_PACKAGE_DIRECTORY");
  89. if (!pdir) {
  90. cmCPackLogger(cmCPackLog::LOG_ERROR,
  91. "CPACK_PACKAGE_DIRECTORY not specified" << std::endl);
  92. return 0;
  93. }
  94. std::string destFile = pdir;
  95. this->SetOptionIfNotSet("CPACK_OUTPUT_FILE_PREFIX", destFile.c_str());
  96. destFile += "/" + outName;
  97. std::string outFile = topDirectory + "/" + outName;
  98. this->SetOptionIfNotSet("CPACK_TOPLEVEL_DIRECTORY", topDirectory.c_str());
  99. this->SetOptionIfNotSet("CPACK_TEMPORARY_DIRECTORY", tempDirectory.c_str());
  100. this->SetOptionIfNotSet("CPACK_OUTPUT_FILE_NAME", outName.c_str());
  101. this->SetOptionIfNotSet("CPACK_OUTPUT_FILE_PATH", destFile.c_str());
  102. this->SetOptionIfNotSet("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  103. outFile.c_str());
  104. this->SetOptionIfNotSet("CPACK_INSTALL_DIRECTORY", this->GetInstallPath());
  105. this->SetOptionIfNotSet(
  106. "CPACK_NATIVE_INSTALL_DIRECTORY",
  107. cmsys::SystemTools::ConvertToOutputPath(this->GetInstallPath()).c_str());
  108. this->SetOptionIfNotSet("CPACK_TEMPORARY_INSTALL_DIRECTORY",
  109. tempDirectory.c_str());
  110. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  111. "Look for: CPACK_PACKAGE_DESCRIPTION_FILE" << std::endl);
  112. const char* descFileName = this->GetOption("CPACK_PACKAGE_DESCRIPTION_FILE");
  113. if (descFileName) {
  114. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Look for: " << descFileName
  115. << std::endl);
  116. if (!cmSystemTools::FileExists(descFileName)) {
  117. cmCPackLogger(cmCPackLog::LOG_ERROR,
  118. "Cannot find description file name: ["
  119. << descFileName << "]" << std::endl);
  120. return 0;
  121. }
  122. cmsys::ifstream ifs(descFileName);
  123. if (!ifs) {
  124. cmCPackLogger(cmCPackLog::LOG_ERROR,
  125. "Cannot open description file name: " << descFileName
  126. << std::endl);
  127. return 0;
  128. }
  129. std::ostringstream ostr;
  130. std::string line;
  131. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  132. "Read description file: " << descFileName << std::endl);
  133. while (ifs && cmSystemTools::GetLineFromStream(ifs, line)) {
  134. ostr << cmXMLSafe(line) << std::endl;
  135. }
  136. this->SetOptionIfNotSet("CPACK_PACKAGE_DESCRIPTION", ostr.str().c_str());
  137. }
  138. if (!this->GetOption("CPACK_PACKAGE_DESCRIPTION")) {
  139. cmCPackLogger(
  140. cmCPackLog::LOG_ERROR,
  141. "Project description not specified. Please specify "
  142. "CPACK_PACKAGE_DESCRIPTION or CPACK_PACKAGE_DESCRIPTION_FILE."
  143. << std::endl);
  144. return 0;
  145. }
  146. const char* algoSignature = this->GetOption("CPACK_PACKAGE_CHECKSUM");
  147. if (algoSignature) {
  148. if (!cmCryptoHash::New(algoSignature)) {
  149. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot recognize algorithm: "
  150. << algoSignature << std::endl);
  151. return 0;
  152. }
  153. }
  154. this->SetOptionIfNotSet("CPACK_REMOVE_TOPLEVEL_DIRECTORY", "1");
  155. return 1;
  156. }
  157. int cmCPackGenerator::InstallProject()
  158. {
  159. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Install projects" << std::endl);
  160. this->CleanTemporaryDirectory();
  161. std::string bareTempInstallDirectory =
  162. this->GetOption("CPACK_TEMPORARY_INSTALL_DIRECTORY");
  163. std::string tempInstallDirectoryStr = bareTempInstallDirectory;
  164. bool setDestDir = cmSystemTools::IsOn(this->GetOption("CPACK_SET_DESTDIR")) |
  165. cmSystemTools::IsInternallyOn(this->GetOption("CPACK_SET_DESTDIR"));
  166. if (!setDestDir) {
  167. tempInstallDirectoryStr += this->GetPackagingInstallPrefix();
  168. }
  169. const char* tempInstallDirectory = tempInstallDirectoryStr.c_str();
  170. int res = 1;
  171. if (!cmsys::SystemTools::MakeDirectory(bareTempInstallDirectory.c_str())) {
  172. cmCPackLogger(cmCPackLog::LOG_ERROR,
  173. "Problem creating temporary directory: "
  174. << (tempInstallDirectory ? tempInstallDirectory : "(NULL)")
  175. << std::endl);
  176. return 0;
  177. }
  178. if (setDestDir) {
  179. std::string destDir = "DESTDIR=";
  180. destDir += tempInstallDirectory;
  181. cmSystemTools::PutEnv(destDir);
  182. } else {
  183. // Make sure there is no destdir
  184. cmSystemTools::PutEnv("DESTDIR=");
  185. }
  186. // prepare default created directory permissions
  187. mode_t default_dir_mode_v = 0;
  188. mode_t* default_dir_mode = nullptr;
  189. const char* default_dir_install_permissions =
  190. this->GetOption("CPACK_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS");
  191. if (default_dir_install_permissions && *default_dir_install_permissions) {
  192. std::vector<std::string> items;
  193. cmSystemTools::ExpandListArgument(default_dir_install_permissions, items);
  194. for (const auto& arg : items) {
  195. if (!cmFSPermissions::stringToModeT(arg, default_dir_mode_v)) {
  196. cmCPackLogger(cmCPackLog::LOG_ERROR, "Invalid permission value '"
  197. << arg
  198. << "'."
  199. " CPACK_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS "
  200. "value is invalid."
  201. << std::endl);
  202. return 0;
  203. }
  204. }
  205. default_dir_mode = &default_dir_mode_v;
  206. }
  207. // If the CPackConfig file sets CPACK_INSTALL_COMMANDS then run them
  208. // as listed
  209. if (!this->InstallProjectViaInstallCommands(setDestDir,
  210. tempInstallDirectory)) {
  211. return 0;
  212. }
  213. // If the CPackConfig file sets CPACK_INSTALL_SCRIPT then run them
  214. // as listed
  215. if (!this->InstallProjectViaInstallScript(setDestDir,
  216. tempInstallDirectory)) {
  217. return 0;
  218. }
  219. // If the CPackConfig file sets CPACK_INSTALLED_DIRECTORIES
  220. // then glob it and copy it to CPACK_TEMPORARY_DIRECTORY
  221. // This is used in Source packaging
  222. if (!this->InstallProjectViaInstalledDirectories(
  223. setDestDir, tempInstallDirectory, default_dir_mode)) {
  224. return 0;
  225. }
  226. // If the project is a CMAKE project then run pre-install
  227. // and then read the cmake_install script to run it
  228. if (!this->InstallProjectViaInstallCMakeProjects(
  229. setDestDir, bareTempInstallDirectory, default_dir_mode)) {
  230. return 0;
  231. }
  232. if (setDestDir) {
  233. cmSystemTools::PutEnv("DESTDIR=");
  234. }
  235. return res;
  236. }
  237. int cmCPackGenerator::InstallProjectViaInstallCommands(
  238. bool setDestDir, const std::string& tempInstallDirectory)
  239. {
  240. (void)setDestDir;
  241. const char* installCommands = this->GetOption("CPACK_INSTALL_COMMANDS");
  242. if (installCommands && *installCommands) {
  243. std::string tempInstallDirectoryEnv = "CMAKE_INSTALL_PREFIX=";
  244. tempInstallDirectoryEnv += tempInstallDirectory;
  245. cmSystemTools::PutEnv(tempInstallDirectoryEnv);
  246. std::vector<std::string> installCommandsVector;
  247. cmSystemTools::ExpandListArgument(installCommands, installCommandsVector);
  248. for (std::string const& ic : installCommandsVector) {
  249. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Execute: " << ic << std::endl);
  250. std::string output;
  251. int retVal = 1;
  252. bool resB =
  253. cmSystemTools::RunSingleCommand(ic.c_str(), &output, &output, &retVal,
  254. nullptr, this->GeneratorVerbose, 0);
  255. if (!resB || retVal) {
  256. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  257. tmpFile += "/InstallOutput.log";
  258. cmGeneratedFileStream ofs(tmpFile.c_str());
  259. ofs << "# Run command: " << ic << std::endl
  260. << "# Output:" << std::endl
  261. << output << std::endl;
  262. cmCPackLogger(
  263. cmCPackLog::LOG_ERROR, "Problem running install command: "
  264. << ic << std::endl
  265. << "Please check " << tmpFile << " for errors" << std::endl);
  266. return 0;
  267. }
  268. }
  269. }
  270. return 1;
  271. }
  272. int cmCPackGenerator::InstallProjectViaInstalledDirectories(
  273. bool setDestDir, const std::string& tempInstallDirectory,
  274. const mode_t* default_dir_mode)
  275. {
  276. (void)setDestDir;
  277. (void)tempInstallDirectory;
  278. std::vector<cmsys::RegularExpression> ignoreFilesRegex;
  279. const char* cpackIgnoreFiles = this->GetOption("CPACK_IGNORE_FILES");
  280. if (cpackIgnoreFiles) {
  281. std::vector<std::string> ignoreFilesRegexString;
  282. cmSystemTools::ExpandListArgument(cpackIgnoreFiles,
  283. ignoreFilesRegexString);
  284. for (std::string const& ifr : ignoreFilesRegexString) {
  285. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  286. "Create ignore files regex for: " << ifr << std::endl);
  287. ignoreFilesRegex.emplace_back(ifr);
  288. }
  289. }
  290. const char* installDirectories =
  291. this->GetOption("CPACK_INSTALLED_DIRECTORIES");
  292. if (installDirectories && *installDirectories) {
  293. std::vector<std::string> installDirectoriesVector;
  294. cmSystemTools::ExpandListArgument(installDirectories,
  295. installDirectoriesVector);
  296. if (installDirectoriesVector.size() % 2 != 0) {
  297. cmCPackLogger(
  298. cmCPackLog::LOG_ERROR,
  299. "CPACK_INSTALLED_DIRECTORIES should contain pairs of <directory> and "
  300. "<subdirectory>. The <subdirectory> can be '.' to be installed in "
  301. "the toplevel directory of installation."
  302. << std::endl);
  303. return 0;
  304. }
  305. std::vector<std::string>::iterator it;
  306. const std::string& tempDir = tempInstallDirectory;
  307. for (it = installDirectoriesVector.begin();
  308. it != installDirectoriesVector.end(); ++it) {
  309. std::vector<std::pair<std::string, std::string>> symlinkedFiles;
  310. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Find files" << std::endl);
  311. cmsys::Glob gl;
  312. std::string top = *it;
  313. it++;
  314. std::string subdir = *it;
  315. std::string findExpr = top;
  316. findExpr += "/*";
  317. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  318. "- Install directory: " << top << std::endl);
  319. gl.RecurseOn();
  320. gl.SetRecurseListDirs(true);
  321. if (!gl.FindFiles(findExpr)) {
  322. cmCPackLogger(cmCPackLog::LOG_ERROR,
  323. "Cannot find any files in the installed directory"
  324. << std::endl);
  325. return 0;
  326. }
  327. files = gl.GetFiles();
  328. std::vector<std::string>::iterator gfit;
  329. std::vector<cmsys::RegularExpression>::iterator regIt;
  330. for (std::string const& gf : files) {
  331. bool skip = false;
  332. std::string inFile = gf;
  333. if (cmSystemTools::FileIsDirectory(gf)) {
  334. inFile += '/';
  335. }
  336. for (cmsys::RegularExpression& reg : ignoreFilesRegex) {
  337. if (reg.find(inFile.c_str())) {
  338. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  339. "Ignore file: " << inFile << std::endl);
  340. skip = true;
  341. }
  342. }
  343. if (skip) {
  344. continue;
  345. }
  346. std::string filePath = tempDir;
  347. filePath += "/" + subdir + "/" +
  348. cmSystemTools::RelativePath(top.c_str(), gf.c_str());
  349. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Copy file: "
  350. << inFile << " -> " << filePath << std::endl);
  351. /* If the file is a symlink we will have to re-create it */
  352. if (cmSystemTools::FileIsSymlink(inFile)) {
  353. std::string targetFile;
  354. std::string inFileRelative =
  355. cmSystemTools::RelativePath(top.c_str(), inFile.c_str());
  356. cmSystemTools::ReadSymlink(inFile, targetFile);
  357. symlinkedFiles.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, 0);
  564. if (!resB || retVal) {
  565. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  566. tmpFile += "/PreinstallOutput.log";
  567. cmGeneratedFileStream ofs(tmpFile.c_str());
  568. ofs << "# Run command: " << buildCommand << std::endl
  569. << "# Directory: " << installDirectory << std::endl
  570. << "# Output:" << std::endl
  571. << output << std::endl;
  572. cmCPackLogger(
  573. cmCPackLog::LOG_ERROR, "Problem running install command: "
  574. << buildCommand << std::endl
  575. << "Please check " << tmpFile << " for errors" << std::endl);
  576. return 0;
  577. }
  578. }
  579. delete globalGenerator;
  580. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  581. "- Install project: " << installProjectName << std::endl);
  582. // Run the installation for each component
  583. for (std::string const& component : componentsVector) {
  584. std::string tempInstallDirectory = baseTempInstallDirectory;
  585. installComponent = component;
  586. if (componentInstall) {
  587. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "- Install component: "
  588. << installComponent << std::endl);
  589. }
  590. cmake cm(cmake::RoleScript);
  591. cm.SetHomeDirectory("");
  592. cm.SetHomeOutputDirectory("");
  593. cm.GetCurrentSnapshot().SetDefaultDefinitions();
  594. cm.AddCMakePaths();
  595. cm.SetProgressCallback(cmCPackGeneratorProgress, this);
  596. cm.SetTrace(this->Trace);
  597. cm.SetTraceExpand(this->TraceExpand);
  598. cmGlobalGenerator gg(&cm);
  599. cmMakefile mf(&gg, cm.GetCurrentSnapshot());
  600. if (!installSubDirectory.empty() && installSubDirectory != "/" &&
  601. installSubDirectory != ".") {
  602. tempInstallDirectory += installSubDirectory;
  603. }
  604. if (componentInstall) {
  605. tempInstallDirectory += "/";
  606. // Some CPack generators would rather chose
  607. // the local installation directory suffix.
  608. // Some (e.g. RPM) use
  609. // one install directory for each component **GROUP**
  610. // instead of the default
  611. // one install directory for each component.
  612. tempInstallDirectory +=
  613. GetComponentInstallDirNameSuffix(installComponent);
  614. if (this->IsOn("CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY")) {
  615. tempInstallDirectory += "/";
  616. tempInstallDirectory += this->GetOption("CPACK_PACKAGE_FILE_NAME");
  617. }
  618. }
  619. const char* default_dir_inst_permissions =
  620. this->GetOption("CPACK_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS");
  621. if (default_dir_inst_permissions && *default_dir_inst_permissions) {
  622. mf.AddDefinition("CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS",
  623. default_dir_inst_permissions);
  624. }
  625. if (!setDestDir) {
  626. tempInstallDirectory += this->GetPackagingInstallPrefix();
  627. }
  628. if (setDestDir) {
  629. // For DESTDIR based packaging, use the *project*
  630. // CMAKE_INSTALL_PREFIX underneath the tempInstallDirectory. The
  631. // value of the project's CMAKE_INSTALL_PREFIX is sent in here as
  632. // the value of the CPACK_INSTALL_PREFIX variable.
  633. //
  634. // If DESTDIR has been 'internally set ON' this means that
  635. // the underlying CPack specific generator did ask for that
  636. // In this case we may override CPACK_INSTALL_PREFIX with
  637. // CPACK_PACKAGING_INSTALL_PREFIX
  638. // I know this is tricky and awkward but it's the price for
  639. // CPACK_SET_DESTDIR backward compatibility.
  640. if (cmSystemTools::IsInternallyOn(
  641. this->GetOption("CPACK_SET_DESTDIR"))) {
  642. this->SetOption("CPACK_INSTALL_PREFIX",
  643. this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX"));
  644. }
  645. std::string dir;
  646. if (this->GetOption("CPACK_INSTALL_PREFIX")) {
  647. dir += this->GetOption("CPACK_INSTALL_PREFIX");
  648. }
  649. mf.AddDefinition("CMAKE_INSTALL_PREFIX", dir.c_str());
  650. cmCPackLogger(
  651. cmCPackLog::LOG_DEBUG,
  652. "- Using DESTDIR + CPACK_INSTALL_PREFIX... (mf.AddDefinition)"
  653. << std::endl);
  654. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  655. "- Setting CMAKE_INSTALL_PREFIX to '" << dir << "'"
  656. << std::endl);
  657. // Make sure that DESTDIR + CPACK_INSTALL_PREFIX directory
  658. // exists:
  659. //
  660. if (cmSystemTools::StringStartsWith(dir.c_str(), "/")) {
  661. dir = tempInstallDirectory + dir;
  662. } else {
  663. dir = tempInstallDirectory + "/" + dir;
  664. }
  665. /*
  666. * We must re-set DESTDIR for each component
  667. * We must not add the CPACK_INSTALL_PREFIX part because
  668. * it will be added using the override of CMAKE_INSTALL_PREFIX
  669. * The main reason for this awkward trick is that
  670. * are using DESTDIR for 2 different reasons:
  671. * - Because it was asked by the CPack Generator or the user
  672. * using CPACK_SET_DESTDIR
  673. * - Because it was already used for component install
  674. * in order to put things in subdirs...
  675. */
  676. cmSystemTools::PutEnv(std::string("DESTDIR=") +
  677. tempInstallDirectory);
  678. cmCPackLogger(cmCPackLog::LOG_DEBUG, "- Creating directory: '"
  679. << dir << "'" << std::endl);
  680. if (!cmsys::SystemTools::MakeDirectory(dir, default_dir_mode)) {
  681. cmCPackLogger(
  682. cmCPackLog::LOG_ERROR,
  683. "Problem creating temporary directory: " << dir << std::endl);
  684. return 0;
  685. }
  686. } else {
  687. mf.AddDefinition("CMAKE_INSTALL_PREFIX",
  688. tempInstallDirectory.c_str());
  689. if (!cmsys::SystemTools::MakeDirectory(tempInstallDirectory,
  690. default_dir_mode)) {
  691. cmCPackLogger(cmCPackLog::LOG_ERROR,
  692. "Problem creating temporary directory: "
  693. << tempInstallDirectory << std::endl);
  694. return 0;
  695. }
  696. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  697. "- Using non-DESTDIR install... (mf.AddDefinition)"
  698. << std::endl);
  699. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  700. "- Setting CMAKE_INSTALL_PREFIX to '"
  701. << tempInstallDirectory << "'" << std::endl);
  702. }
  703. if (!buildConfig.empty()) {
  704. mf.AddDefinition("BUILD_TYPE", buildConfig.c_str());
  705. }
  706. std::string installComponentLowerCase =
  707. cmSystemTools::LowerCase(installComponent);
  708. if (installComponentLowerCase != "all") {
  709. mf.AddDefinition("CMAKE_INSTALL_COMPONENT",
  710. installComponent.c_str());
  711. }
  712. // strip on TRUE, ON, 1, one or several file names, but not on
  713. // FALSE, OFF, 0 and an empty string
  714. if (!cmSystemTools::IsOff(this->GetOption("CPACK_STRIP_FILES"))) {
  715. mf.AddDefinition("CMAKE_INSTALL_DO_STRIP", "1");
  716. }
  717. // Remember the list of files before installation
  718. // of the current component (if we are in component install)
  719. const char* InstallPrefix = tempInstallDirectory.c_str();
  720. std::vector<std::string> filesBefore;
  721. std::string findExpr(InstallPrefix);
  722. if (componentInstall) {
  723. cmsys::Glob glB;
  724. findExpr += "/*";
  725. glB.RecurseOn();
  726. glB.SetRecurseListDirs(true);
  727. glB.FindFiles(findExpr);
  728. filesBefore = glB.GetFiles();
  729. std::sort(filesBefore.begin(), filesBefore.end());
  730. }
  731. // If CPack was asked to warn on ABSOLUTE INSTALL DESTINATION
  732. // then forward request to cmake_install.cmake script
  733. if (this->IsOn("CPACK_WARN_ON_ABSOLUTE_INSTALL_DESTINATION")) {
  734. mf.AddDefinition("CMAKE_WARN_ON_ABSOLUTE_INSTALL_DESTINATION", "1");
  735. }
  736. // If current CPack generator does support
  737. // ABSOLUTE INSTALL DESTINATION or CPack has been asked for
  738. // then ask cmake_install.cmake script to error out
  739. // as soon as it occurs (before installing file)
  740. if (!SupportsAbsoluteDestination() ||
  741. this->IsOn("CPACK_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION")) {
  742. mf.AddDefinition("CMAKE_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION", "1");
  743. }
  744. // do installation
  745. int res = mf.ReadListFile(installFile.c_str());
  746. // forward definition of CMAKE_ABSOLUTE_DESTINATION_FILES
  747. // to CPack (may be used by generators like CPack RPM or DEB)
  748. // in order to transparently handle ABSOLUTE PATH
  749. if (mf.GetDefinition("CMAKE_ABSOLUTE_DESTINATION_FILES")) {
  750. mf.AddDefinition(
  751. "CPACK_ABSOLUTE_DESTINATION_FILES",
  752. mf.GetDefinition("CMAKE_ABSOLUTE_DESTINATION_FILES"));
  753. }
  754. // Now rebuild the list of files after installation
  755. // of the current component (if we are in component install)
  756. if (componentInstall) {
  757. cmsys::Glob glA;
  758. glA.RecurseOn();
  759. glA.SetRecurseListDirs(true);
  760. glA.SetRecurseThroughSymlinks(false);
  761. glA.FindFiles(findExpr);
  762. std::vector<std::string> filesAfter = glA.GetFiles();
  763. std::sort(filesAfter.begin(), filesAfter.end());
  764. std::vector<std::string>::iterator diff;
  765. std::vector<std::string> result(filesAfter.size());
  766. diff = std::set_difference(filesAfter.begin(), filesAfter.end(),
  767. filesBefore.begin(), filesBefore.end(),
  768. result.begin());
  769. std::vector<std::string>::iterator fit;
  770. std::string localFileName;
  771. // Populate the File field of each component
  772. for (fit = result.begin(); fit != diff; ++fit) {
  773. localFileName =
  774. cmSystemTools::RelativePath(InstallPrefix, fit->c_str());
  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. }