cmake.cxx 134 KB

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