cmCPackGenerator.cxx 57 KB

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