cmCPackGenerator.cxx 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  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 <algorithm>
  5. #include <cmsys/FStream.hxx>
  6. #include <cmsys/Glob.hxx>
  7. #include <cmsys/RegularExpression.hxx>
  8. #include <list>
  9. #include <utility>
  10. #include "cmCPackComponentGroup.h"
  11. #include "cmCPackLog.h"
  12. #include "cmCryptoHash.h"
  13. #include "cmGeneratedFileStream.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmMakefile.h"
  16. #include "cmStateSnapshot.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 = CM_NULLPTR;
  28. this->Logger = CM_NULLPTR;
  29. this->componentPackageMethod = ONE_PACKAGE_PER_GROUP;
  30. }
  31. cmCPackGenerator::~cmCPackGenerator()
  32. {
  33. this->MakefileMap = CM_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() == CM_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. CM_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::list<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::list<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. cmSystemTools::ChangeDirectory(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. cmSystemTools::ChangeDirectory(curDir);
  385. }
  386. }
  387. }
  388. return 1;
  389. }
  390. int cmCPackGenerator::InstallProjectViaInstallScript(
  391. bool setDestDir, const std::string& tempInstallDirectory)
  392. {
  393. const char* cmakeScripts = this->GetOption("CPACK_INSTALL_SCRIPT");
  394. if (cmakeScripts && *cmakeScripts) {
  395. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "- Install scripts: " << cmakeScripts
  396. << std::endl);
  397. std::vector<std::string> cmakeScriptsVector;
  398. cmSystemTools::ExpandListArgument(cmakeScripts, cmakeScriptsVector);
  399. std::vector<std::string>::iterator it;
  400. for (it = cmakeScriptsVector.begin(); it != cmakeScriptsVector.end();
  401. ++it) {
  402. std::string installScript = *it;
  403. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  404. "- Install script: " << installScript << std::endl);
  405. if (setDestDir) {
  406. // For DESTDIR based packaging, use the *project* CMAKE_INSTALL_PREFIX
  407. // underneath the tempInstallDirectory. The value of the project's
  408. // CMAKE_INSTALL_PREFIX is sent in here as the value of the
  409. // CPACK_INSTALL_PREFIX variable.
  410. std::string dir;
  411. if (this->GetOption("CPACK_INSTALL_PREFIX")) {
  412. dir += this->GetOption("CPACK_INSTALL_PREFIX");
  413. }
  414. this->SetOption("CMAKE_INSTALL_PREFIX", dir.c_str());
  415. cmCPackLogger(
  416. cmCPackLog::LOG_DEBUG,
  417. "- Using DESTDIR + CPACK_INSTALL_PREFIX... (this->SetOption)"
  418. << std::endl);
  419. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  420. "- Setting CMAKE_INSTALL_PREFIX to '" << dir << "'"
  421. << std::endl);
  422. } else {
  423. this->SetOption("CMAKE_INSTALL_PREFIX", tempInstallDirectory.c_str());
  424. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  425. "- Using non-DESTDIR install... (this->SetOption)"
  426. << std::endl);
  427. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  428. "- Setting CMAKE_INSTALL_PREFIX to '"
  429. << tempInstallDirectory << "'" << std::endl);
  430. }
  431. this->SetOptionIfNotSet("CMAKE_CURRENT_BINARY_DIR",
  432. tempInstallDirectory.c_str());
  433. this->SetOptionIfNotSet("CMAKE_CURRENT_SOURCE_DIR",
  434. tempInstallDirectory.c_str());
  435. int res = this->MakefileMap->ReadListFile(installScript.c_str());
  436. if (cmSystemTools::GetErrorOccuredFlag() || !res) {
  437. return 0;
  438. }
  439. }
  440. }
  441. return 1;
  442. }
  443. int cmCPackGenerator::InstallProjectViaInstallCMakeProjects(
  444. bool setDestDir, const std::string& baseTempInstallDirectory)
  445. {
  446. const char* cmakeProjects = this->GetOption("CPACK_INSTALL_CMAKE_PROJECTS");
  447. const char* cmakeGenerator = this->GetOption("CPACK_CMAKE_GENERATOR");
  448. std::string absoluteDestFiles;
  449. if (cmakeProjects && *cmakeProjects) {
  450. if (!cmakeGenerator) {
  451. cmCPackLogger(cmCPackLog::LOG_ERROR,
  452. "CPACK_INSTALL_CMAKE_PROJECTS is specified, but "
  453. "CPACK_CMAKE_GENERATOR is not. CPACK_CMAKE_GENERATOR "
  454. "is required to install the project."
  455. << std::endl);
  456. return 0;
  457. }
  458. std::vector<std::string> cmakeProjectsVector;
  459. cmSystemTools::ExpandListArgument(cmakeProjects, cmakeProjectsVector);
  460. std::vector<std::string>::iterator it;
  461. for (it = cmakeProjectsVector.begin(); it != cmakeProjectsVector.end();
  462. ++it) {
  463. if (it + 1 == cmakeProjectsVector.end() ||
  464. it + 2 == cmakeProjectsVector.end() ||
  465. it + 3 == cmakeProjectsVector.end()) {
  466. cmCPackLogger(
  467. cmCPackLog::LOG_ERROR,
  468. "Not enough items on list: CPACK_INSTALL_CMAKE_PROJECTS. "
  469. "CPACK_INSTALL_CMAKE_PROJECTS should hold quadruplet of install "
  470. "directory, install project name, install component, and install "
  471. "subdirectory."
  472. << std::endl);
  473. return 0;
  474. }
  475. std::string installDirectory = *it;
  476. ++it;
  477. std::string installProjectName = *it;
  478. ++it;
  479. std::string installComponent = *it;
  480. ++it;
  481. std::string installSubDirectory = *it;
  482. std::string installFile = installDirectory + "/cmake_install.cmake";
  483. std::vector<std::string> componentsVector;
  484. bool componentInstall = false;
  485. /*
  486. * We do a component install iff
  487. * - the CPack generator support component
  488. * - the user did not request Monolithic install
  489. * (this works at CPack time too)
  490. */
  491. if (this->SupportsComponentInstallation() &
  492. !(this->IsOn("CPACK_MONOLITHIC_INSTALL"))) {
  493. // Determine the installation types for this project (if provided).
  494. std::string installTypesVar = "CPACK_" +
  495. cmSystemTools::UpperCase(installComponent) + "_INSTALL_TYPES";
  496. const char* installTypes = this->GetOption(installTypesVar);
  497. if (installTypes && *installTypes) {
  498. std::vector<std::string> installTypesVector;
  499. cmSystemTools::ExpandListArgument(installTypes, installTypesVector);
  500. std::vector<std::string>::iterator installTypeIt;
  501. for (installTypeIt = installTypesVector.begin();
  502. installTypeIt != installTypesVector.end(); ++installTypeIt) {
  503. this->GetInstallationType(installProjectName, *installTypeIt);
  504. }
  505. }
  506. // Determine the set of components that will be used in this project
  507. std::string componentsVar =
  508. "CPACK_COMPONENTS_" + cmSystemTools::UpperCase(installComponent);
  509. const char* components = this->GetOption(componentsVar);
  510. if (components && *components) {
  511. cmSystemTools::ExpandListArgument(components, componentsVector);
  512. std::vector<std::string>::iterator compIt;
  513. for (compIt = componentsVector.begin();
  514. compIt != componentsVector.end(); ++compIt) {
  515. GetComponent(installProjectName, *compIt);
  516. }
  517. componentInstall = true;
  518. }
  519. }
  520. if (componentsVector.empty()) {
  521. componentsVector.push_back(installComponent);
  522. }
  523. const char* buildConfigCstr = this->GetOption("CPACK_BUILD_CONFIG");
  524. std::string buildConfig = buildConfigCstr ? buildConfigCstr : "";
  525. cmGlobalGenerator* globalGenerator =
  526. this->MakefileMap->GetCMakeInstance()->CreateGlobalGenerator(
  527. cmakeGenerator);
  528. if (!globalGenerator) {
  529. cmCPackLogger(cmCPackLog::LOG_ERROR,
  530. "Specified package generator not found. "
  531. "CPACK_CMAKE_GENERATOR value is invalid."
  532. << std::endl);
  533. return 0;
  534. }
  535. // set the global flag for unix style paths on cmSystemTools as
  536. // soon as the generator is set. This allows gmake to be used
  537. // on windows.
  538. cmSystemTools::SetForceUnixPaths(globalGenerator->GetForceUnixPaths());
  539. // Does this generator require pre-install?
  540. if (const char* preinstall =
  541. globalGenerator->GetPreinstallTargetName()) {
  542. std::string buildCommand = globalGenerator->GenerateCMakeBuildCommand(
  543. preinstall, buildConfig, "", false);
  544. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  545. "- Install command: " << buildCommand << std::endl);
  546. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "- Run preinstall target for: "
  547. << installProjectName << std::endl);
  548. std::string output;
  549. int retVal = 1;
  550. bool resB = cmSystemTools::RunSingleCommand(
  551. buildCommand.c_str(), &output, &output, &retVal,
  552. installDirectory.c_str(), this->GeneratorVerbose, 0);
  553. if (!resB || retVal) {
  554. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  555. tmpFile += "/PreinstallOutput.log";
  556. cmGeneratedFileStream ofs(tmpFile.c_str());
  557. ofs << "# Run command: " << buildCommand << std::endl
  558. << "# Directory: " << installDirectory << std::endl
  559. << "# Output:" << std::endl
  560. << output << std::endl;
  561. cmCPackLogger(
  562. cmCPackLog::LOG_ERROR, "Problem running install command: "
  563. << buildCommand << std::endl
  564. << "Please check " << tmpFile << " for errors" << std::endl);
  565. return 0;
  566. }
  567. }
  568. delete globalGenerator;
  569. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  570. "- Install project: " << installProjectName << std::endl);
  571. // Run the installation for each component
  572. std::vector<std::string>::iterator componentIt;
  573. for (componentIt = componentsVector.begin();
  574. componentIt != componentsVector.end(); ++componentIt) {
  575. std::string tempInstallDirectory = baseTempInstallDirectory;
  576. installComponent = *componentIt;
  577. if (componentInstall) {
  578. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "- Install component: "
  579. << installComponent << std::endl);
  580. }
  581. cmake cm;
  582. cm.SetHomeDirectory("");
  583. cm.SetHomeOutputDirectory("");
  584. cm.GetCurrentSnapshot().SetDefaultDefinitions();
  585. cm.AddCMakePaths();
  586. cm.SetProgressCallback(cmCPackGeneratorProgress, this);
  587. cmGlobalGenerator gg(&cm);
  588. CM_AUTO_PTR<cmMakefile> mf(
  589. new cmMakefile(&gg, cm.GetCurrentSnapshot()));
  590. if (!installSubDirectory.empty() && installSubDirectory != "/" &&
  591. installSubDirectory != ".") {
  592. tempInstallDirectory += installSubDirectory;
  593. }
  594. if (componentInstall) {
  595. tempInstallDirectory += "/";
  596. // Some CPack generators would rather chose
  597. // the local installation directory suffix.
  598. // Some (e.g. RPM) use
  599. // one install directory for each component **GROUP**
  600. // instead of the default
  601. // one install directory for each component.
  602. tempInstallDirectory +=
  603. GetComponentInstallDirNameSuffix(installComponent);
  604. if (this->IsOn("CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY")) {
  605. tempInstallDirectory += "/";
  606. tempInstallDirectory += this->GetOption("CPACK_PACKAGE_FILE_NAME");
  607. }
  608. }
  609. if (!setDestDir) {
  610. tempInstallDirectory += this->GetPackagingInstallPrefix();
  611. }
  612. if (setDestDir) {
  613. // For DESTDIR based packaging, use the *project*
  614. // CMAKE_INSTALL_PREFIX underneath the tempInstallDirectory. The
  615. // value of the project's CMAKE_INSTALL_PREFIX is sent in here as
  616. // the value of the CPACK_INSTALL_PREFIX variable.
  617. //
  618. // If DESTDIR has been 'internally set ON' this means that
  619. // the underlying CPack specific generator did ask for that
  620. // In this case we may override CPACK_INSTALL_PREFIX with
  621. // CPACK_PACKAGING_INSTALL_PREFIX
  622. // I know this is tricky and awkward but it's the price for
  623. // CPACK_SET_DESTDIR backward compatibility.
  624. if (cmSystemTools::IsInternallyOn(
  625. this->GetOption("CPACK_SET_DESTDIR"))) {
  626. this->SetOption("CPACK_INSTALL_PREFIX",
  627. this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX"));
  628. }
  629. std::string dir;
  630. if (this->GetOption("CPACK_INSTALL_PREFIX")) {
  631. dir += this->GetOption("CPACK_INSTALL_PREFIX");
  632. }
  633. mf->AddDefinition("CMAKE_INSTALL_PREFIX", dir.c_str());
  634. cmCPackLogger(
  635. cmCPackLog::LOG_DEBUG,
  636. "- Using DESTDIR + CPACK_INSTALL_PREFIX... (mf->AddDefinition)"
  637. << std::endl);
  638. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  639. "- Setting CMAKE_INSTALL_PREFIX to '" << dir << "'"
  640. << std::endl);
  641. // Make sure that DESTDIR + CPACK_INSTALL_PREFIX directory
  642. // exists:
  643. //
  644. if (cmSystemTools::StringStartsWith(dir.c_str(), "/")) {
  645. dir = tempInstallDirectory + dir;
  646. } else {
  647. dir = tempInstallDirectory + "/" + dir;
  648. }
  649. /*
  650. * We must re-set DESTDIR for each component
  651. * We must not add the CPACK_INSTALL_PREFIX part because
  652. * it will be added using the override of CMAKE_INSTALL_PREFIX
  653. * The main reason for this awkward trick is that
  654. * are using DESTDIR for 2 different reasons:
  655. * - Because it was asked by the CPack Generator or the user
  656. * using CPACK_SET_DESTDIR
  657. * - Because it was already used for component install
  658. * in order to put things in subdirs...
  659. */
  660. cmSystemTools::PutEnv(std::string("DESTDIR=") +
  661. tempInstallDirectory);
  662. cmCPackLogger(cmCPackLog::LOG_DEBUG, "- Creating directory: '"
  663. << dir << "'" << std::endl);
  664. if (!cmsys::SystemTools::MakeDirectory(dir.c_str())) {
  665. cmCPackLogger(
  666. cmCPackLog::LOG_ERROR,
  667. "Problem creating temporary directory: " << dir << std::endl);
  668. return 0;
  669. }
  670. } else {
  671. mf->AddDefinition("CMAKE_INSTALL_PREFIX",
  672. tempInstallDirectory.c_str());
  673. if (!cmsys::SystemTools::MakeDirectory(
  674. tempInstallDirectory.c_str())) {
  675. cmCPackLogger(cmCPackLog::LOG_ERROR,
  676. "Problem creating temporary directory: "
  677. << tempInstallDirectory << std::endl);
  678. return 0;
  679. }
  680. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  681. "- Using non-DESTDIR install... (mf->AddDefinition)"
  682. << std::endl);
  683. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  684. "- Setting CMAKE_INSTALL_PREFIX to '"
  685. << tempInstallDirectory << "'" << std::endl);
  686. }
  687. if (!buildConfig.empty()) {
  688. mf->AddDefinition("BUILD_TYPE", buildConfig.c_str());
  689. }
  690. std::string installComponentLowerCase =
  691. cmSystemTools::LowerCase(installComponent);
  692. if (installComponentLowerCase != "all") {
  693. mf->AddDefinition("CMAKE_INSTALL_COMPONENT",
  694. installComponent.c_str());
  695. }
  696. // strip on TRUE, ON, 1, one or several file names, but not on
  697. // FALSE, OFF, 0 and an empty string
  698. if (!cmSystemTools::IsOff(this->GetOption("CPACK_STRIP_FILES"))) {
  699. mf->AddDefinition("CMAKE_INSTALL_DO_STRIP", "1");
  700. }
  701. // Remember the list of files before installation
  702. // of the current component (if we are in component install)
  703. const char* InstallPrefix = tempInstallDirectory.c_str();
  704. std::vector<std::string> filesBefore;
  705. std::string findExpr(InstallPrefix);
  706. if (componentInstall) {
  707. cmsys::Glob glB;
  708. findExpr += "/*";
  709. glB.RecurseOn();
  710. glB.SetRecurseListDirs(true);
  711. glB.FindFiles(findExpr);
  712. filesBefore = glB.GetFiles();
  713. std::sort(filesBefore.begin(), filesBefore.end());
  714. }
  715. // If CPack was asked to warn on ABSOLUTE INSTALL DESTINATION
  716. // then forward request to cmake_install.cmake script
  717. if (this->IsOn("CPACK_WARN_ON_ABSOLUTE_INSTALL_DESTINATION")) {
  718. mf->AddDefinition("CMAKE_WARN_ON_ABSOLUTE_INSTALL_DESTINATION", "1");
  719. }
  720. // If current CPack generator does support
  721. // ABSOLUTE INSTALL DESTINATION or CPack has been asked for
  722. // then ask cmake_install.cmake script to error out
  723. // as soon as it occurs (before installing file)
  724. if (!SupportsAbsoluteDestination() ||
  725. this->IsOn("CPACK_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION")) {
  726. mf->AddDefinition("CMAKE_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION",
  727. "1");
  728. }
  729. // do installation
  730. int res = mf->ReadListFile(installFile.c_str());
  731. // forward definition of CMAKE_ABSOLUTE_DESTINATION_FILES
  732. // to CPack (may be used by generators like CPack RPM or DEB)
  733. // in order to transparently handle ABSOLUTE PATH
  734. if (mf->GetDefinition("CMAKE_ABSOLUTE_DESTINATION_FILES")) {
  735. mf->AddDefinition(
  736. "CPACK_ABSOLUTE_DESTINATION_FILES",
  737. mf->GetDefinition("CMAKE_ABSOLUTE_DESTINATION_FILES"));
  738. }
  739. // Now rebuild the list of files after installation
  740. // of the current component (if we are in component install)
  741. if (componentInstall) {
  742. cmsys::Glob glA;
  743. glA.RecurseOn();
  744. glA.SetRecurseListDirs(true);
  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 = localFileName.substr(
  760. localFileName.find_first_not_of('/'), std::string::npos);
  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 (CM_NULLPTR !=
  768. mf->GetDefinition("CPACK_ABSOLUTE_DESTINATION_FILES")) {
  769. if (!absoluteDestFiles.empty()) {
  770. absoluteDestFiles += ";";
  771. }
  772. absoluteDestFiles +=
  773. mf->GetDefinition("CPACK_ABSOLUTE_DESTINATION_FILES");
  774. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  775. "Got some ABSOLUTE DESTINATION FILES: "
  776. << absoluteDestFiles << std::endl);
  777. // define component specific var
  778. if (componentInstall) {
  779. std::string absoluteDestFileComponent =
  780. std::string("CPACK_ABSOLUTE_DESTINATION_FILES") + "_" +
  781. GetComponentInstallDirNameSuffix(installComponent);
  782. if (CM_NULLPTR != this->GetOption(absoluteDestFileComponent)) {
  783. std::string absoluteDestFilesListComponent =
  784. this->GetOption(absoluteDestFileComponent);
  785. absoluteDestFilesListComponent += ";";
  786. absoluteDestFilesListComponent +=
  787. mf->GetDefinition("CPACK_ABSOLUTE_DESTINATION_FILES");
  788. this->SetOption(absoluteDestFileComponent,
  789. absoluteDestFilesListComponent.c_str());
  790. } else {
  791. this->SetOption(
  792. absoluteDestFileComponent,
  793. mf->GetDefinition("CPACK_ABSOLUTE_DESTINATION_FILES"));
  794. }
  795. }
  796. }
  797. if (cmSystemTools::GetErrorOccuredFlag() || !res) {
  798. return 0;
  799. }
  800. }
  801. }
  802. }
  803. this->SetOption("CPACK_ABSOLUTE_DESTINATION_FILES",
  804. absoluteDestFiles.c_str());
  805. return 1;
  806. }
  807. bool cmCPackGenerator::ReadListFile(const char* moduleName)
  808. {
  809. bool retval;
  810. std::string fullPath = this->MakefileMap->GetModulesFile(moduleName);
  811. retval = this->MakefileMap->ReadListFile(fullPath.c_str());
  812. // include FATAL_ERROR and ERROR in the return status
  813. retval = retval && (!cmSystemTools::GetErrorOccuredFlag());
  814. return retval;
  815. }
  816. void cmCPackGenerator::SetOptionIfNotSet(const std::string& op,
  817. const char* value)
  818. {
  819. const char* def = this->MakefileMap->GetDefinition(op);
  820. if (def && *def) {
  821. return;
  822. }
  823. this->SetOption(op, value);
  824. }
  825. void cmCPackGenerator::SetOption(const std::string& op, const char* value)
  826. {
  827. if (!value) {
  828. this->MakefileMap->RemoveDefinition(op);
  829. return;
  830. }
  831. cmCPackLogger(cmCPackLog::LOG_DEBUG, this->GetNameOfClass()
  832. << "::SetOption(" << op << ", " << value << ")"
  833. << std::endl);
  834. this->MakefileMap->AddDefinition(op, value);
  835. }
  836. int cmCPackGenerator::DoPackage()
  837. {
  838. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Create package using " << this->Name
  839. << std::endl);
  840. // Prepare CPack internal name and check
  841. // values for many CPACK_xxx vars
  842. if (!this->PrepareNames()) {
  843. return 0;
  844. }
  845. // Digest Component grouping specification
  846. if (!this->PrepareGroupingKind()) {
  847. return 0;
  848. }
  849. if (cmSystemTools::IsOn(
  850. this->GetOption("CPACK_REMOVE_TOPLEVEL_DIRECTORY"))) {
  851. const char* toplevelDirectory =
  852. this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  853. if (cmSystemTools::FileExists(toplevelDirectory)) {
  854. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Remove toplevel directory: "
  855. << toplevelDirectory << std::endl);
  856. if (!cmSystemTools::RepeatedRemoveDirectory(toplevelDirectory)) {
  857. cmCPackLogger(cmCPackLog::LOG_ERROR,
  858. "Problem removing toplevel directory: "
  859. << toplevelDirectory << std::endl);
  860. return 0;
  861. }
  862. }
  863. }
  864. cmCPackLogger(cmCPackLog::LOG_DEBUG, "About to install project "
  865. << std::endl);
  866. if (!this->InstallProject()) {
  867. return 0;
  868. }
  869. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Done install project " << std::endl);
  870. const char* tempPackageFileName =
  871. this->GetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME");
  872. const char* tempDirectory = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  873. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Find files" << std::endl);
  874. cmsys::Glob gl;
  875. std::string findExpr = tempDirectory;
  876. findExpr += "/*";
  877. gl.RecurseOn();
  878. gl.SetRecurseListDirs(true);
  879. gl.SetRecurseThroughSymlinks(false);
  880. if (!gl.FindFiles(findExpr)) {
  881. cmCPackLogger(cmCPackLog::LOG_ERROR,
  882. "Cannot find any files in the packaging tree" << std::endl);
  883. return 0;
  884. }
  885. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Create package" << std::endl);
  886. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Package files to: "
  887. << (tempPackageFileName ? tempPackageFileName : "(NULL)")
  888. << std::endl);
  889. if (cmSystemTools::FileExists(tempPackageFileName)) {
  890. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Remove old package file"
  891. << std::endl);
  892. cmSystemTools::RemoveFile(tempPackageFileName);
  893. }
  894. if (cmSystemTools::IsOn(
  895. this->GetOption("CPACK_INCLUDE_TOPLEVEL_DIRECTORY"))) {
  896. tempDirectory = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  897. }
  898. // The files to be installed
  899. files = gl.GetFiles();
  900. packageFileNames.clear();
  901. /* Put at least one file name into the list of
  902. * wanted packageFileNames. The specific generator
  903. * may update this during PackageFiles.
  904. * (either putting several names or updating the provided one)
  905. */
  906. packageFileNames.push_back(tempPackageFileName ? tempPackageFileName : "");
  907. toplevel = tempDirectory;
  908. if (!this->PackageFiles() || cmSystemTools::GetErrorOccuredFlag()) {
  909. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem compressing the directory"
  910. << std::endl);
  911. return 0;
  912. }
  913. /* Prepare checksum algorithm*/
  914. const char* algo = this->GetOption("CPACK_PACKAGE_CHECKSUM");
  915. CM_AUTO_PTR<cmCryptoHash> crypto = cmCryptoHash::New(algo ? algo : "");
  916. /*
  917. * Copy the generated packages to final destination
  918. * - there may be several of them
  919. * - the initially provided name may have changed
  920. * (because the specific generator did 'normalize' it)
  921. */
  922. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Copying final package(s) ["
  923. << packageFileNames.size() << "]:" << std::endl);
  924. std::vector<std::string>::iterator it;
  925. /* now copy package one by one */
  926. for (it = packageFileNames.begin(); it != packageFileNames.end(); ++it) {
  927. std::string tmpPF(this->GetOption("CPACK_OUTPUT_FILE_PREFIX"));
  928. std::string filename(cmSystemTools::GetFilenameName(*it));
  929. tempPackageFileName = it->c_str();
  930. tmpPF += "/" + filename;
  931. const char* packageFileName = tmpPF.c_str();
  932. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Copy final package(s): "
  933. << (tempPackageFileName ? tempPackageFileName : "(NULL)")
  934. << " to " << (packageFileName ? packageFileName : "(NULL)")
  935. << std::endl);
  936. if (!cmSystemTools::CopyFileIfDifferent(tempPackageFileName,
  937. packageFileName)) {
  938. cmCPackLogger(
  939. cmCPackLog::LOG_ERROR, "Problem copying the package: "
  940. << (tempPackageFileName ? tempPackageFileName : "(NULL)") << " to "
  941. << (packageFileName ? packageFileName : "(NULL)") << std::endl);
  942. return 0;
  943. }
  944. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "- package: "
  945. << packageFileName << " generated." << std::endl);
  946. /* Generate checksum file */
  947. if (crypto.get() != CM_NULLPTR) {
  948. std::string hashFile(this->GetOption("CPACK_OUTPUT_FILE_PREFIX"));
  949. hashFile +=
  950. "/" + filename.substr(0, filename.rfind(this->GetOutputExtension()));
  951. hashFile += "." + cmSystemTools::LowerCase(algo);
  952. cmsys::ofstream outF(hashFile.c_str());
  953. if (!outF) {
  954. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot create checksum file: "
  955. << hashFile << std::endl);
  956. return 0;
  957. }
  958. outF << crypto->HashFile(packageFileName) << " " << filename << "\n";
  959. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "- checksum file: "
  960. << hashFile << " generated." << std::endl);
  961. }
  962. }
  963. return 1;
  964. }
  965. int cmCPackGenerator::Initialize(const std::string& name, cmMakefile* mf)
  966. {
  967. this->MakefileMap = mf;
  968. this->Name = name;
  969. // set the running generator name
  970. this->SetOption("CPACK_GENERATOR", this->Name.c_str());
  971. // Load the project specific config file
  972. const char* config = this->GetOption("CPACK_PROJECT_CONFIG_FILE");
  973. if (config) {
  974. mf->ReadListFile(config);
  975. }
  976. int result = this->InitializeInternal();
  977. if (cmSystemTools::GetErrorOccuredFlag()) {
  978. return 0;
  979. }
  980. // If a generator subclass did not already set this option in its
  981. // InitializeInternal implementation, and the project did not already set
  982. // it, the default value should be:
  983. this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/");
  984. return result;
  985. }
  986. int cmCPackGenerator::InitializeInternal()
  987. {
  988. return 1;
  989. }
  990. bool cmCPackGenerator::IsSet(const std::string& name) const
  991. {
  992. return this->MakefileMap->IsSet(name);
  993. }
  994. bool cmCPackGenerator::IsOn(const std::string& name) const
  995. {
  996. return cmSystemTools::IsOn(GetOption(name));
  997. }
  998. const char* cmCPackGenerator::GetOption(const std::string& op) const
  999. {
  1000. const char* ret = this->MakefileMap->GetDefinition(op);
  1001. if (!ret) {
  1002. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  1003. "Warning, GetOption return NULL for: " << op << std::endl);
  1004. }
  1005. return ret;
  1006. }
  1007. std::vector<std::string> cmCPackGenerator::GetOptions() const
  1008. {
  1009. return this->MakefileMap->GetDefinitions();
  1010. }
  1011. int cmCPackGenerator::PackageFiles()
  1012. {
  1013. return 0;
  1014. }
  1015. const char* cmCPackGenerator::GetInstallPath()
  1016. {
  1017. if (!this->InstallPath.empty()) {
  1018. return this->InstallPath.c_str();
  1019. }
  1020. #if defined(_WIN32) && !defined(__CYGWIN__)
  1021. std::string prgfiles;
  1022. std::string sysDrive;
  1023. if (cmsys::SystemTools::GetEnv("ProgramFiles", prgfiles)) {
  1024. this->InstallPath = prgfiles;
  1025. } else if (cmsys::SystemTools::GetEnv("SystemDrive", sysDrive)) {
  1026. this->InstallPath = sysDrive;
  1027. this->InstallPath += "/Program Files";
  1028. } else {
  1029. this->InstallPath = "c:/Program Files";
  1030. }
  1031. this->InstallPath += "/";
  1032. this->InstallPath += this->GetOption("CPACK_PACKAGE_NAME");
  1033. this->InstallPath += "-";
  1034. this->InstallPath += this->GetOption("CPACK_PACKAGE_VERSION");
  1035. #elif defined(__HAIKU__)
  1036. char dir[B_PATH_NAME_LENGTH];
  1037. if (find_directory(B_SYSTEM_DIRECTORY, -1, false, dir, sizeof(dir)) ==
  1038. B_OK) {
  1039. this->InstallPath = dir;
  1040. } else {
  1041. this->InstallPath = "/boot/system";
  1042. }
  1043. #else
  1044. this->InstallPath = "/usr/local/";
  1045. #endif
  1046. return this->InstallPath.c_str();
  1047. }
  1048. const char* cmCPackGenerator::GetPackagingInstallPrefix()
  1049. {
  1050. cmCPackLogger(cmCPackLog::LOG_DEBUG, "GetPackagingInstallPrefix: '"
  1051. << this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX") << "'"
  1052. << std::endl);
  1053. return this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX");
  1054. }
  1055. std::string cmCPackGenerator::FindTemplate(const char* name)
  1056. {
  1057. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Look for template: "
  1058. << (name ? name : "(NULL)") << std::endl);
  1059. std::string ffile = this->MakefileMap->GetModulesFile(name);
  1060. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Found template: " << ffile
  1061. << std::endl);
  1062. return ffile;
  1063. }
  1064. bool cmCPackGenerator::ConfigureString(const std::string& inString,
  1065. std::string& outString)
  1066. {
  1067. this->MakefileMap->ConfigureString(inString, outString, true, false);
  1068. return true;
  1069. }
  1070. bool cmCPackGenerator::ConfigureFile(const char* inName, const char* outName,
  1071. bool copyOnly /* = false */)
  1072. {
  1073. return this->MakefileMap->ConfigureFile(inName, outName, copyOnly, true,
  1074. false) == 1;
  1075. }
  1076. int cmCPackGenerator::CleanTemporaryDirectory()
  1077. {
  1078. std::string tempInstallDirectoryWithPostfix =
  1079. this->GetOption("CPACK_TEMPORARY_INSTALL_DIRECTORY");
  1080. const char* tempInstallDirectory = tempInstallDirectoryWithPostfix.c_str();
  1081. if (cmsys::SystemTools::FileExists(tempInstallDirectory)) {
  1082. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  1083. "- Clean temporary : " << tempInstallDirectory << std::endl);
  1084. if (!cmSystemTools::RepeatedRemoveDirectory(tempInstallDirectory)) {
  1085. cmCPackLogger(cmCPackLog::LOG_ERROR,
  1086. "Problem removing temporary directory: "
  1087. << tempInstallDirectory << std::endl);
  1088. return 0;
  1089. }
  1090. }
  1091. return 1;
  1092. }
  1093. cmInstalledFile const* cmCPackGenerator::GetInstalledFile(
  1094. std::string const& name) const
  1095. {
  1096. cmake const* cm = this->MakefileMap->GetCMakeInstance();
  1097. return cm->GetInstalledFile(name);
  1098. }
  1099. int cmCPackGenerator::PrepareGroupingKind()
  1100. {
  1101. // find a component package method specified by the user
  1102. ComponentPackageMethod method = UNKNOWN_COMPONENT_PACKAGE_METHOD;
  1103. if (this->GetOption("CPACK_COMPONENTS_ALL_IN_ONE_PACKAGE")) {
  1104. method = ONE_PACKAGE;
  1105. }
  1106. if (this->GetOption("CPACK_COMPONENTS_IGNORE_GROUPS")) {
  1107. method = ONE_PACKAGE_PER_COMPONENT;
  1108. }
  1109. if (this->GetOption("CPACK_COMPONENTS_ONE_PACKAGE_PER_GROUP")) {
  1110. method = ONE_PACKAGE_PER_GROUP;
  1111. }
  1112. std::string groupingType;
  1113. // Second way to specify grouping
  1114. if (CM_NULLPTR != this->GetOption("CPACK_COMPONENTS_GROUPING")) {
  1115. groupingType = this->GetOption("CPACK_COMPONENTS_GROUPING");
  1116. }
  1117. if (!groupingType.empty()) {
  1118. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "["
  1119. << this->Name << "]"
  1120. << " requested component grouping = " << groupingType
  1121. << std::endl);
  1122. if (groupingType == "ALL_COMPONENTS_IN_ONE") {
  1123. method = ONE_PACKAGE;
  1124. } else if (groupingType == "IGNORE") {
  1125. method = ONE_PACKAGE_PER_COMPONENT;
  1126. } else if (groupingType == "ONE_PER_GROUP") {
  1127. method = ONE_PACKAGE_PER_GROUP;
  1128. } else {
  1129. cmCPackLogger(
  1130. cmCPackLog::LOG_WARNING, "["
  1131. << this->Name << "]"
  1132. << " requested component grouping type <" << groupingType
  1133. << "> UNKNOWN not in (ALL_COMPONENTS_IN_ONE,IGNORE,ONE_PER_GROUP)"
  1134. << std::endl);
  1135. }
  1136. }
  1137. // Some components were defined but NO group
  1138. // fallback to default if not group based
  1139. if (method == ONE_PACKAGE_PER_GROUP && this->ComponentGroups.empty() &&
  1140. !this->Components.empty()) {
  1141. if (componentPackageMethod == ONE_PACKAGE) {
  1142. method = ONE_PACKAGE;
  1143. } else {
  1144. method = ONE_PACKAGE_PER_COMPONENT;
  1145. }
  1146. cmCPackLogger(
  1147. cmCPackLog::LOG_WARNING, "["
  1148. << this->Name << "]"
  1149. << " One package per component group requested, "
  1150. << "but NO component groups exist: Ignoring component group."
  1151. << std::endl);
  1152. }
  1153. // if user specified packaging method, override the default packaging method
  1154. if (method != UNKNOWN_COMPONENT_PACKAGE_METHOD) {
  1155. componentPackageMethod = method;
  1156. }
  1157. const char* method_names[] = { "ALL_COMPONENTS_IN_ONE", "IGNORE_GROUPS",
  1158. "ONE_PER_GROUP" };
  1159. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "["
  1160. << this->Name << "]"
  1161. << " requested component grouping = "
  1162. << method_names[componentPackageMethod] << std::endl);
  1163. return 1;
  1164. }
  1165. std::string cmCPackGenerator::GetComponentInstallDirNameSuffix(
  1166. const std::string& componentName)
  1167. {
  1168. return componentName;
  1169. }
  1170. std::string cmCPackGenerator::GetComponentPackageFileName(
  1171. const std::string& initialPackageFileName,
  1172. const std::string& groupOrComponentName, bool isGroupName)
  1173. {
  1174. /*
  1175. * the default behavior is to use the
  1176. * component [group] name as a suffix
  1177. */
  1178. std::string suffix = "-" + groupOrComponentName;
  1179. /* check if we should use DISPLAY name */
  1180. std::string dispNameVar = "CPACK_" + Name + "_USE_DISPLAY_NAME_IN_FILENAME";
  1181. if (IsOn(dispNameVar)) {
  1182. /* the component Group case */
  1183. if (isGroupName) {
  1184. std::string groupDispVar = "CPACK_COMPONENT_GROUP_" +
  1185. cmSystemTools::UpperCase(groupOrComponentName) + "_DISPLAY_NAME";
  1186. const char* groupDispName = GetOption(groupDispVar);
  1187. if (groupDispName) {
  1188. suffix = "-" + std::string(groupDispName);
  1189. }
  1190. }
  1191. /* the [single] component case */
  1192. else {
  1193. std::string dispVar = "CPACK_COMPONENT_" +
  1194. cmSystemTools::UpperCase(groupOrComponentName) + "_DISPLAY_NAME";
  1195. const char* dispName = GetOption(dispVar);
  1196. if (dispName) {
  1197. suffix = "-" + std::string(dispName);
  1198. }
  1199. }
  1200. }
  1201. return initialPackageFileName + suffix;
  1202. }
  1203. enum cmCPackGenerator::CPackSetDestdirSupport
  1204. cmCPackGenerator::SupportsSetDestdir() const
  1205. {
  1206. return cmCPackGenerator::SETDESTDIR_SUPPORTED;
  1207. }
  1208. bool cmCPackGenerator::SupportsAbsoluteDestination() const
  1209. {
  1210. return true;
  1211. }
  1212. bool cmCPackGenerator::SupportsComponentInstallation() const
  1213. {
  1214. return false;
  1215. }
  1216. bool cmCPackGenerator::WantsComponentInstallation() const
  1217. {
  1218. return (!IsOn("CPACK_MONOLITHIC_INSTALL") && SupportsComponentInstallation()
  1219. // check that we have at least one group or component
  1220. && (!this->ComponentGroups.empty() || !this->Components.empty()));
  1221. }
  1222. cmCPackInstallationType* cmCPackGenerator::GetInstallationType(
  1223. const std::string& projectName, const std::string& name)
  1224. {
  1225. (void)projectName;
  1226. bool hasInstallationType = this->InstallationTypes.count(name) != 0;
  1227. cmCPackInstallationType* installType = &this->InstallationTypes[name];
  1228. if (!hasInstallationType) {
  1229. // Define the installation type
  1230. std::string macroPrefix =
  1231. "CPACK_INSTALL_TYPE_" + cmsys::SystemTools::UpperCase(name);
  1232. installType->Name = name;
  1233. const char* displayName = this->GetOption(macroPrefix + "_DISPLAY_NAME");
  1234. if (displayName && *displayName) {
  1235. installType->DisplayName = displayName;
  1236. } else {
  1237. installType->DisplayName = installType->Name;
  1238. }
  1239. installType->Index = static_cast<unsigned>(this->InstallationTypes.size());
  1240. }
  1241. return installType;
  1242. }
  1243. cmCPackComponent* cmCPackGenerator::GetComponent(
  1244. const std::string& projectName, const std::string& name)
  1245. {
  1246. bool hasComponent = this->Components.count(name) != 0;
  1247. cmCPackComponent* component = &this->Components[name];
  1248. if (!hasComponent) {
  1249. // Define the component
  1250. std::string macroPrefix =
  1251. "CPACK_COMPONENT_" + cmsys::SystemTools::UpperCase(name);
  1252. component->Name = name;
  1253. const char* displayName = this->GetOption(macroPrefix + "_DISPLAY_NAME");
  1254. if (displayName && *displayName) {
  1255. component->DisplayName = displayName;
  1256. } else {
  1257. component->DisplayName = component->Name;
  1258. }
  1259. component->IsHidden = this->IsOn(macroPrefix + "_HIDDEN");
  1260. component->IsRequired = this->IsOn(macroPrefix + "_REQUIRED");
  1261. component->IsDisabledByDefault = this->IsOn(macroPrefix + "_DISABLED");
  1262. component->IsDownloaded = this->IsOn(macroPrefix + "_DOWNLOADED") ||
  1263. cmSystemTools::IsOn(this->GetOption("CPACK_DOWNLOAD_ALL"));
  1264. const char* archiveFile = this->GetOption(macroPrefix + "_ARCHIVE_FILE");
  1265. if (archiveFile && *archiveFile) {
  1266. component->ArchiveFile = archiveFile;
  1267. }
  1268. const char* groupName = this->GetOption(macroPrefix + "_GROUP");
  1269. if (groupName && *groupName) {
  1270. component->Group = GetComponentGroup(projectName, groupName);
  1271. component->Group->Components.push_back(component);
  1272. } else {
  1273. component->Group = CM_NULLPTR;
  1274. }
  1275. const char* description = this->GetOption(macroPrefix + "_DESCRIPTION");
  1276. if (description && *description) {
  1277. component->Description = description;
  1278. }
  1279. // Determine the installation types.
  1280. const char* installTypes = this->GetOption(macroPrefix + "_INSTALL_TYPES");
  1281. if (installTypes && *installTypes) {
  1282. std::vector<std::string> installTypesVector;
  1283. cmSystemTools::ExpandListArgument(installTypes, installTypesVector);
  1284. std::vector<std::string>::iterator installTypesIt;
  1285. for (installTypesIt = installTypesVector.begin();
  1286. installTypesIt != installTypesVector.end(); ++installTypesIt) {
  1287. component->InstallationTypes.push_back(
  1288. this->GetInstallationType(projectName, *installTypesIt));
  1289. }
  1290. }
  1291. // Determine the component dependencies.
  1292. const char* depends = this->GetOption(macroPrefix + "_DEPENDS");
  1293. if (depends && *depends) {
  1294. std::vector<std::string> dependsVector;
  1295. cmSystemTools::ExpandListArgument(depends, dependsVector);
  1296. std::vector<std::string>::iterator dependIt;
  1297. for (dependIt = dependsVector.begin(); dependIt != dependsVector.end();
  1298. ++dependIt) {
  1299. cmCPackComponent* child = GetComponent(projectName, *dependIt);
  1300. component->Dependencies.push_back(child);
  1301. child->ReverseDependencies.push_back(component);
  1302. }
  1303. }
  1304. }
  1305. return component;
  1306. }
  1307. cmCPackComponentGroup* cmCPackGenerator::GetComponentGroup(
  1308. const std::string& projectName, const std::string& name)
  1309. {
  1310. (void)projectName;
  1311. std::string macroPrefix =
  1312. "CPACK_COMPONENT_GROUP_" + cmsys::SystemTools::UpperCase(name);
  1313. bool hasGroup = this->ComponentGroups.count(name) != 0;
  1314. cmCPackComponentGroup* group = &this->ComponentGroups[name];
  1315. if (!hasGroup) {
  1316. // Define the group
  1317. group->Name = name;
  1318. const char* displayName = this->GetOption(macroPrefix + "_DISPLAY_NAME");
  1319. if (displayName && *displayName) {
  1320. group->DisplayName = displayName;
  1321. } else {
  1322. group->DisplayName = group->Name;
  1323. }
  1324. const char* description = this->GetOption(macroPrefix + "_DESCRIPTION");
  1325. if (description && *description) {
  1326. group->Description = description;
  1327. }
  1328. group->IsBold = this->IsOn(macroPrefix + "_BOLD_TITLE");
  1329. group->IsExpandedByDefault = this->IsOn(macroPrefix + "_EXPANDED");
  1330. const char* parentGroupName =
  1331. this->GetOption(macroPrefix + "_PARENT_GROUP");
  1332. if (parentGroupName && *parentGroupName) {
  1333. group->ParentGroup = GetComponentGroup(projectName, parentGroupName);
  1334. group->ParentGroup->Subgroups.push_back(group);
  1335. } else {
  1336. group->ParentGroup = CM_NULLPTR;
  1337. }
  1338. }
  1339. return group;
  1340. }