cmCPackGenerator.cxx 55 KB

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