cmCPackGenerator.cxx 57 KB

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