cmake.cxx 122 KB

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