cmake.cxx 138 KB

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