cmCPackGenerator.cxx 54 KB

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