cmake.cxx 139 KB

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