cmake.cxx 92 KB

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