cmake.cxx 117 KB

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