cmake.cxx 140 KB

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