cmake.cxx 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053
  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 "cmDocumentVariables.h"
  15. #include "time.h"
  16. #include "cmCacheManager.h"
  17. #include "cmMakefile.h"
  18. #include "cmLocalGenerator.h"
  19. #include "cmExternalMakefileProjectGenerator.h"
  20. #include "cmCommands.h"
  21. #include "cmCommand.h"
  22. #include "cmFileTimeComparison.h"
  23. #include "cmGeneratedFileStream.h"
  24. #include "cmSourceFile.h"
  25. #include "cmVersion.h"
  26. #include "cmTest.h"
  27. #if defined(CMAKE_BUILD_WITH_CMAKE)
  28. # include "cmDependsFortran.h" // For -E cmake_copy_f90_mod callback.
  29. # include "cmVariableWatch.h"
  30. # include <cmsys/Terminal.h>
  31. #endif
  32. #include <cmsys/Directory.hxx>
  33. #include <cmsys/Process.h>
  34. #include <cmsys/Glob.hxx>
  35. #include <cmsys/RegularExpression.hxx>
  36. // only build kdevelop generator on non-windows platforms
  37. // when not bootstrapping cmake
  38. #if !defined(_WIN32)
  39. # if defined(CMAKE_BUILD_WITH_CMAKE)
  40. # define CMAKE_USE_KDEVELOP
  41. # endif
  42. #endif
  43. #if defined(CMAKE_BUILD_WITH_CMAKE)
  44. # define CMAKE_USE_ECLIPSE
  45. #endif
  46. #if defined(__MINGW32__) && !defined(CMAKE_BUILD_WITH_CMAKE)
  47. # define CMAKE_BOOT_MINGW
  48. #endif
  49. // include the generator
  50. #if defined(_WIN32) && !defined(__CYGWIN__)
  51. # if !defined(CMAKE_BOOT_MINGW)
  52. # include "cmGlobalVisualStudio6Generator.h"
  53. # include "cmGlobalVisualStudio7Generator.h"
  54. # include "cmGlobalVisualStudio71Generator.h"
  55. # include "cmGlobalVisualStudio8Generator.h"
  56. # include "cmGlobalVisualStudio9Generator.h"
  57. # include "cmGlobalVisualStudio9Win64Generator.h"
  58. # include "cmGlobalVisualStudio8Win64Generator.h"
  59. # include "cmGlobalBorlandMakefileGenerator.h"
  60. # include "cmGlobalNMakeMakefileGenerator.h"
  61. # include "cmGlobalWatcomWMakeGenerator.h"
  62. # define CMAKE_HAVE_VS_GENERATORS
  63. # endif
  64. # include "cmGlobalMSYSMakefileGenerator.h"
  65. # include "cmGlobalMinGWMakefileGenerator.h"
  66. # include "cmWin32ProcessExecution.h"
  67. #else
  68. #endif
  69. #include "cmGlobalUnixMakefileGenerator3.h"
  70. #if defined(CMAKE_HAVE_VS_GENERATORS)
  71. #include "cmCallVisualStudioMacro.h"
  72. #endif
  73. #if !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
  74. # include "cmExtraCodeBlocksGenerator.h"
  75. #endif
  76. #ifdef CMAKE_USE_KDEVELOP
  77. # include "cmGlobalKdevelopGenerator.h"
  78. #endif
  79. #ifdef CMAKE_USE_ECLIPSE
  80. # include "cmExtraEclipseCDT4Generator.h"
  81. #endif
  82. #include <stdlib.h> // required for atoi
  83. #if defined( __APPLE__ )
  84. # if defined(CMAKE_BUILD_WITH_CMAKE)
  85. # include "cmGlobalXCodeGenerator.h"
  86. # define CMAKE_USE_XCODE 1
  87. # endif
  88. # include <sys/types.h>
  89. # include <sys/time.h>
  90. # include <sys/resource.h>
  91. #endif
  92. #include <sys/stat.h> // struct stat
  93. #include <memory> // auto_ptr
  94. static bool cmakeCheckStampFile(const char* stampName);
  95. void cmNeedBackwardsCompatibility(const std::string& variable,
  96. int access_type, void*, const char*, const cmMakefile*)
  97. {
  98. #ifdef CMAKE_BUILD_WITH_CMAKE
  99. if (access_type == cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS)
  100. {
  101. std::string message = "An attempt was made to access a variable: ";
  102. message += variable;
  103. message +=
  104. " that has not been defined. Some variables were always defined "
  105. "by CMake in versions prior to 1.6. To fix this you might need to set "
  106. "the cache value of CMAKE_BACKWARDS_COMPATIBILITY to 1.4 or less. If "
  107. "you are writing a CMakeList file, (or have already set "
  108. "CMAKE_BACKWARDS_COMPATABILITY to 1.4 or less) then you probably need "
  109. "to include a CMake module to test for the feature this variable "
  110. "defines.";
  111. cmSystemTools::Error(message.c_str());
  112. }
  113. #else
  114. (void)variable;
  115. (void)access_type;
  116. #endif
  117. }
  118. cmake::cmake()
  119. {
  120. this->DebugOutput = false;
  121. this->DebugTryCompile = false;
  122. this->ClearBuildSystem = false;
  123. this->FileComparison = new cmFileTimeComparison;
  124. this->Policies = new cmPolicies();
  125. this->Properties.SetCMakeInstance(this);
  126. // initialize properties
  127. cmSourceFile::DefineProperties(this);
  128. cmTarget::DefineProperties(this);
  129. cmMakefile::DefineProperties(this);
  130. cmTest::DefineProperties(this);
  131. cmake::DefineProperties(this);
  132. #ifdef __APPLE__
  133. struct rlimit rlp;
  134. if(!getrlimit(RLIMIT_STACK, &rlp))
  135. {
  136. if(rlp.rlim_cur != rlp.rlim_max)
  137. {
  138. rlp.rlim_cur = rlp.rlim_max;
  139. setrlimit(RLIMIT_STACK, &rlp);
  140. }
  141. }
  142. #endif
  143. // If MAKEFLAGS are given in the environment, remove the environment
  144. // variable. This will prevent try-compile from succeeding when it
  145. // should fail (if "-i" is an option). We cannot simply test
  146. // whether "-i" is given and remove it because some make programs
  147. // encode the MAKEFLAGS variable in a strange way.
  148. if(getenv("MAKEFLAGS"))
  149. {
  150. cmSystemTools::PutEnv("MAKEFLAGS=");
  151. }
  152. this->Verbose = false;
  153. this->InTryCompile = false;
  154. this->CacheManager = new cmCacheManager;
  155. this->GlobalGenerator = 0;
  156. this->ProgressCallback = 0;
  157. this->ProgressCallbackClientData = 0;
  158. this->ScriptMode = false;
  159. #ifdef CMAKE_BUILD_WITH_CMAKE
  160. this->VariableWatch = new cmVariableWatch;
  161. this->VariableWatch->AddWatch("CMAKE_WORDS_BIGENDIAN",
  162. cmNeedBackwardsCompatibility);
  163. this->VariableWatch->AddWatch("CMAKE_SIZEOF_INT",
  164. cmNeedBackwardsCompatibility);
  165. this->VariableWatch->AddWatch("CMAKE_X_LIBS",
  166. cmNeedBackwardsCompatibility);
  167. #endif
  168. this->AddDefaultGenerators();
  169. this->AddDefaultExtraGenerators();
  170. this->AddDefaultCommands();
  171. // Make sure we can capture the build tool output.
  172. cmSystemTools::EnableVSConsoleOutput();
  173. }
  174. cmake::~cmake()
  175. {
  176. delete this->CacheManager;
  177. delete this->Policies;
  178. if (this->GlobalGenerator)
  179. {
  180. delete this->GlobalGenerator;
  181. this->GlobalGenerator = 0;
  182. }
  183. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  184. j != this->Commands.end(); ++j)
  185. {
  186. delete (*j).second;
  187. }
  188. #ifdef CMAKE_BUILD_WITH_CMAKE
  189. delete this->VariableWatch;
  190. #endif
  191. delete this->FileComparison;
  192. }
  193. void cmake::CleanupCommandsAndMacros()
  194. {
  195. std::vector<cmCommand*> commands;
  196. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  197. j != this->Commands.end(); ++j)
  198. {
  199. if ( !j->second->IsA("cmMacroHelperCommand") &&
  200. !j->second->IsA("cmFunctionHelperCommand"))
  201. {
  202. commands.push_back(j->second);
  203. }
  204. else
  205. {
  206. delete j->second;
  207. }
  208. }
  209. this->Commands.erase(this->Commands.begin(), this->Commands.end());
  210. std::vector<cmCommand*>::iterator it;
  211. for ( it = commands.begin(); it != commands.end();
  212. ++ it )
  213. {
  214. this->Commands[cmSystemTools::LowerCase((*it)->GetName())] = *it;
  215. }
  216. }
  217. bool cmake::CommandExists(const char* name) const
  218. {
  219. std::string sName = cmSystemTools::LowerCase(name);
  220. return (this->Commands.find(sName) != this->Commands.end());
  221. }
  222. cmCommand *cmake::GetCommand(const char *name)
  223. {
  224. cmCommand* rm = 0;
  225. std::string sName = cmSystemTools::LowerCase(name);
  226. RegisteredCommandsMap::iterator pos = this->Commands.find(sName);
  227. if (pos != this->Commands.end())
  228. {
  229. rm = (*pos).second;
  230. }
  231. return rm;
  232. }
  233. void cmake::RenameCommand(const char*oldName, const char* newName)
  234. {
  235. // if the command already exists, free the old one
  236. std::string sOldName = cmSystemTools::LowerCase(oldName);
  237. std::string sNewName = cmSystemTools::LowerCase(newName);
  238. RegisteredCommandsMap::iterator pos = this->Commands.find(sOldName);
  239. if ( pos == this->Commands.end() )
  240. {
  241. return;
  242. }
  243. cmCommand* cmd = pos->second;
  244. pos = this->Commands.find(sNewName);
  245. if (pos != this->Commands.end())
  246. {
  247. delete pos->second;
  248. this->Commands.erase(pos);
  249. }
  250. this->Commands.insert(RegisteredCommandsMap::value_type(sNewName, cmd));
  251. pos = this->Commands.find(sOldName);
  252. this->Commands.erase(pos);
  253. }
  254. void cmake::RemoveCommand(const char* name)
  255. {
  256. std::string sName = cmSystemTools::LowerCase(name);
  257. RegisteredCommandsMap::iterator pos = this->Commands.find(sName);
  258. if ( pos != this->Commands.end() )
  259. {
  260. delete pos->second;
  261. this->Commands.erase(pos);
  262. }
  263. }
  264. void cmake::AddCommand(cmCommand* wg)
  265. {
  266. std::string name = cmSystemTools::LowerCase(wg->GetName());
  267. // if the command already exists, free the old one
  268. RegisteredCommandsMap::iterator pos = this->Commands.find(name);
  269. if (pos != this->Commands.end())
  270. {
  271. delete pos->second;
  272. this->Commands.erase(pos);
  273. }
  274. this->Commands.insert( RegisteredCommandsMap::value_type(name, wg));
  275. }
  276. void cmake::RemoveUnscriptableCommands()
  277. {
  278. std::vector<std::string> unscriptableCommands;
  279. cmake::RegisteredCommandsMap* commands = this->GetCommands();
  280. for (cmake::RegisteredCommandsMap::const_iterator pos = commands->begin();
  281. pos != commands->end();
  282. ++pos)
  283. {
  284. if (!pos->second->IsScriptable())
  285. {
  286. unscriptableCommands.push_back(pos->first);
  287. }
  288. }
  289. for(std::vector<std::string>::const_iterator it=unscriptableCommands.begin();
  290. it != unscriptableCommands.end();
  291. ++it)
  292. {
  293. this->RemoveCommand(it->c_str());
  294. }
  295. }
  296. // Parse the args
  297. bool cmake::SetCacheArgs(const std::vector<std::string>& args)
  298. {
  299. for(unsigned int i=1; i < args.size(); ++i)
  300. {
  301. std::string arg = args[i];
  302. if(arg.find("-D",0) == 0)
  303. {
  304. std::string entry = arg.substr(2);
  305. if(entry.size() == 0)
  306. {
  307. ++i;
  308. if(i < args.size())
  309. {
  310. entry = args[i];
  311. }
  312. else
  313. {
  314. cmSystemTools::Error("-D must be followed with VAR=VALUE.");
  315. return false;
  316. }
  317. }
  318. std::string var, value;
  319. cmCacheManager::CacheEntryType type = cmCacheManager::UNINITIALIZED;
  320. if(cmCacheManager::ParseEntry(entry.c_str(), var, value, type) ||
  321. cmCacheManager::ParseEntry(entry.c_str(), var, value))
  322. {
  323. this->CacheManager->AddCacheEntry(var.c_str(), value.c_str(),
  324. "No help, variable specified on the command line.", type);
  325. }
  326. else
  327. {
  328. std::cerr << "Parse error in command line argument: " << arg << "\n"
  329. << "Should be: VAR:type=value\n";
  330. cmSystemTools::Error("No cmake scrpt provided.");
  331. return false;
  332. }
  333. }
  334. else if(arg.find("-U",0) == 0)
  335. {
  336. std::string entryPattern = arg.substr(2);
  337. if(entryPattern.size() == 0)
  338. {
  339. ++i;
  340. if(i < args.size())
  341. {
  342. entryPattern = args[i];
  343. }
  344. else
  345. {
  346. cmSystemTools::Error("-U must be followed with VAR.");
  347. return false;
  348. }
  349. }
  350. cmsys::RegularExpression regex(
  351. cmsys::Glob::PatternToRegex(entryPattern.c_str(), true).c_str());
  352. //go through all cache entries and collect the vars which will be removed
  353. std::vector<std::string> entriesToDelete;
  354. cmCacheManager::CacheIterator it =
  355. this->CacheManager->GetCacheIterator();
  356. for ( it.Begin(); !it.IsAtEnd(); it.Next() )
  357. {
  358. cmCacheManager::CacheEntryType t = it.GetType();
  359. if(t != cmCacheManager::STATIC)
  360. {
  361. std::string entryName = it.GetName();
  362. if (regex.find(entryName.c_str()))
  363. {
  364. entriesToDelete.push_back(entryName);
  365. }
  366. }
  367. }
  368. // now remove them from the cache
  369. for(std::vector<std::string>::const_iterator currentEntry =
  370. entriesToDelete.begin();
  371. currentEntry != entriesToDelete.end();
  372. ++currentEntry)
  373. {
  374. this->CacheManager->RemoveCacheEntry(currentEntry->c_str());
  375. }
  376. }
  377. else if(arg.find("-C",0) == 0)
  378. {
  379. std::string path = arg.substr(2);
  380. if ( path.size() == 0 )
  381. {
  382. ++i;
  383. if(i < args.size())
  384. {
  385. path = args[i];
  386. }
  387. else
  388. {
  389. cmSystemTools::Error("-C must be followed by a file name.");
  390. return false;
  391. }
  392. }
  393. std::cerr << "loading initial cache file " << path.c_str() << "\n";
  394. this->ReadListFile(path.c_str());
  395. }
  396. else if(arg.find("-P",0) == 0)
  397. {
  398. i++;
  399. if(i >= args.size())
  400. {
  401. cmSystemTools::Error("-P must be followed by a file name.");
  402. return false;
  403. }
  404. std::string path = args[i];
  405. if ( path.size() == 0 )
  406. {
  407. cmSystemTools::Error("No cmake script provided.");
  408. return false;
  409. }
  410. this->ReadListFile(path.c_str());
  411. }
  412. }
  413. return true;
  414. }
  415. void cmake::ReadListFile(const char *path)
  416. {
  417. // if a generator was not yet created, temporarily create one
  418. cmGlobalGenerator *gg = this->GetGlobalGenerator();
  419. bool created = false;
  420. // if a generator was not specified use a generic one
  421. if (!gg)
  422. {
  423. gg = new cmGlobalGenerator;
  424. gg->SetCMakeInstance(this);
  425. created = true;
  426. }
  427. // read in the list file to fill the cache
  428. if(path)
  429. {
  430. std::auto_ptr<cmLocalGenerator> lg(gg->CreateLocalGenerator());
  431. lg->SetGlobalGenerator(gg);
  432. lg->GetMakefile()->SetHomeOutputDirectory
  433. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  434. lg->GetMakefile()->SetStartOutputDirectory
  435. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  436. lg->GetMakefile()->SetHomeDirectory
  437. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  438. lg->GetMakefile()->SetStartDirectory
  439. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  440. if (!lg->GetMakefile()->ReadListFile(0, path))
  441. {
  442. cmSystemTools::Error("Error processing file:", path);
  443. }
  444. }
  445. // free generic one if generated
  446. if (created)
  447. {
  448. delete gg;
  449. }
  450. }
  451. // Parse the args
  452. void cmake::SetArgs(const std::vector<std::string>& args)
  453. {
  454. bool directoriesSet = false;
  455. for(unsigned int i=1; i < args.size(); ++i)
  456. {
  457. std::string arg = args[i];
  458. if(arg.find("-H",0) == 0)
  459. {
  460. directoriesSet = true;
  461. std::string path = arg.substr(2);
  462. path = cmSystemTools::CollapseFullPath(path.c_str());
  463. cmSystemTools::ConvertToUnixSlashes(path);
  464. this->SetHomeDirectory(path.c_str());
  465. }
  466. else if(arg.find("-S",0) == 0)
  467. {
  468. // There is no local generate anymore. Ignore -S option.
  469. }
  470. else if(arg.find("-O",0) == 0)
  471. {
  472. // There is no local generate anymore. Ignore -O option.
  473. }
  474. else if(arg.find("-B",0) == 0)
  475. {
  476. directoriesSet = true;
  477. std::string path = arg.substr(2);
  478. path = cmSystemTools::CollapseFullPath(path.c_str());
  479. cmSystemTools::ConvertToUnixSlashes(path);
  480. this->SetHomeOutputDirectory(path.c_str());
  481. }
  482. else if((i < args.size()-1) && (arg.find("--check-build-system",0) == 0))
  483. {
  484. this->CheckBuildSystemArgument = args[++i];
  485. this->ClearBuildSystem = (atoi(args[++i].c_str()) > 0);
  486. }
  487. else if((i < args.size()-1) && (arg.find("--check-stamp-file",0) == 0))
  488. {
  489. this->CheckStampFile = args[++i];
  490. }
  491. #if defined(CMAKE_HAVE_VS_GENERATORS)
  492. else if((i < args.size()-1) && (arg.find("--vs-solution-file",0) == 0))
  493. {
  494. this->VSSolutionFile = args[++i];
  495. }
  496. #endif
  497. else if(arg.find("-V",0) == 0)
  498. {
  499. this->Verbose = true;
  500. }
  501. else if(arg.find("-D",0) == 0)
  502. {
  503. // skip for now
  504. }
  505. else if(arg.find("-U",0) == 0)
  506. {
  507. // skip for now
  508. }
  509. else if(arg.find("-C",0) == 0)
  510. {
  511. // skip for now
  512. }
  513. else if(arg.find("-P",0) == 0)
  514. {
  515. // skip for now
  516. i++;
  517. }
  518. else if(arg.find("--graphviz=",0) == 0)
  519. {
  520. std::string path = arg.substr(strlen("--graphviz="));
  521. path = cmSystemTools::CollapseFullPath(path.c_str());
  522. cmSystemTools::ConvertToUnixSlashes(path);
  523. this->GraphVizFile = path;
  524. if ( this->GraphVizFile.empty() )
  525. {
  526. cmSystemTools::Error("No file specified for --graphviz");
  527. }
  528. }
  529. else if(arg.find("--debug-trycompile",0) == 0)
  530. {
  531. std::cout << "debug trycompile on\n";
  532. this->DebugTryCompileOn();
  533. }
  534. else if(arg.find("--debug-output",0) == 0)
  535. {
  536. std::cout << "Running with debug output on.\n";
  537. this->DebugOutputOn();
  538. }
  539. else if(arg.find("-G",0) == 0)
  540. {
  541. std::string value = arg.substr(2);
  542. if(value.size() == 0)
  543. {
  544. ++i;
  545. if(i >= args.size())
  546. {
  547. cmSystemTools::Error("No generator specified for -G");
  548. return;
  549. }
  550. value = args[i];
  551. }
  552. cmGlobalGenerator* gen =
  553. this->CreateGlobalGenerator(value.c_str());
  554. if(!gen)
  555. {
  556. cmSystemTools::Error("Could not create named generator ",
  557. value.c_str());
  558. }
  559. else
  560. {
  561. this->SetGlobalGenerator(gen);
  562. }
  563. }
  564. // no option assume it is the path to the source
  565. else
  566. {
  567. directoriesSet = true;
  568. this->SetDirectoriesFromFile(arg.c_str());
  569. }
  570. }
  571. if(!directoriesSet)
  572. {
  573. this->SetHomeOutputDirectory
  574. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  575. this->SetStartOutputDirectory
  576. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  577. this->SetHomeDirectory
  578. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  579. this->SetStartDirectory
  580. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  581. }
  582. this->SetStartDirectory(this->GetHomeDirectory());
  583. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  584. }
  585. //----------------------------------------------------------------------------
  586. void cmake::SetDirectoriesFromFile(const char* arg)
  587. {
  588. // Check if the argument refers to a CMakeCache.txt or
  589. // CMakeLists.txt file.
  590. std::string listPath;
  591. std::string cachePath;
  592. bool argIsFile = false;
  593. if(cmSystemTools::FileIsDirectory(arg))
  594. {
  595. std::string path = cmSystemTools::CollapseFullPath(arg);
  596. cmSystemTools::ConvertToUnixSlashes(path);
  597. std::string cacheFile = path;
  598. cacheFile += "/CMakeCache.txt";
  599. std::string listFile = path;
  600. listFile += "/CMakeLists.txt";
  601. if(cmSystemTools::FileExists(cacheFile.c_str()))
  602. {
  603. cachePath = path;
  604. }
  605. if(cmSystemTools::FileExists(listFile.c_str()))
  606. {
  607. listPath = path;
  608. }
  609. }
  610. else if(cmSystemTools::FileExists(arg))
  611. {
  612. argIsFile = true;
  613. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  614. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  615. name = cmSystemTools::LowerCase(name);
  616. if(name == "cmakecache.txt")
  617. {
  618. cachePath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  619. }
  620. else if(name == "cmakelists.txt")
  621. {
  622. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  623. }
  624. }
  625. else
  626. {
  627. // Specified file or directory does not exist. Try to set things
  628. // up to produce a meaningful error message.
  629. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  630. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  631. name = cmSystemTools::LowerCase(name);
  632. if(name == "cmakecache.txt" || name == "cmakelists.txt")
  633. {
  634. argIsFile = true;
  635. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  636. }
  637. else
  638. {
  639. listPath = fullPath;
  640. }
  641. }
  642. // If there is a CMakeCache.txt file, use its settings.
  643. if(cachePath.length() > 0)
  644. {
  645. cmCacheManager* cachem = this->GetCacheManager();
  646. cmCacheManager::CacheIterator it = cachem->NewIterator();
  647. if(cachem->LoadCache(cachePath.c_str()) &&
  648. it.Find("CMAKE_HOME_DIRECTORY"))
  649. {
  650. this->SetHomeOutputDirectory(cachePath.c_str());
  651. this->SetStartOutputDirectory(cachePath.c_str());
  652. this->SetHomeDirectory(it.GetValue());
  653. this->SetStartDirectory(it.GetValue());
  654. return;
  655. }
  656. }
  657. // If there is a CMakeLists.txt file, use it as the source tree.
  658. if(listPath.length() > 0)
  659. {
  660. this->SetHomeDirectory(listPath.c_str());
  661. this->SetStartDirectory(listPath.c_str());
  662. if(argIsFile)
  663. {
  664. // Source CMakeLists.txt file given. It was probably dropped
  665. // onto the executable in a GUI. Default to an in-source build.
  666. this->SetHomeOutputDirectory(listPath.c_str());
  667. this->SetStartOutputDirectory(listPath.c_str());
  668. }
  669. else
  670. {
  671. // Source directory given on command line. Use current working
  672. // directory as build tree.
  673. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  674. this->SetHomeOutputDirectory(cwd.c_str());
  675. this->SetStartOutputDirectory(cwd.c_str());
  676. }
  677. return;
  678. }
  679. // We didn't find a CMakeLists.txt or CMakeCache.txt file from the
  680. // argument. Assume it is the path to the source tree, and use the
  681. // current working directory as the build tree.
  682. std::string full = cmSystemTools::CollapseFullPath(arg);
  683. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  684. this->SetHomeDirectory(full.c_str());
  685. this->SetStartDirectory(full.c_str());
  686. this->SetHomeOutputDirectory(cwd.c_str());
  687. this->SetStartOutputDirectory(cwd.c_str());
  688. }
  689. // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the
  690. // cache
  691. int cmake::AddCMakePaths()
  692. {
  693. // Find the cmake executable
  694. std::string cMakeSelf = cmSystemTools::GetExecutableDirectory();
  695. cMakeSelf += "/cmake";
  696. cMakeSelf += cmSystemTools::GetExecutableExtension();
  697. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  698. {
  699. cmSystemTools::Error("CMake executable cannot be found at ",
  700. cMakeSelf.c_str());
  701. return 0;
  702. }
  703. // Save the value in the cache
  704. this->CacheManager->AddCacheEntry
  705. ("CMAKE_COMMAND",cMakeSelf.c_str(), "Path to CMake executable.",
  706. cmCacheManager::INTERNAL);
  707. // if the edit command is not yet in the cache,
  708. // or if CMakeEditCommand has been set on this object,
  709. // then set the CMAKE_EDIT_COMMAND in the cache
  710. // This will mean that the last gui to edit the cache
  711. // will be the one that make edit_cache uses.
  712. if(!this->GetCacheDefinition("CMAKE_EDIT_COMMAND")
  713. || !this->CMakeEditCommand.empty())
  714. {
  715. // Find and save the command to edit the cache
  716. std::string editCacheCommand;
  717. if(!this->CMakeEditCommand.empty())
  718. {
  719. editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf)
  720. + std::string("/")
  721. + this->CMakeEditCommand
  722. + cmSystemTools::GetFilenameExtension(cMakeSelf);
  723. }
  724. if( !cmSystemTools::FileExists(editCacheCommand.c_str()))
  725. {
  726. editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  727. "/ccmake" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  728. }
  729. if( !cmSystemTools::FileExists(editCacheCommand.c_str()))
  730. {
  731. editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  732. "/CMakeSetup" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  733. }
  734. if(cmSystemTools::FileExists(editCacheCommand.c_str()))
  735. {
  736. this->CacheManager->AddCacheEntry
  737. ("CMAKE_EDIT_COMMAND", editCacheCommand.c_str(),
  738. "Path to cache edit program executable.", cmCacheManager::INTERNAL);
  739. }
  740. }
  741. std::string ctestCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  742. "/ctest" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  743. if(cmSystemTools::FileExists(ctestCommand.c_str()))
  744. {
  745. this->CacheManager->AddCacheEntry
  746. ("CMAKE_CTEST_COMMAND", ctestCommand.c_str(),
  747. "Path to ctest program executable.", cmCacheManager::INTERNAL);
  748. }
  749. std::string cpackCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  750. "/cpack" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  751. if(cmSystemTools::FileExists(ctestCommand.c_str()))
  752. {
  753. this->CacheManager->AddCacheEntry
  754. ("CMAKE_CPACK_COMMAND", cpackCommand.c_str(),
  755. "Path to cpack program executable.", cmCacheManager::INTERNAL);
  756. }
  757. // do CMAKE_ROOT, look for the environment variable first
  758. std::string cMakeRoot;
  759. std::string modules;
  760. if (getenv("CMAKE_ROOT"))
  761. {
  762. cMakeRoot = getenv("CMAKE_ROOT");
  763. modules = cMakeRoot + "/Modules/CMake.cmake";
  764. }
  765. if(!cmSystemTools::FileExists(modules.c_str()))
  766. {
  767. // next try exe/..
  768. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  769. std::string::size_type slashPos = cMakeRoot.rfind("/");
  770. if(slashPos != std::string::npos)
  771. {
  772. cMakeRoot = cMakeRoot.substr(0, slashPos);
  773. }
  774. // is there no Modules direcory there?
  775. modules = cMakeRoot + "/Modules/CMake.cmake";
  776. }
  777. if (!cmSystemTools::FileExists(modules.c_str()))
  778. {
  779. // try exe/../share/cmake
  780. cMakeRoot += CMAKE_DATA_DIR;
  781. modules = cMakeRoot + "/Modules/CMake.cmake";
  782. }
  783. #ifdef CMAKE_ROOT_DIR
  784. if (!cmSystemTools::FileExists(modules.c_str()))
  785. {
  786. // try compiled in root directory
  787. cMakeRoot = CMAKE_ROOT_DIR;
  788. modules = cMakeRoot + "/Modules/CMake.cmake";
  789. }
  790. #endif
  791. if (!cmSystemTools::FileExists(modules.c_str()))
  792. {
  793. // try
  794. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  795. cMakeRoot += CMAKE_DATA_DIR;
  796. modules = cMakeRoot + "/Modules/CMake.cmake";
  797. }
  798. if(!cmSystemTools::FileExists(modules.c_str()))
  799. {
  800. // next try exe
  801. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  802. // is there no Modules direcory there?
  803. modules = cMakeRoot + "/Modules/CMake.cmake";
  804. }
  805. if (!cmSystemTools::FileExists(modules.c_str()))
  806. {
  807. // couldn't find modules
  808. cmSystemTools::Error("Could not find CMAKE_ROOT !!!\n"
  809. "CMake has most likely not been installed correctly.\n"
  810. "Modules directory not found in\n",
  811. cMakeRoot.c_str());
  812. return 0;
  813. }
  814. this->CacheManager->AddCacheEntry
  815. ("CMAKE_ROOT", cMakeRoot.c_str(),
  816. "Path to CMake installation.", cmCacheManager::INTERNAL);
  817. #ifdef _WIN32
  818. std::string comspec = "cmw9xcom.exe";
  819. cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
  820. #endif
  821. return 1;
  822. }
  823. void CMakeCommandUsage(const char* program)
  824. {
  825. cmOStringStream errorStream;
  826. #ifdef CMAKE_BUILD_WITH_CMAKE
  827. errorStream
  828. << "cmake version " << cmVersion::GetCMakeVersion() << "\n";
  829. #else
  830. errorStream
  831. << "cmake bootstrap\n";
  832. #endif
  833. errorStream
  834. << "Usage: " << program << " -E [command] [arguments ...]\n"
  835. << "Available commands: \n"
  836. << " chdir dir cmd [args]... - run command in a given directory\n"
  837. << " copy file destination - copy file to destination (either file "
  838. "or directory)\n"
  839. << " copy_if_different in-file out-file - copy file if input has "
  840. "changed\n"
  841. << " copy_directory source destination - copy directory 'source' "
  842. "content to directory 'destination'\n"
  843. << " compare_files file1 file2 - check if file1 is same as file2\n"
  844. << " echo [string]... - displays arguments as text\n"
  845. << " echo_append [string]... - displays arguments as text but no new "
  846. "line\n"
  847. << " environment - display the current enviroment\n"
  848. << " make_directory dir - create a directory\n"
  849. << " md5sum file1 [...] - compute md5sum of files\n"
  850. << " remove_directory dir - remove a directory and its contents\n"
  851. << " remove [-f] file1 file2 ... - remove the file(s), use -f to force "
  852. "it\n"
  853. << " tar [cxt][vfz] file.tar file/dir1 file/dir2 ... - create a tar "
  854. "archive\n"
  855. << " time command [args] ... - run command and return elapsed time\n"
  856. << " touch file - touch a file.\n"
  857. << " touch_nocreate file - touch a file but do not create it.\n"
  858. #if defined(_WIN32) && !defined(__CYGWIN__)
  859. << " write_regv key value - write registry value\n"
  860. << " delete_regv key - delete registry value\n"
  861. << " comspec - on windows 9x use this for RunCommand\n"
  862. #else
  863. << " create_symlink old new - create a symbolic link new -> old\n"
  864. #endif
  865. ;
  866. cmSystemTools::Error(errorStream.str().c_str());
  867. }
  868. int cmake::ExecuteCMakeCommand(std::vector<std::string>& args)
  869. {
  870. if (args.size() > 1)
  871. {
  872. // Copy file
  873. if (args[1] == "copy" && args.size() == 4)
  874. {
  875. if(!cmSystemTools::cmCopyFile(args[2].c_str(), args[3].c_str()))
  876. {
  877. std::cerr << "Error copying file \"" << args[2].c_str()
  878. << "\" to \"" << args[3].c_str() << "\".\n";
  879. return 1;
  880. }
  881. return 0;
  882. }
  883. // Copy file if different.
  884. if (args[1] == "copy_if_different" && args.size() == 4)
  885. {
  886. if(!cmSystemTools::CopyFileIfDifferent(args[2].c_str(),
  887. args[3].c_str()))
  888. {
  889. std::cerr << "Error copying file (if different) from \""
  890. << args[2].c_str() << "\" to \"" << args[3].c_str()
  891. << "\".\n";
  892. return 1;
  893. }
  894. return 0;
  895. }
  896. // Copy directory content
  897. if (args[1] == "copy_directory" && args.size() == 4)
  898. {
  899. if(!cmSystemTools::CopyADirectory(args[2].c_str(), args[3].c_str()))
  900. {
  901. std::cerr << "Error copying directory from \""
  902. << args[2].c_str() << "\" to \"" << args[3].c_str()
  903. << "\".\n";
  904. return 1;
  905. }
  906. return 0;
  907. }
  908. // Compare files
  909. if (args[1] == "compare_files" && args.size() == 4)
  910. {
  911. if(cmSystemTools::FilesDiffer(args[2].c_str(), args[3].c_str()))
  912. {
  913. std::cerr << "Files \""
  914. << args[2].c_str() << "\" to \"" << args[3].c_str()
  915. << "\" are different.\n";
  916. return 1;
  917. }
  918. return 0;
  919. }
  920. // Echo string
  921. else if (args[1] == "echo" )
  922. {
  923. unsigned int cc;
  924. const char* space = "";
  925. for ( cc = 2; cc < args.size(); cc ++ )
  926. {
  927. std::cout << space << args[cc];
  928. space = " ";
  929. }
  930. std::cout << std::endl;
  931. return 0;
  932. }
  933. // Echo string no new line
  934. else if (args[1] == "echo_append" )
  935. {
  936. unsigned int cc;
  937. const char* space = "";
  938. for ( cc = 2; cc < args.size(); cc ++ )
  939. {
  940. std::cout << space << args[cc];
  941. space = " ";
  942. }
  943. return 0;
  944. }
  945. #if defined(CMAKE_BUILD_WITH_CMAKE)
  946. // Command to create a symbolic link. Fails on platforms not
  947. // supporting them.
  948. else if (args[1] == "environment" )
  949. {
  950. std::vector<std::string> env = cmSystemTools::GetEnvironmentVariables();
  951. std::vector<std::string>::iterator it;
  952. for ( it = env.begin(); it != env.end(); ++ it )
  953. {
  954. std::cout << it->c_str() << std::endl;
  955. }
  956. return 0;
  957. }
  958. #endif
  959. else if (args[1] == "make_directory" && args.size() == 3)
  960. {
  961. if(!cmSystemTools::MakeDirectory(args[2].c_str()))
  962. {
  963. std::cerr << "Error making directory \"" << args[2].c_str()
  964. << "\".\n";
  965. return 1;
  966. }
  967. return 0;
  968. }
  969. else if (args[1] == "remove_directory" && args.size() == 3)
  970. {
  971. if(cmSystemTools::FileIsDirectory(args[2].c_str()) &&
  972. !cmSystemTools::RemoveADirectory(args[2].c_str()))
  973. {
  974. std::cerr << "Error removing directory \"" << args[2].c_str()
  975. << "\".\n";
  976. return 1;
  977. }
  978. return 0;
  979. }
  980. // Remove file
  981. else if (args[1] == "remove" && args.size() > 2)
  982. {
  983. bool force = false;
  984. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  985. {
  986. if(args[cc] == "\\-f" || args[cc] == "-f")
  987. {
  988. force = true;
  989. }
  990. else
  991. {
  992. // Complain if the file could not be removed, still exists,
  993. // and the -f option was not given.
  994. if(!cmSystemTools::RemoveFile(args[cc].c_str()) && !force &&
  995. cmSystemTools::FileExists(args[cc].c_str()))
  996. {
  997. return 1;
  998. }
  999. }
  1000. }
  1001. return 0;
  1002. }
  1003. // Touch file
  1004. else if (args[1] == "touch" && args.size() > 2)
  1005. {
  1006. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  1007. {
  1008. // Complain if the file could not be removed, still exists,
  1009. // and the -f option was not given.
  1010. if(!cmSystemTools::Touch(args[cc].c_str(), true))
  1011. {
  1012. return 1;
  1013. }
  1014. }
  1015. return 0;
  1016. }
  1017. // Touch file
  1018. else if (args[1] == "touch_nocreate" && args.size() > 2)
  1019. {
  1020. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  1021. {
  1022. // Complain if the file could not be removed, still exists,
  1023. // and the -f option was not given.
  1024. if(!cmSystemTools::Touch(args[cc].c_str(), false))
  1025. {
  1026. return 1;
  1027. }
  1028. }
  1029. return 0;
  1030. }
  1031. // Clock command
  1032. else if (args[1] == "time" && args.size() > 2)
  1033. {
  1034. std::string command = args[2];
  1035. for (std::string::size_type cc = 3; cc < args.size(); cc ++)
  1036. {
  1037. command += " ";
  1038. command += args[cc];
  1039. }
  1040. clock_t clock_start, clock_finish;
  1041. time_t time_start, time_finish;
  1042. time(&time_start);
  1043. clock_start = clock();
  1044. cmSystemTools::RunSingleCommand(command.c_str());
  1045. clock_finish = clock();
  1046. time(&time_finish);
  1047. double clocks_per_sec = static_cast<double>(CLOCKS_PER_SEC);
  1048. std::cout << "Elapsed time: "
  1049. << static_cast<long>(time_finish - time_start) << " s. (time)"
  1050. << ", "
  1051. << static_cast<double>(clock_finish - clock_start) / clocks_per_sec
  1052. << " s. (clock)"
  1053. << "\n";
  1054. return 0;
  1055. }
  1056. // Command to calculate the md5sum of a file
  1057. else if (args[1] == "md5sum" && args.size() >= 3)
  1058. {
  1059. char md5out[32];
  1060. int retval = 0;
  1061. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  1062. {
  1063. const char *filename = args[cc].c_str();
  1064. // Cannot compute md5sum of a directory
  1065. if(cmSystemTools::FileIsDirectory(filename))
  1066. {
  1067. std::cerr << "Error: " << filename << " is a directory" << std::endl;
  1068. retval++;
  1069. }
  1070. else if(!cmSystemTools::ComputeFileMD5(filename, md5out))
  1071. {
  1072. // To mimic md5sum behavior in a shell:
  1073. std::cerr << filename << ": No such file or directory" << std::endl;
  1074. retval++;
  1075. }
  1076. else
  1077. {
  1078. std::cout << std::string(md5out,32) << " " << filename << std::endl;
  1079. }
  1080. }
  1081. return retval;
  1082. }
  1083. // Command to change directory and run a program.
  1084. else if (args[1] == "chdir" && args.size() >= 4)
  1085. {
  1086. std::string directory = args[2];
  1087. if(!cmSystemTools::FileExists(directory.c_str()))
  1088. {
  1089. cmSystemTools::Error("Directory does not exist for chdir command: ",
  1090. args[2].c_str());
  1091. return 0;
  1092. }
  1093. std::string command = "\"";
  1094. command += args[3];
  1095. command += "\"";
  1096. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  1097. {
  1098. command += " \"";
  1099. command += args[cc];
  1100. command += "\"";
  1101. }
  1102. int retval = 0;
  1103. int timeout = 0;
  1104. if ( cmSystemTools::RunSingleCommand(command.c_str(), 0, &retval,
  1105. directory.c_str(), true, timeout) )
  1106. {
  1107. return retval;
  1108. }
  1109. return 1;
  1110. }
  1111. // Command to start progress for a build
  1112. else if (args[1] == "cmake_progress_start" && args.size() == 4)
  1113. {
  1114. // bascially remove the directory
  1115. std::string dirName = args[2];
  1116. dirName += "/Progress";
  1117. cmSystemTools::RemoveADirectory(dirName.c_str());
  1118. // is the last argument a filename that exists?
  1119. FILE *countFile = fopen(args[3].c_str(),"r");
  1120. int count;
  1121. if (countFile)
  1122. {
  1123. fscanf(countFile,"%i",&count);
  1124. fclose(countFile);
  1125. }
  1126. else
  1127. {
  1128. count = atoi(args[3].c_str());
  1129. }
  1130. if (count)
  1131. {
  1132. cmSystemTools::MakeDirectory(dirName.c_str());
  1133. // write the count into the directory
  1134. std::string fName = dirName;
  1135. fName += "/count.txt";
  1136. FILE *progFile = fopen(fName.c_str(),"w");
  1137. if (progFile)
  1138. {
  1139. fprintf(progFile,"%i\n",count);
  1140. fclose(progFile);
  1141. }
  1142. }
  1143. return 0;
  1144. }
  1145. // Command to report progress for a build
  1146. else if (args[1] == "cmake_progress_report" && args.size() >= 3)
  1147. {
  1148. std::string dirName = args[2];
  1149. dirName += "/Progress";
  1150. std::string fName;
  1151. FILE *progFile;
  1152. // read the count
  1153. fName = dirName;
  1154. fName += "/count.txt";
  1155. progFile = fopen(fName.c_str(),"r");
  1156. int count = 0;
  1157. if (!progFile)
  1158. {
  1159. return 0;
  1160. }
  1161. else
  1162. {
  1163. fscanf(progFile,"%i",&count);
  1164. fclose(progFile);
  1165. }
  1166. unsigned int i;
  1167. for (i = 3; i < args.size(); ++i)
  1168. {
  1169. fName = dirName;
  1170. fName += "/";
  1171. fName += args[i];
  1172. progFile = fopen(fName.c_str(),"w");
  1173. if (progFile)
  1174. {
  1175. fprintf(progFile,"empty");
  1176. fclose(progFile);
  1177. }
  1178. }
  1179. int fileNum = static_cast<int>
  1180. (cmsys::Directory::GetNumberOfFilesInDirectory(dirName.c_str()));
  1181. if (count > 0)
  1182. {
  1183. // print the progress
  1184. fprintf(stdout,"[%3i%%] ",((fileNum-3)*100)/count);
  1185. }
  1186. return 0;
  1187. }
  1188. // Command to create a symbolic link. Fails on platforms not
  1189. // supporting them.
  1190. else if (args[1] == "create_symlink" && args.size() == 4)
  1191. {
  1192. const char* destinationFileName = args[3].c_str();
  1193. if ( cmSystemTools::FileExists(destinationFileName) )
  1194. {
  1195. if ( cmSystemTools::FileIsSymlink(destinationFileName) )
  1196. {
  1197. if ( !cmSystemTools::RemoveFile(destinationFileName) ||
  1198. cmSystemTools::FileExists(destinationFileName) )
  1199. {
  1200. return 0;
  1201. }
  1202. }
  1203. else
  1204. {
  1205. return 0;
  1206. }
  1207. }
  1208. return cmSystemTools::CreateSymlink(args[2].c_str(),
  1209. args[3].c_str())? 0:1;
  1210. }
  1211. // Internal CMake shared library support.
  1212. else if (args[1] == "cmake_symlink_library" && args.size() == 5)
  1213. {
  1214. int result = 0;
  1215. std::string realName = args[2];
  1216. std::string soName = args[3];
  1217. std::string name = args[4];
  1218. if(soName != realName)
  1219. {
  1220. std::string fname = cmSystemTools::GetFilenameName(realName);
  1221. if(cmSystemTools::FileExists(soName.c_str()))
  1222. {
  1223. cmSystemTools::RemoveFile(soName.c_str());
  1224. }
  1225. if(!cmSystemTools::CreateSymlink(fname.c_str(), soName.c_str()))
  1226. {
  1227. result = 1;
  1228. }
  1229. }
  1230. if(name != soName)
  1231. {
  1232. std::string fname = cmSystemTools::GetFilenameName(soName);
  1233. if(cmSystemTools::FileExists(soName.c_str()))
  1234. {
  1235. cmSystemTools::RemoveFile(name.c_str());
  1236. }
  1237. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  1238. {
  1239. result = 1;
  1240. }
  1241. }
  1242. return result;
  1243. }
  1244. // Internal CMake versioned executable support.
  1245. else if (args[1] == "cmake_symlink_executable" && args.size() == 4)
  1246. {
  1247. int result = 0;
  1248. std::string realName = args[2];
  1249. std::string name = args[3];
  1250. if(name != realName)
  1251. {
  1252. std::string fname = cmSystemTools::GetFilenameName(realName);
  1253. if(cmSystemTools::FileExists(realName.c_str()))
  1254. {
  1255. cmSystemTools::RemoveFile(name.c_str());
  1256. }
  1257. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  1258. {
  1259. result = 1;
  1260. }
  1261. }
  1262. return result;
  1263. }
  1264. #if defined(CMAKE_HAVE_VS_GENERATORS)
  1265. // Internal CMake support for calling Visual Studio macros.
  1266. else if (args[1] == "cmake_call_visual_studio_macro" && args.size() >= 4)
  1267. {
  1268. // args[2] = full path to .sln file or "ALL"
  1269. // args[3] = name of Visual Studio macro to call
  1270. // args[4..args.size()-1] = [optional] args for Visual Studio macro
  1271. std::string macroArgs;
  1272. if (args.size() > 4)
  1273. {
  1274. macroArgs = args[4];
  1275. for (size_t i = 5; i < args.size(); ++i)
  1276. {
  1277. macroArgs += " ";
  1278. macroArgs += args[i];
  1279. }
  1280. }
  1281. return cmCallVisualStudioMacro::CallMacro(args[2], args[3], macroArgs);
  1282. }
  1283. #endif
  1284. // Internal CMake dependency scanning support.
  1285. else if (args[1] == "cmake_depends" && args.size() >= 6)
  1286. {
  1287. // Use the make system's VERBOSE environment variable to enable
  1288. // verbose output.
  1289. bool verbose = cmSystemTools::GetEnv("VERBOSE") != 0;
  1290. // Create a cmake object instance to process dependencies.
  1291. cmake cm;
  1292. std::string gen;
  1293. std::string homeDir;
  1294. std::string startDir;
  1295. std::string homeOutDir;
  1296. std::string startOutDir;
  1297. std::string depInfo;
  1298. bool color = true;
  1299. if(args.size() >= 8)
  1300. {
  1301. // Full signature:
  1302. //
  1303. // -E cmake_depends <generator>
  1304. // <home-src-dir> <start-src-dir>
  1305. // <home-out-dir> <start-out-dir>
  1306. // <dep-info> [--color=$(COLOR)]
  1307. //
  1308. // All paths are provided.
  1309. gen = args[2];
  1310. homeDir = args[3];
  1311. startDir = args[4];
  1312. homeOutDir = args[5];
  1313. startOutDir = args[6];
  1314. depInfo = args[7];
  1315. if(args.size() >= 9 &&
  1316. args[8].length() > 8 &&
  1317. args[8].substr(0, 8) == "--color=")
  1318. {
  1319. // Enable or disable color based on the switch value.
  1320. color = cmSystemTools::IsOn(args[8].substr(8).c_str());
  1321. }
  1322. }
  1323. else
  1324. {
  1325. // Support older signature for existing makefiles:
  1326. //
  1327. // -E cmake_depends <generator>
  1328. // <home-out-dir> <start-out-dir>
  1329. // <dep-info>
  1330. //
  1331. // Just pretend the source directories are the same as the
  1332. // binary directories so at least scanning will work.
  1333. gen = args[2];
  1334. homeDir = args[3];
  1335. startDir = args[4];
  1336. homeOutDir = args[3];
  1337. startOutDir = args[3];
  1338. depInfo = args[5];
  1339. }
  1340. // Create a local generator configured for the directory in
  1341. // which dependencies will be scanned.
  1342. homeDir = cmSystemTools::CollapseFullPath(homeDir.c_str());
  1343. startDir = cmSystemTools::CollapseFullPath(startDir.c_str());
  1344. homeOutDir = cmSystemTools::CollapseFullPath(homeOutDir.c_str());
  1345. startOutDir = cmSystemTools::CollapseFullPath(startOutDir.c_str());
  1346. cm.SetHomeDirectory(homeDir.c_str());
  1347. cm.SetStartDirectory(startDir.c_str());
  1348. cm.SetHomeOutputDirectory(homeOutDir.c_str());
  1349. cm.SetStartOutputDirectory(startOutDir.c_str());
  1350. if(cmGlobalGenerator* ggd = cm.CreateGlobalGenerator(gen.c_str()))
  1351. {
  1352. cm.SetGlobalGenerator(ggd);
  1353. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  1354. lgd->SetGlobalGenerator(ggd);
  1355. lgd->GetMakefile()->SetStartDirectory(startDir.c_str());
  1356. lgd->GetMakefile()->SetStartOutputDirectory(startOutDir.c_str());
  1357. lgd->GetMakefile()->MakeStartDirectoriesCurrent();
  1358. // Actually scan dependencies.
  1359. return lgd->UpdateDependencies(depInfo.c_str(),
  1360. verbose, color)? 0 : 2;
  1361. }
  1362. return 1;
  1363. }
  1364. // Internal CMake link script support.
  1365. else if (args[1] == "cmake_link_script" && args.size() >= 3)
  1366. {
  1367. return cmake::ExecuteLinkScript(args);
  1368. }
  1369. // Internal CMake unimplemented feature notification.
  1370. else if (args[1] == "cmake_unimplemented_variable")
  1371. {
  1372. std::cerr << "Feature not implemented for this platform.";
  1373. if(args.size() == 3)
  1374. {
  1375. std::cerr << " Variable " << args[2] << " is not set.";
  1376. }
  1377. std::cerr << std::endl;
  1378. return 1;
  1379. }
  1380. else if (args[1] == "vs_link_exe")
  1381. {
  1382. return cmake::VisualStudioLink(args, 1);
  1383. }
  1384. else if (args[1] == "vs_link_dll")
  1385. {
  1386. return cmake::VisualStudioLink(args, 2);
  1387. }
  1388. #ifdef CMAKE_BUILD_WITH_CMAKE
  1389. // Internal CMake color makefile support.
  1390. else if (args[1] == "cmake_echo_color")
  1391. {
  1392. return cmake::ExecuteEchoColor(args);
  1393. }
  1394. #endif
  1395. // Tar files
  1396. else if (args[1] == "tar" && args.size() > 3)
  1397. {
  1398. std::string flags = args[2];
  1399. std::string outFile = args[3];
  1400. std::vector<cmStdString> files;
  1401. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  1402. {
  1403. files.push_back(args[cc]);
  1404. }
  1405. bool gzip = false;
  1406. bool verbose = false;
  1407. if ( flags.find_first_of('z') != flags.npos )
  1408. {
  1409. gzip = true;
  1410. }
  1411. if ( flags.find_first_of('v') != flags.npos )
  1412. {
  1413. verbose = true;
  1414. }
  1415. if ( flags.find_first_of('t') != flags.npos )
  1416. {
  1417. if ( !cmSystemTools::ListTar(outFile.c_str(), files, gzip, verbose) )
  1418. {
  1419. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  1420. return 1;
  1421. }
  1422. }
  1423. else if ( flags.find_first_of('c') != flags.npos )
  1424. {
  1425. if ( !cmSystemTools::CreateTar(
  1426. outFile.c_str(), files, gzip, verbose) )
  1427. {
  1428. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  1429. return 1;
  1430. }
  1431. }
  1432. else if ( flags.find_first_of('x') != flags.npos )
  1433. {
  1434. if ( !cmSystemTools::ExtractTar(
  1435. outFile.c_str(), files, gzip, verbose) )
  1436. {
  1437. cmSystemTools::Error("Problem extracting tar: ", outFile.c_str());
  1438. return 1;
  1439. }
  1440. }
  1441. return 0;
  1442. }
  1443. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1444. // Internal CMake Fortran module support.
  1445. else if (args[1] == "cmake_copy_f90_mod" && args.size() >= 4)
  1446. {
  1447. return cmDependsFortran::CopyModule(args)? 0 : 1;
  1448. }
  1449. #endif
  1450. #if defined(_WIN32) && !defined(__CYGWIN__)
  1451. // Write registry value
  1452. else if (args[1] == "write_regv" && args.size() > 3)
  1453. {
  1454. return cmSystemTools::WriteRegistryValue(args[2].c_str(),
  1455. args[3].c_str()) ? 0 : 1;
  1456. }
  1457. // Delete registry value
  1458. else if (args[1] == "delete_regv" && args.size() > 2)
  1459. {
  1460. return cmSystemTools::DeleteRegistryValue(args[2].c_str()) ? 0 : 1;
  1461. }
  1462. // Remove file
  1463. else if (args[1] == "comspec" && args.size() > 2)
  1464. {
  1465. unsigned int cc;
  1466. std::string command = args[2];
  1467. for ( cc = 3; cc < args.size(); cc ++ )
  1468. {
  1469. command += " " + args[cc];
  1470. }
  1471. return cmWin32ProcessExecution::Windows9xHack(command.c_str());
  1472. }
  1473. #endif
  1474. }
  1475. ::CMakeCommandUsage(args[0].c_str());
  1476. return 1;
  1477. }
  1478. void cmake::AddExtraGenerator(const char* name,
  1479. CreateExtraGeneratorFunctionType newFunction)
  1480. {
  1481. cmExternalMakefileProjectGenerator* extraGenerator = newFunction();
  1482. const std::vector<std::string>& supportedGlobalGenerators =
  1483. extraGenerator->GetSupportedGlobalGenerators();
  1484. for(std::vector<std::string>::const_iterator
  1485. it = supportedGlobalGenerators.begin();
  1486. it != supportedGlobalGenerators.end();
  1487. ++it )
  1488. {
  1489. std::string fullName = cmExternalMakefileProjectGenerator::
  1490. CreateFullGeneratorName(it->c_str(), name);
  1491. this->ExtraGenerators[fullName.c_str()] = newFunction;
  1492. }
  1493. delete extraGenerator;
  1494. }
  1495. void cmake::AddDefaultExtraGenerators()
  1496. {
  1497. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1498. #if defined(_WIN32) && !defined(__CYGWIN__)
  1499. // e.g. kdevelop4 ?
  1500. #endif
  1501. #if !defined(__CYGWIN__)
  1502. this->AddExtraGenerator(cmExtraCodeBlocksGenerator::GetActualName(),
  1503. &cmExtraCodeBlocksGenerator::New);
  1504. #endif
  1505. #ifdef CMAKE_USE_ECLIPSE
  1506. this->AddExtraGenerator(cmExtraEclipseCDT4Generator::GetActualName(),
  1507. &cmExtraEclipseCDT4Generator::New);
  1508. #endif
  1509. #ifdef CMAKE_USE_KDEVELOP
  1510. this->AddExtraGenerator(cmGlobalKdevelopGenerator::GetActualName(),
  1511. &cmGlobalKdevelopGenerator::New);
  1512. // for kdevelop also add the generator with just the name of the
  1513. // extra generator, since it was this way since cmake 2.2
  1514. this->ExtraGenerators[cmGlobalKdevelopGenerator::GetActualName()]
  1515. = &cmGlobalKdevelopGenerator::New;
  1516. #endif
  1517. #endif
  1518. }
  1519. //----------------------------------------------------------------------------
  1520. void cmake::GetRegisteredGenerators(std::vector<std::string>& names)
  1521. {
  1522. for(RegisteredGeneratorsMap::const_iterator i = this->Generators.begin();
  1523. i != this->Generators.end(); ++i)
  1524. {
  1525. names.push_back(i->first);
  1526. }
  1527. for(RegisteredExtraGeneratorsMap::const_iterator
  1528. i = this->ExtraGenerators.begin();
  1529. i != this->ExtraGenerators.end(); ++i)
  1530. {
  1531. names.push_back(i->first);
  1532. }
  1533. }
  1534. cmGlobalGenerator* cmake::CreateGlobalGenerator(const char* name)
  1535. {
  1536. cmGlobalGenerator* generator = 0;
  1537. cmExternalMakefileProjectGenerator* extraGenerator = 0;
  1538. RegisteredGeneratorsMap::const_iterator genIt = this->Generators.find(name);
  1539. if(genIt == this->Generators.end())
  1540. {
  1541. RegisteredExtraGeneratorsMap::const_iterator extraGenIt =
  1542. this->ExtraGenerators.find(name);
  1543. if (extraGenIt == this->ExtraGenerators.end())
  1544. {
  1545. return 0;
  1546. }
  1547. extraGenerator = (extraGenIt->second)();
  1548. genIt=this->Generators.find(extraGenerator->GetGlobalGeneratorName(name));
  1549. if(genIt == this->Generators.end())
  1550. {
  1551. delete extraGenerator;
  1552. return 0;
  1553. }
  1554. }
  1555. generator = (genIt->second)();
  1556. generator->SetCMakeInstance(this);
  1557. generator->SetExternalMakefileProjectGenerator(extraGenerator);
  1558. return generator;
  1559. }
  1560. void cmake::SetHomeDirectory(const char* dir)
  1561. {
  1562. this->cmHomeDirectory = dir;
  1563. cmSystemTools::ConvertToUnixSlashes(this->cmHomeDirectory);
  1564. }
  1565. void cmake::SetHomeOutputDirectory(const char* lib)
  1566. {
  1567. this->HomeOutputDirectory = lib;
  1568. cmSystemTools::ConvertToUnixSlashes(this->HomeOutputDirectory);
  1569. }
  1570. void cmake::SetGlobalGenerator(cmGlobalGenerator *gg)
  1571. {
  1572. if(!gg)
  1573. {
  1574. cmSystemTools::Error("Error SetGlobalGenerator called with null");
  1575. return;
  1576. }
  1577. // delete the old generator
  1578. if (this->GlobalGenerator)
  1579. {
  1580. delete this->GlobalGenerator;
  1581. // restore the original environment variables CXX and CC
  1582. // Restor CC
  1583. std::string env = "CC=";
  1584. if(this->CCEnvironment.size())
  1585. {
  1586. env += this->CCEnvironment;
  1587. }
  1588. cmSystemTools::PutEnv(env.c_str());
  1589. env = "CXX=";
  1590. if(this->CXXEnvironment.size())
  1591. {
  1592. env += this->CXXEnvironment;
  1593. }
  1594. cmSystemTools::PutEnv(env.c_str());
  1595. }
  1596. // set the new
  1597. this->GlobalGenerator = gg;
  1598. // set the global flag for unix style paths on cmSystemTools as soon as
  1599. // the generator is set. This allows gmake to be used on windows.
  1600. cmSystemTools::SetForceUnixPaths
  1601. (this->GlobalGenerator->GetForceUnixPaths());
  1602. // Save the environment variables CXX and CC
  1603. const char* cxx = getenv("CXX");
  1604. const char* cc = getenv("CC");
  1605. if(cxx)
  1606. {
  1607. this->CXXEnvironment = cxx;
  1608. }
  1609. else
  1610. {
  1611. this->CXXEnvironment = "";
  1612. }
  1613. if(cc)
  1614. {
  1615. this->CCEnvironment = cc;
  1616. }
  1617. else
  1618. {
  1619. this->CCEnvironment = "";
  1620. }
  1621. // set the cmake instance just to be sure
  1622. gg->SetCMakeInstance(this);
  1623. }
  1624. int cmake::DoPreConfigureChecks()
  1625. {
  1626. // Make sure the Start directory contains a CMakeLists.txt file.
  1627. std::string srcList = this->GetHomeDirectory();
  1628. srcList += "/CMakeLists.txt";
  1629. if(!cmSystemTools::FileExists(srcList.c_str()))
  1630. {
  1631. cmOStringStream err;
  1632. if(cmSystemTools::FileIsDirectory(this->GetHomeDirectory()))
  1633. {
  1634. err << "The source directory \"" << this->GetHomeDirectory()
  1635. << "\" does not appear to contain CMakeLists.txt.\n";
  1636. }
  1637. else if(cmSystemTools::FileExists(this->GetHomeDirectory()))
  1638. {
  1639. err << "The source directory \"" << this->GetHomeDirectory()
  1640. << "\" is a file, not a directory.\n";
  1641. }
  1642. else
  1643. {
  1644. err << "The source directory \"" << this->GetHomeDirectory()
  1645. << "\" does not exist.\n";
  1646. }
  1647. err << "Specify --help for usage, or press the help button on the CMake "
  1648. "GUI.";
  1649. cmSystemTools::Error(err.str().c_str());
  1650. return -2;
  1651. }
  1652. // do a sanity check on some values
  1653. if(this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY"))
  1654. {
  1655. std::string cacheStart =
  1656. this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY");
  1657. cacheStart += "/CMakeLists.txt";
  1658. std::string currentStart = this->GetHomeDirectory();
  1659. currentStart += "/CMakeLists.txt";
  1660. if(!cmSystemTools::SameFile(cacheStart.c_str(), currentStart.c_str()))
  1661. {
  1662. std::string message = "The source \"";
  1663. message += currentStart;
  1664. message += "\" does not match the source \"";
  1665. message += cacheStart;
  1666. message += "\" used to generate cache. ";
  1667. message += "Re-run cmake with a different source directory.";
  1668. cmSystemTools::Error(message.c_str());
  1669. return -2;
  1670. }
  1671. }
  1672. else
  1673. {
  1674. return 0;
  1675. }
  1676. return 1;
  1677. }
  1678. struct SaveCacheEntry
  1679. {
  1680. std::string key;
  1681. std::string value;
  1682. std::string help;
  1683. cmCacheManager::CacheEntryType type;
  1684. };
  1685. int cmake::HandleDeleteCacheVariables(const char* var)
  1686. {
  1687. std::vector<std::string> argsSplit;
  1688. cmSystemTools::ExpandListArgument(std::string(var), argsSplit);
  1689. // erase the property to avoid infinite recursion
  1690. this->SetProperty("__CMAKE_DELETE_CACHE_CHANGE_VARS_", "");
  1691. cmCacheManager::CacheIterator ci = this->CacheManager->NewIterator();
  1692. std::vector<SaveCacheEntry> saved;
  1693. cmOStringStream warning;
  1694. warning
  1695. << "You have changed variables that require your cache to be deleted.\n"
  1696. << "Configure will be re-run and you may have to reset some variables.\n"
  1697. << "The following variables have changed:\n";
  1698. for(std::vector<std::string>::iterator i = argsSplit.begin();
  1699. i != argsSplit.end(); ++i)
  1700. {
  1701. SaveCacheEntry save;
  1702. save.key = *i;
  1703. warning << *i << "= ";
  1704. i++;
  1705. save.value = *i;
  1706. warning << *i << "\n";
  1707. if(ci.Find(save.key.c_str()))
  1708. {
  1709. save.type = ci.GetType();
  1710. save.help = ci.GetProperty("HELPSTRING");
  1711. }
  1712. saved.push_back(save);
  1713. }
  1714. // remove the cache
  1715. this->CacheManager->DeleteCache(this->GetStartOutputDirectory());
  1716. // load the empty cache
  1717. this->LoadCache();
  1718. // restore the changed compilers
  1719. for(std::vector<SaveCacheEntry>::iterator i = saved.begin();
  1720. i != saved.end(); ++i)
  1721. {
  1722. this->AddCacheEntry(i->key.c_str(), i->value.c_str(),
  1723. i->help.c_str(), i->type);
  1724. }
  1725. cmSystemTools::Message(warning.str().c_str());
  1726. // avoid reconfigure if there were errors
  1727. if(!cmSystemTools::GetErrorOccuredFlag())
  1728. {
  1729. // re-run configure
  1730. return this->Configure();
  1731. }
  1732. return 0;
  1733. }
  1734. int cmake::Configure()
  1735. {
  1736. int ret = this->ActualConfigure();
  1737. const char* delCacheVars =
  1738. this->GetProperty("__CMAKE_DELETE_CACHE_CHANGE_VARS_");
  1739. if(delCacheVars && delCacheVars[0] != 0)
  1740. {
  1741. return this->HandleDeleteCacheVariables(delCacheVars);
  1742. }
  1743. return ret;
  1744. }
  1745. int cmake::ActualConfigure()
  1746. {
  1747. // Construct right now our path conversion table before it's too late:
  1748. this->UpdateConversionPathTable();
  1749. this->CleanupCommandsAndMacros();
  1750. int res = 0;
  1751. if ( !this->ScriptMode )
  1752. {
  1753. res = this->DoPreConfigureChecks();
  1754. }
  1755. if ( res < 0 )
  1756. {
  1757. return -2;
  1758. }
  1759. if ( !res )
  1760. {
  1761. this->CacheManager->AddCacheEntry
  1762. ("CMAKE_HOME_DIRECTORY",
  1763. this->GetHomeDirectory(),
  1764. "Start directory with the top level CMakeLists.txt file for this "
  1765. "project",
  1766. cmCacheManager::INTERNAL);
  1767. }
  1768. // set the default BACKWARDS compatibility to the current version
  1769. if(!this->CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1770. {
  1771. char ver[256];
  1772. sprintf(ver,"%i.%i",cmVersion::GetMajorVersion(),
  1773. cmVersion::GetMinorVersion());
  1774. this->CacheManager->AddCacheEntry
  1775. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1776. "For backwards compatibility, what version of CMake commands and "
  1777. "syntax should this version of CMake allow.",
  1778. cmCacheManager::STRING);
  1779. }
  1780. // no generator specified on the command line
  1781. if(!this->GlobalGenerator)
  1782. {
  1783. const char* genName =
  1784. this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1785. const char* extraGenName =
  1786. this->CacheManager->GetCacheValue("CMAKE_EXTRA_GENERATOR");
  1787. if(genName)
  1788. {
  1789. std::string fullName = cmExternalMakefileProjectGenerator::
  1790. CreateFullGeneratorName(genName, extraGenName);
  1791. this->GlobalGenerator = this->CreateGlobalGenerator(fullName.c_str());
  1792. }
  1793. if(this->GlobalGenerator)
  1794. {
  1795. // set the global flag for unix style paths on cmSystemTools as
  1796. // soon as the generator is set. This allows gmake to be used
  1797. // on windows.
  1798. cmSystemTools::SetForceUnixPaths
  1799. (this->GlobalGenerator->GetForceUnixPaths());
  1800. }
  1801. else
  1802. {
  1803. #if defined(__BORLANDC__) && defined(_WIN32)
  1804. this->SetGlobalGenerator(new cmGlobalBorlandMakefileGenerator);
  1805. #elif defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
  1806. std::string installedCompiler;
  1807. std::string mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1808. "\\VisualStudio\\8.0\\Setup;Dbghelp_path]";
  1809. cmSystemTools::ExpandRegistryValues(mp);
  1810. if (!(mp == "/registry"))
  1811. {
  1812. installedCompiler = "Visual Studio 8 2005";
  1813. }
  1814. else
  1815. {
  1816. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1817. "\\VisualStudio\\7.1;InstallDir]";
  1818. cmSystemTools::ExpandRegistryValues(mp);
  1819. if (!(mp == "/registry"))
  1820. {
  1821. installedCompiler = "Visual Studio 7 .NET 2003";
  1822. }
  1823. else
  1824. {
  1825. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1826. "\\VisualStudio\\7.0;InstallDir]";
  1827. cmSystemTools::ExpandRegistryValues(mp);
  1828. if (!(mp == "/registry"))
  1829. {
  1830. installedCompiler = "Visual Studio 7";
  1831. }
  1832. else
  1833. {
  1834. installedCompiler = "Visual Studio 6";
  1835. }
  1836. }
  1837. }
  1838. cmGlobalGenerator* gen
  1839. = this->CreateGlobalGenerator(installedCompiler.c_str());
  1840. if(!gen)
  1841. {
  1842. gen = new cmGlobalNMakeMakefileGenerator;
  1843. }
  1844. this->SetGlobalGenerator(gen);
  1845. #else
  1846. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator3);
  1847. #endif
  1848. }
  1849. if(!this->GlobalGenerator)
  1850. {
  1851. cmSystemTools::Error("Could not create generator");
  1852. return -1;
  1853. }
  1854. }
  1855. const char* genName = this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1856. if(genName)
  1857. {
  1858. if(strcmp(this->GlobalGenerator->GetName(), genName) != 0)
  1859. {
  1860. std::string message = "Error: generator : ";
  1861. message += this->GlobalGenerator->GetName();
  1862. message += "\nDoes not match the generator used previously: ";
  1863. message += genName;
  1864. message +=
  1865. "\nEither remove the CMakeCache.txt file or choose a different"
  1866. " binary directory.";
  1867. cmSystemTools::Error(message.c_str());
  1868. return -2;
  1869. }
  1870. }
  1871. if(!this->CacheManager->GetCacheValue("CMAKE_GENERATOR"))
  1872. {
  1873. this->CacheManager->AddCacheEntry("CMAKE_GENERATOR",
  1874. this->GlobalGenerator->GetName(),
  1875. "Name of generator.",
  1876. cmCacheManager::INTERNAL);
  1877. this->CacheManager->AddCacheEntry("CMAKE_EXTRA_GENERATOR",
  1878. this->GlobalGenerator->GetExtraGeneratorName(),
  1879. "Name of external makefile project generator.",
  1880. cmCacheManager::INTERNAL);
  1881. }
  1882. // reset any system configuration information, except for when we are
  1883. // InTryCompile. With TryCompile the system info is taken from the parent's
  1884. // info to save time
  1885. if (!this->InTryCompile)
  1886. {
  1887. this->GlobalGenerator->ClearEnabledLanguages();
  1888. }
  1889. this->CleanupWrittenFiles();
  1890. // Truncate log files
  1891. if (!this->InTryCompile)
  1892. {
  1893. this->TruncateOutputLog("CMakeOutput.log");
  1894. this->TruncateOutputLog("CMakeError.log");
  1895. }
  1896. // actually do the configure
  1897. this->GlobalGenerator->Configure();
  1898. // Before saving the cache
  1899. // if the project did not define one of the entries below, add them now
  1900. // so users can edit the values in the cache:
  1901. // LIBRARY_OUTPUT_PATH
  1902. // EXECUTABLE_OUTPUT_PATH
  1903. if(!this->CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  1904. {
  1905. this->CacheManager->AddCacheEntry
  1906. ("LIBRARY_OUTPUT_PATH", "",
  1907. "Single output directory for building all libraries.",
  1908. cmCacheManager::PATH);
  1909. }
  1910. if(!this->CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  1911. {
  1912. this->CacheManager->AddCacheEntry
  1913. ("EXECUTABLE_OUTPUT_PATH", "",
  1914. "Single output directory for building all executables.",
  1915. cmCacheManager::PATH);
  1916. }
  1917. if(!this->CacheManager->GetCacheValue("CMAKE_USE_RELATIVE_PATHS"))
  1918. {
  1919. this->CacheManager->AddCacheEntry
  1920. ("CMAKE_USE_RELATIVE_PATHS", false,
  1921. "If true, cmake will use relative paths in makefiles and projects.");
  1922. cmCacheManager::CacheIterator it =
  1923. this->CacheManager->GetCacheIterator("CMAKE_USE_RELATIVE_PATHS");
  1924. if ( !it.PropertyExists("ADVANCED") )
  1925. {
  1926. it.SetProperty("ADVANCED", "1");
  1927. }
  1928. }
  1929. if(cmSystemTools::GetFatalErrorOccured() &&
  1930. (!this->CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM") ||
  1931. cmSystemTools::IsOff(this->CacheManager->
  1932. GetCacheValue("CMAKE_MAKE_PROGRAM"))))
  1933. {
  1934. // We must have a bad generator selection. Wipe the cache entry so the
  1935. // user can select another.
  1936. this->CacheManager->RemoveCacheEntry("CMAKE_GENERATOR");
  1937. this->CacheManager->RemoveCacheEntry("CMAKE_EXTRA_GENERATOR");
  1938. }
  1939. // only save the cache if there were no fatal errors
  1940. if ( !this->ScriptMode )
  1941. {
  1942. this->CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1943. }
  1944. if ( !this->GraphVizFile.empty() )
  1945. {
  1946. std::cout << "Generate graphviz: " << this->GraphVizFile << std::endl;
  1947. this->GenerateGraphViz(this->GraphVizFile.c_str());
  1948. }
  1949. if(cmSystemTools::GetErrorOccuredFlag())
  1950. {
  1951. return -1;
  1952. }
  1953. return 0;
  1954. }
  1955. bool cmake::CacheVersionMatches()
  1956. {
  1957. const char* majv =
  1958. this->CacheManager->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION");
  1959. const char* minv =
  1960. this->CacheManager->GetCacheValue("CMAKE_CACHE_MINOR_VERSION");
  1961. const char* relv =
  1962. this->CacheManager->GetCacheValue("CMAKE_CACHE_RELEASE_VERSION");
  1963. bool cacheSameCMake = false;
  1964. if(majv &&
  1965. atoi(majv) == static_cast<int>(cmVersion::GetMajorVersion())
  1966. && minv &&
  1967. atoi(minv) == static_cast<int>(cmVersion::GetMinorVersion())
  1968. && relv && (strcmp(relv, cmVersion::GetReleaseVersion().c_str()) == 0))
  1969. {
  1970. cacheSameCMake = true;
  1971. }
  1972. return cacheSameCMake;
  1973. }
  1974. void cmake::PreLoadCMakeFiles()
  1975. {
  1976. std::string pre_load = this->GetHomeDirectory();
  1977. if ( pre_load.size() > 0 )
  1978. {
  1979. pre_load += "/PreLoad.cmake";
  1980. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1981. {
  1982. this->ReadListFile(pre_load.c_str());
  1983. }
  1984. }
  1985. pre_load = this->GetHomeOutputDirectory();
  1986. if ( pre_load.size() > 0 )
  1987. {
  1988. pre_load += "/PreLoad.cmake";
  1989. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1990. {
  1991. this->ReadListFile(pre_load.c_str());
  1992. }
  1993. }
  1994. }
  1995. // handle a command line invocation
  1996. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  1997. {
  1998. // Process the arguments
  1999. this->SetArgs(args);
  2000. if(cmSystemTools::GetErrorOccuredFlag())
  2001. {
  2002. return -1;
  2003. }
  2004. // If we are given a stamp file check if it is really out of date.
  2005. if(!this->CheckStampFile.empty() &&
  2006. cmakeCheckStampFile(this->CheckStampFile.c_str()))
  2007. {
  2008. return 0;
  2009. }
  2010. // set the cmake command
  2011. this->CMakeCommand = args[0];
  2012. if ( !this->ScriptMode )
  2013. {
  2014. // load the cache
  2015. if(this->LoadCache() < 0)
  2016. {
  2017. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  2018. return -1;
  2019. }
  2020. }
  2021. else
  2022. {
  2023. this->AddCMakePaths();
  2024. }
  2025. // Add any cache args
  2026. if ( !this->SetCacheArgs(args) )
  2027. {
  2028. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  2029. return -1;
  2030. }
  2031. // In script mode we terminate after running the script.
  2032. if(this->ScriptMode)
  2033. {
  2034. if(cmSystemTools::GetErrorOccuredFlag())
  2035. {
  2036. return -1;
  2037. }
  2038. else
  2039. {
  2040. return 0;
  2041. }
  2042. }
  2043. this->PreLoadCMakeFiles();
  2044. std::string systemFile = this->GetHomeOutputDirectory();
  2045. systemFile += "/CMakeSystem.cmake";
  2046. if ( noconfigure )
  2047. {
  2048. return 0;
  2049. }
  2050. // now run the global generate
  2051. // Check the state of the build system to see if we need to regenerate.
  2052. if(!this->CheckBuildSystem())
  2053. {
  2054. return 0;
  2055. }
  2056. // If we are doing global generate, we better set start and start
  2057. // output directory to the root of the project.
  2058. std::string oldstartdir = this->GetStartDirectory();
  2059. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  2060. this->SetStartDirectory(this->GetHomeDirectory());
  2061. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  2062. int ret = this->Configure();
  2063. if (ret || this->ScriptMode)
  2064. {
  2065. #if defined(CMAKE_HAVE_VS_GENERATORS)
  2066. if(!this->VSSolutionFile.empty() && this->GlobalGenerator)
  2067. {
  2068. // CMake is running to regenerate a Visual Studio build tree
  2069. // during a build from the VS IDE. The build files cannot be
  2070. // regenerated, so we should stop the build.
  2071. cmSystemTools::Message(
  2072. "CMake Configure step failed. "
  2073. "Build files cannot be regenerated correctly. "
  2074. "Attempting to stop IDE build.");
  2075. cmGlobalVisualStudioGenerator* gg =
  2076. static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator);
  2077. gg->CallVisualStudioMacro(cmGlobalVisualStudioGenerator::MacroStop,
  2078. this->VSSolutionFile.c_str());
  2079. }
  2080. #endif
  2081. return ret;
  2082. }
  2083. ret = this->Generate();
  2084. std::string message = "Build files have been written to: ";
  2085. message += this->GetHomeOutputDirectory();
  2086. this->UpdateProgress(message.c_str(), -1);
  2087. if(ret)
  2088. {
  2089. return ret;
  2090. }
  2091. this->SetStartDirectory(oldstartdir.c_str());
  2092. this->SetStartOutputDirectory(oldstartoutputdir.c_str());
  2093. return ret;
  2094. }
  2095. int cmake::Generate()
  2096. {
  2097. if(!this->GlobalGenerator)
  2098. {
  2099. return -1;
  2100. }
  2101. this->GlobalGenerator->Generate();
  2102. if(cmSystemTools::GetErrorOccuredFlag())
  2103. {
  2104. return -1;
  2105. }
  2106. if (this->GetProperty("REPORT_UNDEFINED_PROPERTIES"))
  2107. {
  2108. this->ReportUndefinedPropertyAccesses
  2109. (this->GetProperty("REPORT_UNDEFINED_PROPERTIES"));
  2110. }
  2111. return 0;
  2112. }
  2113. void cmake::AddCacheEntry(const char* key, const char* value,
  2114. const char* helpString,
  2115. int type)
  2116. {
  2117. this->CacheManager->AddCacheEntry(key, value,
  2118. helpString,
  2119. cmCacheManager::CacheEntryType(type));
  2120. }
  2121. const char* cmake::GetCacheDefinition(const char* name) const
  2122. {
  2123. return this->CacheManager->GetCacheValue(name);
  2124. }
  2125. int cmake::DumpDocumentationToFile(std::ostream& f)
  2126. {
  2127. #ifdef CMAKE_BUILD_WITH_CMAKE
  2128. // Loop over all registered commands and print out documentation
  2129. const char *name;
  2130. const char *terse;
  2131. const char *full;
  2132. char tmp[1024];
  2133. sprintf(tmp,"Version %d.%d (%s)", cmVersion::GetMajorVersion(),
  2134. cmVersion::GetMinorVersion(),
  2135. cmVersion::GetReleaseVersion().c_str());
  2136. f << "<html>\n";
  2137. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  2138. f << "<ul>\n";
  2139. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  2140. j != this->Commands.end(); ++j)
  2141. {
  2142. name = (*j).second->GetName();
  2143. terse = (*j).second->GetTerseDocumentation();
  2144. full = (*j).second->GetFullDocumentation();
  2145. f << "<li><b>" << name << "</b> - " << terse << std::endl
  2146. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  2147. }
  2148. f << "</ul></html>\n";
  2149. #else
  2150. (void)f;
  2151. #endif
  2152. return 1;
  2153. }
  2154. void cmake::AddDefaultCommands()
  2155. {
  2156. std::list<cmCommand*> commands;
  2157. GetBootstrapCommands(commands);
  2158. GetPredefinedCommands(commands);
  2159. for(std::list<cmCommand*>::iterator i = commands.begin();
  2160. i != commands.end(); ++i)
  2161. {
  2162. this->AddCommand(*i);
  2163. }
  2164. }
  2165. void cmake::AddDefaultGenerators()
  2166. {
  2167. #if defined(_WIN32) && !defined(__CYGWIN__)
  2168. # if !defined(CMAKE_BOOT_MINGW)
  2169. this->Generators[cmGlobalVisualStudio6Generator::GetActualName()] =
  2170. &cmGlobalVisualStudio6Generator::New;
  2171. this->Generators[cmGlobalVisualStudio7Generator::GetActualName()] =
  2172. &cmGlobalVisualStudio7Generator::New;
  2173. this->Generators[cmGlobalVisualStudio71Generator::GetActualName()] =
  2174. &cmGlobalVisualStudio71Generator::New;
  2175. this->Generators[cmGlobalVisualStudio8Generator::GetActualName()] =
  2176. &cmGlobalVisualStudio8Generator::New;
  2177. this->Generators[cmGlobalVisualStudio9Generator::GetActualName()] =
  2178. &cmGlobalVisualStudio9Generator::New;
  2179. this->Generators[cmGlobalVisualStudio9Win64Generator::GetActualName()] =
  2180. &cmGlobalVisualStudio9Win64Generator::New;
  2181. this->Generators[cmGlobalVisualStudio8Win64Generator::GetActualName()] =
  2182. &cmGlobalVisualStudio8Win64Generator::New;
  2183. this->Generators[cmGlobalBorlandMakefileGenerator::GetActualName()] =
  2184. &cmGlobalBorlandMakefileGenerator::New;
  2185. this->Generators[cmGlobalNMakeMakefileGenerator::GetActualName()] =
  2186. &cmGlobalNMakeMakefileGenerator::New;
  2187. this->Generators[cmGlobalWatcomWMakeGenerator::GetActualName()] =
  2188. &cmGlobalWatcomWMakeGenerator::New;
  2189. # endif
  2190. this->Generators[cmGlobalMSYSMakefileGenerator::GetActualName()] =
  2191. &cmGlobalMSYSMakefileGenerator::New;
  2192. this->Generators[cmGlobalMinGWMakefileGenerator::GetActualName()] =
  2193. &cmGlobalMinGWMakefileGenerator::New;
  2194. #endif
  2195. this->Generators[cmGlobalUnixMakefileGenerator3::GetActualName()] =
  2196. &cmGlobalUnixMakefileGenerator3::New;
  2197. #ifdef CMAKE_USE_XCODE
  2198. this->Generators[cmGlobalXCodeGenerator::GetActualName()] =
  2199. &cmGlobalXCodeGenerator::New;
  2200. #endif
  2201. }
  2202. int cmake::LoadCache()
  2203. {
  2204. // could we not read the cache
  2205. if (!this->CacheManager->LoadCache(this->GetHomeOutputDirectory()))
  2206. {
  2207. // if it does exist, but isn;t readable then warn the user
  2208. std::string cacheFile = this->GetHomeOutputDirectory();
  2209. cacheFile += "/CMakeCache.txt";
  2210. if(cmSystemTools::FileExists(cacheFile.c_str()))
  2211. {
  2212. cmSystemTools::Error(
  2213. "There is a CMakeCache.txt file for the current binary tree but "
  2214. "cmake does not have permission to read it. Please check the "
  2215. "permissions of the directory you are trying to run CMake on.");
  2216. return -1;
  2217. }
  2218. }
  2219. if (this->CMakeCommand.size() < 2)
  2220. {
  2221. cmSystemTools::Error(
  2222. "cmake command was not specified prior to loading the cache in "
  2223. "cmake.cxx");
  2224. return -1;
  2225. }
  2226. // setup CMAKE_ROOT and CMAKE_COMMAND
  2227. if(!this->AddCMakePaths())
  2228. {
  2229. return -3;
  2230. }
  2231. // set the default BACKWARDS compatibility to the current version
  2232. if(!this->CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  2233. {
  2234. char ver[256];
  2235. sprintf(ver,"%i.%i",cmVersion::GetMajorVersion(),
  2236. cmVersion::GetMinorVersion());
  2237. this->CacheManager->AddCacheEntry
  2238. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  2239. "For backwards compatibility, what version of CMake commands and "
  2240. "syntax should this version of CMake allow.",
  2241. cmCacheManager::STRING);
  2242. }
  2243. return 0;
  2244. }
  2245. void cmake::SetProgressCallback(ProgressCallbackType f, void *cd)
  2246. {
  2247. this->ProgressCallback = f;
  2248. this->ProgressCallbackClientData = cd;
  2249. }
  2250. void cmake::UpdateProgress(const char *msg, float prog)
  2251. {
  2252. if(this->ProgressCallback && !this->InTryCompile)
  2253. {
  2254. (*this->ProgressCallback)(msg, prog, this->ProgressCallbackClientData);
  2255. return;
  2256. }
  2257. }
  2258. void cmake::GetCommandDocumentation(std::vector<cmDocumentationEntry>& v,
  2259. bool withCurrentCommands,
  2260. bool withCompatCommands) const
  2261. {
  2262. for(RegisteredCommandsMap::const_iterator j = this->Commands.begin();
  2263. j != this->Commands.end(); ++j)
  2264. {
  2265. if ((( withCompatCommands == false) && ( (*j).second->IsDiscouraged()))
  2266. || ((withCurrentCommands == false) && (!(*j).second->IsDiscouraged())))
  2267. {
  2268. continue;
  2269. }
  2270. cmDocumentationEntry e((*j).second->GetName(),
  2271. (*j).second->GetTerseDocumentation(),
  2272. (*j).second->GetFullDocumentation());
  2273. v.push_back(e);
  2274. }
  2275. }
  2276. void cmake::GetPropertiesDocumentation(std::map<std::string,
  2277. cmDocumentationSection *>& v)
  2278. {
  2279. // loop over the properties and put them into the doc structure
  2280. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>::iterator i;
  2281. i = this->PropertyDefinitions.begin();
  2282. for (;i != this->PropertyDefinitions.end(); ++i)
  2283. {
  2284. i->second.GetPropertiesDocumentation(v);
  2285. }
  2286. }
  2287. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  2288. {
  2289. for(RegisteredGeneratorsMap::const_iterator i = this->Generators.begin();
  2290. i != this->Generators.end(); ++i)
  2291. {
  2292. cmDocumentationEntry e;
  2293. cmGlobalGenerator* generator = (i->second)();
  2294. generator->GetDocumentation(e);
  2295. delete generator;
  2296. v.push_back(e);
  2297. }
  2298. for(RegisteredExtraGeneratorsMap::const_iterator
  2299. i = this->ExtraGenerators.begin(); i != this->ExtraGenerators.end(); ++i)
  2300. {
  2301. cmDocumentationEntry e;
  2302. cmExternalMakefileProjectGenerator* generator = (i->second)();
  2303. generator->GetDocumentation(e, i->first.c_str());
  2304. e.Name = i->first;
  2305. delete generator;
  2306. v.push_back(e);
  2307. }
  2308. }
  2309. void cmake::AddWrittenFile(const char* file)
  2310. {
  2311. this->WrittenFiles.insert(file);
  2312. }
  2313. bool cmake::HasWrittenFile(const char* file)
  2314. {
  2315. return this->WrittenFiles.find(file) != this->WrittenFiles.end();
  2316. }
  2317. void cmake::CleanupWrittenFiles()
  2318. {
  2319. this->WrittenFiles.clear();
  2320. }
  2321. void cmake::UpdateConversionPathTable()
  2322. {
  2323. // Update the path conversion table with any specified file:
  2324. const char* tablepath =
  2325. this->CacheManager->GetCacheValue("CMAKE_PATH_TRANSLATION_FILE");
  2326. if(tablepath)
  2327. {
  2328. std::ifstream table( tablepath );
  2329. if(!table)
  2330. {
  2331. cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ", tablepath,
  2332. ". CMake can not open file.");
  2333. cmSystemTools::ReportLastSystemError("CMake can not open file.");
  2334. }
  2335. else
  2336. {
  2337. std::string a, b;
  2338. while(!table.eof())
  2339. {
  2340. // two entries per line
  2341. table >> a; table >> b;
  2342. cmSystemTools::AddTranslationPath( a.c_str(), b.c_str());
  2343. }
  2344. }
  2345. }
  2346. }
  2347. //----------------------------------------------------------------------------
  2348. int cmake::CheckBuildSystem()
  2349. {
  2350. // We do not need to rerun CMake. Check dependency integrity. Use
  2351. // the make system's VERBOSE environment variable to enable verbose
  2352. // output.
  2353. bool verbose = cmSystemTools::GetEnv("VERBOSE") != 0;
  2354. // This method will check the integrity of the build system if the
  2355. // option was given on the command line. It reads the given file to
  2356. // determine whether CMake should rerun.
  2357. // If no file is provided for the check, we have to rerun.
  2358. if(this->CheckBuildSystemArgument.size() == 0)
  2359. {
  2360. if(verbose)
  2361. {
  2362. cmOStringStream msg;
  2363. msg << "Re-run cmake no build system arguments\n";
  2364. cmSystemTools::Stdout(msg.str().c_str());
  2365. }
  2366. return 1;
  2367. }
  2368. // If the file provided does not exist, we have to rerun.
  2369. if(!cmSystemTools::FileExists(this->CheckBuildSystemArgument.c_str()))
  2370. {
  2371. if(verbose)
  2372. {
  2373. cmOStringStream msg;
  2374. msg << "Re-run cmake missing file: "
  2375. << this->CheckBuildSystemArgument.c_str() << "\n";
  2376. cmSystemTools::Stdout(msg.str().c_str());
  2377. }
  2378. return 1;
  2379. }
  2380. // Read the rerun check file and use it to decide whether to do the
  2381. // global generate.
  2382. cmake cm;
  2383. cmGlobalGenerator gg;
  2384. gg.SetCMakeInstance(&cm);
  2385. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  2386. lg->SetGlobalGenerator(&gg);
  2387. cmMakefile* mf = lg->GetMakefile();
  2388. if(!mf->ReadListFile(0, this->CheckBuildSystemArgument.c_str()) ||
  2389. cmSystemTools::GetErrorOccuredFlag())
  2390. {
  2391. if(verbose)
  2392. {
  2393. cmOStringStream msg;
  2394. msg << "Re-run cmake error reading : "
  2395. << this->CheckBuildSystemArgument.c_str() << "\n";
  2396. cmSystemTools::Stdout(msg.str().c_str());
  2397. }
  2398. // There was an error reading the file. Just rerun.
  2399. return 1;
  2400. }
  2401. if(this->ClearBuildSystem)
  2402. {
  2403. // Get the generator used for this build system.
  2404. const char* genName = mf->GetDefinition("CMAKE_DEPENDS_GENERATOR");
  2405. if(!genName || genName[0] == '\0')
  2406. {
  2407. genName = "Unix Makefiles";
  2408. }
  2409. // Create the generator and use it to clear the dependencies.
  2410. std::auto_ptr<cmGlobalGenerator>
  2411. ggd(this->CreateGlobalGenerator(genName));
  2412. if(ggd.get())
  2413. {
  2414. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  2415. lgd->SetGlobalGenerator(ggd.get());
  2416. lgd->ClearDependencies(mf, verbose);
  2417. }
  2418. }
  2419. // Get the set of dependencies and outputs.
  2420. std::vector<std::string> depends;
  2421. std::vector<std::string> outputs;
  2422. const char* dependsStr = mf->GetDefinition("CMAKE_MAKEFILE_DEPENDS");
  2423. const char* outputsStr = mf->GetDefinition("CMAKE_MAKEFILE_OUTPUTS");
  2424. if(dependsStr && outputsStr)
  2425. {
  2426. cmSystemTools::ExpandListArgument(dependsStr, depends);
  2427. cmSystemTools::ExpandListArgument(outputsStr, outputs);
  2428. }
  2429. if(depends.empty() || outputs.empty())
  2430. {
  2431. // Not enough information was provided to do the test. Just rerun.
  2432. if(verbose)
  2433. {
  2434. cmOStringStream msg;
  2435. msg << "Re-run cmake no CMAKE_MAKEFILE_DEPENDS "
  2436. "or CMAKE_MAKEFILE_OUTPUTS :\n";
  2437. cmSystemTools::Stdout(msg.str().c_str());
  2438. }
  2439. return 1;
  2440. }
  2441. // Find find the newest dependency.
  2442. std::vector<std::string>::iterator dep = depends.begin();
  2443. std::string dep_newest = *dep++;
  2444. for(;dep != depends.end(); ++dep)
  2445. {
  2446. int result = 0;
  2447. if(this->FileComparison->FileTimeCompare(dep_newest.c_str(),
  2448. dep->c_str(), &result))
  2449. {
  2450. if(result < 0)
  2451. {
  2452. dep_newest = *dep;
  2453. }
  2454. }
  2455. else
  2456. {
  2457. if(verbose)
  2458. {
  2459. cmOStringStream msg;
  2460. msg << "Re-run cmake: build system dependency is missing\n";
  2461. cmSystemTools::Stdout(msg.str().c_str());
  2462. }
  2463. return 1;
  2464. }
  2465. }
  2466. // Find find the oldest output.
  2467. std::vector<std::string>::iterator out = outputs.begin();
  2468. std::string out_oldest = *out++;
  2469. for(;out != outputs.end(); ++out)
  2470. {
  2471. int result = 0;
  2472. if(this->FileComparison->FileTimeCompare(out_oldest.c_str(),
  2473. out->c_str(), &result))
  2474. {
  2475. if(result > 0)
  2476. {
  2477. out_oldest = *out;
  2478. }
  2479. }
  2480. else
  2481. {
  2482. if(verbose)
  2483. {
  2484. cmOStringStream msg;
  2485. msg << "Re-run cmake: build system output is missing\n";
  2486. cmSystemTools::Stdout(msg.str().c_str());
  2487. }
  2488. return 1;
  2489. }
  2490. }
  2491. // If any output is older than any dependency then rerun.
  2492. {
  2493. int result = 0;
  2494. if(!this->FileComparison->FileTimeCompare(out_oldest.c_str(),
  2495. dep_newest.c_str(),
  2496. &result) ||
  2497. result < 0)
  2498. {
  2499. if(verbose)
  2500. {
  2501. cmOStringStream msg;
  2502. msg << "Re-run cmake file: " << out_oldest.c_str()
  2503. << " older than: " << dep_newest.c_str() << "\n";
  2504. cmSystemTools::Stdout(msg.str().c_str());
  2505. }
  2506. return 1;
  2507. }
  2508. }
  2509. // No need to rerun.
  2510. return 0;
  2511. }
  2512. //----------------------------------------------------------------------------
  2513. void cmake::TruncateOutputLog(const char* fname)
  2514. {
  2515. std::string fullPath = this->GetHomeOutputDirectory();
  2516. fullPath += "/";
  2517. fullPath += fname;
  2518. struct stat st;
  2519. if ( ::stat(fullPath.c_str(), &st) )
  2520. {
  2521. return;
  2522. }
  2523. if ( !this->CacheManager->GetCacheValue("CMAKE_CACHEFILE_DIR") )
  2524. {
  2525. cmSystemTools::RemoveFile(fullPath.c_str());
  2526. return;
  2527. }
  2528. off_t fsize = st.st_size;
  2529. const off_t maxFileSize = 50 * 1024;
  2530. if ( fsize < maxFileSize )
  2531. {
  2532. //TODO: truncate file
  2533. return;
  2534. }
  2535. }
  2536. inline std::string removeQuotes(const std::string& s)
  2537. {
  2538. if(s[0] == '\"' && s[s.size()-1] == '\"')
  2539. {
  2540. return s.substr(1, s.size()-2);
  2541. }
  2542. return s;
  2543. }
  2544. std::string cmake::FindCMakeProgram(const char* name) const
  2545. {
  2546. std::string path;
  2547. if ((name) && (*name))
  2548. {
  2549. const cmMakefile* mf
  2550. = this->GetGlobalGenerator()->GetLocalGenerators()[0]->GetMakefile();
  2551. #ifdef CMAKE_BUILD_WITH_CMAKE
  2552. path = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2553. path = removeQuotes(path);
  2554. path = cmSystemTools::GetFilenamePath(path.c_str());
  2555. path += "/";
  2556. path += name;
  2557. path += cmSystemTools::GetExecutableExtension();
  2558. if(!cmSystemTools::FileExists(path.c_str()))
  2559. {
  2560. path = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2561. path = cmSystemTools::GetFilenamePath(path.c_str());
  2562. path += "/Debug/";
  2563. path += name;
  2564. path += cmSystemTools::GetExecutableExtension();
  2565. }
  2566. if(!cmSystemTools::FileExists(path.c_str()))
  2567. {
  2568. path = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2569. path = cmSystemTools::GetFilenamePath(path.c_str());
  2570. path += "/Release/";
  2571. path += name;
  2572. path += cmSystemTools::GetExecutableExtension();
  2573. }
  2574. #else
  2575. // Only for bootstrap
  2576. path += mf->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  2577. path += "/";
  2578. path += name;
  2579. path += cmSystemTools::GetExecutableExtension();
  2580. #endif
  2581. }
  2582. return path;
  2583. }
  2584. const char* cmake::GetCTestCommand()
  2585. {
  2586. if ( this->CTestCommand.empty() )
  2587. {
  2588. this->CTestCommand = this->FindCMakeProgram("ctest");
  2589. }
  2590. if ( this->CTestCommand.empty() )
  2591. {
  2592. cmSystemTools::Error("Cannot find the CTest executable");
  2593. this->CTestCommand = "CTEST-COMMAND-NOT-FOUND";
  2594. }
  2595. return this->CTestCommand.c_str();
  2596. }
  2597. const char* cmake::GetCPackCommand()
  2598. {
  2599. if ( this->CPackCommand.empty() )
  2600. {
  2601. this->CPackCommand = this->FindCMakeProgram("cpack");
  2602. }
  2603. if ( this->CPackCommand.empty() )
  2604. {
  2605. cmSystemTools::Error("Cannot find the CPack executable");
  2606. this->CPackCommand = "CPACK-COMMAND-NOT-FOUND";
  2607. }
  2608. return this->CPackCommand.c_str();
  2609. }
  2610. void cmake::GenerateGraphViz(const char* fileName) const
  2611. {
  2612. cmGeneratedFileStream str(fileName);
  2613. if ( !str )
  2614. {
  2615. return;
  2616. }
  2617. cmake cm;
  2618. cmGlobalGenerator ggi;
  2619. ggi.SetCMakeInstance(&cm);
  2620. std::auto_ptr<cmLocalGenerator> lg(ggi.CreateLocalGenerator());
  2621. lg->SetGlobalGenerator(&ggi);
  2622. cmMakefile *mf = lg->GetMakefile();
  2623. std::string infile = this->GetHomeOutputDirectory();
  2624. infile += "/CMakeGraphVizOptions.cmake";
  2625. if ( !cmSystemTools::FileExists(infile.c_str()) )
  2626. {
  2627. infile = this->GetHomeDirectory();
  2628. infile += "/CMakeGraphVizOptions.cmake";
  2629. if ( !cmSystemTools::FileExists(infile.c_str()) )
  2630. {
  2631. infile = "";
  2632. }
  2633. }
  2634. if ( !infile.empty() )
  2635. {
  2636. if ( !mf->ReadListFile(0, infile.c_str()) )
  2637. {
  2638. cmSystemTools::Error("Problem opening GraphViz options file: ",
  2639. infile.c_str());
  2640. return;
  2641. }
  2642. std::cout << "Read GraphViz options file: " << infile.c_str()
  2643. << std::endl;
  2644. }
  2645. #define __set_if_not_set(var, value, cmakeDefinition) \
  2646. const char* var = mf->GetDefinition(cmakeDefinition); \
  2647. if ( !var ) \
  2648. { \
  2649. var = value; \
  2650. }
  2651. __set_if_not_set(graphType, "digraph", "GRAPHVIZ_GRAPH_TYPE");
  2652. __set_if_not_set(graphName, "GG", "GRAPHVIZ_GRAPH_NAME");
  2653. __set_if_not_set(graphHeader, "node [\n fontsize = \"12\"\n];",
  2654. "GRAPHVIZ_GRAPH_HEADER");
  2655. __set_if_not_set(graphNodePrefix, "node", "GRAPHVIZ_NODE_PREFIX");
  2656. const char* ignoreTargets = mf->GetDefinition("GRAPHVIZ_IGNORE_TARGETS");
  2657. std::set<cmStdString> ignoreTargetsSet;
  2658. if ( ignoreTargets )
  2659. {
  2660. std::vector<std::string> ignoreTargetsVector;
  2661. cmSystemTools::ExpandListArgument(ignoreTargets,ignoreTargetsVector);
  2662. std::vector<std::string>::iterator itvIt;
  2663. for ( itvIt = ignoreTargetsVector.begin();
  2664. itvIt != ignoreTargetsVector.end();
  2665. ++ itvIt )
  2666. {
  2667. ignoreTargetsSet.insert(itvIt->c_str());
  2668. }
  2669. }
  2670. str << graphType << " " << graphName << " {" << std::endl;
  2671. str << graphHeader << std::endl;
  2672. const cmGlobalGenerator* gg = this->GetGlobalGenerator();
  2673. const std::vector<cmLocalGenerator*>& localGenerators =
  2674. gg->GetLocalGenerators();
  2675. std::vector<cmLocalGenerator*>::const_iterator lit;
  2676. // for target deps
  2677. // 1 - cmake target
  2678. // 2 - external target
  2679. // 0 - no deps
  2680. std::map<cmStdString, int> targetDeps;
  2681. std::map<cmStdString, const cmTarget*> targetPtrs;
  2682. std::map<cmStdString, cmStdString> targetNamesNodes;
  2683. int cnt = 0;
  2684. // First pass get the list of all cmake targets
  2685. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2686. {
  2687. const cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2688. cmTargets::const_iterator tit;
  2689. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2690. {
  2691. const char* realTargetName = tit->first.c_str();
  2692. if ( ignoreTargetsSet.find(realTargetName) != ignoreTargetsSet.end() )
  2693. {
  2694. // Skip ignored targets
  2695. continue;
  2696. }
  2697. //std::cout << "Found target: " << tit->first.c_str() << std::endl;
  2698. cmOStringStream ostr;
  2699. ostr << graphNodePrefix << cnt++;
  2700. targetNamesNodes[realTargetName] = ostr.str();
  2701. targetPtrs[realTargetName] = &tit->second;
  2702. }
  2703. }
  2704. // Ok, now find all the stuff we link to that is not in cmake
  2705. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2706. {
  2707. const cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2708. cmTargets::const_iterator tit;
  2709. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2710. {
  2711. const cmTarget::LinkLibraryVectorType* ll
  2712. = &(tit->second.GetOriginalLinkLibraries());
  2713. cmTarget::LinkLibraryVectorType::const_iterator llit;
  2714. const char* realTargetName = tit->first.c_str();
  2715. if ( ignoreTargetsSet.find(realTargetName) != ignoreTargetsSet.end() )
  2716. {
  2717. // Skip ignored targets
  2718. continue;
  2719. }
  2720. if ( ll->size() > 0 )
  2721. {
  2722. targetDeps[realTargetName] = 1;
  2723. }
  2724. for ( llit = ll->begin(); llit != ll->end(); ++ llit )
  2725. {
  2726. const char* libName = llit->first.c_str();
  2727. std::map<cmStdString, cmStdString>::const_iterator tarIt
  2728. = targetNamesNodes.find(libName);
  2729. if ( ignoreTargetsSet.find(libName) != ignoreTargetsSet.end() )
  2730. {
  2731. // Skip ignored targets
  2732. continue;
  2733. }
  2734. if ( tarIt == targetNamesNodes.end() )
  2735. {
  2736. cmOStringStream ostr;
  2737. ostr << graphNodePrefix << cnt++;
  2738. targetDeps[libName] = 2;
  2739. targetNamesNodes[libName] = ostr.str();
  2740. //str << " \"" << ostr.c_str() << "\" [ label=\"" << libName
  2741. //<< "\" shape=\"ellipse\"];" << std::endl;
  2742. }
  2743. else
  2744. {
  2745. std::map<cmStdString, int>::const_iterator depIt
  2746. = targetDeps.find(libName);
  2747. if ( depIt == targetDeps.end() )
  2748. {
  2749. targetDeps[libName] = 1;
  2750. }
  2751. }
  2752. }
  2753. }
  2754. }
  2755. // Write out nodes
  2756. std::map<cmStdString, int>::const_iterator depIt;
  2757. for ( depIt = targetDeps.begin(); depIt != targetDeps.end(); ++ depIt )
  2758. {
  2759. const char* newTargetName = depIt->first.c_str();
  2760. std::map<cmStdString, cmStdString>::const_iterator tarIt
  2761. = targetNamesNodes.find(newTargetName);
  2762. if ( tarIt == targetNamesNodes.end() )
  2763. {
  2764. // We should not be here.
  2765. std::cout << __LINE__ << " Cannot find library: " << newTargetName
  2766. << " even though it was added in the previous pass" << std::endl;
  2767. abort();
  2768. }
  2769. str << " \"" << tarIt->second.c_str() << "\" [ label=\""
  2770. << newTargetName << "\" shape=\"";
  2771. if ( depIt->second == 1 )
  2772. {
  2773. std::map<cmStdString, const cmTarget*>::const_iterator tarTypeIt =
  2774. targetPtrs.find(newTargetName);
  2775. if ( tarTypeIt == targetPtrs.end() )
  2776. {
  2777. // We should not be here.
  2778. std::cout << __LINE__ << " Cannot find library: " << newTargetName
  2779. << " even though it was added in the previous pass" << std::endl;
  2780. abort();
  2781. }
  2782. const cmTarget* tg = tarTypeIt->second;
  2783. switch ( tg->GetType() )
  2784. {
  2785. case cmTarget::EXECUTABLE:
  2786. str << "house";
  2787. break;
  2788. case cmTarget::STATIC_LIBRARY:
  2789. str << "diamond";
  2790. break;
  2791. case cmTarget::SHARED_LIBRARY:
  2792. str << "polygon";
  2793. break;
  2794. case cmTarget::MODULE_LIBRARY:
  2795. str << "octagon";
  2796. break;
  2797. default:
  2798. str << "box";
  2799. }
  2800. }
  2801. else
  2802. {
  2803. str << "ellipse";
  2804. }
  2805. str << "\"];" << std::endl;
  2806. }
  2807. // Now generate the connectivity
  2808. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2809. {
  2810. const cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2811. cmTargets::const_iterator tit;
  2812. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2813. {
  2814. std::map<cmStdString, int>::iterator dependIt
  2815. = targetDeps.find(tit->first.c_str());
  2816. if ( dependIt == targetDeps.end() )
  2817. {
  2818. continue;
  2819. }
  2820. std::map<cmStdString, cmStdString>::iterator cmakeTarIt
  2821. = targetNamesNodes.find(tit->first.c_str());
  2822. const cmTarget::LinkLibraryVectorType* ll
  2823. = &(tit->second.GetOriginalLinkLibraries());
  2824. cmTarget::LinkLibraryVectorType::const_iterator llit;
  2825. for ( llit = ll->begin(); llit != ll->end(); ++ llit )
  2826. {
  2827. const char* libName = llit->first.c_str();
  2828. std::map<cmStdString, cmStdString>::const_iterator tarIt
  2829. = targetNamesNodes.find(libName);
  2830. if ( tarIt == targetNamesNodes.end() )
  2831. {
  2832. // We should not be here.
  2833. std::cout << __LINE__ << " Cannot find library: " << libName
  2834. << " even though it was added in the previous pass" << std::endl;
  2835. abort();
  2836. }
  2837. str << " \"" << cmakeTarIt->second.c_str() << "\" -> \""
  2838. << tarIt->second.c_str() << "\"" << std::endl;
  2839. }
  2840. }
  2841. }
  2842. // TODO: Use dotted or something for external libraries
  2843. //str << " \"node0\":f4 -> \"node12\"[color=\"#0000ff\" style=dotted]"
  2844. //<< std::endl;
  2845. //
  2846. str << "}" << std::endl;
  2847. }
  2848. //----------------------------------------------------------------------------
  2849. #ifdef CMAKE_BUILD_WITH_CMAKE
  2850. int cmake::ExecuteEchoColor(std::vector<std::string>& args)
  2851. {
  2852. // The arguments are
  2853. // argv[0] == <cmake-executable>
  2854. // argv[1] == cmake_echo_color
  2855. bool enabled = true;
  2856. int color = cmsysTerminal_Color_Normal;
  2857. bool newline = true;
  2858. for(unsigned int i=2; i < args.size(); ++i)
  2859. {
  2860. if(args[i].find("--switch=") == 0)
  2861. {
  2862. // Enable or disable color based on the switch value.
  2863. std::string value = args[i].substr(9);
  2864. if(!value.empty())
  2865. {
  2866. if(cmSystemTools::IsOn(value.c_str()))
  2867. {
  2868. enabled = true;
  2869. }
  2870. else
  2871. {
  2872. enabled = false;
  2873. }
  2874. }
  2875. }
  2876. else if(args[i] == "--normal")
  2877. {
  2878. color = cmsysTerminal_Color_Normal;
  2879. }
  2880. else if(args[i] == "--black")
  2881. {
  2882. color = cmsysTerminal_Color_ForegroundBlack;
  2883. }
  2884. else if(args[i] == "--red")
  2885. {
  2886. color = cmsysTerminal_Color_ForegroundRed;
  2887. }
  2888. else if(args[i] == "--green")
  2889. {
  2890. color = cmsysTerminal_Color_ForegroundGreen;
  2891. }
  2892. else if(args[i] == "--yellow")
  2893. {
  2894. color = cmsysTerminal_Color_ForegroundYellow;
  2895. }
  2896. else if(args[i] == "--blue")
  2897. {
  2898. color = cmsysTerminal_Color_ForegroundBlue;
  2899. }
  2900. else if(args[i] == "--magenta")
  2901. {
  2902. color = cmsysTerminal_Color_ForegroundMagenta;
  2903. }
  2904. else if(args[i] == "--cyan")
  2905. {
  2906. color = cmsysTerminal_Color_ForegroundCyan;
  2907. }
  2908. else if(args[i] == "--white")
  2909. {
  2910. color = cmsysTerminal_Color_ForegroundWhite;
  2911. }
  2912. else if(args[i] == "--bold")
  2913. {
  2914. color |= cmsysTerminal_Color_ForegroundBold;
  2915. }
  2916. else if(args[i] == "--no-newline")
  2917. {
  2918. newline = false;
  2919. }
  2920. else if(args[i] == "--newline")
  2921. {
  2922. newline = true;
  2923. }
  2924. else
  2925. {
  2926. // Color is enabled. Print with the current color.
  2927. cmSystemTools::MakefileColorEcho(color, args[i].c_str(),
  2928. newline, enabled);
  2929. }
  2930. }
  2931. return 0;
  2932. }
  2933. #else
  2934. int cmake::ExecuteEchoColor(std::vector<std::string>&)
  2935. {
  2936. return 1;
  2937. }
  2938. #endif
  2939. //----------------------------------------------------------------------------
  2940. int cmake::ExecuteLinkScript(std::vector<std::string>& args)
  2941. {
  2942. // The arguments are
  2943. // argv[0] == <cmake-executable>
  2944. // argv[1] == cmake_link_script
  2945. // argv[2] == <link-script-name>
  2946. // argv[3] == --verbose=?
  2947. bool verbose = false;
  2948. if(args.size() >= 4)
  2949. {
  2950. if(args[3].find("--verbose=") == 0)
  2951. {
  2952. if(!cmSystemTools::IsOff(args[3].substr(10).c_str()))
  2953. {
  2954. verbose = true;
  2955. }
  2956. }
  2957. }
  2958. // Allocate a process instance.
  2959. cmsysProcess* cp = cmsysProcess_New();
  2960. if(!cp)
  2961. {
  2962. std::cerr << "Error allocating process instance in link script."
  2963. << std::endl;
  2964. return 1;
  2965. }
  2966. // Children should share stdout and stderr with this process.
  2967. cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDOUT, 1);
  2968. cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDERR, 1);
  2969. // Run the command lines verbatim.
  2970. cmsysProcess_SetOption(cp, cmsysProcess_Option_Verbatim, 1);
  2971. // Read command lines from the script.
  2972. std::ifstream fin(args[2].c_str());
  2973. if(!fin)
  2974. {
  2975. std::cerr << "Error opening link script \""
  2976. << args[2] << "\"" << std::endl;
  2977. return 1;
  2978. }
  2979. // Run one command at a time.
  2980. std::string command;
  2981. int result = 0;
  2982. while(result == 0 && cmSystemTools::GetLineFromStream(fin, command))
  2983. {
  2984. // Setup this command line.
  2985. const char* cmd[2] = {command.c_str(), 0};
  2986. cmsysProcess_SetCommand(cp, cmd);
  2987. // Report the command if verbose output is enabled.
  2988. if(verbose)
  2989. {
  2990. std::cout << command << std::endl;
  2991. }
  2992. // Run the command and wait for it to exit.
  2993. cmsysProcess_Execute(cp);
  2994. cmsysProcess_WaitForExit(cp, 0);
  2995. // Report failure if any.
  2996. switch(cmsysProcess_GetState(cp))
  2997. {
  2998. case cmsysProcess_State_Exited:
  2999. {
  3000. int value = cmsysProcess_GetExitValue(cp);
  3001. if(value != 0)
  3002. {
  3003. result = value;
  3004. }
  3005. }
  3006. break;
  3007. case cmsysProcess_State_Exception:
  3008. std::cerr << "Error running link command: "
  3009. << cmsysProcess_GetExceptionString(cp) << std::endl;
  3010. result = 1;
  3011. break;
  3012. case cmsysProcess_State_Error:
  3013. std::cerr << "Error running link command: "
  3014. << cmsysProcess_GetErrorString(cp) << std::endl;
  3015. result = 2;
  3016. break;
  3017. default:
  3018. break;
  3019. };
  3020. }
  3021. // Free the process instance.
  3022. cmsysProcess_Delete(cp);
  3023. // Return the final resulting return value.
  3024. return result;
  3025. }
  3026. void cmake::DefineProperties(cmake *cm)
  3027. {
  3028. cm->DefineProperty
  3029. ("REPORT_UNDEFINED_PROPERTIES", cmProperty::GLOBAL,
  3030. "If set, report any undefined properties to this file.",
  3031. "If this property is set to a filename then when CMake runs "
  3032. "it will report any properties or variables that were accessed "
  3033. "but not defined into the filename specified in this property."
  3034. );
  3035. cm->DefineProperty
  3036. ("TARGET_SUPPORTS_SHARED_LIBS", cmProperty::GLOBAL,
  3037. "Does the target platform support shared libraries.",
  3038. "TARGET_SUPPORTS_SHARED_LIBS is a boolean specifying whether the target "
  3039. "platform supports shared libraries. Basically all current general "
  3040. "general purpose OS do so, the exception are usually embedded systems "
  3041. "with no or special OSs.");
  3042. cm->DefineProperty
  3043. ("TARGET_ARCHIVES_MAY_BE_SHARED_LIBS", cmProperty::GLOBAL,
  3044. "Set if shared libraries may be named like archives.",
  3045. "On AIX shared libraries may be named \"lib<name>.a\". "
  3046. "This property is set to true on such platforms.");
  3047. cm->DefineProperty
  3048. ("FIND_LIBRARY_USE_LIB64_PATHS", cmProperty::GLOBAL,
  3049. "Whether FIND_LIBRARY should automatically search lib64 directories.",
  3050. "FIND_LIBRARY_USE_LIB64_PATHS is a boolean specifying whether the "
  3051. "FIND_LIBRARY command should automatically search the lib64 variant of "
  3052. "directories called lib in the search path when building 64-bit "
  3053. "binaries.");
  3054. cm->DefineProperty
  3055. ("ENABLED_FEATURES", cmProperty::GLOBAL,
  3056. "List of features which are enabled during the CMake run.",
  3057. "List of features which are enabled during the CMake run. Be default "
  3058. "it contains the names of all packages which were found. This is "
  3059. "determined using the <NAME>_FOUND variables. Packages which are "
  3060. "searched QUIET are not listed. A project can add its own features to "
  3061. "this list.This property is used by the macros in FeatureSummary.cmake.");
  3062. cm->DefineProperty
  3063. ("DISABLED_FEATURES", cmProperty::GLOBAL,
  3064. "List of features which are disabled during the CMake run.",
  3065. "List of features which are disabled during the CMake run. Be default "
  3066. "it contains the names of all packages which were not found. This is "
  3067. "determined using the <NAME>_FOUND variables. Packages which are "
  3068. "searched QUIET are not listed. A project can add its own features to "
  3069. "this list.This property is used by the macros in FeatureSummary.cmake.");
  3070. cm->DefineProperty
  3071. ("PACKAGES_FOUND", cmProperty::GLOBAL,
  3072. "List of packages which were found during the CMake run.",
  3073. "List of packages which were found during the CMake run. Whether a "
  3074. "package has been found is determined using the <NAME>_FOUND variables.");
  3075. cm->DefineProperty
  3076. ("PACKAGES_NOT_FOUND", cmProperty::GLOBAL,
  3077. "List of packages which were not found during the CMake run.",
  3078. "List of packages which were not found during the CMake run. Whether a "
  3079. "package has been found is determined using the <NAME>_FOUND variables.");
  3080. cm->DefineProperty
  3081. ("PACKAGES_NOT_FOUND", cmProperty::GLOBAL,
  3082. "List of packages which were not found during the CMake run.",
  3083. "List of packages which were not found during the CMake run. Whether a "
  3084. "package has been found is determined using the <NAME>_FOUND variables.");
  3085. cm->DefineProperty(
  3086. "__CMAKE_DELETE_CACHE_CHANGE_VARS_", cmProperty::GLOBAL,
  3087. "Internal property",
  3088. "Used to detect compiler changes, Do not set.");
  3089. cm->DefineProperty(
  3090. "GLOBAL_DEPENDS_DEBUG_MODE", cmProperty::GLOBAL,
  3091. "Enable global target dependency graph debug mode.",
  3092. "CMake automatically analyzes the global inter-target dependency graph "
  3093. "at the beginning of native build system generation. "
  3094. "This property causes it to display details of its analysis to stderr.");
  3095. cm->DefineProperty(
  3096. "ALLOW_DUPLICATE_CUSTOM_TARGETS", cmProperty::GLOBAL,
  3097. "Allow duplicate custom targets to be created.",
  3098. "Normally CMake requires that all targets built in a project have "
  3099. "globally unique names. "
  3100. "This is necessary to generate meaningful project file names in "
  3101. "Xcode and VS IDE generators. "
  3102. "It also allows the target names to be referenced unambiguously.\n"
  3103. "Makefile generators are capable of supporting duplicate custom target "
  3104. "names. "
  3105. "For projects that care only about Makefile generators and do "
  3106. "not wish to support Xcode or VS IDE generators, one may set this "
  3107. "property to true to allow duplicate custom targets. "
  3108. "The property allows multiple add_custom_target command calls in "
  3109. "*different directories* to specify the same target name. "
  3110. "However, setting this property will cause non-Makefile generators "
  3111. "to produce an error and refuse to generate the project."
  3112. );
  3113. cm->DefineProperty
  3114. ("IN_TRY_COMPILE", cmProperty::GLOBAL,
  3115. "Read-only property that is true during a try-compile configuration.",
  3116. "True when building a project inside a TRY_COMPILE or TRY_RUN command.");
  3117. // ================================================================
  3118. // define variables as well
  3119. // ================================================================
  3120. cmDocumentVariables::DefineVariables(cm);
  3121. }
  3122. void cmake::DefineProperty(const char *name, cmProperty::ScopeType scope,
  3123. const char *ShortDescription,
  3124. const char *FullDescription,
  3125. bool chained, const char *docSection)
  3126. {
  3127. this->PropertyDefinitions[scope].DefineProperty(name,scope,ShortDescription,
  3128. FullDescription,
  3129. docSection,
  3130. chained);
  3131. }
  3132. cmPropertyDefinition *cmake
  3133. ::GetPropertyDefinition(const char *name,
  3134. cmProperty::ScopeType scope)
  3135. {
  3136. if (this->IsPropertyDefined(name,scope))
  3137. {
  3138. return &(this->PropertyDefinitions[scope][name]);
  3139. }
  3140. return 0;
  3141. }
  3142. void cmake::RecordPropertyAccess(const char *name,
  3143. cmProperty::ScopeType scope)
  3144. {
  3145. this->AccessedProperties.insert
  3146. (std::pair<cmStdString,cmProperty::ScopeType>(name,scope));
  3147. }
  3148. void cmake::ReportUndefinedPropertyAccesses(const char *filename)
  3149. {
  3150. FILE *progFile = fopen(filename,"w");
  3151. if (!progFile || !this->GlobalGenerator)
  3152. {
  3153. return;
  3154. }
  3155. // what are the enabled languages?
  3156. std::vector<std::string> enLangs;
  3157. this->GlobalGenerator->GetEnabledLanguages(enLangs);
  3158. // Common configuration names.
  3159. // TODO: Compute current configuration(s).
  3160. std::vector<std::string> enConfigs;
  3161. enConfigs.push_back("");
  3162. enConfigs.push_back("DEBUG");
  3163. enConfigs.push_back("RELEASE");
  3164. enConfigs.push_back("MINSIZEREL");
  3165. enConfigs.push_back("RELWITHDEBINFO");
  3166. // take all the defined properties and add definitions for all the enabled
  3167. // languages
  3168. std::set<std::pair<cmStdString,cmProperty::ScopeType> > aliasedProperties;
  3169. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>::iterator i;
  3170. i = this->PropertyDefinitions.begin();
  3171. for (;i != this->PropertyDefinitions.end(); ++i)
  3172. {
  3173. cmPropertyDefinitionMap::iterator j;
  3174. for (j = i->second.begin(); j != i->second.end(); ++j)
  3175. {
  3176. // TODO: What if both <LANG> and <CONFIG> appear?
  3177. if (j->first.find("<CONFIG>") != std::string::npos)
  3178. {
  3179. std::vector<std::string>::const_iterator k;
  3180. for (k = enConfigs.begin(); k != enConfigs.end(); ++k)
  3181. {
  3182. std::string tmp = j->first;
  3183. cmSystemTools::ReplaceString(tmp, "<CONFIG>", k->c_str());
  3184. // add alias
  3185. aliasedProperties.insert
  3186. (std::pair<cmStdString,cmProperty::ScopeType>(tmp,i->first));
  3187. }
  3188. }
  3189. if (j->first.find("<LANG>") != std::string::npos)
  3190. {
  3191. std::vector<std::string>::const_iterator k;
  3192. for (k = enLangs.begin(); k != enLangs.end(); ++k)
  3193. {
  3194. std::string tmp = j->first;
  3195. cmSystemTools::ReplaceString(tmp, "<LANG>", k->c_str());
  3196. // add alias
  3197. aliasedProperties.insert
  3198. (std::pair<cmStdString,cmProperty::ScopeType>(tmp,i->first));
  3199. }
  3200. }
  3201. }
  3202. }
  3203. std::set<std::pair<cmStdString,cmProperty::ScopeType> >::const_iterator ap;
  3204. ap = this->AccessedProperties.begin();
  3205. for (;ap != this->AccessedProperties.end(); ++ap)
  3206. {
  3207. if (!this->IsPropertyDefined(ap->first.c_str(),ap->second) &&
  3208. aliasedProperties.find(std::pair<cmStdString,cmProperty::ScopeType>
  3209. (ap->first,ap->second)) ==
  3210. aliasedProperties.end())
  3211. {
  3212. const char *scopeStr = "";
  3213. switch (ap->second)
  3214. {
  3215. case cmProperty::TARGET:
  3216. scopeStr = "TARGET";
  3217. break;
  3218. case cmProperty::SOURCE_FILE:
  3219. scopeStr = "SOURCE_FILE";
  3220. break;
  3221. case cmProperty::DIRECTORY:
  3222. scopeStr = "DIRECTORY";
  3223. break;
  3224. case cmProperty::TEST:
  3225. scopeStr = "TEST";
  3226. break;
  3227. case cmProperty::VARIABLE:
  3228. scopeStr = "VARIABLE";
  3229. break;
  3230. case cmProperty::CACHED_VARIABLE:
  3231. scopeStr = "CACHED_VARIABLE";
  3232. break;
  3233. default:
  3234. scopeStr = "unknown";
  3235. break;
  3236. }
  3237. fprintf(progFile,"%s with scope %s\n",ap->first.c_str(),scopeStr);
  3238. }
  3239. }
  3240. fclose(progFile);
  3241. }
  3242. bool cmake::IsPropertyDefined(const char *name, cmProperty::ScopeType scope)
  3243. {
  3244. return this->PropertyDefinitions[scope].IsPropertyDefined(name);
  3245. }
  3246. bool cmake::IsPropertyChained(const char *name, cmProperty::ScopeType scope)
  3247. {
  3248. return this->PropertyDefinitions[scope].IsPropertyChained(name);
  3249. }
  3250. void cmake::SetProperty(const char* prop, const char* value)
  3251. {
  3252. if (!prop)
  3253. {
  3254. return;
  3255. }
  3256. if (!value)
  3257. {
  3258. value = "NOTFOUND";
  3259. }
  3260. this->Properties.SetProperty(prop, value, cmProperty::GLOBAL);
  3261. }
  3262. void cmake::AppendProperty(const char* prop, const char* value)
  3263. {
  3264. if (!prop)
  3265. {
  3266. return;
  3267. }
  3268. this->Properties.AppendProperty(prop, value, cmProperty::GLOBAL);
  3269. }
  3270. const char *cmake::GetProperty(const char* prop)
  3271. {
  3272. return this->GetProperty(prop, cmProperty::GLOBAL);
  3273. }
  3274. const char *cmake::GetProperty(const char* prop, cmProperty::ScopeType scope)
  3275. {
  3276. bool chain = false;
  3277. // watch for special properties
  3278. std::string propname = prop;
  3279. std::string output = "";
  3280. if ( propname == "CACHE_VARIABLES" )
  3281. {
  3282. cmCacheManager::CacheIterator cit =
  3283. this->GetCacheManager()->GetCacheIterator();
  3284. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  3285. {
  3286. if ( output.size() )
  3287. {
  3288. output += ";";
  3289. }
  3290. output += cit.GetName();
  3291. }
  3292. this->SetProperty("CACHE_VARIABLES", output.c_str());
  3293. }
  3294. else if ( propname == "COMMANDS" )
  3295. {
  3296. cmake::RegisteredCommandsMap::iterator cmds
  3297. = this->GetCommands()->begin();
  3298. for (unsigned int cc=0 ; cmds != this->GetCommands()->end(); ++ cmds )
  3299. {
  3300. if ( cc > 0 )
  3301. {
  3302. output += ";";
  3303. }
  3304. output += cmds->first.c_str();
  3305. cc++;
  3306. }
  3307. this->SetProperty("COMMANDS",output.c_str());
  3308. }
  3309. else if ( propname == "IN_TRY_COMPILE" )
  3310. {
  3311. this->SetProperty("IN_TRY_COMPILE",
  3312. this->GetIsInTryCompile()? "1":"0");
  3313. }
  3314. return this->Properties.GetPropertyValue(prop, scope, chain);
  3315. }
  3316. bool cmake::GetPropertyAsBool(const char* prop)
  3317. {
  3318. return cmSystemTools::IsOn(this->GetProperty(prop));
  3319. }
  3320. int cmake::GetSystemInformation(std::vector<std::string>& args)
  3321. {
  3322. // so create the directory
  3323. std::string resultFile;
  3324. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  3325. std::string destPath = cwd + "/__cmake_systeminformation";
  3326. cmSystemTools::RemoveADirectory(destPath.c_str());
  3327. if (!cmSystemTools::MakeDirectory(destPath.c_str()))
  3328. {
  3329. std::cerr << "Error: --system-information must be run from a "
  3330. "writable directory!\n";
  3331. return 1;
  3332. }
  3333. // process the arguments
  3334. bool writeToStdout = true;
  3335. for(unsigned int i=1; i < args.size(); ++i)
  3336. {
  3337. std::string arg = args[i];
  3338. if(arg.find("-V",0) == 0)
  3339. {
  3340. this->Verbose = true;
  3341. }
  3342. else if(arg.find("-G",0) == 0)
  3343. {
  3344. std::string value = arg.substr(2);
  3345. if(value.size() == 0)
  3346. {
  3347. ++i;
  3348. if(i >= args.size())
  3349. {
  3350. cmSystemTools::Error("No generator specified for -G");
  3351. return -1;
  3352. }
  3353. value = args[i];
  3354. }
  3355. cmGlobalGenerator* gen =
  3356. this->CreateGlobalGenerator(value.c_str());
  3357. if(!gen)
  3358. {
  3359. cmSystemTools::Error("Could not create named generator ",
  3360. value.c_str());
  3361. }
  3362. else
  3363. {
  3364. this->SetGlobalGenerator(gen);
  3365. }
  3366. }
  3367. // no option assume it is the output file
  3368. else
  3369. {
  3370. if (!cmSystemTools::FileIsFullPath(arg.c_str()))
  3371. {
  3372. resultFile = cwd;
  3373. resultFile += "/";
  3374. }
  3375. resultFile += arg;
  3376. writeToStdout = false;
  3377. }
  3378. }
  3379. // we have to find the module directory, so we can copy the files
  3380. this->AddCMakePaths();
  3381. std::string modulesPath =
  3382. this->CacheManager->GetCacheValue("CMAKE_ROOT");
  3383. modulesPath += "/Modules";
  3384. std::string inFile = modulesPath;
  3385. inFile += "/SystemInformation.cmake";
  3386. std::string outFile = destPath;
  3387. outFile += "/CMakeLists.txt";
  3388. // Copy file
  3389. if(!cmSystemTools::cmCopyFile(inFile.c_str(), outFile.c_str()))
  3390. {
  3391. std::cerr << "Error copying file \"" << inFile.c_str()
  3392. << "\" to \"" << outFile.c_str() << "\".\n";
  3393. return 1;
  3394. }
  3395. // do we write to a file or to stdout?
  3396. if (resultFile.size() == 0)
  3397. {
  3398. resultFile = cwd;
  3399. resultFile += "/__cmake_systeminformation/results.txt";
  3400. }
  3401. // now run cmake on the CMakeLists file
  3402. cmSystemTools::ChangeDirectory(destPath.c_str());
  3403. std::vector<std::string> args2;
  3404. args2.push_back(args[0]);
  3405. args2.push_back(destPath);
  3406. std::string resultArg = "-DRESULT_FILE=";
  3407. resultArg += resultFile;
  3408. args2.push_back(resultArg);
  3409. int res = this->Run(args2, false);
  3410. if (res != 0)
  3411. {
  3412. std::cerr << "Error: --system-information failed on internal CMake!\n";
  3413. return res;
  3414. }
  3415. // change back to the original directory
  3416. cmSystemTools::ChangeDirectory(cwd.c_str());
  3417. // echo results to stdout if needed
  3418. if (writeToStdout)
  3419. {
  3420. FILE* fin = fopen(resultFile.c_str(), "r");
  3421. if(fin)
  3422. {
  3423. const int bufferSize = 4096;
  3424. char buffer[bufferSize];
  3425. size_t n;
  3426. while((n = fread(buffer, 1, bufferSize, fin)) > 0)
  3427. {
  3428. for(char* c = buffer; c < buffer+n; ++c)
  3429. {
  3430. putc(*c, stdout);
  3431. }
  3432. fflush(stdout);
  3433. }
  3434. fclose(fin);
  3435. }
  3436. }
  3437. // clean up the directory
  3438. cmSystemTools::RemoveADirectory(destPath.c_str());
  3439. return 0;
  3440. }
  3441. //----------------------------------------------------------------------------
  3442. static bool cmakeCheckStampFile(const char* stampName)
  3443. {
  3444. // If the stamp file still exists then it must really be out of
  3445. // date.
  3446. if(cmSystemTools::FileExists(stampName))
  3447. {
  3448. return false;
  3449. }
  3450. // The stamp file does not exist. Use the stamp dependencies to
  3451. // determine whether it is really out of date. This works in
  3452. // conjunction with cmLocalVisualStudio7Generator to avoid
  3453. // repeatedly re-running CMake when the user rebuilds the entire
  3454. // solution.
  3455. std::string stampDepends = stampName;
  3456. stampDepends += ".depend";
  3457. #if defined(_WIN32) || defined(__CYGWIN__)
  3458. std::ifstream fin(stampDepends.c_str(), std::ios::in | std::ios::binary);
  3459. #else
  3460. std::ifstream fin(stampDepends.c_str(), std::ios::in);
  3461. #endif
  3462. if(!fin)
  3463. {
  3464. // The stamp dependencies file cannot be read. Just assume the
  3465. // build system is really out of date.
  3466. return false;
  3467. }
  3468. // Compare the stamp dependencies against the dependency file itself.
  3469. cmFileTimeComparison ftc;
  3470. std::string dep;
  3471. while(cmSystemTools::GetLineFromStream(fin, dep))
  3472. {
  3473. int result;
  3474. if(dep.length() >= 1 && dep[0] != '#' &&
  3475. (!ftc.FileTimeCompare(stampDepends.c_str(), dep.c_str(), &result)
  3476. || result < 0))
  3477. {
  3478. // The stamp depends file is older than this dependency. The
  3479. // build system is really out of date.
  3480. return false;
  3481. }
  3482. }
  3483. // The build system is up to date. The stamp file has been removed
  3484. // by the VS IDE due to a "rebuild" request. Just restore it.
  3485. std::ofstream stamp(stampName);
  3486. stamp << "# CMake generation timestamp file this directory.\n";
  3487. if(stamp)
  3488. {
  3489. // Notify the user why CMake is not re-running. It is safe to
  3490. // just print to stdout here because this code is only reachable
  3491. // through an undocumented flag used by the VS generator.
  3492. std::cout << "CMake does not need to re-run because the "
  3493. << "generation timestamp is up-to-date.\n";
  3494. return true;
  3495. }
  3496. else
  3497. {
  3498. cmSystemTools::Error("Cannot restore timestamp ", stampName);
  3499. return false;
  3500. }
  3501. }
  3502. // For visual studio 2005 and newer manifest files need to be embeded into
  3503. // exe and dll's. This code does that in such a way that incremental linking
  3504. // still works.
  3505. int cmake::VisualStudioLink(std::vector<std::string>& args, int type)
  3506. {
  3507. if(args.size() < 2)
  3508. {
  3509. return -1;
  3510. }
  3511. bool verbose = false;
  3512. if(cmSystemTools::GetEnv("VERBOSE"))
  3513. {
  3514. verbose = true;
  3515. }
  3516. std::vector<std::string> expandedArgs;
  3517. for(std::vector<std::string>::iterator i = args.begin();
  3518. i != args.end(); ++i)
  3519. {
  3520. // check for nmake temporary files
  3521. if((*i)[0] == '@')
  3522. {
  3523. std::ifstream fin(i->substr(1).c_str());
  3524. std::string line;
  3525. while(cmSystemTools::GetLineFromStream(fin,
  3526. line))
  3527. {
  3528. cmSystemTools::ParseWindowsCommandLine(line.c_str(), expandedArgs);
  3529. }
  3530. }
  3531. else
  3532. {
  3533. expandedArgs.push_back(*i);
  3534. }
  3535. }
  3536. // figure out if this is an incremental link or not and run the correct
  3537. // link function.
  3538. for(std::vector<std::string>::iterator i = expandedArgs.begin();
  3539. i != expandedArgs.end(); ++i)
  3540. {
  3541. if(cmSystemTools::Strucmp(i->c_str(), "/INCREMENTAL:YES") == 0)
  3542. {
  3543. if(verbose)
  3544. {
  3545. std::cout << "Visual Studio Incremental Link\n";
  3546. }
  3547. return cmake::VisualStudioLinkIncremental(expandedArgs, type, verbose);
  3548. }
  3549. }
  3550. if(verbose)
  3551. {
  3552. std::cout << "Visual Studio Non-Incremental Link\n";
  3553. }
  3554. return cmake::VisualStudioLinkNonIncremental(expandedArgs, type, verbose);
  3555. }
  3556. int cmake::ParseVisualStudioLinkCommand(std::vector<std::string>& args,
  3557. std::vector<cmStdString>& command,
  3558. std::string& targetName)
  3559. {
  3560. std::vector<std::string>::iterator i = args.begin();
  3561. i++; // skip -E
  3562. i++; // skip vs_link_dll or vs_link_exe
  3563. command.push_back(*i);
  3564. i++; // move past link command
  3565. for(; i != args.end(); ++i)
  3566. {
  3567. command.push_back(*i);
  3568. if(i->find("/Fe") == 0)
  3569. {
  3570. targetName = i->substr(3);
  3571. }
  3572. if(i->find("/out:") == 0)
  3573. {
  3574. targetName = i->substr(5);
  3575. }
  3576. }
  3577. if(targetName.size() == 0 || command.size() == 0)
  3578. {
  3579. return -1;
  3580. }
  3581. return 0;
  3582. }
  3583. bool cmake::RunCommand(const char* comment,
  3584. std::vector<cmStdString>& command,
  3585. bool verbose,
  3586. int* retCodeOut)
  3587. {
  3588. if(verbose)
  3589. {
  3590. std::cout << comment << ":\n";
  3591. for(std::vector<cmStdString>::iterator i = command.begin();
  3592. i != command.end(); ++i)
  3593. {
  3594. std::cout << i->c_str() << " ";
  3595. }
  3596. std::cout << "\n";
  3597. }
  3598. std::string output;
  3599. int retCode =0;
  3600. // use rc command to create .res file
  3601. cmSystemTools::RunSingleCommand(command,
  3602. &output,
  3603. &retCode);
  3604. if(verbose)
  3605. {
  3606. std::cout << output << "\n";
  3607. }
  3608. // if retCodeOut is requested then always return true
  3609. // and set the retCodeOut to retCode
  3610. if(retCodeOut)
  3611. {
  3612. *retCodeOut = retCode;
  3613. return true;
  3614. }
  3615. if(retCode != 0)
  3616. {
  3617. std::cout << comment << " failed. with " << retCode << "\n";
  3618. }
  3619. return retCode == 0;
  3620. }
  3621. int cmake::VisualStudioLinkIncremental(std::vector<std::string>& args,
  3622. int type, bool verbose)
  3623. {
  3624. // This follows the steps listed here:
  3625. // http://blogs.msdn.com/zakramer/archive/2006/05/22/603558.aspx
  3626. // 1. Compiler compiles the application and generates the *.obj files.
  3627. // 2. An empty manifest file is generated if this is a clean build and if
  3628. // not the previous one is reused.
  3629. // 3. The resource compiler (rc.exe) compiles the *.manifest file to a
  3630. // *.res file.
  3631. // 4. Linker generates the binary (EXE or DLL) with the /incremental
  3632. // switch and embeds the dummy manifest file. The linker also generates
  3633. // the real manifest file based on the binaries that your binary depends
  3634. // on.
  3635. // 5. The manifest tool (mt.exe) is then used to generate the final
  3636. // manifest.
  3637. // If the final manifest is changed, then 6 and 7 are run, if not
  3638. // they are skipped, and it is done.
  3639. // 6. The resource compiler is invoked one more time.
  3640. // 7. Finally, the Linker does another incremental link, but since the
  3641. // only thing that has changed is the *.res file that contains the
  3642. // manifest it is a short link.
  3643. std::vector<cmStdString> linkCommand;
  3644. std::string targetName;
  3645. if(cmake::ParseVisualStudioLinkCommand(args, linkCommand, targetName) == -1)
  3646. {
  3647. return -1;
  3648. }
  3649. std::string manifestArg = "/MANIFESTFILE:";
  3650. std::vector<cmStdString> rcCommand;
  3651. rcCommand.push_back(cmSystemTools::FindProgram("rc.exe"));
  3652. std::vector<cmStdString> mtCommand;
  3653. mtCommand.push_back(cmSystemTools::FindProgram("mt.exe"));
  3654. std::string tempManifest;
  3655. tempManifest = targetName;
  3656. tempManifest += ".intermediate.manifest";
  3657. std::string resourceInputFile = targetName;
  3658. resourceInputFile += ".resource.txt";
  3659. if(verbose)
  3660. {
  3661. std::cout << "Create " << resourceInputFile.c_str() << "\n";
  3662. }
  3663. // Create input file for rc command
  3664. std::ofstream fout(resourceInputFile.c_str());
  3665. if(!fout)
  3666. {
  3667. return -1;
  3668. }
  3669. std::string manifestFile = targetName;
  3670. manifestFile += ".embed.manifest";
  3671. std::string fullPath= cmSystemTools::CollapseFullPath(manifestFile.c_str());
  3672. fout << type << " /* CREATEPROCESS_MANIFEST_RESOURCE_ID "
  3673. "*/ 24 /* RT_MANIFEST */ " << "\"" << fullPath.c_str() << "\"";
  3674. fout.close();
  3675. manifestArg += tempManifest;
  3676. // add the manifest arg to the linkCommand
  3677. linkCommand.push_back(manifestArg);
  3678. // if manifestFile is not yet created, create an
  3679. // empty one
  3680. if(!cmSystemTools::FileExists(manifestFile.c_str()))
  3681. {
  3682. if(verbose)
  3683. {
  3684. std::cout << "Create empty: " << manifestFile.c_str() << "\n";
  3685. }
  3686. std::ofstream foutTmp(manifestFile.c_str());
  3687. }
  3688. std::string resourceFile = manifestFile;
  3689. resourceFile += ".res";
  3690. // add the resource file to the end of the link command
  3691. linkCommand.push_back(resourceFile);
  3692. std::string outputOpt = "/fo";
  3693. outputOpt += resourceFile;
  3694. rcCommand.push_back(outputOpt);
  3695. rcCommand.push_back(resourceInputFile);
  3696. // Run rc command to create resource
  3697. if(!cmake::RunCommand("RC Pass 1", rcCommand, verbose))
  3698. {
  3699. return -1;
  3700. }
  3701. // Now run the link command to link and create manifest
  3702. if(!cmake::RunCommand("LINK Pass 1", linkCommand, verbose))
  3703. {
  3704. return -1;
  3705. }
  3706. // create mt command
  3707. std::string outArg("/out:");
  3708. outArg+= manifestFile;
  3709. mtCommand.push_back("/nologo");
  3710. mtCommand.push_back(outArg);
  3711. mtCommand.push_back("/notify_update");
  3712. mtCommand.push_back("/manifest");
  3713. mtCommand.push_back(tempManifest);
  3714. // now run mt.exe to create the final manifest file
  3715. int mtRet =0;
  3716. cmake::RunCommand("MT", mtCommand, verbose, &mtRet);
  3717. // if mt returns 0, then the manifest was not changed and
  3718. // we do not need to do another link step
  3719. if(mtRet == 0)
  3720. {
  3721. return 0;
  3722. }
  3723. // check for magic mt return value if mt returns the magic number
  3724. // 1090650113 then it means that it updated the manifest file and we need
  3725. // to do the final link. If mt has any value other than 0 or 1090650113
  3726. // then there was some problem with the command itself and there was an
  3727. // error so return the error code back out of cmake so make can report it.
  3728. if(mtRet != 1090650113)
  3729. {
  3730. return mtRet;
  3731. }
  3732. // update the resource file with the new manifest from the mt command.
  3733. if(!cmake::RunCommand("RC Pass 2", rcCommand, verbose))
  3734. {
  3735. return -1;
  3736. }
  3737. // Run the final incremental link that will put the new manifest resource
  3738. // into the file incrementally.
  3739. if(!cmake::RunCommand("FINAL LINK", linkCommand, verbose))
  3740. {
  3741. return -1;
  3742. }
  3743. return 0;
  3744. }
  3745. int cmake::VisualStudioLinkNonIncremental(std::vector<std::string>& args,
  3746. int type,
  3747. bool verbose)
  3748. {
  3749. std::vector<cmStdString> linkCommand;
  3750. std::string targetName;
  3751. if(cmake::ParseVisualStudioLinkCommand(args, linkCommand, targetName) == -1)
  3752. {
  3753. return -1;
  3754. }
  3755. // Run the link command as given
  3756. if(!cmake::RunCommand("LINK", linkCommand, verbose))
  3757. {
  3758. return -1;
  3759. }
  3760. std::vector<cmStdString> mtCommand;
  3761. mtCommand.push_back(cmSystemTools::FindProgram("mt.exe"));
  3762. mtCommand.push_back("/nologo");
  3763. mtCommand.push_back("/manifest");
  3764. std::string manifestFile = targetName;
  3765. manifestFile += ".manifest";
  3766. mtCommand.push_back(manifestFile);
  3767. std::string outresource = "/outputresource:";
  3768. outresource += targetName;
  3769. outresource += ";#";
  3770. if(type == 1)
  3771. {
  3772. outresource += "1";
  3773. }
  3774. else if(type == 2)
  3775. {
  3776. outresource += "2";
  3777. }
  3778. mtCommand.push_back(outresource);
  3779. // Now use the mt tool to embed the manifest into the exe or dll
  3780. if(!cmake::RunCommand("MT", mtCommand, verbose))
  3781. {
  3782. return -1;
  3783. }
  3784. return 0;
  3785. }