cmake.cxx 139 KB

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