cmCPackGenerator.cxx 56 KB

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