cmCPackGenerator.cxx 57 KB

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