cmCPackGenerator.cxx 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  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 = 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::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
  354. symlinkedIt;
  355. std::string curDir = cmSystemTools::GetCurrentWorkingDirectory();
  356. std::string goToDir = tempDir;
  357. goToDir += "/" + subdir;
  358. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Change dir to: " << goToDir
  359. << std::endl);
  360. cmWorkingDirectory workdir(goToDir);
  361. for (symlinkedIt = symlinkedFiles.begin();
  362. symlinkedIt != symlinkedFiles.end(); ++symlinkedIt) {
  363. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Will create a symlink: "
  364. << symlinkedIt->second << "--> "
  365. << symlinkedIt->first << std::endl);
  366. // make sure directory exists for symlink
  367. std::string destDir =
  368. cmSystemTools::GetFilenamePath(symlinkedIt->second);
  369. if (!destDir.empty() && !cmSystemTools::MakeDirectory(destDir)) {
  370. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot create dir: "
  371. << destDir << "\nTrying to create symlink: "
  372. << symlinkedIt->second << "--> "
  373. << symlinkedIt->first << std::endl);
  374. }
  375. if (!cmSystemTools::CreateSymlink(symlinkedIt->first,
  376. symlinkedIt->second)) {
  377. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot create symlink: "
  378. << symlinkedIt->second << "--> "
  379. << symlinkedIt->first << std::endl);
  380. return 0;
  381. }
  382. }
  383. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Going back to: " << curDir
  384. << std::endl);
  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(cmake::RoleScript);
  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.SetRecurseThroughSymlinks(false);
  746. glA.FindFiles(findExpr);
  747. std::vector<std::string> filesAfter = glA.GetFiles();
  748. std::sort(filesAfter.begin(), filesAfter.end());
  749. std::vector<std::string>::iterator diff;
  750. std::vector<std::string> result(filesAfter.size());
  751. diff = std::set_difference(filesAfter.begin(), filesAfter.end(),
  752. filesBefore.begin(), filesBefore.end(),
  753. result.begin());
  754. std::vector<std::string>::iterator fit;
  755. std::string localFileName;
  756. // Populate the File field of each component
  757. for (fit = result.begin(); fit != diff; ++fit) {
  758. localFileName =
  759. cmSystemTools::RelativePath(InstallPrefix, fit->c_str());
  760. localFileName =
  761. localFileName.substr(localFileName.find_first_not_of('/'));
  762. Components[installComponent].Files.push_back(localFileName);
  763. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Adding file <"
  764. << localFileName << "> to component <"
  765. << installComponent << ">" << std::endl);
  766. }
  767. }
  768. if (CM_NULLPTR !=
  769. mf->GetDefinition("CPACK_ABSOLUTE_DESTINATION_FILES")) {
  770. if (!absoluteDestFiles.empty()) {
  771. absoluteDestFiles += ";";
  772. }
  773. absoluteDestFiles +=
  774. mf->GetDefinition("CPACK_ABSOLUTE_DESTINATION_FILES");
  775. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  776. "Got some ABSOLUTE DESTINATION FILES: "
  777. << absoluteDestFiles << std::endl);
  778. // define component specific var
  779. if (componentInstall) {
  780. std::string absoluteDestFileComponent =
  781. std::string("CPACK_ABSOLUTE_DESTINATION_FILES") + "_" +
  782. GetComponentInstallDirNameSuffix(installComponent);
  783. if (CM_NULLPTR != this->GetOption(absoluteDestFileComponent)) {
  784. std::string absoluteDestFilesListComponent =
  785. this->GetOption(absoluteDestFileComponent);
  786. absoluteDestFilesListComponent += ";";
  787. absoluteDestFilesListComponent +=
  788. mf->GetDefinition("CPACK_ABSOLUTE_DESTINATION_FILES");
  789. this->SetOption(absoluteDestFileComponent,
  790. absoluteDestFilesListComponent.c_str());
  791. } else {
  792. this->SetOption(
  793. absoluteDestFileComponent,
  794. mf->GetDefinition("CPACK_ABSOLUTE_DESTINATION_FILES"));
  795. }
  796. }
  797. }
  798. if (cmSystemTools::GetErrorOccuredFlag() || !res) {
  799. return 0;
  800. }
  801. }
  802. }
  803. }
  804. this->SetOption("CPACK_ABSOLUTE_DESTINATION_FILES",
  805. absoluteDestFiles.c_str());
  806. return 1;
  807. }
  808. bool cmCPackGenerator::ReadListFile(const char* moduleName)
  809. {
  810. bool retval;
  811. std::string fullPath = this->MakefileMap->GetModulesFile(moduleName);
  812. retval = this->MakefileMap->ReadListFile(fullPath.c_str());
  813. // include FATAL_ERROR and ERROR in the return status
  814. retval = retval && (!cmSystemTools::GetErrorOccuredFlag());
  815. return retval;
  816. }
  817. void cmCPackGenerator::SetOptionIfNotSet(const std::string& op,
  818. const char* value)
  819. {
  820. const char* def = this->MakefileMap->GetDefinition(op);
  821. if (def && *def) {
  822. return;
  823. }
  824. this->SetOption(op, value);
  825. }
  826. void cmCPackGenerator::SetOption(const std::string& op, const char* value)
  827. {
  828. if (!value) {
  829. this->MakefileMap->RemoveDefinition(op);
  830. return;
  831. }
  832. cmCPackLogger(cmCPackLog::LOG_DEBUG, this->GetNameOfClass()
  833. << "::SetOption(" << op << ", " << value << ")"
  834. << std::endl);
  835. this->MakefileMap->AddDefinition(op, value);
  836. }
  837. int cmCPackGenerator::DoPackage()
  838. {
  839. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Create package using " << this->Name
  840. << std::endl);
  841. // Prepare CPack internal name and check
  842. // values for many CPACK_xxx vars
  843. if (!this->PrepareNames()) {
  844. return 0;
  845. }
  846. // Digest Component grouping specification
  847. if (!this->PrepareGroupingKind()) {
  848. return 0;
  849. }
  850. if (cmSystemTools::IsOn(
  851. this->GetOption("CPACK_REMOVE_TOPLEVEL_DIRECTORY"))) {
  852. const char* toplevelDirectory =
  853. this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  854. if (cmSystemTools::FileExists(toplevelDirectory)) {
  855. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Remove toplevel directory: "
  856. << toplevelDirectory << std::endl);
  857. if (!cmSystemTools::RepeatedRemoveDirectory(toplevelDirectory)) {
  858. cmCPackLogger(cmCPackLog::LOG_ERROR,
  859. "Problem removing toplevel directory: "
  860. << toplevelDirectory << std::endl);
  861. return 0;
  862. }
  863. }
  864. }
  865. cmCPackLogger(cmCPackLog::LOG_DEBUG, "About to install project "
  866. << std::endl);
  867. if (!this->InstallProject()) {
  868. return 0;
  869. }
  870. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Done install project " << std::endl);
  871. const char* tempPackageFileName =
  872. this->GetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME");
  873. const char* tempDirectory = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  874. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Find files" << std::endl);
  875. cmsys::Glob gl;
  876. std::string findExpr = tempDirectory;
  877. findExpr += "/*";
  878. gl.RecurseOn();
  879. gl.SetRecurseListDirs(true);
  880. gl.SetRecurseThroughSymlinks(false);
  881. if (!gl.FindFiles(findExpr)) {
  882. cmCPackLogger(cmCPackLog::LOG_ERROR,
  883. "Cannot find any files in the packaging tree" << std::endl);
  884. return 0;
  885. }
  886. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Create package" << std::endl);
  887. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Package files to: "
  888. << (tempPackageFileName ? tempPackageFileName : "(NULL)")
  889. << std::endl);
  890. if (cmSystemTools::FileExists(tempPackageFileName)) {
  891. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Remove old package file"
  892. << std::endl);
  893. cmSystemTools::RemoveFile(tempPackageFileName);
  894. }
  895. if (cmSystemTools::IsOn(
  896. this->GetOption("CPACK_INCLUDE_TOPLEVEL_DIRECTORY"))) {
  897. tempDirectory = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  898. }
  899. // The files to be installed
  900. files = gl.GetFiles();
  901. packageFileNames.clear();
  902. /* Put at least one file name into the list of
  903. * wanted packageFileNames. The specific generator
  904. * may update this during PackageFiles.
  905. * (either putting several names or updating the provided one)
  906. */
  907. packageFileNames.push_back(tempPackageFileName ? tempPackageFileName : "");
  908. toplevel = tempDirectory;
  909. if (!this->PackageFiles() || cmSystemTools::GetErrorOccuredFlag()) {
  910. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem compressing the directory"
  911. << std::endl);
  912. return 0;
  913. }
  914. /* Prepare checksum algorithm*/
  915. const char* algo = this->GetOption("CPACK_PACKAGE_CHECKSUM");
  916. CM_AUTO_PTR<cmCryptoHash> crypto = cmCryptoHash::New(algo ? algo : "");
  917. /*
  918. * Copy the generated packages to final destination
  919. * - there may be several of them
  920. * - the initially provided name may have changed
  921. * (because the specific generator did 'normalize' it)
  922. */
  923. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Copying final package(s) ["
  924. << packageFileNames.size() << "]:" << std::endl);
  925. std::vector<std::string>::iterator it;
  926. /* now copy package one by one */
  927. for (it = packageFileNames.begin(); it != packageFileNames.end(); ++it) {
  928. std::string tmpPF(this->GetOption("CPACK_OUTPUT_FILE_PREFIX"));
  929. std::string filename(cmSystemTools::GetFilenameName(*it));
  930. tempPackageFileName = it->c_str();
  931. tmpPF += "/" + filename;
  932. const char* packageFileName = tmpPF.c_str();
  933. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Copy final package(s): "
  934. << (tempPackageFileName ? tempPackageFileName : "(NULL)")
  935. << " to " << (packageFileName ? packageFileName : "(NULL)")
  936. << std::endl);
  937. if (!cmSystemTools::CopyFileIfDifferent(tempPackageFileName,
  938. packageFileName)) {
  939. cmCPackLogger(
  940. cmCPackLog::LOG_ERROR, "Problem copying the package: "
  941. << (tempPackageFileName ? tempPackageFileName : "(NULL)") << " to "
  942. << (packageFileName ? packageFileName : "(NULL)") << std::endl);
  943. return 0;
  944. }
  945. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "- package: "
  946. << packageFileName << " generated." << std::endl);
  947. /* Generate checksum file */
  948. if (crypto.get() != CM_NULLPTR) {
  949. std::string hashFile(this->GetOption("CPACK_OUTPUT_FILE_PREFIX"));
  950. hashFile +=
  951. "/" + filename.substr(0, filename.rfind(this->GetOutputExtension()));
  952. hashFile += "." + cmSystemTools::LowerCase(algo);
  953. cmsys::ofstream outF(hashFile.c_str());
  954. if (!outF) {
  955. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot create checksum file: "
  956. << hashFile << std::endl);
  957. return 0;
  958. }
  959. outF << crypto->HashFile(packageFileName) << " " << filename << "\n";
  960. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "- checksum file: "
  961. << hashFile << " generated." << std::endl);
  962. }
  963. }
  964. return 1;
  965. }
  966. int cmCPackGenerator::Initialize(const std::string& name, cmMakefile* mf)
  967. {
  968. this->MakefileMap = mf;
  969. this->Name = name;
  970. // set the running generator name
  971. this->SetOption("CPACK_GENERATOR", this->Name.c_str());
  972. // Load the project specific config file
  973. const char* config = this->GetOption("CPACK_PROJECT_CONFIG_FILE");
  974. if (config) {
  975. mf->ReadListFile(config);
  976. }
  977. int result = this->InitializeInternal();
  978. if (cmSystemTools::GetErrorOccuredFlag()) {
  979. return 0;
  980. }
  981. // If a generator subclass did not already set this option in its
  982. // InitializeInternal implementation, and the project did not already set
  983. // it, the default value should be:
  984. this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/");
  985. return result;
  986. }
  987. int cmCPackGenerator::InitializeInternal()
  988. {
  989. return 1;
  990. }
  991. bool cmCPackGenerator::IsSet(const std::string& name) const
  992. {
  993. return this->MakefileMap->IsSet(name);
  994. }
  995. bool cmCPackGenerator::IsOn(const std::string& name) const
  996. {
  997. return cmSystemTools::IsOn(GetOption(name));
  998. }
  999. bool cmCPackGenerator::IsSetToOff(const std::string& op) const
  1000. {
  1001. const char* ret = this->MakefileMap->GetDefinition(op);
  1002. if (ret && *ret) {
  1003. return cmSystemTools::IsOff(ret);
  1004. }
  1005. return false;
  1006. }
  1007. bool cmCPackGenerator::IsSetToEmpty(const std::string& op) const
  1008. {
  1009. const char* ret = this->MakefileMap->GetDefinition(op);
  1010. if (ret) {
  1011. return !*ret;
  1012. }
  1013. return false;
  1014. }
  1015. const char* cmCPackGenerator::GetOption(const std::string& op) const
  1016. {
  1017. const char* ret = this->MakefileMap->GetDefinition(op);
  1018. if (!ret) {
  1019. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  1020. "Warning, GetOption return NULL for: " << op << std::endl);
  1021. }
  1022. return ret;
  1023. }
  1024. std::vector<std::string> cmCPackGenerator::GetOptions() const
  1025. {
  1026. return this->MakefileMap->GetDefinitions();
  1027. }
  1028. int cmCPackGenerator::PackageFiles()
  1029. {
  1030. return 0;
  1031. }
  1032. const char* cmCPackGenerator::GetInstallPath()
  1033. {
  1034. if (!this->InstallPath.empty()) {
  1035. return this->InstallPath.c_str();
  1036. }
  1037. #if defined(_WIN32) && !defined(__CYGWIN__)
  1038. std::string prgfiles;
  1039. std::string sysDrive;
  1040. if (cmsys::SystemTools::GetEnv("ProgramFiles", prgfiles)) {
  1041. this->InstallPath = prgfiles;
  1042. } else if (cmsys::SystemTools::GetEnv("SystemDrive", sysDrive)) {
  1043. this->InstallPath = sysDrive;
  1044. this->InstallPath += "/Program Files";
  1045. } else {
  1046. this->InstallPath = "c:/Program Files";
  1047. }
  1048. this->InstallPath += "/";
  1049. this->InstallPath += this->GetOption("CPACK_PACKAGE_NAME");
  1050. this->InstallPath += "-";
  1051. this->InstallPath += this->GetOption("CPACK_PACKAGE_VERSION");
  1052. #elif defined(__HAIKU__)
  1053. char dir[B_PATH_NAME_LENGTH];
  1054. if (find_directory(B_SYSTEM_DIRECTORY, -1, false, dir, sizeof(dir)) ==
  1055. B_OK) {
  1056. this->InstallPath = dir;
  1057. } else {
  1058. this->InstallPath = "/boot/system";
  1059. }
  1060. #else
  1061. this->InstallPath = "/usr/local/";
  1062. #endif
  1063. return this->InstallPath.c_str();
  1064. }
  1065. const char* cmCPackGenerator::GetPackagingInstallPrefix()
  1066. {
  1067. cmCPackLogger(cmCPackLog::LOG_DEBUG, "GetPackagingInstallPrefix: '"
  1068. << this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX") << "'"
  1069. << std::endl);
  1070. return this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX");
  1071. }
  1072. std::string cmCPackGenerator::FindTemplate(const char* name)
  1073. {
  1074. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Look for template: "
  1075. << (name ? name : "(NULL)") << std::endl);
  1076. std::string ffile = this->MakefileMap->GetModulesFile(name);
  1077. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Found template: " << ffile
  1078. << std::endl);
  1079. return ffile;
  1080. }
  1081. bool cmCPackGenerator::ConfigureString(const std::string& inString,
  1082. std::string& outString)
  1083. {
  1084. this->MakefileMap->ConfigureString(inString, outString, true, false);
  1085. return true;
  1086. }
  1087. bool cmCPackGenerator::ConfigureFile(const char* inName, const char* outName,
  1088. bool copyOnly /* = false */)
  1089. {
  1090. return this->MakefileMap->ConfigureFile(inName, outName, copyOnly, true,
  1091. false) == 1;
  1092. }
  1093. int cmCPackGenerator::CleanTemporaryDirectory()
  1094. {
  1095. std::string tempInstallDirectoryWithPostfix =
  1096. this->GetOption("CPACK_TEMPORARY_INSTALL_DIRECTORY");
  1097. const char* tempInstallDirectory = tempInstallDirectoryWithPostfix.c_str();
  1098. if (cmsys::SystemTools::FileExists(tempInstallDirectory)) {
  1099. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  1100. "- Clean temporary : " << tempInstallDirectory << std::endl);
  1101. if (!cmSystemTools::RepeatedRemoveDirectory(tempInstallDirectory)) {
  1102. cmCPackLogger(cmCPackLog::LOG_ERROR,
  1103. "Problem removing temporary directory: "
  1104. << tempInstallDirectory << std::endl);
  1105. return 0;
  1106. }
  1107. }
  1108. return 1;
  1109. }
  1110. cmInstalledFile const* cmCPackGenerator::GetInstalledFile(
  1111. std::string const& name) const
  1112. {
  1113. cmake const* cm = this->MakefileMap->GetCMakeInstance();
  1114. return cm->GetInstalledFile(name);
  1115. }
  1116. int cmCPackGenerator::PrepareGroupingKind()
  1117. {
  1118. // find a component package method specified by the user
  1119. ComponentPackageMethod method = UNKNOWN_COMPONENT_PACKAGE_METHOD;
  1120. if (this->GetOption("CPACK_COMPONENTS_ALL_IN_ONE_PACKAGE")) {
  1121. method = ONE_PACKAGE;
  1122. }
  1123. if (this->GetOption("CPACK_COMPONENTS_IGNORE_GROUPS")) {
  1124. method = ONE_PACKAGE_PER_COMPONENT;
  1125. }
  1126. if (this->GetOption("CPACK_COMPONENTS_ONE_PACKAGE_PER_GROUP")) {
  1127. method = ONE_PACKAGE_PER_GROUP;
  1128. }
  1129. std::string groupingType;
  1130. // Second way to specify grouping
  1131. if (CM_NULLPTR != this->GetOption("CPACK_COMPONENTS_GROUPING")) {
  1132. groupingType = this->GetOption("CPACK_COMPONENTS_GROUPING");
  1133. }
  1134. if (!groupingType.empty()) {
  1135. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "["
  1136. << this->Name << "]"
  1137. << " requested component grouping = " << groupingType
  1138. << std::endl);
  1139. if (groupingType == "ALL_COMPONENTS_IN_ONE") {
  1140. method = ONE_PACKAGE;
  1141. } else if (groupingType == "IGNORE") {
  1142. method = ONE_PACKAGE_PER_COMPONENT;
  1143. } else if (groupingType == "ONE_PER_GROUP") {
  1144. method = ONE_PACKAGE_PER_GROUP;
  1145. } else {
  1146. cmCPackLogger(
  1147. cmCPackLog::LOG_WARNING, "["
  1148. << this->Name << "]"
  1149. << " requested component grouping type <" << groupingType
  1150. << "> UNKNOWN not in (ALL_COMPONENTS_IN_ONE,IGNORE,ONE_PER_GROUP)"
  1151. << std::endl);
  1152. }
  1153. }
  1154. // Some components were defined but NO group
  1155. // fallback to default if not group based
  1156. if (method == ONE_PACKAGE_PER_GROUP && this->ComponentGroups.empty() &&
  1157. !this->Components.empty()) {
  1158. if (componentPackageMethod == ONE_PACKAGE) {
  1159. method = ONE_PACKAGE;
  1160. } else {
  1161. method = ONE_PACKAGE_PER_COMPONENT;
  1162. }
  1163. cmCPackLogger(
  1164. cmCPackLog::LOG_WARNING, "["
  1165. << this->Name << "]"
  1166. << " One package per component group requested, "
  1167. << "but NO component groups exist: Ignoring component group."
  1168. << std::endl);
  1169. }
  1170. // if user specified packaging method, override the default packaging method
  1171. if (method != UNKNOWN_COMPONENT_PACKAGE_METHOD) {
  1172. componentPackageMethod = method;
  1173. }
  1174. const char* method_names[] = { "ALL_COMPONENTS_IN_ONE", "IGNORE_GROUPS",
  1175. "ONE_PER_GROUP" };
  1176. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "["
  1177. << this->Name << "]"
  1178. << " requested component grouping = "
  1179. << method_names[componentPackageMethod] << std::endl);
  1180. return 1;
  1181. }
  1182. std::string cmCPackGenerator::GetComponentInstallDirNameSuffix(
  1183. const std::string& componentName)
  1184. {
  1185. return componentName;
  1186. }
  1187. std::string cmCPackGenerator::GetComponentPackageFileName(
  1188. const std::string& initialPackageFileName,
  1189. const std::string& groupOrComponentName, bool isGroupName)
  1190. {
  1191. /*
  1192. * the default behavior is to use the
  1193. * component [group] name as a suffix
  1194. */
  1195. std::string suffix = "-" + groupOrComponentName;
  1196. /* check if we should use DISPLAY name */
  1197. std::string dispNameVar = "CPACK_" + Name + "_USE_DISPLAY_NAME_IN_FILENAME";
  1198. if (IsOn(dispNameVar)) {
  1199. /* the component Group case */
  1200. if (isGroupName) {
  1201. std::string groupDispVar = "CPACK_COMPONENT_GROUP_" +
  1202. cmSystemTools::UpperCase(groupOrComponentName) + "_DISPLAY_NAME";
  1203. const char* groupDispName = GetOption(groupDispVar);
  1204. if (groupDispName) {
  1205. suffix = "-" + std::string(groupDispName);
  1206. }
  1207. }
  1208. /* the [single] component case */
  1209. else {
  1210. std::string dispVar = "CPACK_COMPONENT_" +
  1211. cmSystemTools::UpperCase(groupOrComponentName) + "_DISPLAY_NAME";
  1212. const char* dispName = GetOption(dispVar);
  1213. if (dispName) {
  1214. suffix = "-" + std::string(dispName);
  1215. }
  1216. }
  1217. }
  1218. return initialPackageFileName + suffix;
  1219. }
  1220. enum cmCPackGenerator::CPackSetDestdirSupport
  1221. cmCPackGenerator::SupportsSetDestdir() const
  1222. {
  1223. return cmCPackGenerator::SETDESTDIR_SUPPORTED;
  1224. }
  1225. bool cmCPackGenerator::SupportsAbsoluteDestination() const
  1226. {
  1227. return true;
  1228. }
  1229. bool cmCPackGenerator::SupportsComponentInstallation() const
  1230. {
  1231. return false;
  1232. }
  1233. bool cmCPackGenerator::WantsComponentInstallation() const
  1234. {
  1235. return (!IsOn("CPACK_MONOLITHIC_INSTALL") && SupportsComponentInstallation()
  1236. // check that we have at least one group or component
  1237. && (!this->ComponentGroups.empty() || !this->Components.empty()));
  1238. }
  1239. cmCPackInstallationType* cmCPackGenerator::GetInstallationType(
  1240. const std::string& projectName, const std::string& name)
  1241. {
  1242. (void)projectName;
  1243. bool hasInstallationType = this->InstallationTypes.count(name) != 0;
  1244. cmCPackInstallationType* installType = &this->InstallationTypes[name];
  1245. if (!hasInstallationType) {
  1246. // Define the installation type
  1247. std::string macroPrefix =
  1248. "CPACK_INSTALL_TYPE_" + cmsys::SystemTools::UpperCase(name);
  1249. installType->Name = name;
  1250. const char* displayName = this->GetOption(macroPrefix + "_DISPLAY_NAME");
  1251. if (displayName && *displayName) {
  1252. installType->DisplayName = displayName;
  1253. } else {
  1254. installType->DisplayName = installType->Name;
  1255. }
  1256. installType->Index = static_cast<unsigned>(this->InstallationTypes.size());
  1257. }
  1258. return installType;
  1259. }
  1260. cmCPackComponent* cmCPackGenerator::GetComponent(
  1261. const std::string& projectName, const std::string& name)
  1262. {
  1263. bool hasComponent = this->Components.count(name) != 0;
  1264. cmCPackComponent* component = &this->Components[name];
  1265. if (!hasComponent) {
  1266. // Define the component
  1267. std::string macroPrefix =
  1268. "CPACK_COMPONENT_" + cmsys::SystemTools::UpperCase(name);
  1269. component->Name = name;
  1270. const char* displayName = this->GetOption(macroPrefix + "_DISPLAY_NAME");
  1271. if (displayName && *displayName) {
  1272. component->DisplayName = displayName;
  1273. } else {
  1274. component->DisplayName = component->Name;
  1275. }
  1276. component->IsHidden = this->IsOn(macroPrefix + "_HIDDEN");
  1277. component->IsRequired = this->IsOn(macroPrefix + "_REQUIRED");
  1278. component->IsDisabledByDefault = this->IsOn(macroPrefix + "_DISABLED");
  1279. component->IsDownloaded = this->IsOn(macroPrefix + "_DOWNLOADED") ||
  1280. cmSystemTools::IsOn(this->GetOption("CPACK_DOWNLOAD_ALL"));
  1281. const char* archiveFile = this->GetOption(macroPrefix + "_ARCHIVE_FILE");
  1282. if (archiveFile && *archiveFile) {
  1283. component->ArchiveFile = archiveFile;
  1284. }
  1285. const char* plist = this->GetOption(macroPrefix + "_PLIST");
  1286. if (plist && *plist) {
  1287. component->Plist = plist;
  1288. }
  1289. const char* groupName = this->GetOption(macroPrefix + "_GROUP");
  1290. if (groupName && *groupName) {
  1291. component->Group = GetComponentGroup(projectName, groupName);
  1292. component->Group->Components.push_back(component);
  1293. } else {
  1294. component->Group = CM_NULLPTR;
  1295. }
  1296. const char* description = this->GetOption(macroPrefix + "_DESCRIPTION");
  1297. if (description && *description) {
  1298. component->Description = description;
  1299. }
  1300. // Determine the installation types.
  1301. const char* installTypes = this->GetOption(macroPrefix + "_INSTALL_TYPES");
  1302. if (installTypes && *installTypes) {
  1303. std::vector<std::string> installTypesVector;
  1304. cmSystemTools::ExpandListArgument(installTypes, installTypesVector);
  1305. std::vector<std::string>::iterator installTypesIt;
  1306. for (installTypesIt = installTypesVector.begin();
  1307. installTypesIt != installTypesVector.end(); ++installTypesIt) {
  1308. component->InstallationTypes.push_back(
  1309. this->GetInstallationType(projectName, *installTypesIt));
  1310. }
  1311. }
  1312. // Determine the component dependencies.
  1313. const char* depends = this->GetOption(macroPrefix + "_DEPENDS");
  1314. if (depends && *depends) {
  1315. std::vector<std::string> dependsVector;
  1316. cmSystemTools::ExpandListArgument(depends, dependsVector);
  1317. std::vector<std::string>::iterator dependIt;
  1318. for (dependIt = dependsVector.begin(); dependIt != dependsVector.end();
  1319. ++dependIt) {
  1320. cmCPackComponent* child = GetComponent(projectName, *dependIt);
  1321. component->Dependencies.push_back(child);
  1322. child->ReverseDependencies.push_back(component);
  1323. }
  1324. }
  1325. }
  1326. return component;
  1327. }
  1328. cmCPackComponentGroup* cmCPackGenerator::GetComponentGroup(
  1329. const std::string& projectName, const std::string& name)
  1330. {
  1331. (void)projectName;
  1332. std::string macroPrefix =
  1333. "CPACK_COMPONENT_GROUP_" + cmsys::SystemTools::UpperCase(name);
  1334. bool hasGroup = this->ComponentGroups.count(name) != 0;
  1335. cmCPackComponentGroup* group = &this->ComponentGroups[name];
  1336. if (!hasGroup) {
  1337. // Define the group
  1338. group->Name = name;
  1339. const char* displayName = this->GetOption(macroPrefix + "_DISPLAY_NAME");
  1340. if (displayName && *displayName) {
  1341. group->DisplayName = displayName;
  1342. } else {
  1343. group->DisplayName = group->Name;
  1344. }
  1345. const char* description = this->GetOption(macroPrefix + "_DESCRIPTION");
  1346. if (description && *description) {
  1347. group->Description = description;
  1348. }
  1349. group->IsBold = this->IsOn(macroPrefix + "_BOLD_TITLE");
  1350. group->IsExpandedByDefault = this->IsOn(macroPrefix + "_EXPANDED");
  1351. const char* parentGroupName =
  1352. this->GetOption(macroPrefix + "_PARENT_GROUP");
  1353. if (parentGroupName && *parentGroupName) {
  1354. group->ParentGroup = GetComponentGroup(projectName, parentGroupName);
  1355. group->ParentGroup->Subgroups.push_back(group);
  1356. } else {
  1357. group->ParentGroup = CM_NULLPTR;
  1358. }
  1359. }
  1360. return group;
  1361. }