cmake.cxx 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmake.h"
  14. #include "time.h"
  15. #include "cmCacheManager.h"
  16. #include "cmMakefile.h"
  17. #include "cmLocalGenerator.h"
  18. #include "cmExternalMakefileProjectGenerator.h"
  19. #include "cmCommands.h"
  20. #include "cmCommand.h"
  21. #include "cmFileTimeComparison.h"
  22. #include "cmGeneratedFileStream.h"
  23. #include "cmSourceFile.h"
  24. #include "cmVersion.h"
  25. #include "cmTest.h"
  26. #if defined(CMAKE_BUILD_WITH_CMAKE)
  27. # include "cmDependsFortran.h" // For -E cmake_copy_f90_mod callback.
  28. # include "cmVariableWatch.h"
  29. # include <cmsys/Terminal.h>
  30. #endif
  31. #include <cmsys/Directory.hxx>
  32. #include <cmsys/Process.h>
  33. #include <cmsys/Glob.hxx>
  34. #include <cmsys/RegularExpression.hxx>
  35. // only build kdevelop generator on non-windows platforms
  36. // when not bootstrapping cmake
  37. #if !defined(_WIN32)
  38. # if defined(CMAKE_BUILD_WITH_CMAKE)
  39. # define CMAKE_USE_KDEVELOP
  40. # endif
  41. #endif
  42. #if defined(__MINGW32__) && !defined(CMAKE_BUILD_WITH_CMAKE)
  43. # define CMAKE_BOOT_MINGW
  44. #endif
  45. // include the generator
  46. #if defined(_WIN32) && !defined(__CYGWIN__)
  47. # if !defined(CMAKE_BOOT_MINGW)
  48. # include "cmGlobalVisualStudio6Generator.h"
  49. # include "cmGlobalVisualStudio7Generator.h"
  50. # include "cmGlobalVisualStudio71Generator.h"
  51. # include "cmGlobalVisualStudio8Generator.h"
  52. # include "cmGlobalVisualStudio8Win64Generator.h"
  53. # include "cmGlobalBorlandMakefileGenerator.h"
  54. # include "cmGlobalNMakeMakefileGenerator.h"
  55. # include "cmGlobalWatcomWMakeGenerator.h"
  56. # endif
  57. # include "cmGlobalMSYSMakefileGenerator.h"
  58. # include "cmGlobalMinGWMakefileGenerator.h"
  59. # include "cmWin32ProcessExecution.h"
  60. #else
  61. #endif
  62. #include "cmGlobalUnixMakefileGenerator3.h"
  63. #ifdef CMAKE_USE_KDEVELOP
  64. # include "cmGlobalKdevelopGenerator.h"
  65. #endif
  66. #include <stdlib.h> // required for atoi
  67. #if defined( __APPLE__ )
  68. # if defined(CMAKE_BUILD_WITH_CMAKE)
  69. # include "cmGlobalXCodeGenerator.h"
  70. # define CMAKE_USE_XCODE 1
  71. # endif
  72. # include <sys/types.h>
  73. # include <sys/time.h>
  74. # include <sys/resource.h>
  75. #endif
  76. #include <sys/stat.h> // struct stat
  77. #include <memory> // auto_ptr
  78. void cmNeedBackwardsCompatibility(const std::string& variable,
  79. int access_type, void*, const char*, const cmMakefile*)
  80. {
  81. #ifdef CMAKE_BUILD_WITH_CMAKE
  82. if (access_type == cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS)
  83. {
  84. std::string message = "An attempt was made to access a variable: ";
  85. message += variable;
  86. message +=
  87. " that has not been defined. Some variables were always defined "
  88. "by CMake in versions prior to 1.6. To fix this you might need to set "
  89. "the cache value of CMAKE_BACKWARDS_COMPATIBILITY to 1.4 or less. If "
  90. "you are writing a CMakeList file, (or have already set "
  91. "CMAKE_BACKWARDS_COMPATABILITY to 1.4 or less) then you probably need "
  92. "to include a CMake module to test for the feature this variable "
  93. "defines.";
  94. cmSystemTools::Error(message.c_str());
  95. }
  96. #else
  97. (void)variable;
  98. (void)access_type;
  99. #endif
  100. }
  101. cmake::cmake()
  102. {
  103. this->DebugOutput = false;
  104. this->DebugTryCompile = false;
  105. this->ClearBuildSystem = false;
  106. this->FileComparison = new cmFileTimeComparison;
  107. this->Properties.SetCMakeInstance(this);
  108. // initialize properties
  109. cmSourceFile::DefineProperties(this);
  110. cmTarget::DefineProperties(this);
  111. cmMakefile::DefineProperties(this);
  112. cmTest::DefineProperties(this);
  113. #ifdef __APPLE__
  114. struct rlimit rlp;
  115. if(!getrlimit(RLIMIT_STACK, &rlp))
  116. {
  117. if(rlp.rlim_cur != rlp.rlim_max)
  118. {
  119. rlp.rlim_cur = rlp.rlim_max;
  120. setrlimit(RLIMIT_STACK, &rlp);
  121. }
  122. }
  123. #endif
  124. // If MAKEFLAGS are given in the environment, remove the environment
  125. // variable. This will prevent try-compile from succeeding when it
  126. // should fail (if "-i" is an option). We cannot simply test
  127. // whether "-i" is given and remove it because some make programs
  128. // encode the MAKEFLAGS variable in a strange way.
  129. if(getenv("MAKEFLAGS"))
  130. {
  131. cmSystemTools::PutEnv("MAKEFLAGS=");
  132. }
  133. this->Verbose = false;
  134. this->InTryCompile = false;
  135. this->CacheManager = new cmCacheManager;
  136. this->GlobalGenerator = 0;
  137. this->ProgressCallback = 0;
  138. this->ProgressCallbackClientData = 0;
  139. this->ScriptMode = false;
  140. #ifdef CMAKE_BUILD_WITH_CMAKE
  141. this->VariableWatch = new cmVariableWatch;
  142. this->VariableWatch->AddWatch("CMAKE_WORDS_BIGENDIAN",
  143. cmNeedBackwardsCompatibility);
  144. this->VariableWatch->AddWatch("CMAKE_SIZEOF_INT",
  145. cmNeedBackwardsCompatibility);
  146. this->VariableWatch->AddWatch("CMAKE_X_LIBS",
  147. cmNeedBackwardsCompatibility);
  148. #endif
  149. this->AddDefaultGenerators();
  150. this->AddDefaultExtraGenerators();
  151. this->AddDefaultCommands();
  152. // Make sure we can capture the build tool output.
  153. cmSystemTools::EnableVSConsoleOutput();
  154. }
  155. cmake::~cmake()
  156. {
  157. delete this->CacheManager;
  158. if (this->GlobalGenerator)
  159. {
  160. delete this->GlobalGenerator;
  161. this->GlobalGenerator = 0;
  162. }
  163. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  164. j != this->Commands.end(); ++j)
  165. {
  166. delete (*j).second;
  167. }
  168. #ifdef CMAKE_BUILD_WITH_CMAKE
  169. delete this->VariableWatch;
  170. #endif
  171. delete this->FileComparison;
  172. }
  173. void cmake::CleanupCommandsAndMacros()
  174. {
  175. std::vector<cmCommand*> commands;
  176. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  177. j != this->Commands.end(); ++j)
  178. {
  179. if ( !j->second->IsA("cmMacroHelperCommand") )
  180. {
  181. commands.push_back(j->second);
  182. }
  183. else
  184. {
  185. delete j->second;
  186. }
  187. }
  188. this->Commands.erase(this->Commands.begin(), this->Commands.end());
  189. std::vector<cmCommand*>::iterator it;
  190. for ( it = commands.begin(); it != commands.end();
  191. ++ it )
  192. {
  193. this->Commands[cmSystemTools::LowerCase((*it)->GetName())] = *it;
  194. }
  195. }
  196. bool cmake::CommandExists(const char* name) const
  197. {
  198. std::string sName = cmSystemTools::LowerCase(name);
  199. return (this->Commands.find(sName) != this->Commands.end());
  200. }
  201. cmCommand *cmake::GetCommand(const char *name)
  202. {
  203. cmCommand* rm = 0;
  204. std::string sName = cmSystemTools::LowerCase(name);
  205. RegisteredCommandsMap::iterator pos = this->Commands.find(sName);
  206. if (pos != this->Commands.end())
  207. {
  208. rm = (*pos).second;
  209. }
  210. return rm;
  211. }
  212. void cmake::RenameCommand(const char*oldName, const char* newName)
  213. {
  214. // if the command already exists, free the old one
  215. std::string sOldName = cmSystemTools::LowerCase(oldName);
  216. std::string sNewName = cmSystemTools::LowerCase(newName);
  217. RegisteredCommandsMap::iterator pos = this->Commands.find(sOldName);
  218. if ( pos == this->Commands.end() )
  219. {
  220. return;
  221. }
  222. cmCommand* cmd = pos->second;
  223. pos = this->Commands.find(sNewName);
  224. if (pos != this->Commands.end())
  225. {
  226. delete pos->second;
  227. this->Commands.erase(pos);
  228. }
  229. this->Commands.insert(RegisteredCommandsMap::value_type(sNewName, cmd));
  230. pos = this->Commands.find(sOldName);
  231. this->Commands.erase(pos);
  232. }
  233. void cmake::RemoveCommand(const char* name)
  234. {
  235. std::string sName = cmSystemTools::LowerCase(name);
  236. RegisteredCommandsMap::iterator pos = this->Commands.find(sName);
  237. if ( pos != this->Commands.end() )
  238. {
  239. delete pos->second;
  240. this->Commands.erase(pos);
  241. }
  242. }
  243. void cmake::AddCommand(cmCommand* wg)
  244. {
  245. std::string name = cmSystemTools::LowerCase(wg->GetName());
  246. // if the command already exists, free the old one
  247. RegisteredCommandsMap::iterator pos = this->Commands.find(name);
  248. if (pos != this->Commands.end())
  249. {
  250. delete pos->second;
  251. this->Commands.erase(pos);
  252. }
  253. this->Commands.insert( RegisteredCommandsMap::value_type(name, wg));
  254. }
  255. // Parse the args
  256. bool cmake::SetCacheArgs(const std::vector<std::string>& args)
  257. {
  258. for(unsigned int i=1; i < args.size(); ++i)
  259. {
  260. std::string arg = args[i];
  261. if(arg.find("-D",0) == 0)
  262. {
  263. std::string entry = arg.substr(2);
  264. if(entry.size() == 0)
  265. {
  266. ++i;
  267. if(i < args.size())
  268. {
  269. entry = args[i];
  270. }
  271. else
  272. {
  273. cmSystemTools::Error("-D must be followed with VAR=VALUE.");
  274. return false;
  275. }
  276. }
  277. std::string var, value;
  278. cmCacheManager::CacheEntryType type = cmCacheManager::UNINITIALIZED;
  279. if(cmCacheManager::ParseEntry(entry.c_str(), var, value, type) ||
  280. cmCacheManager::ParseEntry(entry.c_str(), var, value))
  281. {
  282. this->CacheManager->AddCacheEntry(var.c_str(), value.c_str(),
  283. "No help, variable specified on the command line.", type);
  284. }
  285. else
  286. {
  287. std::cerr << "Parse error in command line argument: " << arg << "\n"
  288. << "Should be: VAR:type=value\n";
  289. cmSystemTools::Error("No cmake scrpt provided.");
  290. return false;
  291. }
  292. }
  293. else if(arg.find("-U",0) == 0)
  294. {
  295. std::string entryPattern = arg.substr(2);
  296. if(entryPattern.size() == 0)
  297. {
  298. ++i;
  299. if(i < args.size())
  300. {
  301. entryPattern = args[i];
  302. }
  303. else
  304. {
  305. cmSystemTools::Error("-U must be followed with VAR.");
  306. return false;
  307. }
  308. }
  309. cmsys::RegularExpression regex(
  310. cmsys::Glob::PatternToRegex(entryPattern.c_str(), true).c_str());
  311. //go through all cache entries and collect the vars which will be removed
  312. std::vector<std::string> entriesToDelete;
  313. cmCacheManager::CacheIterator it =
  314. this->CacheManager->GetCacheIterator();
  315. for ( it.Begin(); !it.IsAtEnd(); it.Next() )
  316. {
  317. cmCacheManager::CacheEntryType t = it.GetType();
  318. if(t != cmCacheManager::STATIC)
  319. {
  320. std::string entryName = it.GetName();
  321. if (regex.find(entryName.c_str()))
  322. {
  323. entriesToDelete.push_back(entryName);
  324. }
  325. }
  326. }
  327. // now remove them from the cache
  328. for(std::vector<std::string>::const_iterator currentEntry =
  329. entriesToDelete.begin();
  330. currentEntry != entriesToDelete.end();
  331. ++currentEntry)
  332. {
  333. this->CacheManager->RemoveCacheEntry(currentEntry->c_str());
  334. }
  335. }
  336. else if(arg.find("-C",0) == 0)
  337. {
  338. std::string path = arg.substr(2);
  339. if ( path.size() == 0 )
  340. {
  341. ++i;
  342. if(i < args.size())
  343. {
  344. path = args[i];
  345. }
  346. else
  347. {
  348. cmSystemTools::Error("-C must be followed by a file name.");
  349. return false;
  350. }
  351. }
  352. std::cerr << "loading initial cache file " << path.c_str() << "\n";
  353. this->ReadListFile(path.c_str());
  354. }
  355. else if(arg.find("-P",0) == 0)
  356. {
  357. i++;
  358. if(i >= args.size())
  359. {
  360. cmSystemTools::Error("-P must be followed by a file name.");
  361. return false;
  362. }
  363. std::string path = args[i];
  364. if ( path.size() == 0 )
  365. {
  366. cmSystemTools::Error("No cmake script provided.");
  367. return false;
  368. }
  369. this->ReadListFile(path.c_str());
  370. }
  371. }
  372. return true;
  373. }
  374. void cmake::ReadListFile(const char *path)
  375. {
  376. // if a generator was not yet created, temporarily create one
  377. cmGlobalGenerator *gg = this->GetGlobalGenerator();
  378. bool created = false;
  379. // if a generator was not specified use a generic one
  380. if (!gg)
  381. {
  382. gg = new cmGlobalGenerator;
  383. gg->SetCMakeInstance(this);
  384. created = true;
  385. }
  386. // read in the list file to fill the cache
  387. if(path)
  388. {
  389. std::auto_ptr<cmLocalGenerator> lg(gg->CreateLocalGenerator());
  390. lg->SetGlobalGenerator(gg);
  391. lg->GetMakefile()->SetHomeOutputDirectory
  392. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  393. lg->GetMakefile()->SetStartOutputDirectory
  394. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  395. lg->GetMakefile()->SetHomeDirectory
  396. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  397. lg->GetMakefile()->SetStartDirectory
  398. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  399. if (!lg->GetMakefile()->ReadListFile(0, path))
  400. {
  401. cmSystemTools::Error("Error processing file:", path);
  402. }
  403. }
  404. // free generic one if generated
  405. if (created)
  406. {
  407. delete gg;
  408. }
  409. }
  410. // Parse the args
  411. void cmake::SetArgs(const std::vector<std::string>& args)
  412. {
  413. bool directoriesSet = false;
  414. for(unsigned int i=1; i < args.size(); ++i)
  415. {
  416. std::string arg = args[i];
  417. if(arg.find("-H",0) == 0)
  418. {
  419. directoriesSet = true;
  420. std::string path = arg.substr(2);
  421. path = cmSystemTools::CollapseFullPath(path.c_str());
  422. cmSystemTools::ConvertToUnixSlashes(path);
  423. this->SetHomeDirectory(path.c_str());
  424. }
  425. else if(arg.find("-S",0) == 0)
  426. {
  427. // There is no local generate anymore. Ignore -S option.
  428. }
  429. else if(arg.find("-O",0) == 0)
  430. {
  431. // There is no local generate anymore. Ignore -O option.
  432. }
  433. else if(arg.find("-B",0) == 0)
  434. {
  435. directoriesSet = true;
  436. std::string path = arg.substr(2);
  437. path = cmSystemTools::CollapseFullPath(path.c_str());
  438. cmSystemTools::ConvertToUnixSlashes(path);
  439. this->SetHomeOutputDirectory(path.c_str());
  440. }
  441. else if((i < args.size()-1) && (arg.find("--check-build-system",0) == 0))
  442. {
  443. this->CheckBuildSystemArgument = args[++i];
  444. this->ClearBuildSystem = (atoi(args[++i].c_str()) > 0);
  445. }
  446. else if(arg.find("-V",0) == 0)
  447. {
  448. this->Verbose = true;
  449. }
  450. else if(arg.find("-D",0) == 0)
  451. {
  452. // skip for now
  453. }
  454. else if(arg.find("-U",0) == 0)
  455. {
  456. // skip for now
  457. }
  458. else if(arg.find("-C",0) == 0)
  459. {
  460. // skip for now
  461. }
  462. else if(arg.find("-P",0) == 0)
  463. {
  464. // skip for now
  465. i++;
  466. }
  467. else if(arg.find("--graphviz=",0) == 0)
  468. {
  469. std::string path = arg.substr(strlen("--graphviz="));
  470. path = cmSystemTools::CollapseFullPath(path.c_str());
  471. cmSystemTools::ConvertToUnixSlashes(path);
  472. this->GraphVizFile = path;
  473. if ( this->GraphVizFile.empty() )
  474. {
  475. cmSystemTools::Error("No file specified for --graphviz");
  476. }
  477. }
  478. else if(arg.find("--debug-trycompile",0) == 0)
  479. {
  480. std::cout << "debug trycompile on\n";
  481. this->DebugTryCompileOn();
  482. }
  483. else if(arg.find("--debug-output",0) == 0)
  484. {
  485. std::cout << "Running with debug output on.\n";
  486. this->DebugOutputOn();
  487. }
  488. else if(arg.find("-G",0) == 0)
  489. {
  490. std::string value = arg.substr(2);
  491. if(value.size() == 0)
  492. {
  493. ++i;
  494. if(i >= args.size())
  495. {
  496. cmSystemTools::Error("No generator specified for -G");
  497. return;
  498. }
  499. value = args[i];
  500. }
  501. cmGlobalGenerator* gen =
  502. this->CreateGlobalGenerator(value.c_str());
  503. if(!gen)
  504. {
  505. cmSystemTools::Error("Could not create named generator ",
  506. value.c_str());
  507. }
  508. else
  509. {
  510. this->SetGlobalGenerator(gen);
  511. }
  512. }
  513. // no option assume it is the path to the source
  514. else
  515. {
  516. directoriesSet = true;
  517. this->SetDirectoriesFromFile(arg.c_str());
  518. }
  519. }
  520. if(!directoriesSet)
  521. {
  522. this->SetHomeOutputDirectory
  523. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  524. this->SetStartOutputDirectory
  525. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  526. this->SetHomeDirectory
  527. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  528. this->SetStartDirectory
  529. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  530. }
  531. this->SetStartDirectory(this->GetHomeDirectory());
  532. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  533. }
  534. //----------------------------------------------------------------------------
  535. void cmake::SetDirectoriesFromFile(const char* arg)
  536. {
  537. // Check if the argument refers to a CMakeCache.txt or
  538. // CMakeLists.txt file.
  539. std::string listPath;
  540. std::string cachePath;
  541. bool argIsFile = false;
  542. if(cmSystemTools::FileIsDirectory(arg))
  543. {
  544. std::string path = cmSystemTools::CollapseFullPath(arg);
  545. cmSystemTools::ConvertToUnixSlashes(path);
  546. std::string cacheFile = path;
  547. cacheFile += "/CMakeCache.txt";
  548. std::string listFile = path;
  549. listFile += "/CMakeLists.txt";
  550. if(cmSystemTools::FileExists(cacheFile.c_str()))
  551. {
  552. cachePath = path;
  553. }
  554. if(cmSystemTools::FileExists(listFile.c_str()))
  555. {
  556. listPath = path;
  557. }
  558. }
  559. else if(cmSystemTools::FileExists(arg))
  560. {
  561. argIsFile = true;
  562. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  563. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  564. name = cmSystemTools::LowerCase(name);
  565. if(name == "cmakecache.txt")
  566. {
  567. cachePath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  568. }
  569. else if(name == "cmakelists.txt")
  570. {
  571. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  572. }
  573. }
  574. else
  575. {
  576. // Specified file or directory does not exist. Try to set things
  577. // up to produce a meaningful error message.
  578. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  579. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  580. name = cmSystemTools::LowerCase(name);
  581. if(name == "cmakecache.txt" || name == "cmakelists.txt")
  582. {
  583. argIsFile = true;
  584. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  585. }
  586. else
  587. {
  588. listPath = fullPath;
  589. }
  590. }
  591. // If there is a CMakeCache.txt file, use its settings.
  592. if(cachePath.length() > 0)
  593. {
  594. cmCacheManager* cachem = this->GetCacheManager();
  595. cmCacheManager::CacheIterator it = cachem->NewIterator();
  596. if(cachem->LoadCache(cachePath.c_str()) &&
  597. it.Find("CMAKE_HOME_DIRECTORY"))
  598. {
  599. this->SetHomeOutputDirectory(cachePath.c_str());
  600. this->SetStartOutputDirectory(cachePath.c_str());
  601. this->SetHomeDirectory(it.GetValue());
  602. this->SetStartDirectory(it.GetValue());
  603. return;
  604. }
  605. }
  606. // If there is a CMakeLists.txt file, use it as the source tree.
  607. if(listPath.length() > 0)
  608. {
  609. this->SetHomeDirectory(listPath.c_str());
  610. this->SetStartDirectory(listPath.c_str());
  611. if(argIsFile)
  612. {
  613. // Source CMakeLists.txt file given. It was probably dropped
  614. // onto the executable in a GUI. Default to an in-source build.
  615. this->SetHomeOutputDirectory(listPath.c_str());
  616. this->SetStartOutputDirectory(listPath.c_str());
  617. }
  618. else
  619. {
  620. // Source directory given on command line. Use current working
  621. // directory as build tree.
  622. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  623. this->SetHomeOutputDirectory(cwd.c_str());
  624. this->SetStartOutputDirectory(cwd.c_str());
  625. }
  626. return;
  627. }
  628. // We didn't find a CMakeLists.txt or CMakeCache.txt file from the
  629. // argument. Assume it is the path to the source tree, and use the
  630. // current working directory as the build tree.
  631. std::string full = cmSystemTools::CollapseFullPath(arg);
  632. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  633. this->SetHomeDirectory(full.c_str());
  634. this->SetStartDirectory(full.c_str());
  635. this->SetHomeOutputDirectory(cwd.c_str());
  636. this->SetStartOutputDirectory(cwd.c_str());
  637. }
  638. // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the
  639. // cache
  640. int cmake::AddCMakePaths(const char *arg0)
  641. {
  642. // Find our own executable.
  643. std::vector<cmStdString> failures;
  644. std::string cMakeSelf = arg0;
  645. cmSystemTools::ConvertToUnixSlashes(cMakeSelf);
  646. failures.push_back(cMakeSelf);
  647. cMakeSelf = cmSystemTools::FindProgram(cMakeSelf.c_str());
  648. cmSystemTools::ConvertToUnixSlashes(cMakeSelf);
  649. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  650. {
  651. #ifdef CMAKE_BUILD_DIR
  652. std::string intdir = ".";
  653. #ifdef CMAKE_INTDIR
  654. intdir = CMAKE_INTDIR;
  655. #endif
  656. cMakeSelf = CMAKE_BUILD_DIR;
  657. cMakeSelf += "/bin/";
  658. cMakeSelf += intdir;
  659. cMakeSelf += "/cmake";
  660. cMakeSelf += cmSystemTools::GetExecutableExtension();
  661. #endif
  662. }
  663. #ifdef CMAKE_PREFIX
  664. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  665. {
  666. failures.push_back(cMakeSelf);
  667. cMakeSelf = CMAKE_PREFIX "/bin/cmake";
  668. }
  669. #endif
  670. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  671. {
  672. failures.push_back(cMakeSelf);
  673. cmOStringStream msg;
  674. msg << "CMAKE can not find the command line program cmake.\n";
  675. msg << " argv[0] = \"" << arg0 << "\"\n";
  676. msg << " Attempted paths:\n";
  677. std::vector<cmStdString>::iterator i;
  678. for(i=failures.begin(); i != failures.end(); ++i)
  679. {
  680. msg << " \"" << i->c_str() << "\"\n";
  681. }
  682. cmSystemTools::Error(msg.str().c_str());
  683. return 0;
  684. }
  685. // Save the value in the cache
  686. this->CacheManager->AddCacheEntry
  687. ("CMAKE_COMMAND",cMakeSelf.c_str(), "Path to CMake executable.",
  688. cmCacheManager::INTERNAL);
  689. // Find and save the command to edit the cache
  690. std::string editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  691. "/ccmake" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  692. if( !cmSystemTools::FileExists(editCacheCommand.c_str()))
  693. {
  694. editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  695. "/CMakeSetup" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  696. }
  697. std::string ctestCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  698. "/ctest" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  699. if(cmSystemTools::FileExists(ctestCommand.c_str()))
  700. {
  701. this->CacheManager->AddCacheEntry
  702. ("CMAKE_CTEST_COMMAND", ctestCommand.c_str(),
  703. "Path to ctest program executable.", cmCacheManager::INTERNAL);
  704. }
  705. if(cmSystemTools::FileExists(editCacheCommand.c_str()))
  706. {
  707. this->CacheManager->AddCacheEntry
  708. ("CMAKE_EDIT_COMMAND", editCacheCommand.c_str(),
  709. "Path to cache edit program executable.", cmCacheManager::INTERNAL);
  710. }
  711. // do CMAKE_ROOT, look for the environment variable first
  712. std::string cMakeRoot;
  713. std::string modules;
  714. if (getenv("CMAKE_ROOT"))
  715. {
  716. cMakeRoot = getenv("CMAKE_ROOT");
  717. modules = cMakeRoot + "/Modules/CMake.cmake";
  718. }
  719. if(!cmSystemTools::FileExists(modules.c_str()))
  720. {
  721. // next try exe/..
  722. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  723. std::string::size_type slashPos = cMakeRoot.rfind("/");
  724. if(slashPos != std::string::npos)
  725. {
  726. cMakeRoot = cMakeRoot.substr(0, slashPos);
  727. }
  728. // is there no Modules direcory there?
  729. modules = cMakeRoot + "/Modules/CMake.cmake";
  730. }
  731. if (!cmSystemTools::FileExists(modules.c_str()))
  732. {
  733. // try exe/../share/cmake
  734. cMakeRoot += CMAKE_DATA_DIR;
  735. modules = cMakeRoot + "/Modules/CMake.cmake";
  736. }
  737. #ifdef CMAKE_ROOT_DIR
  738. if (!cmSystemTools::FileExists(modules.c_str()))
  739. {
  740. // try compiled in root directory
  741. cMakeRoot = CMAKE_ROOT_DIR;
  742. modules = cMakeRoot + "/Modules/CMake.cmake";
  743. }
  744. #endif
  745. #ifdef CMAKE_PREFIX
  746. if (!cmSystemTools::FileExists(modules.c_str()))
  747. {
  748. // try compiled in install prefix
  749. cMakeRoot = CMAKE_PREFIX CMAKE_DATA_DIR;
  750. modules = cMakeRoot + "/Modules/CMake.cmake";
  751. }
  752. #endif
  753. if (!cmSystemTools::FileExists(modules.c_str()))
  754. {
  755. // try
  756. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  757. cMakeRoot += CMAKE_DATA_DIR;
  758. modules = cMakeRoot + "/Modules/CMake.cmake";
  759. }
  760. if(!cmSystemTools::FileExists(modules.c_str()))
  761. {
  762. // next try exe
  763. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  764. // is there no Modules direcory there?
  765. modules = cMakeRoot + "/Modules/CMake.cmake";
  766. }
  767. if (!cmSystemTools::FileExists(modules.c_str()))
  768. {
  769. // couldn't find modules
  770. cmSystemTools::Error("Could not find CMAKE_ROOT !!!\n"
  771. "CMake has most likely not been installed correctly.\n"
  772. "Modules directory not found in\n",
  773. cMakeRoot.c_str());
  774. return 0;
  775. }
  776. this->CacheManager->AddCacheEntry
  777. ("CMAKE_ROOT", cMakeRoot.c_str(),
  778. "Path to CMake installation.", cmCacheManager::INTERNAL);
  779. #ifdef _WIN32
  780. std::string comspec = "cmw9xcom.exe";
  781. cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
  782. #endif
  783. return 1;
  784. }
  785. void CMakeCommandUsage(const char* program)
  786. {
  787. cmOStringStream errorStream;
  788. #ifdef CMAKE_BUILD_WITH_CMAKE
  789. errorStream
  790. << "cmake version " << cmVersion::GetCMakeVersion() << "\n";
  791. #else
  792. errorStream
  793. << "cmake bootstrap\n";
  794. #endif
  795. errorStream
  796. << "Usage: " << program << " -E [command] [arguments ...]\n"
  797. << "Available commands: \n"
  798. << " chdir dir cmd [args]... - run command in a given directory\n"
  799. << " copy file destination - copy file to destination (either file or "
  800. "directory)\n"
  801. << " copy_if_different in-file out-file - copy file if input has "
  802. "changed\n"
  803. << " copy_directory source destination - copy directory 'source' "
  804. "content to directory 'destination'\n"
  805. << " compare_files file1 file2 - check if file1 is same as file2\n"
  806. << " echo [string]... - displays arguments as text\n"
  807. << " echo_append [string]... - displays arguments as text but no new"
  808. " line\n"
  809. << " environment - display the current enviroment\n"
  810. << " make_directory dir - create a directory\n"
  811. << " remove_directory dir - remove a directory and its contents\n"
  812. << " remove file1 file2 ... - remove the file(s)\n"
  813. << " tar [cxt][vfz] file.tar file/dir1 file/dir2 ... - create a tar.\n"
  814. << " time command [args] ... - run command and return elapsed time\n"
  815. #if defined(_WIN32) && !defined(__CYGWIN__)
  816. << " write_regv key value - write registry value\n"
  817. << " delete_regv key - delete registry value\n"
  818. << " comspec - on windows 9x use this for RunCommand\n"
  819. #else
  820. << " create_symlink old new - create a symbolic link new -> old\n"
  821. #endif
  822. ;
  823. cmSystemTools::Error(errorStream.str().c_str());
  824. }
  825. int cmake::ExecuteCMakeCommand(std::vector<std::string>& args)
  826. {
  827. if (args.size() > 1)
  828. {
  829. // Copy file
  830. if (args[1] == "copy" && args.size() == 4)
  831. {
  832. if(!cmSystemTools::cmCopyFile(args[2].c_str(), args[3].c_str()))
  833. {
  834. std::cerr << "Error copying file \"" << args[2].c_str()
  835. << "\" to \"" << args[3].c_str() << "\".\n";
  836. return 1;
  837. }
  838. return 0;
  839. }
  840. // Copy file if different.
  841. if (args[1] == "copy_if_different" && args.size() == 4)
  842. {
  843. if(!cmSystemTools::CopyFileIfDifferent(args[2].c_str(),
  844. args[3].c_str()))
  845. {
  846. std::cerr << "Error copying file (if different) from \""
  847. << args[2].c_str() << "\" to \"" << args[3].c_str()
  848. << "\".\n";
  849. return 1;
  850. }
  851. return 0;
  852. }
  853. // Copy directory content
  854. if (args[1] == "copy_directory" && args.size() == 4)
  855. {
  856. if(!cmSystemTools::CopyADirectory(args[2].c_str(), args[3].c_str()))
  857. {
  858. std::cerr << "Error copying directory from \""
  859. << args[2].c_str() << "\" to \"" << args[3].c_str()
  860. << "\".\n";
  861. return 1;
  862. }
  863. return 0;
  864. }
  865. // Compare files
  866. if (args[1] == "compare_files" && args.size() == 4)
  867. {
  868. if(cmSystemTools::FilesDiffer(args[2].c_str(), args[3].c_str()))
  869. {
  870. std::cerr << "Files \""
  871. << args[2].c_str() << "\" to \"" << args[3].c_str()
  872. << "\" are different.\n";
  873. return 1;
  874. }
  875. return 0;
  876. }
  877. // Echo string
  878. else if (args[1] == "echo" )
  879. {
  880. unsigned int cc;
  881. const char* space = "";
  882. for ( cc = 2; cc < args.size(); cc ++ )
  883. {
  884. std::cout << space << args[cc];
  885. space = " ";
  886. }
  887. std::cout << std::endl;
  888. return 0;
  889. }
  890. // Echo string no new line
  891. else if (args[1] == "echo_append" )
  892. {
  893. unsigned int cc;
  894. const char* space = "";
  895. for ( cc = 2; cc < args.size(); cc ++ )
  896. {
  897. std::cout << space << args[cc];
  898. space = " ";
  899. }
  900. return 0;
  901. }
  902. #if defined(CMAKE_BUILD_WITH_CMAKE)
  903. // Command to create a symbolic link. Fails on platforms not
  904. // supporting them.
  905. else if (args[1] == "environment" )
  906. {
  907. std::vector<std::string> env = cmSystemTools::GetEnvironmentVariables();
  908. std::vector<std::string>::iterator it;
  909. for ( it = env.begin(); it != env.end(); ++ it )
  910. {
  911. std::cout << it->c_str() << std::endl;
  912. }
  913. return 0;
  914. }
  915. #endif
  916. else if (args[1] == "make_directory" && args.size() == 3)
  917. {
  918. if(!cmSystemTools::MakeDirectory(args[2].c_str()))
  919. {
  920. std::cerr << "Error making directory \"" << args[2].c_str()
  921. << "\".\n";
  922. return 1;
  923. }
  924. return 0;
  925. }
  926. else if (args[1] == "remove_directory" && args.size() == 3)
  927. {
  928. if(!cmSystemTools::RemoveADirectory(args[2].c_str()))
  929. {
  930. std::cerr << "Error removing directory \"" << args[2].c_str()
  931. << "\".\n";
  932. return 1;
  933. }
  934. return 0;
  935. }
  936. // Remove file
  937. else if (args[1] == "remove" && args.size() > 2)
  938. {
  939. bool force = false;
  940. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  941. {
  942. if(args[cc] == "\\-f" || args[cc] == "-f")
  943. {
  944. force = true;
  945. }
  946. else
  947. {
  948. // Complain if the file could not be removed, still exists,
  949. // and the -f option was not given.
  950. if(!cmSystemTools::RemoveFile(args[cc].c_str()) && !force &&
  951. cmSystemTools::FileExists(args[cc].c_str()))
  952. {
  953. return 1;
  954. }
  955. }
  956. }
  957. return 0;
  958. }
  959. // Clock command
  960. else if (args[1] == "time" && args.size() > 2)
  961. {
  962. std::string command = args[2];
  963. for (std::string::size_type cc = 3; cc < args.size(); cc ++)
  964. {
  965. command += " ";
  966. command += args[cc];
  967. }
  968. clock_t clock_start, clock_finish;
  969. time_t time_start, time_finish;
  970. time(&time_start);
  971. clock_start = clock();
  972. cmSystemTools::RunSingleCommand(command.c_str());
  973. clock_finish = clock();
  974. time(&time_finish);
  975. double clocks_per_sec = static_cast<double>(CLOCKS_PER_SEC);
  976. std::cout << "Elapsed time: "
  977. << static_cast<long>(time_finish - time_start) << " s. (time)"
  978. << ", "
  979. << static_cast<double>(clock_finish - clock_start) / clocks_per_sec
  980. << " s. (clock)"
  981. << "\n";
  982. return 0;
  983. }
  984. // Command to change directory and run a program.
  985. else if (args[1] == "chdir" && args.size() >= 4)
  986. {
  987. std::string directory = args[2];
  988. if(!cmSystemTools::FileExists(directory.c_str()))
  989. {
  990. cmSystemTools::Error("Directory does not exist for chdir command: ",
  991. args[2].c_str());
  992. return 0;
  993. }
  994. std::string command = "\"";
  995. command += args[3];
  996. command += "\"";
  997. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  998. {
  999. command += " \"";
  1000. command += args[cc];
  1001. command += "\"";
  1002. }
  1003. int retval = 0;
  1004. int timeout = 0;
  1005. if ( cmSystemTools::RunSingleCommand(command.c_str(), 0, &retval,
  1006. directory.c_str(), true, timeout) )
  1007. {
  1008. return retval;
  1009. }
  1010. return 1;
  1011. }
  1012. // Command to start progress for a build
  1013. else if (args[1] == "cmake_progress_start" && args.size() == 4)
  1014. {
  1015. // bascially remove the directory
  1016. std::string dirName = args[2];
  1017. dirName += "/Progress";
  1018. cmSystemTools::RemoveADirectory(dirName.c_str());
  1019. int count = atoi(args[3].c_str());
  1020. if (count)
  1021. {
  1022. cmSystemTools::MakeDirectory(dirName.c_str());
  1023. // write the count into the directory
  1024. std::string fName = dirName;
  1025. fName += "/count.txt";
  1026. FILE *progFile = fopen(fName.c_str(),"w");
  1027. if (progFile)
  1028. {
  1029. fprintf(progFile,"%i\n",count);
  1030. fclose(progFile);
  1031. }
  1032. }
  1033. return 0;
  1034. }
  1035. // Command to report progress for a build
  1036. else if (args[1] == "cmake_progress_report" && args.size() >= 3)
  1037. {
  1038. std::string dirName = args[2];
  1039. dirName += "/Progress";
  1040. std::string fName;
  1041. FILE *progFile;
  1042. // read the count
  1043. fName = dirName;
  1044. fName += "/count.txt";
  1045. progFile = fopen(fName.c_str(),"r");
  1046. int count = 0;
  1047. if (!progFile)
  1048. {
  1049. return 0;
  1050. }
  1051. else
  1052. {
  1053. fscanf(progFile,"%i",&count);
  1054. fclose(progFile);
  1055. }
  1056. unsigned int i;
  1057. for (i = 3; i < args.size(); ++i)
  1058. {
  1059. fName = dirName;
  1060. fName += "/";
  1061. fName += args[i];
  1062. progFile = fopen(fName.c_str(),"w");
  1063. if (progFile)
  1064. {
  1065. fprintf(progFile,"empty");
  1066. fclose(progFile);
  1067. }
  1068. }
  1069. int fileNum = static_cast<int>
  1070. (cmsys::Directory::GetNumberOfFilesInDirectory(dirName.c_str()));
  1071. if (count > 0)
  1072. {
  1073. // print the progress
  1074. fprintf(stdout,"[%3i%%] ",((fileNum-3)*100)/count);
  1075. }
  1076. return 0;
  1077. }
  1078. // Command to create a symbolic link. Fails on platforms not
  1079. // supporting them.
  1080. else if (args[1] == "create_symlink" && args.size() == 4)
  1081. {
  1082. const char* destinationFileName = args[3].c_str();
  1083. if ( cmSystemTools::FileExists(destinationFileName) )
  1084. {
  1085. if ( cmSystemTools::FileIsSymlink(destinationFileName) )
  1086. {
  1087. if ( !cmSystemTools::RemoveFile(destinationFileName) ||
  1088. cmSystemTools::FileExists(destinationFileName) )
  1089. {
  1090. return 0;
  1091. }
  1092. }
  1093. else
  1094. {
  1095. return 0;
  1096. }
  1097. }
  1098. return cmSystemTools::CreateSymlink(args[2].c_str(),
  1099. args[3].c_str())? 0:1;
  1100. }
  1101. // Internal CMake shared library support.
  1102. else if (args[1] == "cmake_symlink_library" && args.size() == 5)
  1103. {
  1104. int result = 0;
  1105. std::string realName = args[2];
  1106. std::string soName = args[3];
  1107. std::string name = args[4];
  1108. if(soName != realName)
  1109. {
  1110. std::string fname = cmSystemTools::GetFilenameName(realName);
  1111. if(cmSystemTools::FileExists(soName.c_str()))
  1112. {
  1113. cmSystemTools::RemoveFile(soName.c_str());
  1114. }
  1115. if(!cmSystemTools::CreateSymlink(fname.c_str(), soName.c_str()))
  1116. {
  1117. result = 1;
  1118. }
  1119. }
  1120. if(name != soName)
  1121. {
  1122. std::string fname = cmSystemTools::GetFilenameName(soName);
  1123. if(cmSystemTools::FileExists(soName.c_str()))
  1124. {
  1125. cmSystemTools::RemoveFile(name.c_str());
  1126. }
  1127. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  1128. {
  1129. result = 1;
  1130. }
  1131. }
  1132. return result;
  1133. }
  1134. // Internal CMake versioned executable support.
  1135. else if (args[1] == "cmake_symlink_executable" && args.size() == 4)
  1136. {
  1137. int result = 0;
  1138. std::string realName = args[2];
  1139. std::string name = args[3];
  1140. if(name != realName)
  1141. {
  1142. std::string fname = cmSystemTools::GetFilenameName(realName);
  1143. if(cmSystemTools::FileExists(realName.c_str()))
  1144. {
  1145. cmSystemTools::RemoveFile(name.c_str());
  1146. }
  1147. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  1148. {
  1149. result = 1;
  1150. }
  1151. }
  1152. return result;
  1153. }
  1154. // Internal CMake dependency scanning support.
  1155. else if (args[1] == "cmake_depends" && args.size() >= 6)
  1156. {
  1157. // Create a cmake object instance to process dependencies.
  1158. cmake cm;
  1159. std::string gen;
  1160. std::string homeDir;
  1161. std::string startDir;
  1162. std::string homeOutDir;
  1163. std::string startOutDir;
  1164. std::string depInfo;
  1165. if(args.size() >= 8)
  1166. {
  1167. // Full signature:
  1168. //
  1169. // -E cmake_depends <generator>
  1170. // <home-src-dir> <start-src-dir>
  1171. // <home-out-dir> <start-out-dir>
  1172. // <dep-info>
  1173. //
  1174. // All paths are provided.
  1175. gen = args[2];
  1176. homeDir = args[3];
  1177. startDir = args[4];
  1178. homeOutDir = args[5];
  1179. startOutDir = args[6];
  1180. depInfo = args[7];
  1181. }
  1182. else
  1183. {
  1184. // Support older signature for existing makefiles:
  1185. //
  1186. // -E cmake_depends <generator>
  1187. // <home-out-dir> <start-out-dir>
  1188. // <dep-info>
  1189. //
  1190. // Just pretend the source directories are the same as the
  1191. // binary directories so at least scanning will work.
  1192. gen = args[2];
  1193. homeDir = args[3];
  1194. startDir = args[4];
  1195. homeOutDir = args[3];
  1196. startOutDir = args[3];
  1197. depInfo = args[5];
  1198. }
  1199. // Create a local generator configured for the directory in
  1200. // which dependencies will be scanned.
  1201. homeDir = cmSystemTools::CollapseFullPath(homeDir.c_str());
  1202. startDir = cmSystemTools::CollapseFullPath(startDir.c_str());
  1203. homeOutDir = cmSystemTools::CollapseFullPath(homeOutDir.c_str());
  1204. startOutDir = cmSystemTools::CollapseFullPath(startOutDir.c_str());
  1205. cm.SetHomeDirectory(homeDir.c_str());
  1206. cm.SetStartDirectory(startDir.c_str());
  1207. cm.SetHomeOutputDirectory(homeOutDir.c_str());
  1208. cm.SetStartOutputDirectory(startOutDir.c_str());
  1209. if(cmGlobalGenerator* ggd = cm.CreateGlobalGenerator(gen.c_str()))
  1210. {
  1211. cm.SetGlobalGenerator(ggd);
  1212. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  1213. lgd->SetGlobalGenerator(ggd);
  1214. lgd->GetMakefile()->SetStartDirectory(startDir.c_str());
  1215. lgd->GetMakefile()->SetStartOutputDirectory(startOutDir.c_str());
  1216. lgd->GetMakefile()->MakeStartDirectoriesCurrent();
  1217. // Actually scan dependencies.
  1218. return lgd->ScanDependencies(depInfo.c_str())? 0 : 2;
  1219. }
  1220. return 1;
  1221. }
  1222. // Internal CMake link script support.
  1223. else if (args[1] == "cmake_link_script" && args.size() >= 3)
  1224. {
  1225. return cmake::ExecuteLinkScript(args);
  1226. }
  1227. // Internal CMake unimplemented feature notification.
  1228. else if (args[1] == "cmake_unimplemented_variable")
  1229. {
  1230. std::cerr << "Feature not implemented for this platform.";
  1231. if(args.size() == 3)
  1232. {
  1233. std::cerr << " Variable " << args[2] << " is not set.";
  1234. }
  1235. std::cerr << std::endl;
  1236. return 1;
  1237. }
  1238. #ifdef CMAKE_BUILD_WITH_CMAKE
  1239. // Internal CMake color makefile support.
  1240. else if (args[1] == "cmake_echo_color")
  1241. {
  1242. return cmake::ExecuteEchoColor(args);
  1243. }
  1244. #endif
  1245. // Tar files
  1246. else if (args[1] == "tar" && args.size() > 3)
  1247. {
  1248. std::string flags = args[2];
  1249. std::string outFile = args[3];
  1250. std::vector<cmStdString> files;
  1251. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  1252. {
  1253. files.push_back(args[cc]);
  1254. }
  1255. bool gzip = false;
  1256. bool verbose = false;
  1257. if ( flags.find_first_of('z') != flags.npos )
  1258. {
  1259. gzip = true;
  1260. }
  1261. if ( flags.find_first_of('v') != flags.npos )
  1262. {
  1263. verbose = true;
  1264. }
  1265. if ( flags.find_first_of('t') != flags.npos )
  1266. {
  1267. if ( !cmSystemTools::ListTar(outFile.c_str(), files, gzip, verbose) )
  1268. {
  1269. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  1270. return 1;
  1271. }
  1272. }
  1273. else if ( flags.find_first_of('c') != flags.npos )
  1274. {
  1275. if ( !cmSystemTools::CreateTar(
  1276. outFile.c_str(), files, gzip, verbose) )
  1277. {
  1278. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  1279. return 1;
  1280. }
  1281. }
  1282. else if ( flags.find_first_of('x') != flags.npos )
  1283. {
  1284. if ( !cmSystemTools::ExtractTar(
  1285. outFile.c_str(), files, gzip, verbose) )
  1286. {
  1287. cmSystemTools::Error("Problem extracting tar: ", outFile.c_str());
  1288. return 1;
  1289. }
  1290. }
  1291. return 0;
  1292. }
  1293. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1294. // Internal CMake Fortran module support.
  1295. else if (args[1] == "cmake_copy_f90_mod" && args.size() >= 4)
  1296. {
  1297. return cmDependsFortran::CopyModule(args)? 0 : 1;
  1298. }
  1299. #endif
  1300. #if defined(_WIN32) && !defined(__CYGWIN__)
  1301. // Write registry value
  1302. else if (args[1] == "write_regv" && args.size() > 3)
  1303. {
  1304. return cmSystemTools::WriteRegistryValue(args[2].c_str(),
  1305. args[3].c_str()) ? 0 : 1;
  1306. }
  1307. // Delete registry value
  1308. else if (args[1] == "delete_regv" && args.size() > 2)
  1309. {
  1310. return cmSystemTools::DeleteRegistryValue(args[2].c_str()) ? 0 : 1;
  1311. }
  1312. // Remove file
  1313. else if (args[1] == "comspec" && args.size() > 2)
  1314. {
  1315. unsigned int cc;
  1316. std::string command = args[2];
  1317. for ( cc = 3; cc < args.size(); cc ++ )
  1318. {
  1319. command += " " + args[cc];
  1320. }
  1321. return cmWin32ProcessExecution::Windows9xHack(command.c_str());
  1322. }
  1323. #endif
  1324. }
  1325. ::CMakeCommandUsage(args[0].c_str());
  1326. return 1;
  1327. }
  1328. void cmake::AddExtraGenerator(const char* name,
  1329. CreateExtraGeneratorFunctionType newFunction)
  1330. {
  1331. this->ExtraGenerators[name] = newFunction;
  1332. cmExternalMakefileProjectGenerator* extraGenerator = newFunction();
  1333. const std::vector<std::string>& supportedGlobalGenerators =
  1334. extraGenerator->GetSupportedGlobalGenerators();
  1335. for(std::vector<std::string>::const_iterator
  1336. it = supportedGlobalGenerators.begin();
  1337. it != supportedGlobalGenerators.end();
  1338. ++it )
  1339. {
  1340. std::string fullName = cmExternalMakefileProjectGenerator::
  1341. CreateFullGeneratorName(it->c_str(), name);
  1342. this->ExtraGenerators[fullName.c_str()] = newFunction;
  1343. }
  1344. delete extraGenerator;
  1345. }
  1346. void cmake::AddDefaultExtraGenerators()
  1347. {
  1348. #if defined(_WIN32) && !defined(__CYGWIN__)
  1349. # if !defined(CMAKE_BOOT_MINGW)
  1350. // e.g. codeblocks, kdevelop4 ?
  1351. # endif
  1352. #endif
  1353. // e.g. eclipse ?
  1354. #ifdef CMAKE_USE_KDEVELOP
  1355. this->AddExtraGenerator(cmGlobalKdevelopGenerator::GetActualName(), &cmGlobalKdevelopGenerator::New);
  1356. #endif
  1357. }
  1358. //----------------------------------------------------------------------------
  1359. void cmake::GetRegisteredGenerators(std::vector<std::string>& names)
  1360. {
  1361. for(RegisteredGeneratorsMap::const_iterator i = this->Generators.begin();
  1362. i != this->Generators.end(); ++i)
  1363. {
  1364. names.push_back(i->first);
  1365. }
  1366. }
  1367. cmGlobalGenerator* cmake::CreateGlobalGenerator(const char* name)
  1368. {
  1369. cmGlobalGenerator* generator = 0;
  1370. cmExternalMakefileProjectGenerator* extraGenerator = 0;
  1371. RegisteredGeneratorsMap::const_iterator genIt = this->Generators.find(name);
  1372. if(genIt == this->Generators.end())
  1373. {
  1374. RegisteredExtraGeneratorsMap::const_iterator extraGenIt =
  1375. this->ExtraGenerators.find(name);
  1376. if (extraGenIt == this->ExtraGenerators.end())
  1377. {
  1378. return 0;
  1379. }
  1380. extraGenerator = (extraGenIt->second)();
  1381. genIt=this->Generators.find(extraGenerator->GetGlobalGeneratorName(name));
  1382. if(genIt == this->Generators.end())
  1383. {
  1384. delete extraGenerator;
  1385. return 0;
  1386. }
  1387. }
  1388. generator = (genIt->second)();
  1389. generator->SetCMakeInstance(this);
  1390. generator->SetExternalMakefileProjectGenerator(extraGenerator);
  1391. return generator;
  1392. }
  1393. void cmake::SetHomeDirectory(const char* dir)
  1394. {
  1395. this->cmHomeDirectory = dir;
  1396. cmSystemTools::ConvertToUnixSlashes(this->cmHomeDirectory);
  1397. }
  1398. void cmake::SetHomeOutputDirectory(const char* lib)
  1399. {
  1400. this->HomeOutputDirectory = lib;
  1401. cmSystemTools::ConvertToUnixSlashes(this->HomeOutputDirectory);
  1402. }
  1403. void cmake::SetGlobalGenerator(cmGlobalGenerator *gg)
  1404. {
  1405. if(!gg)
  1406. {
  1407. cmSystemTools::Error("Error SetGlobalGenerator called with null");
  1408. return;
  1409. }
  1410. // delete the old generator
  1411. if (this->GlobalGenerator)
  1412. {
  1413. delete this->GlobalGenerator;
  1414. // restore the original environment variables CXX and CC
  1415. // Restor CC
  1416. std::string env = "CC=";
  1417. if(this->CCEnvironment.size())
  1418. {
  1419. env += this->CCEnvironment;
  1420. }
  1421. cmSystemTools::PutEnv(env.c_str());
  1422. env = "CXX=";
  1423. if(this->CXXEnvironment.size())
  1424. {
  1425. env += this->CXXEnvironment;
  1426. }
  1427. cmSystemTools::PutEnv(env.c_str());
  1428. }
  1429. // set the new
  1430. this->GlobalGenerator = gg;
  1431. // set the global flag for unix style paths on cmSystemTools as soon as
  1432. // the generator is set. This allows gmake to be used on windows.
  1433. cmSystemTools::SetForceUnixPaths
  1434. (this->GlobalGenerator->GetForceUnixPaths());
  1435. // Save the environment variables CXX and CC
  1436. const char* cxx = getenv("CXX");
  1437. const char* cc = getenv("CC");
  1438. if(cxx)
  1439. {
  1440. this->CXXEnvironment = cxx;
  1441. }
  1442. else
  1443. {
  1444. this->CXXEnvironment = "";
  1445. }
  1446. if(cc)
  1447. {
  1448. this->CCEnvironment = cc;
  1449. }
  1450. else
  1451. {
  1452. this->CCEnvironment = "";
  1453. }
  1454. // set the cmake instance just to be sure
  1455. gg->SetCMakeInstance(this);
  1456. }
  1457. int cmake::DoPreConfigureChecks()
  1458. {
  1459. // Make sure the Start directory contains a CMakeLists.txt file.
  1460. std::string srcList = this->GetHomeDirectory();
  1461. srcList += "/CMakeLists.txt";
  1462. if(!cmSystemTools::FileExists(srcList.c_str()))
  1463. {
  1464. cmOStringStream err;
  1465. if(cmSystemTools::FileIsDirectory(this->GetHomeDirectory()))
  1466. {
  1467. err << "The source directory \"" << this->GetHomeDirectory()
  1468. << "\" does not appear to contain CMakeLists.txt.\n";
  1469. }
  1470. else if(cmSystemTools::FileExists(this->GetHomeDirectory()))
  1471. {
  1472. err << "The source directory \"" << this->GetHomeDirectory()
  1473. << "\" is a file, not a directory.\n";
  1474. }
  1475. else
  1476. {
  1477. err << "The source directory \"" << this->GetHomeDirectory()
  1478. << "\" does not exist.\n";
  1479. }
  1480. err << "Specify --help for usage, or press the help button on the CMake "
  1481. "GUI.";
  1482. cmSystemTools::Error(err.str().c_str());
  1483. return -2;
  1484. }
  1485. // do a sanity check on some values
  1486. if(this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY"))
  1487. {
  1488. std::string cacheStart =
  1489. this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY");
  1490. cacheStart += "/CMakeLists.txt";
  1491. std::string currentStart = this->GetHomeDirectory();
  1492. currentStart += "/CMakeLists.txt";
  1493. if(!cmSystemTools::SameFile(cacheStart.c_str(), currentStart.c_str()))
  1494. {
  1495. std::string message = "The source \"";
  1496. message += currentStart;
  1497. message += "\" does not match the source \"";
  1498. message += cacheStart;
  1499. message += "\" used to generate cache. ";
  1500. message += "Re-run cmake with a different source directory.";
  1501. cmSystemTools::Error(message.c_str());
  1502. return -2;
  1503. }
  1504. }
  1505. else
  1506. {
  1507. return 0;
  1508. }
  1509. return 1;
  1510. }
  1511. int cmake::Configure()
  1512. {
  1513. // Construct right now our path conversion table before it's too late:
  1514. this->UpdateConversionPathTable();
  1515. this->CleanupCommandsAndMacros();
  1516. int res = 0;
  1517. if ( !this->ScriptMode )
  1518. {
  1519. res = this->DoPreConfigureChecks();
  1520. }
  1521. if ( res < 0 )
  1522. {
  1523. return -2;
  1524. }
  1525. if ( !res )
  1526. {
  1527. this->CacheManager->AddCacheEntry
  1528. ("CMAKE_HOME_DIRECTORY",
  1529. this->GetHomeDirectory(),
  1530. "Start directory with the top level CMakeLists.txt file for this "
  1531. "project",
  1532. cmCacheManager::INTERNAL);
  1533. }
  1534. // set the default BACKWARDS compatibility to the current version
  1535. if(!this->CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1536. {
  1537. char ver[256];
  1538. sprintf(ver,"%i.%i",cmVersion::GetMajorVersion(),
  1539. cmVersion::GetMinorVersion());
  1540. this->CacheManager->AddCacheEntry
  1541. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1542. "For backwards compatibility, what version of CMake commands and "
  1543. "syntax should this version of CMake allow.",
  1544. cmCacheManager::STRING);
  1545. }
  1546. // no generator specified on the command line
  1547. if(!this->GlobalGenerator)
  1548. {
  1549. const char* genName =
  1550. this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1551. const char* extraGenName =
  1552. this->CacheManager->GetCacheValue("CMAKE_EXTRA_GENERATOR");
  1553. if(genName)
  1554. {
  1555. std::string fullName = cmExternalMakefileProjectGenerator::
  1556. CreateFullGeneratorName(genName, extraGenName);
  1557. this->GlobalGenerator = this->CreateGlobalGenerator(fullName.c_str());
  1558. }
  1559. if(this->GlobalGenerator)
  1560. {
  1561. // set the global flag for unix style paths on cmSystemTools as
  1562. // soon as the generator is set. This allows gmake to be used
  1563. // on windows.
  1564. cmSystemTools::SetForceUnixPaths
  1565. (this->GlobalGenerator->GetForceUnixPaths());
  1566. }
  1567. else
  1568. {
  1569. #if defined(__BORLANDC__) && defined(_WIN32)
  1570. this->SetGlobalGenerator(new cmGlobalBorlandMakefileGenerator);
  1571. #elif defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
  1572. std::string installedCompiler;
  1573. std::string mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1574. "\\VisualStudio\\8.0\\Setup;Dbghelp_path]";
  1575. cmSystemTools::ExpandRegistryValues(mp);
  1576. if (!(mp == "/registry"))
  1577. {
  1578. installedCompiler = "Visual Studio 8 2005";
  1579. }
  1580. else
  1581. {
  1582. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1583. "\\VisualStudio\\7.1;InstallDir]";
  1584. cmSystemTools::ExpandRegistryValues(mp);
  1585. if (!(mp == "/registry"))
  1586. {
  1587. installedCompiler = "Visual Studio 7 .NET 2003";
  1588. }
  1589. else
  1590. {
  1591. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1592. "\\VisualStudio\\7.0;InstallDir]";
  1593. cmSystemTools::ExpandRegistryValues(mp);
  1594. if (!(mp == "/registry"))
  1595. {
  1596. installedCompiler = "Visual Studio 7";
  1597. }
  1598. else
  1599. {
  1600. installedCompiler = "Visual Studio 6";
  1601. }
  1602. }
  1603. }
  1604. cmGlobalGenerator* gen
  1605. = this->CreateGlobalGenerator(installedCompiler.c_str());
  1606. if(!gen)
  1607. {
  1608. gen = new cmGlobalNMakeMakefileGenerator;
  1609. }
  1610. this->SetGlobalGenerator(gen);
  1611. #else
  1612. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator3);
  1613. #endif
  1614. }
  1615. if(!this->GlobalGenerator)
  1616. {
  1617. cmSystemTools::Error("Could not create generator");
  1618. return -1;
  1619. }
  1620. }
  1621. const char* genName = this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1622. if(genName)
  1623. {
  1624. if(strcmp(this->GlobalGenerator->GetName(), genName) != 0)
  1625. {
  1626. std::string message = "Error: generator : ";
  1627. message += this->GlobalGenerator->GetName();
  1628. message += "\nDoes not match the generator used previously: ";
  1629. message += genName;
  1630. message +=
  1631. "\nEither remove the CMakeCache.txt file or choose a different"
  1632. " binary directory.";
  1633. cmSystemTools::Error(message.c_str());
  1634. return -2;
  1635. }
  1636. }
  1637. if(!this->CacheManager->GetCacheValue("CMAKE_GENERATOR"))
  1638. {
  1639. this->CacheManager->AddCacheEntry("CMAKE_GENERATOR",
  1640. this->GlobalGenerator->GetName(),
  1641. "Name of generator.",
  1642. cmCacheManager::INTERNAL);
  1643. this->CacheManager->AddCacheEntry("CMAKE_EXTRA_GENERATOR",
  1644. this->GlobalGenerator->GetExtraGeneratorName(),
  1645. "Name of external makefile project generator.",
  1646. cmCacheManager::INTERNAL);
  1647. }
  1648. // reset any system configuration information, except for when we are
  1649. // InTryCompile. With TryCompile the system info is taken from the parent's
  1650. // info to save time
  1651. if (!this->InTryCompile)
  1652. {
  1653. this->GlobalGenerator->ClearEnabledLanguages();
  1654. }
  1655. this->CleanupWrittenFiles();
  1656. // Truncate log files
  1657. if (!this->InTryCompile)
  1658. {
  1659. this->TruncateOutputLog("CMakeOutput.log");
  1660. this->TruncateOutputLog("CMakeError.log");
  1661. }
  1662. // actually do the configure
  1663. this->GlobalGenerator->Configure();
  1664. // Before saving the cache
  1665. // if the project did not define one of the entries below, add them now
  1666. // so users can edit the values in the cache:
  1667. // LIBRARY_OUTPUT_PATH
  1668. // EXECUTABLE_OUTPUT_PATH
  1669. if(!this->CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  1670. {
  1671. this->CacheManager->AddCacheEntry
  1672. ("LIBRARY_OUTPUT_PATH", "",
  1673. "Single output directory for building all libraries.",
  1674. cmCacheManager::PATH);
  1675. }
  1676. if(!this->CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  1677. {
  1678. this->CacheManager->AddCacheEntry
  1679. ("EXECUTABLE_OUTPUT_PATH", "",
  1680. "Single output directory for building all executables.",
  1681. cmCacheManager::PATH);
  1682. }
  1683. if(!this->CacheManager->GetCacheValue("CMAKE_USE_RELATIVE_PATHS"))
  1684. {
  1685. this->CacheManager->AddCacheEntry
  1686. ("CMAKE_USE_RELATIVE_PATHS", false,
  1687. "If true, cmake will use relative paths in makefiles and projects.");
  1688. cmCacheManager::CacheIterator it =
  1689. this->CacheManager->GetCacheIterator("CMAKE_USE_RELATIVE_PATHS");
  1690. if ( !it.PropertyExists("ADVANCED") )
  1691. {
  1692. it.SetProperty("ADVANCED", "1");
  1693. }
  1694. }
  1695. if(cmSystemTools::GetFatalErrorOccured() &&
  1696. (!this->CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM") ||
  1697. cmSystemTools::IsOff(this->CacheManager->
  1698. GetCacheValue("CMAKE_MAKE_PROGRAM"))))
  1699. {
  1700. // We must have a bad generator selection. Wipe the cache entry so the
  1701. // user can select another.
  1702. this->CacheManager->RemoveCacheEntry("CMAKE_GENERATOR");
  1703. this->CacheManager->RemoveCacheEntry("CMAKE_EXTRA_GENERATOR");
  1704. }
  1705. // only save the cache if there were no fatal errors
  1706. if ( !this->ScriptMode )
  1707. {
  1708. this->CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1709. }
  1710. if ( !this->GraphVizFile.empty() )
  1711. {
  1712. std::cout << "Generate graphviz: " << this->GraphVizFile << std::endl;
  1713. this->GenerateGraphViz(this->GraphVizFile.c_str());
  1714. }
  1715. if(cmSystemTools::GetErrorOccuredFlag())
  1716. {
  1717. return -1;
  1718. }
  1719. return 0;
  1720. }
  1721. bool cmake::CacheVersionMatches()
  1722. {
  1723. const char* majv =
  1724. this->CacheManager->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION");
  1725. const char* minv =
  1726. this->CacheManager->GetCacheValue("CMAKE_CACHE_MINOR_VERSION");
  1727. const char* relv =
  1728. this->CacheManager->GetCacheValue("CMAKE_CACHE_RELEASE_VERSION");
  1729. bool cacheSameCMake = false;
  1730. if(majv &&
  1731. atoi(majv) == static_cast<int>(cmVersion::GetMajorVersion())
  1732. && minv &&
  1733. atoi(minv) == static_cast<int>(cmVersion::GetMinorVersion())
  1734. && relv && (strcmp(relv, cmVersion::GetReleaseVersion().c_str()) == 0))
  1735. {
  1736. cacheSameCMake = true;
  1737. }
  1738. return cacheSameCMake;
  1739. }
  1740. void cmake::PreLoadCMakeFiles()
  1741. {
  1742. std::string pre_load = this->GetHomeDirectory();
  1743. if ( pre_load.size() > 0 )
  1744. {
  1745. pre_load += "/PreLoad.cmake";
  1746. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1747. {
  1748. this->ReadListFile(pre_load.c_str());
  1749. }
  1750. }
  1751. pre_load = this->GetHomeOutputDirectory();
  1752. if ( pre_load.size() > 0 )
  1753. {
  1754. pre_load += "/PreLoad.cmake";
  1755. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1756. {
  1757. this->ReadListFile(pre_load.c_str());
  1758. }
  1759. }
  1760. }
  1761. // handle a command line invocation
  1762. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  1763. {
  1764. // Process the arguments
  1765. this->SetArgs(args);
  1766. if(cmSystemTools::GetErrorOccuredFlag())
  1767. {
  1768. return -1;
  1769. }
  1770. // set the cmake command
  1771. this->CMakeCommand = args[0];
  1772. if ( !this->ScriptMode )
  1773. {
  1774. // load the cache
  1775. if(this->LoadCache() < 0)
  1776. {
  1777. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  1778. return -1;
  1779. }
  1780. }
  1781. else
  1782. {
  1783. this->AddCMakePaths(this->CMakeCommand.c_str());
  1784. }
  1785. // Add any cache args
  1786. if ( !this->SetCacheArgs(args) )
  1787. {
  1788. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  1789. return -1;
  1790. }
  1791. // In script mode we terminate after running the script.
  1792. if(this->ScriptMode)
  1793. {
  1794. if(cmSystemTools::GetErrorOccuredFlag())
  1795. {
  1796. return -1;
  1797. }
  1798. else
  1799. {
  1800. return 0;
  1801. }
  1802. }
  1803. this->PreLoadCMakeFiles();
  1804. std::string systemFile = this->GetHomeOutputDirectory();
  1805. systemFile += "/CMakeSystem.cmake";
  1806. if ( noconfigure )
  1807. {
  1808. return 0;
  1809. }
  1810. // now run the global generate
  1811. // Check the state of the build system to see if we need to regenerate.
  1812. if(!this->CheckBuildSystem())
  1813. {
  1814. return 0;
  1815. }
  1816. // If we are doing global generate, we better set start and start
  1817. // output directory to the root of the project.
  1818. std::string oldstartdir = this->GetStartDirectory();
  1819. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  1820. this->SetStartDirectory(this->GetHomeDirectory());
  1821. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  1822. int ret = this->Configure();
  1823. if (ret || this->ScriptMode)
  1824. {
  1825. return ret;
  1826. }
  1827. ret = this->Generate();
  1828. std::string message = "Build files have been written to: ";
  1829. message += this->GetHomeOutputDirectory();
  1830. this->UpdateProgress(message.c_str(), -1);
  1831. if(ret)
  1832. {
  1833. return ret;
  1834. }
  1835. this->SetStartDirectory(oldstartdir.c_str());
  1836. this->SetStartOutputDirectory(oldstartoutputdir.c_str());
  1837. return ret;
  1838. }
  1839. int cmake::Generate()
  1840. {
  1841. if(!this->GlobalGenerator)
  1842. {
  1843. return -1;
  1844. }
  1845. this->GlobalGenerator->Generate();
  1846. if(cmSystemTools::GetErrorOccuredFlag())
  1847. {
  1848. return -1;
  1849. }
  1850. return 0;
  1851. }
  1852. void cmake::AddCacheEntry(const char* key, const char* value,
  1853. const char* helpString,
  1854. int type)
  1855. {
  1856. this->CacheManager->AddCacheEntry(key, value,
  1857. helpString,
  1858. cmCacheManager::CacheEntryType(type));
  1859. }
  1860. const char* cmake::GetCacheDefinition(const char* name) const
  1861. {
  1862. return this->CacheManager->GetCacheValue(name);
  1863. }
  1864. int cmake::DumpDocumentationToFile(std::ostream& f)
  1865. {
  1866. #ifdef CMAKE_BUILD_WITH_CMAKE
  1867. // Loop over all registered commands and print out documentation
  1868. const char *name;
  1869. const char *terse;
  1870. const char *full;
  1871. char tmp[1024];
  1872. sprintf(tmp,"Version %d.%d (%s)", cmVersion::GetMajorVersion(),
  1873. cmVersion::GetMinorVersion(),
  1874. cmVersion::GetReleaseVersion().c_str());
  1875. f << "<html>\n";
  1876. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  1877. f << "<ul>\n";
  1878. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  1879. j != this->Commands.end(); ++j)
  1880. {
  1881. name = (*j).second->GetName();
  1882. terse = (*j).second->GetTerseDocumentation();
  1883. full = (*j).second->GetFullDocumentation();
  1884. f << "<li><b>" << name << "</b> - " << terse << std::endl
  1885. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  1886. }
  1887. f << "</ul></html>\n";
  1888. #else
  1889. (void)f;
  1890. #endif
  1891. return 1;
  1892. }
  1893. void cmake::AddDefaultCommands()
  1894. {
  1895. std::list<cmCommand*> commands;
  1896. GetBootstrapCommands(commands);
  1897. GetPredefinedCommands(commands);
  1898. for(std::list<cmCommand*>::iterator i = commands.begin();
  1899. i != commands.end(); ++i)
  1900. {
  1901. this->AddCommand(*i);
  1902. }
  1903. }
  1904. void cmake::AddDefaultGenerators()
  1905. {
  1906. #if defined(_WIN32) && !defined(__CYGWIN__)
  1907. # if !defined(CMAKE_BOOT_MINGW)
  1908. this->Generators[cmGlobalVisualStudio6Generator::GetActualName()] =
  1909. &cmGlobalVisualStudio6Generator::New;
  1910. this->Generators[cmGlobalVisualStudio7Generator::GetActualName()] =
  1911. &cmGlobalVisualStudio7Generator::New;
  1912. this->Generators[cmGlobalVisualStudio71Generator::GetActualName()] =
  1913. &cmGlobalVisualStudio71Generator::New;
  1914. this->Generators[cmGlobalVisualStudio8Generator::GetActualName()] =
  1915. &cmGlobalVisualStudio8Generator::New;
  1916. this->Generators[cmGlobalVisualStudio8Win64Generator::GetActualName()] =
  1917. &cmGlobalVisualStudio8Win64Generator::New;
  1918. this->Generators[cmGlobalBorlandMakefileGenerator::GetActualName()] =
  1919. &cmGlobalBorlandMakefileGenerator::New;
  1920. this->Generators[cmGlobalNMakeMakefileGenerator::GetActualName()] =
  1921. &cmGlobalNMakeMakefileGenerator::New;
  1922. this->Generators[cmGlobalWatcomWMakeGenerator::GetActualName()] =
  1923. &cmGlobalWatcomWMakeGenerator::New;
  1924. # endif
  1925. this->Generators[cmGlobalMSYSMakefileGenerator::GetActualName()] =
  1926. &cmGlobalMSYSMakefileGenerator::New;
  1927. this->Generators[cmGlobalMinGWMakefileGenerator::GetActualName()] =
  1928. &cmGlobalMinGWMakefileGenerator::New;
  1929. #endif
  1930. this->Generators[cmGlobalUnixMakefileGenerator3::GetActualName()] =
  1931. &cmGlobalUnixMakefileGenerator3::New;
  1932. #ifdef CMAKE_USE_XCODE
  1933. this->Generators[cmGlobalXCodeGenerator::GetActualName()] =
  1934. &cmGlobalXCodeGenerator::New;
  1935. #endif
  1936. }
  1937. int cmake::LoadCache()
  1938. {
  1939. // could we not read the cache
  1940. if (!this->CacheManager->LoadCache(this->GetHomeOutputDirectory()))
  1941. {
  1942. // if it does exist, but isn;t readable then warn the user
  1943. std::string cacheFile = this->GetHomeOutputDirectory();
  1944. cacheFile += "/CMakeCache.txt";
  1945. if(cmSystemTools::FileExists(cacheFile.c_str()))
  1946. {
  1947. cmSystemTools::Error(
  1948. "There is a CMakeCache.txt file for the current binary tree but "
  1949. "cmake does not have permission to read it. Please check the "
  1950. "permissions of the directory you are trying to run CMake on.");
  1951. return -1;
  1952. }
  1953. }
  1954. if (this->CMakeCommand.size() < 2)
  1955. {
  1956. cmSystemTools::Error(
  1957. "cmake command was not specified prior to loading the cache in "
  1958. "cmake.cxx");
  1959. return -1;
  1960. }
  1961. // setup CMAKE_ROOT and CMAKE_COMMAND
  1962. if(!this->AddCMakePaths(this->CMakeCommand.c_str()))
  1963. {
  1964. return -3;
  1965. }
  1966. // set the default BACKWARDS compatibility to the current version
  1967. if(!this->CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1968. {
  1969. char ver[256];
  1970. sprintf(ver,"%i.%i",cmVersion::GetMajorVersion(),
  1971. cmVersion::GetMinorVersion());
  1972. this->CacheManager->AddCacheEntry
  1973. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1974. "For backwards compatibility, what version of CMake commands and "
  1975. "syntax should this version of CMake allow.",
  1976. cmCacheManager::STRING);
  1977. }
  1978. return 0;
  1979. }
  1980. void cmake::SetProgressCallback(ProgressCallbackType f, void *cd)
  1981. {
  1982. this->ProgressCallback = f;
  1983. this->ProgressCallbackClientData = cd;
  1984. }
  1985. void cmake::UpdateProgress(const char *msg, float prog)
  1986. {
  1987. if(this->ProgressCallback && !this->InTryCompile)
  1988. {
  1989. (*this->ProgressCallback)(msg, prog, this->ProgressCallbackClientData);
  1990. return;
  1991. }
  1992. }
  1993. void cmake::GetCommandDocumentation(std::vector<cmDocumentationEntry>& v,
  1994. bool withCurrentCommands,
  1995. bool withCompatCommands) const
  1996. {
  1997. for(RegisteredCommandsMap::const_iterator j = this->Commands.begin();
  1998. j != this->Commands.end(); ++j)
  1999. {
  2000. if ((( withCompatCommands == false) && ( (*j).second->IsDiscouraged()))
  2001. || ((withCurrentCommands == false) && (!(*j).second->IsDiscouraged())))
  2002. {
  2003. continue;
  2004. }
  2005. cmDocumentationEntry e =
  2006. {
  2007. (*j).second->GetName(),
  2008. (*j).second->GetTerseDocumentation(),
  2009. (*j).second->GetFullDocumentation()
  2010. };
  2011. v.push_back(e);
  2012. }
  2013. cmDocumentationEntry empty = {0,0,0};
  2014. v.push_back(empty);
  2015. }
  2016. void cmake::GetPropertiesDocumentation(std::vector<cmDocumentationEntry>& v)
  2017. {
  2018. // get the properties for cmake
  2019. // get them for any generators
  2020. // get them for Directories
  2021. this->DirectoryProperties.GetPropertiesDocumentation(v);
  2022. // get them for targets
  2023. this->TargetProperties.GetPropertiesDocumentation(v);
  2024. // get them for source files
  2025. this->SourceFileProperties.GetPropertiesDocumentation(v);
  2026. // get them for tests
  2027. this->TestProperties.GetPropertiesDocumentation(v);
  2028. cmDocumentationEntry empty = {0,0,0};
  2029. v.push_back(empty);
  2030. }
  2031. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  2032. {
  2033. for(RegisteredGeneratorsMap::const_iterator i = this->Generators.begin();
  2034. i != this->Generators.end(); ++i)
  2035. {
  2036. cmDocumentationEntry e;
  2037. cmGlobalGenerator* generator = (i->second)();
  2038. generator->GetDocumentation(e);
  2039. delete generator;
  2040. v.push_back(e);
  2041. }
  2042. for(RegisteredExtraGeneratorsMap::const_iterator
  2043. i = this->ExtraGenerators.begin(); i != this->ExtraGenerators.end(); ++i)
  2044. {
  2045. cmDocumentationEntry e;
  2046. cmExternalMakefileProjectGenerator* generator = (i->second)();
  2047. generator->GetDocumentation(e, i->first.c_str());
  2048. e.name = i->first.c_str();
  2049. delete generator;
  2050. v.push_back(e);
  2051. }
  2052. cmDocumentationEntry empty = {0,0,0};
  2053. v.push_back(empty);
  2054. }
  2055. void cmake::AddWrittenFile(const char* file)
  2056. {
  2057. this->WrittenFiles.insert(file);
  2058. }
  2059. bool cmake::HasWrittenFile(const char* file)
  2060. {
  2061. return this->WrittenFiles.find(file) != this->WrittenFiles.end();
  2062. }
  2063. void cmake::CleanupWrittenFiles()
  2064. {
  2065. this->WrittenFiles.clear();
  2066. }
  2067. void cmake::UpdateConversionPathTable()
  2068. {
  2069. // Update the path conversion table with any specified file:
  2070. const char* tablepath =
  2071. this->CacheManager->GetCacheValue("CMAKE_PATH_TRANSLATION_FILE");
  2072. if(tablepath)
  2073. {
  2074. std::ifstream table( tablepath );
  2075. if(!table)
  2076. {
  2077. cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ", tablepath,
  2078. ". CMake can not open file.");
  2079. cmSystemTools::ReportLastSystemError("CMake can not open file.");
  2080. }
  2081. else
  2082. {
  2083. std::string a, b;
  2084. while(!table.eof())
  2085. {
  2086. // two entries per line
  2087. table >> a; table >> b;
  2088. cmSystemTools::AddTranslationPath( a.c_str(), b.c_str());
  2089. }
  2090. }
  2091. }
  2092. }
  2093. //----------------------------------------------------------------------------
  2094. int cmake::CheckBuildSystem()
  2095. {
  2096. // We do not need to rerun CMake. Check dependency integrity. Use
  2097. // the make system's VERBOSE environment variable to enable verbose
  2098. // output.
  2099. bool verbose = cmSystemTools::GetEnv("VERBOSE") != 0;
  2100. // This method will check the integrity of the build system if the
  2101. // option was given on the command line. It reads the given file to
  2102. // determine whether CMake should rerun.
  2103. // If no file is provided for the check, we have to rerun.
  2104. if(this->CheckBuildSystemArgument.size() == 0)
  2105. {
  2106. if(verbose)
  2107. {
  2108. cmOStringStream msg;
  2109. msg << "Re-run cmake no build system arguments\n";
  2110. cmSystemTools::Stdout(msg.str().c_str());
  2111. }
  2112. return 1;
  2113. }
  2114. // If the file provided does not exist, we have to rerun.
  2115. if(!cmSystemTools::FileExists(this->CheckBuildSystemArgument.c_str()))
  2116. {
  2117. if(verbose)
  2118. {
  2119. cmOStringStream msg;
  2120. msg << "Re-run cmake missing file: "
  2121. << this->CheckBuildSystemArgument.c_str() << "\n";
  2122. cmSystemTools::Stdout(msg.str().c_str());
  2123. }
  2124. return 1;
  2125. }
  2126. // Read the rerun check file and use it to decide whether to do the
  2127. // global generate.
  2128. cmake cm;
  2129. cmGlobalGenerator gg;
  2130. gg.SetCMakeInstance(&cm);
  2131. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  2132. lg->SetGlobalGenerator(&gg);
  2133. cmMakefile* mf = lg->GetMakefile();
  2134. if(!mf->ReadListFile(0, this->CheckBuildSystemArgument.c_str()) ||
  2135. cmSystemTools::GetErrorOccuredFlag())
  2136. {
  2137. if(verbose)
  2138. {
  2139. cmOStringStream msg;
  2140. msg << "Re-run cmake error reading : "
  2141. << this->CheckBuildSystemArgument.c_str() << "\n";
  2142. cmSystemTools::Stdout(msg.str().c_str());
  2143. }
  2144. // There was an error reading the file. Just rerun.
  2145. return 1;
  2146. }
  2147. // Now that we know the generator used to build the project, use it
  2148. // to check the dependency integrity.
  2149. const char* genName = mf->GetDefinition("CMAKE_DEPENDS_GENERATOR");
  2150. if (!genName || genName[0] == '\0')
  2151. {
  2152. genName = "Unix Makefiles";
  2153. }
  2154. cmGlobalGenerator *ggd = this->CreateGlobalGenerator(genName);
  2155. if (ggd)
  2156. {
  2157. // Check the dependencies in case source files were removed.
  2158. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  2159. lgd->SetGlobalGenerator(ggd);
  2160. lgd->CheckDependencies(mf, verbose, this->ClearBuildSystem);
  2161. // Check for multiple output pairs.
  2162. ggd->CheckMultipleOutputs(mf, verbose);
  2163. }
  2164. // Get the set of dependencies and outputs.
  2165. const char* dependsStr = mf->GetDefinition("CMAKE_MAKEFILE_DEPENDS");
  2166. const char* outputsStr = mf->GetDefinition("CMAKE_MAKEFILE_OUTPUTS");
  2167. if(!dependsStr || !outputsStr)
  2168. {
  2169. // Not enough information was provided to do the test. Just rerun.
  2170. if(verbose)
  2171. {
  2172. cmOStringStream msg;
  2173. msg << "Re-run cmake no CMAKE_MAKEFILE_DEPENDS "
  2174. "or CMAKE_MAKEFILE_OUTPUTS :\n";
  2175. cmSystemTools::Stdout(msg.str().c_str());
  2176. }
  2177. return 1;
  2178. }
  2179. std::vector<std::string> depends;
  2180. std::vector<std::string> outputs;
  2181. cmSystemTools::ExpandListArgument(dependsStr, depends);
  2182. cmSystemTools::ExpandListArgument(outputsStr, outputs);
  2183. // If any output is older than any dependency then rerun.
  2184. for(std::vector<std::string>::iterator dep = depends.begin();
  2185. dep != depends.end(); ++dep)
  2186. {
  2187. for(std::vector<std::string>::iterator out = outputs.begin();
  2188. out != outputs.end(); ++out)
  2189. {
  2190. int result = 0;
  2191. if(!this->FileComparison->FileTimeCompare(out->c_str(),
  2192. dep->c_str(), &result) ||
  2193. result < 0)
  2194. {
  2195. if(verbose)
  2196. {
  2197. cmOStringStream msg;
  2198. msg << "Re-run cmake file: " << out->c_str()
  2199. << " older than: " << dep->c_str() << "\n";
  2200. cmSystemTools::Stdout(msg.str().c_str());
  2201. }
  2202. return 1;
  2203. }
  2204. }
  2205. }
  2206. // No need to rerun.
  2207. return 0;
  2208. }
  2209. //----------------------------------------------------------------------------
  2210. void cmake::TruncateOutputLog(const char* fname)
  2211. {
  2212. std::string fullPath = this->GetHomeOutputDirectory();
  2213. fullPath += "/";
  2214. fullPath += fname;
  2215. struct stat st;
  2216. if ( ::stat(fullPath.c_str(), &st) )
  2217. {
  2218. return;
  2219. }
  2220. if ( !this->CacheManager->GetCacheValue("CMAKE_CACHEFILE_DIR") )
  2221. {
  2222. cmSystemTools::RemoveFile(fullPath.c_str());
  2223. return;
  2224. }
  2225. size_t fsize = st.st_size;
  2226. const size_t maxFileSize = 50 * 1024;
  2227. if ( fsize < maxFileSize )
  2228. {
  2229. //TODO: truncate file
  2230. return;
  2231. }
  2232. }
  2233. inline std::string removeQuotes(const std::string& s)
  2234. {
  2235. if(s[0] == '\"' && s[s.size()-1] == '\"')
  2236. {
  2237. return s.substr(1, s.size()-2);
  2238. }
  2239. return s;
  2240. }
  2241. std::string cmake::FindCMakeProgram(const char* name) const
  2242. {
  2243. std::string path;
  2244. if ((name) && (*name))
  2245. {
  2246. const cmMakefile* mf
  2247. = this->GetGlobalGenerator()->GetLocalGenerators()[0]->GetMakefile();
  2248. #ifdef CMAKE_BUILD_WITH_CMAKE
  2249. path = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2250. path = removeQuotes(path);
  2251. path = cmSystemTools::GetFilenamePath(path.c_str());
  2252. path += "/";
  2253. path += name;
  2254. path += cmSystemTools::GetExecutableExtension();
  2255. if(!cmSystemTools::FileExists(path.c_str()))
  2256. {
  2257. path = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2258. path = cmSystemTools::GetFilenamePath(path.c_str());
  2259. path += "/Debug/";
  2260. path += name;
  2261. path += cmSystemTools::GetExecutableExtension();
  2262. }
  2263. if(!cmSystemTools::FileExists(path.c_str()))
  2264. {
  2265. path = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2266. path = cmSystemTools::GetFilenamePath(path.c_str());
  2267. path += "/Release/";
  2268. path += name;
  2269. path += cmSystemTools::GetExecutableExtension();
  2270. }
  2271. #else
  2272. // Only for bootstrap
  2273. path += mf->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  2274. path += name;
  2275. path += cmSystemTools::GetExecutableExtension();
  2276. #endif
  2277. }
  2278. return path;
  2279. }
  2280. const char* cmake::GetCTestCommand()
  2281. {
  2282. if ( this->CTestCommand.empty() )
  2283. {
  2284. this->CTestCommand = this->FindCMakeProgram("ctest");
  2285. }
  2286. if ( this->CTestCommand.empty() )
  2287. {
  2288. cmSystemTools::Error("Cannot find the CTest executable");
  2289. this->CTestCommand = "CTEST-COMMAND-NOT-FOUND";
  2290. }
  2291. return this->CTestCommand.c_str();
  2292. }
  2293. const char* cmake::GetCPackCommand()
  2294. {
  2295. if ( this->CPackCommand.empty() )
  2296. {
  2297. this->CPackCommand = this->FindCMakeProgram("cpack");
  2298. }
  2299. if ( this->CPackCommand.empty() )
  2300. {
  2301. cmSystemTools::Error("Cannot find the CPack executable");
  2302. this->CPackCommand = "CPACK-COMMAND-NOT-FOUND";
  2303. }
  2304. return this->CPackCommand.c_str();
  2305. }
  2306. void cmake::GenerateGraphViz(const char* fileName) const
  2307. {
  2308. cmGeneratedFileStream str(fileName);
  2309. if ( !str )
  2310. {
  2311. return;
  2312. }
  2313. cmake cm;
  2314. cmGlobalGenerator ggi;
  2315. ggi.SetCMakeInstance(&cm);
  2316. std::auto_ptr<cmLocalGenerator> lg(ggi.CreateLocalGenerator());
  2317. lg->SetGlobalGenerator(&ggi);
  2318. cmMakefile *mf = lg->GetMakefile();
  2319. std::string infile = this->GetHomeOutputDirectory();
  2320. infile += "/CMakeGraphVizOptions.cmake";
  2321. if ( !cmSystemTools::FileExists(infile.c_str()) )
  2322. {
  2323. infile = this->GetHomeDirectory();
  2324. infile += "/CMakeGraphVizOptions.cmake";
  2325. if ( !cmSystemTools::FileExists(infile.c_str()) )
  2326. {
  2327. infile = "";
  2328. }
  2329. }
  2330. if ( !infile.empty() )
  2331. {
  2332. if ( !mf->ReadListFile(0, infile.c_str()) )
  2333. {
  2334. cmSystemTools::Error("Problem opening GraphViz options file: ",
  2335. infile.c_str());
  2336. return;
  2337. }
  2338. std::cout << "Read GraphViz options file: " << infile.c_str()
  2339. << std::endl;
  2340. }
  2341. #define __set_if_not_set(var, value, cmakeDefinition) \
  2342. const char* var = mf->GetDefinition(cmakeDefinition); \
  2343. if ( !var ) \
  2344. { \
  2345. var = value; \
  2346. }
  2347. __set_if_not_set(graphType, "digraph", "GRAPHVIZ_GRAPH_TYPE");
  2348. __set_if_not_set(graphName, "GG", "GRAPHVIZ_GRAPH_NAME");
  2349. __set_if_not_set(graphHeader, "node [\n fontsize = \"12\"\n];",
  2350. "GRAPHVIZ_GRAPH_HEADER");
  2351. __set_if_not_set(graphNodePrefix, "node", "GRAPHVIZ_NODE_PREFIX");
  2352. const char* ignoreTargets = mf->GetDefinition("GRAPHVIZ_IGNORE_TARGETS");
  2353. std::set<cmStdString> ignoreTargetsSet;
  2354. if ( ignoreTargets )
  2355. {
  2356. std::vector<std::string> ignoreTargetsVector;
  2357. cmSystemTools::ExpandListArgument(ignoreTargets,ignoreTargetsVector);
  2358. std::vector<std::string>::iterator itvIt;
  2359. for ( itvIt = ignoreTargetsVector.begin();
  2360. itvIt != ignoreTargetsVector.end();
  2361. ++ itvIt )
  2362. {
  2363. ignoreTargetsSet.insert(itvIt->c_str());
  2364. }
  2365. }
  2366. str << graphType << " " << graphName << " {" << std::endl;
  2367. str << graphHeader << std::endl;
  2368. const cmGlobalGenerator* gg = this->GetGlobalGenerator();
  2369. const std::vector<cmLocalGenerator*>& localGenerators =
  2370. gg->GetLocalGenerators();
  2371. std::vector<cmLocalGenerator*>::const_iterator lit;
  2372. // for target deps
  2373. // 1 - cmake target
  2374. // 2 - external target
  2375. // 0 - no deps
  2376. std::map<cmStdString, int> targetDeps;
  2377. std::map<cmStdString, const cmTarget*> targetPtrs;
  2378. std::map<cmStdString, cmStdString> targetNamesNodes;
  2379. int cnt = 0;
  2380. // First pass get the list of all cmake targets
  2381. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2382. {
  2383. const cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2384. cmTargets::const_iterator tit;
  2385. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2386. {
  2387. const char* realTargetName = tit->first.c_str();
  2388. if ( ignoreTargetsSet.find(realTargetName) != ignoreTargetsSet.end() )
  2389. {
  2390. // Skip ignored targets
  2391. continue;
  2392. }
  2393. //std::cout << "Found target: " << tit->first.c_str() << std::endl;
  2394. cmOStringStream ostr;
  2395. ostr << graphNodePrefix << cnt++;
  2396. targetNamesNodes[realTargetName] = ostr.str();
  2397. targetPtrs[realTargetName] = &tit->second;
  2398. }
  2399. }
  2400. // Ok, now find all the stuff we link to that is not in cmake
  2401. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2402. {
  2403. const cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2404. cmTargets::const_iterator tit;
  2405. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2406. {
  2407. const cmTarget::LinkLibraryVectorType* ll
  2408. = &(tit->second.GetOriginalLinkLibraries());
  2409. cmTarget::LinkLibraryVectorType::const_iterator llit;
  2410. const char* realTargetName = tit->first.c_str();
  2411. if ( ignoreTargetsSet.find(realTargetName) != ignoreTargetsSet.end() )
  2412. {
  2413. // Skip ignored targets
  2414. continue;
  2415. }
  2416. if ( ll->size() > 0 )
  2417. {
  2418. targetDeps[realTargetName] = 1;
  2419. }
  2420. for ( llit = ll->begin(); llit != ll->end(); ++ llit )
  2421. {
  2422. const char* libName = llit->first.c_str();
  2423. std::map<cmStdString, cmStdString>::const_iterator tarIt
  2424. = targetNamesNodes.find(libName);
  2425. if ( ignoreTargetsSet.find(libName) != ignoreTargetsSet.end() )
  2426. {
  2427. // Skip ignored targets
  2428. continue;
  2429. }
  2430. if ( tarIt == targetNamesNodes.end() )
  2431. {
  2432. cmOStringStream ostr;
  2433. ostr << graphNodePrefix << cnt++;
  2434. targetDeps[libName] = 2;
  2435. targetNamesNodes[libName] = ostr.str();
  2436. //str << " \"" << ostr.c_str() << "\" [ label=\"" << libName
  2437. //<< "\" shape=\"ellipse\"];" << std::endl;
  2438. }
  2439. else
  2440. {
  2441. std::map<cmStdString, int>::const_iterator depIt
  2442. = targetDeps.find(libName);
  2443. if ( depIt == targetDeps.end() )
  2444. {
  2445. targetDeps[libName] = 1;
  2446. }
  2447. }
  2448. }
  2449. }
  2450. }
  2451. // Write out nodes
  2452. std::map<cmStdString, int>::const_iterator depIt;
  2453. for ( depIt = targetDeps.begin(); depIt != targetDeps.end(); ++ depIt )
  2454. {
  2455. const char* newTargetName = depIt->first.c_str();
  2456. std::map<cmStdString, cmStdString>::const_iterator tarIt
  2457. = targetNamesNodes.find(newTargetName);
  2458. if ( tarIt == targetNamesNodes.end() )
  2459. {
  2460. // We should not be here.
  2461. std::cout << __LINE__ << " Cannot find library: " << newTargetName
  2462. << " even though it was added in the previous pass" << std::endl;
  2463. abort();
  2464. }
  2465. str << " \"" << tarIt->second.c_str() << "\" [ label=\""
  2466. << newTargetName << "\" shape=\"";
  2467. if ( depIt->second == 1 )
  2468. {
  2469. std::map<cmStdString, const cmTarget*>::const_iterator tarTypeIt =
  2470. targetPtrs.find(newTargetName);
  2471. if ( tarTypeIt == targetPtrs.end() )
  2472. {
  2473. // We should not be here.
  2474. std::cout << __LINE__ << " Cannot find library: " << newTargetName
  2475. << " even though it was added in the previous pass" << std::endl;
  2476. abort();
  2477. }
  2478. const cmTarget* tg = tarTypeIt->second;
  2479. switch ( tg->GetType() )
  2480. {
  2481. case cmTarget::EXECUTABLE:
  2482. str << "house";
  2483. break;
  2484. case cmTarget::STATIC_LIBRARY:
  2485. str << "diamond";
  2486. break;
  2487. case cmTarget::SHARED_LIBRARY:
  2488. str << "polygon";
  2489. break;
  2490. case cmTarget::MODULE_LIBRARY:
  2491. str << "octagon";
  2492. break;
  2493. default:
  2494. str << "box";
  2495. }
  2496. }
  2497. else
  2498. {
  2499. str << "ellipse";
  2500. }
  2501. str << "\"];" << std::endl;
  2502. }
  2503. // Now generate the connectivity
  2504. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2505. {
  2506. const cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2507. cmTargets::const_iterator tit;
  2508. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2509. {
  2510. std::map<cmStdString, int>::iterator dependIt
  2511. = targetDeps.find(tit->first.c_str());
  2512. if ( dependIt == targetDeps.end() )
  2513. {
  2514. continue;
  2515. }
  2516. std::map<cmStdString, cmStdString>::iterator cmakeTarIt
  2517. = targetNamesNodes.find(tit->first.c_str());
  2518. const cmTarget::LinkLibraryVectorType* ll
  2519. = &(tit->second.GetOriginalLinkLibraries());
  2520. cmTarget::LinkLibraryVectorType::const_iterator llit;
  2521. for ( llit = ll->begin(); llit != ll->end(); ++ llit )
  2522. {
  2523. const char* libName = llit->first.c_str();
  2524. std::map<cmStdString, cmStdString>::const_iterator tarIt
  2525. = targetNamesNodes.find(libName);
  2526. if ( tarIt == targetNamesNodes.end() )
  2527. {
  2528. // We should not be here.
  2529. std::cout << __LINE__ << " Cannot find library: " << libName
  2530. << " even though it was added in the previous pass" << std::endl;
  2531. abort();
  2532. }
  2533. str << " \"" << cmakeTarIt->second.c_str() << "\" -> \""
  2534. << tarIt->second.c_str() << "\"" << std::endl;
  2535. }
  2536. }
  2537. }
  2538. // TODO: Use dotted or something for external libraries
  2539. //str << " \"node0\":f4 -> \"node12\"[color=\"#0000ff\" style=dotted]"
  2540. //<< std::endl;
  2541. //
  2542. str << "}" << std::endl;
  2543. }
  2544. //----------------------------------------------------------------------------
  2545. #ifdef CMAKE_BUILD_WITH_CMAKE
  2546. int cmake::ExecuteEchoColor(std::vector<std::string>& args)
  2547. {
  2548. // The arguments are
  2549. // argv[0] == <cmake-executable>
  2550. // argv[1] == cmake_echo_color
  2551. // On some platforms (an MSYS prompt) cmsysTerminal may not be able
  2552. // to determine whether the stream is displayed on a tty. In this
  2553. // case it assumes no unless we tell it otherwise. Since we want
  2554. // color messages to be displayed for users we will assume yes.
  2555. // However, we can test for some situations when the answer is most
  2556. // likely no.
  2557. int assumeTTY = cmsysTerminal_Color_AssumeTTY;
  2558. if(cmSystemTools::GetEnv("DART_TEST_FROM_DART") ||
  2559. cmSystemTools::GetEnv("DASHBOARD_TEST_FROM_CTEST") ||
  2560. cmSystemTools::GetEnv("CTEST_INTERACTIVE_DEBUG_MODE"))
  2561. {
  2562. // Avoid printing color escapes during dashboard builds.
  2563. assumeTTY = 0;
  2564. }
  2565. bool enabled = true;
  2566. int color = cmsysTerminal_Color_Normal;
  2567. bool newline = true;
  2568. for(unsigned int i=2; i < args.size(); ++i)
  2569. {
  2570. if(args[i].find("--switch=") == 0)
  2571. {
  2572. // Enable or disable color based on the switch value.
  2573. std::string value = args[i].substr(9);
  2574. if(!value.empty())
  2575. {
  2576. if(cmSystemTools::IsOn(value.c_str()))
  2577. {
  2578. enabled = true;
  2579. }
  2580. else
  2581. {
  2582. enabled = false;
  2583. }
  2584. }
  2585. }
  2586. else if(args[i] == "--normal")
  2587. {
  2588. color = cmsysTerminal_Color_Normal;
  2589. }
  2590. else if(args[i] == "--black")
  2591. {
  2592. color = cmsysTerminal_Color_ForegroundBlack;
  2593. }
  2594. else if(args[i] == "--red")
  2595. {
  2596. color = cmsysTerminal_Color_ForegroundRed;
  2597. }
  2598. else if(args[i] == "--green")
  2599. {
  2600. color = cmsysTerminal_Color_ForegroundGreen;
  2601. }
  2602. else if(args[i] == "--yellow")
  2603. {
  2604. color = cmsysTerminal_Color_ForegroundYellow;
  2605. }
  2606. else if(args[i] == "--blue")
  2607. {
  2608. color = cmsysTerminal_Color_ForegroundBlue;
  2609. }
  2610. else if(args[i] == "--magenta")
  2611. {
  2612. color = cmsysTerminal_Color_ForegroundMagenta;
  2613. }
  2614. else if(args[i] == "--cyan")
  2615. {
  2616. color = cmsysTerminal_Color_ForegroundCyan;
  2617. }
  2618. else if(args[i] == "--white")
  2619. {
  2620. color = cmsysTerminal_Color_ForegroundWhite;
  2621. }
  2622. else if(args[i] == "--bold")
  2623. {
  2624. color |= cmsysTerminal_Color_ForegroundBold;
  2625. }
  2626. else if(args[i] == "--no-newline")
  2627. {
  2628. newline = false;
  2629. }
  2630. else if(args[i] == "--newline")
  2631. {
  2632. newline = true;
  2633. }
  2634. else if(enabled)
  2635. {
  2636. // Color is enabled. Print with the current color.
  2637. cmsysTerminal_cfprintf(color | assumeTTY, stdout, "%s%s",
  2638. args[i].c_str(), newline? "\n" : "");
  2639. }
  2640. else
  2641. {
  2642. // Color is disabled. Print without color.
  2643. fprintf(stdout, "%s%s", args[i].c_str(), newline? "\n" : "");
  2644. }
  2645. }
  2646. return 0;
  2647. }
  2648. #else
  2649. int cmake::ExecuteEchoColor(std::vector<std::string>&)
  2650. {
  2651. return 1;
  2652. }
  2653. #endif
  2654. //----------------------------------------------------------------------------
  2655. int cmake::ExecuteLinkScript(std::vector<std::string>& args)
  2656. {
  2657. // The arguments are
  2658. // argv[0] == <cmake-executable>
  2659. // argv[1] == cmake_link_script
  2660. // argv[2] == <link-script-name>
  2661. // argv[3] == --verbose=?
  2662. bool verbose = false;
  2663. if(args.size() >= 4)
  2664. {
  2665. if(args[3].find("--verbose=") == 0)
  2666. {
  2667. if(!cmSystemTools::IsOff(args[3].substr(10).c_str()))
  2668. {
  2669. verbose = true;
  2670. }
  2671. }
  2672. }
  2673. // Allocate a process instance.
  2674. cmsysProcess* cp = cmsysProcess_New();
  2675. if(!cp)
  2676. {
  2677. std::cerr << "Error allocating process instance in link script."
  2678. << std::endl;
  2679. return 1;
  2680. }
  2681. // Children should share stdout and stderr with this process.
  2682. cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDOUT, 1);
  2683. cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDERR, 1);
  2684. // Run the command lines verbatim.
  2685. cmsysProcess_SetOption(cp, cmsysProcess_Option_Verbatim, 1);
  2686. // Read command lines from the script.
  2687. std::ifstream fin(args[2].c_str());
  2688. if(!fin)
  2689. {
  2690. std::cerr << "Error opening link script \""
  2691. << args[2] << "\"" << std::endl;
  2692. return 1;
  2693. }
  2694. // Run one command at a time.
  2695. std::string command;
  2696. int result = 0;
  2697. while(result == 0 && cmSystemTools::GetLineFromStream(fin, command))
  2698. {
  2699. // Setup this command line.
  2700. const char* cmd[2] = {command.c_str(), 0};
  2701. cmsysProcess_SetCommand(cp, cmd);
  2702. // Report the command if verbose output is enabled.
  2703. if(verbose)
  2704. {
  2705. std::cout << command << std::endl;
  2706. }
  2707. // Run the command and wait for it to exit.
  2708. cmsysProcess_Execute(cp);
  2709. cmsysProcess_WaitForExit(cp, 0);
  2710. // Report failure if any.
  2711. switch(cmsysProcess_GetState(cp))
  2712. {
  2713. case cmsysProcess_State_Exited:
  2714. {
  2715. int value = cmsysProcess_GetExitValue(cp);
  2716. if(value != 0)
  2717. {
  2718. result = value;
  2719. }
  2720. }
  2721. break;
  2722. case cmsysProcess_State_Exception:
  2723. std::cerr << "Error running link command: "
  2724. << cmsysProcess_GetExceptionString(cp) << std::endl;
  2725. result = 1;
  2726. break;
  2727. case cmsysProcess_State_Error:
  2728. std::cerr << "Error running link command: "
  2729. << cmsysProcess_GetErrorString(cp) << std::endl;
  2730. result = 2;
  2731. break;
  2732. default:
  2733. break;
  2734. };
  2735. }
  2736. // Free the process instance.
  2737. cmsysProcess_Delete(cp);
  2738. // Return the final resulting return value.
  2739. return result;
  2740. }
  2741. void cmake::DefineProperty(const char *name, cmProperty::ScopeType scope,
  2742. const char *ShortDescription,
  2743. const char *FullDescription,
  2744. bool chained)
  2745. {
  2746. switch (scope)
  2747. {
  2748. case cmProperty::GLOBAL:
  2749. this->GlobalProperties.DefineProperty(name,scope,ShortDescription,
  2750. FullDescription, chained);
  2751. break;
  2752. case cmProperty::TARGET:
  2753. this->TargetProperties.DefineProperty(name,scope,ShortDescription,
  2754. FullDescription, chained);
  2755. break;
  2756. case cmProperty::SOURCE_FILE:
  2757. this->SourceFileProperties.DefineProperty(name,scope,ShortDescription,
  2758. FullDescription, chained);
  2759. break;
  2760. case cmProperty::DIRECTORY:
  2761. this->DirectoryProperties.DefineProperty(name,scope,ShortDescription,
  2762. FullDescription, chained);
  2763. break;
  2764. case cmProperty::TEST:
  2765. this->TestProperties.DefineProperty(name,scope,ShortDescription,
  2766. FullDescription, chained);
  2767. break;
  2768. }
  2769. }
  2770. bool cmake::IsPropertyDefined(const char *name, cmProperty::ScopeType scope)
  2771. {
  2772. switch (scope)
  2773. {
  2774. case cmProperty::GLOBAL:
  2775. return this->GlobalProperties.IsPropertyDefined(name);
  2776. case cmProperty::TARGET:
  2777. return this->TargetProperties.IsPropertyDefined(name);
  2778. case cmProperty::SOURCE_FILE:
  2779. return this->SourceFileProperties.IsPropertyDefined(name);
  2780. case cmProperty::DIRECTORY:
  2781. return this->DirectoryProperties.IsPropertyDefined(name);
  2782. case cmProperty::TEST:
  2783. return this->TestProperties.IsPropertyDefined(name);
  2784. }
  2785. return false;
  2786. }
  2787. bool cmake::IsPropertyChained(const char *name, cmProperty::ScopeType scope)
  2788. {
  2789. switch (scope)
  2790. {
  2791. case cmProperty::GLOBAL:
  2792. return this->GlobalProperties.IsPropertyChained(name);
  2793. case cmProperty::TARGET:
  2794. return this->TargetProperties.IsPropertyChained(name);
  2795. case cmProperty::SOURCE_FILE:
  2796. return this->SourceFileProperties.IsPropertyChained(name);
  2797. case cmProperty::DIRECTORY:
  2798. return this->DirectoryProperties.IsPropertyChained(name);
  2799. case cmProperty::TEST:
  2800. return this->DirectoryProperties.IsPropertyChained(name);
  2801. }
  2802. return false;
  2803. }
  2804. void cmake::SetProperty(const char* prop, const char* value)
  2805. {
  2806. if (!prop)
  2807. {
  2808. return;
  2809. }
  2810. if (!value)
  2811. {
  2812. value = "NOTFOUND";
  2813. }
  2814. this->Properties.SetProperty(prop, value, cmProperty::TARGET);
  2815. }
  2816. const char *cmake::GetProperty(const char* prop)
  2817. {
  2818. return this->GetProperty(prop, cmProperty::GLOBAL);
  2819. }
  2820. const char *cmake::GetProperty(const char* prop, cmProperty::ScopeType scope)
  2821. {
  2822. bool chain = false;
  2823. return this->Properties.GetPropertyValue(prop, scope, chain);
  2824. }
  2825. bool cmake::GetPropertyAsBool(const char* prop)
  2826. {
  2827. return cmSystemTools::IsOn(this->GetProperty(prop));
  2828. }
  2829. int cmake::GetSystemInformation(std::vector<std::string>& args)
  2830. {
  2831. // so create the directory
  2832. std::string resultFile;
  2833. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  2834. std::string destPath = cwd + "/__cmake_systeminformation";
  2835. cmSystemTools::RemoveADirectory(destPath.c_str());
  2836. if (!cmSystemTools::MakeDirectory(destPath.c_str()))
  2837. {
  2838. std::cerr << "Error: --system-information must be run from a "
  2839. "writable directory!\n";
  2840. return 1;
  2841. }
  2842. // process the arguments
  2843. bool writeToStdout = true;
  2844. for(unsigned int i=1; i < args.size(); ++i)
  2845. {
  2846. std::string arg = args[i];
  2847. if(arg.find("-V",0) == 0)
  2848. {
  2849. this->Verbose = true;
  2850. }
  2851. else if(arg.find("-G",0) == 0)
  2852. {
  2853. std::string value = arg.substr(2);
  2854. if(value.size() == 0)
  2855. {
  2856. ++i;
  2857. if(i >= args.size())
  2858. {
  2859. cmSystemTools::Error("No generator specified for -G");
  2860. return -1;
  2861. }
  2862. value = args[i];
  2863. }
  2864. cmGlobalGenerator* gen =
  2865. this->CreateGlobalGenerator(value.c_str());
  2866. if(!gen)
  2867. {
  2868. cmSystemTools::Error("Could not create named generator ",
  2869. value.c_str());
  2870. }
  2871. else
  2872. {
  2873. this->SetGlobalGenerator(gen);
  2874. }
  2875. }
  2876. // no option assume it is the output file
  2877. else
  2878. {
  2879. if (!cmSystemTools::FileIsFullPath(arg.c_str()))
  2880. {
  2881. resultFile = cwd;
  2882. resultFile += "/";
  2883. }
  2884. resultFile += arg;
  2885. writeToStdout = false;
  2886. }
  2887. }
  2888. // we have to find the module directory, so we can copy the files
  2889. this->AddCMakePaths(args[0].c_str());
  2890. std::string modulesPath =
  2891. this->CacheManager->GetCacheValue("CMAKE_ROOT");
  2892. modulesPath += "/Modules";
  2893. std::string inFile = modulesPath;
  2894. inFile += "/SystemInformation.cmake";
  2895. std::string outFile = destPath;
  2896. outFile += "/CMakeLists.txt";
  2897. // Copy file
  2898. if(!cmSystemTools::cmCopyFile(inFile.c_str(), outFile.c_str()))
  2899. {
  2900. std::cerr << "Error copying file \"" << inFile.c_str()
  2901. << "\" to \"" << outFile.c_str() << "\".\n";
  2902. return 1;
  2903. }
  2904. // do we write to a file or to stdout?
  2905. if (resultFile.size() == 0)
  2906. {
  2907. resultFile = cwd;
  2908. resultFile += "/__cmake_systeminformation/results.txt";
  2909. }
  2910. // now run cmake on the CMakeLists file
  2911. cmSystemTools::ChangeDirectory(destPath.c_str());
  2912. std::vector<std::string> args2;
  2913. args2.push_back(args[0]);
  2914. args2.push_back(destPath);
  2915. std::string resultArg = "-DRESULT_FILE=";
  2916. resultArg += resultFile;
  2917. args2.push_back(resultArg);
  2918. int res = this->Run(args2, false);
  2919. if (res != 0)
  2920. {
  2921. std::cerr << "Error: --system-information failed on internal CMake!\n";
  2922. return res;
  2923. }
  2924. // change back to the original directory
  2925. cmSystemTools::ChangeDirectory(cwd.c_str());
  2926. // echo results to stdout if needed
  2927. if (writeToStdout)
  2928. {
  2929. FILE* fin = fopen(resultFile.c_str(), "r");
  2930. if(fin)
  2931. {
  2932. const int bufferSize = 4096;
  2933. char buffer[bufferSize];
  2934. size_t n;
  2935. while((n = fread(buffer, 1, bufferSize, fin)) > 0)
  2936. {
  2937. for(char* c = buffer; c < buffer+n; ++c)
  2938. {
  2939. putc(*c, stdout);
  2940. }
  2941. fflush(stdout);
  2942. }
  2943. fclose(fin);
  2944. }
  2945. }
  2946. // clean up the directory
  2947. cmSystemTools::RemoveADirectory(destPath.c_str());
  2948. return 0;
  2949. }