cmMakefile.cxx 133 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438
  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 "cmMakefile.h"
  11. #include "cmVersion.h"
  12. #include "cmCommand.h"
  13. #include "cmSourceFile.h"
  14. #include "cmSourceFileLocation.h"
  15. #include "cmSystemTools.h"
  16. #include "cmGlobalGenerator.h"
  17. #include "cmLocalGenerator.h"
  18. #include "cmCommands.h"
  19. #include "cmCacheManager.h"
  20. #include "cmFunctionBlocker.h"
  21. #include "cmListFileCache.h"
  22. #include "cmCommandArgumentParserHelper.h"
  23. #include "cmGeneratorExpression.h"
  24. #include "cmTest.h"
  25. #ifdef CMAKE_BUILD_WITH_CMAKE
  26. # include "cmVariableWatch.h"
  27. #endif
  28. #include "cmInstallGenerator.h"
  29. #include "cmTestGenerator.h"
  30. #include "cmDefinitions.h"
  31. #include "cmake.h"
  32. #include <stdlib.h> // required for atoi
  33. #include <cmsys/RegularExpression.hxx>
  34. #include <cmsys/auto_ptr.hxx>
  35. #include <stack>
  36. #include <ctype.h> // for isspace
  37. #include <assert.h>
  38. class cmMakefile::Internals
  39. {
  40. public:
  41. std::stack<cmDefinitions, std::list<cmDefinitions> > VarStack;
  42. std::stack<std::set<cmStdString> > VarInitStack;
  43. std::stack<std::set<cmStdString> > VarUsageStack;
  44. bool IsSourceFileTryCompile;
  45. };
  46. // default is not to be building executables
  47. cmMakefile::cmMakefile(): Internal(new Internals)
  48. {
  49. const cmDefinitions& defs = cmDefinitions();
  50. const std::set<cmStdString> globalKeys = defs.LocalKeys();
  51. this->Internal->VarStack.push(defs);
  52. this->Internal->VarInitStack.push(globalKeys);
  53. this->Internal->VarUsageStack.push(globalKeys);
  54. this->Internal->IsSourceFileTryCompile = false;
  55. // Initialize these first since AddDefaultDefinitions calls AddDefinition
  56. this->WarnUnused = false;
  57. this->CheckSystemVars = false;
  58. // Setup the default include file regular expression (match everything).
  59. this->IncludeFileRegularExpression = "^.*$";
  60. // Setup the default include complaint regular expression (match nothing).
  61. this->ComplainFileRegularExpression = "^$";
  62. // Source and header file extensions that we can handle
  63. // Set up a list of source and header extensions
  64. // these are used to find files when the extension
  65. // is not given
  66. // The "c" extension MUST precede the "C" extension.
  67. this->SourceFileExtensions.push_back( "c" );
  68. this->SourceFileExtensions.push_back( "C" );
  69. this->SourceFileExtensions.push_back( "c++" );
  70. this->SourceFileExtensions.push_back( "cc" );
  71. this->SourceFileExtensions.push_back( "cpp" );
  72. this->SourceFileExtensions.push_back( "cxx" );
  73. this->SourceFileExtensions.push_back( "m" );
  74. this->SourceFileExtensions.push_back( "M" );
  75. this->SourceFileExtensions.push_back( "mm" );
  76. this->HeaderFileExtensions.push_back( "h" );
  77. this->HeaderFileExtensions.push_back( "hh" );
  78. this->HeaderFileExtensions.push_back( "h++" );
  79. this->HeaderFileExtensions.push_back( "hm" );
  80. this->HeaderFileExtensions.push_back( "hpp" );
  81. this->HeaderFileExtensions.push_back( "hxx" );
  82. this->HeaderFileExtensions.push_back( "in" );
  83. this->HeaderFileExtensions.push_back( "txx" );
  84. this->DefineFlags = " ";
  85. this->LocalGenerator = 0;
  86. this->AddDefaultDefinitions();
  87. this->Initialize();
  88. this->PreOrder = false;
  89. this->GeneratingBuildSystem = false;
  90. }
  91. cmMakefile::cmMakefile(const cmMakefile& mf): Internal(new Internals)
  92. {
  93. this->Internal->VarStack.push(mf.Internal->VarStack.top().Closure());
  94. this->Internal->VarInitStack.push(mf.Internal->VarInitStack.top());
  95. this->Internal->VarUsageStack.push(mf.Internal->VarUsageStack.top());
  96. this->Prefix = mf.Prefix;
  97. this->AuxSourceDirectories = mf.AuxSourceDirectories;
  98. this->cmStartDirectory = mf.cmStartDirectory;
  99. this->StartOutputDirectory = mf.StartOutputDirectory;
  100. this->cmHomeDirectory = mf.cmHomeDirectory;
  101. this->HomeOutputDirectory = mf.HomeOutputDirectory;
  102. this->cmCurrentListFile = mf.cmCurrentListFile;
  103. this->ProjectName = mf.ProjectName;
  104. this->Targets = mf.Targets;
  105. this->SourceFiles = mf.SourceFiles;
  106. this->Tests = mf.Tests;
  107. this->LinkDirectories = mf.LinkDirectories;
  108. this->SystemIncludeDirectories = mf.SystemIncludeDirectories;
  109. this->ListFiles = mf.ListFiles;
  110. this->OutputFiles = mf.OutputFiles;
  111. this->LinkLibraries = mf.LinkLibraries;
  112. this->InstallGenerators = mf.InstallGenerators;
  113. this->TestGenerators = mf.TestGenerators;
  114. this->IncludeFileRegularExpression = mf.IncludeFileRegularExpression;
  115. this->ComplainFileRegularExpression = mf.ComplainFileRegularExpression;
  116. this->SourceFileExtensions = mf.SourceFileExtensions;
  117. this->HeaderFileExtensions = mf.HeaderFileExtensions;
  118. this->DefineFlags = mf.DefineFlags;
  119. this->DefineFlagsOrig = mf.DefineFlagsOrig;
  120. #if defined(CMAKE_BUILD_WITH_CMAKE)
  121. this->SourceGroups = mf.SourceGroups;
  122. #endif
  123. this->LocalGenerator = mf.LocalGenerator;
  124. this->FunctionBlockers = mf.FunctionBlockers;
  125. this->MacrosMap = mf.MacrosMap;
  126. this->SubDirectoryOrder = mf.SubDirectoryOrder;
  127. this->Properties = mf.Properties;
  128. this->PreOrder = mf.PreOrder;
  129. this->WarnUnused = mf.WarnUnused;
  130. this->Initialize();
  131. this->CheckSystemVars = mf.CheckSystemVars;
  132. this->ListFileStack = mf.ListFileStack;
  133. this->OutputToSource = mf.OutputToSource;
  134. }
  135. //----------------------------------------------------------------------------
  136. void cmMakefile::Initialize()
  137. {
  138. this->cmDefineRegex.compile("#cmakedefine[ \t]+([A-Za-z_0-9]*)");
  139. this->cmDefine01Regex.compile("#cmakedefine01[ \t]+([A-Za-z_0-9]*)");
  140. this->cmAtVarRegex.compile("(@[A-Za-z_0-9/.+-]+@)");
  141. // Enter a policy level for this directory.
  142. this->PushPolicy();
  143. // Protect the directory-level policies.
  144. this->PushPolicyBarrier();
  145. // By default the check is not done. It is enabled by
  146. // cmListFileCache in the top level if necessary.
  147. this->CheckCMP0000 = false;
  148. }
  149. unsigned int cmMakefile::GetCacheMajorVersion()
  150. {
  151. return this->GetCacheManager()->GetCacheMajorVersion();
  152. }
  153. unsigned int cmMakefile::GetCacheMinorVersion()
  154. {
  155. return this->GetCacheManager()->GetCacheMinorVersion();
  156. }
  157. bool cmMakefile::NeedCacheCompatibility(int major, int minor)
  158. {
  159. return this->GetCacheManager()->NeedCacheCompatibility(major, minor);
  160. }
  161. cmMakefile::~cmMakefile()
  162. {
  163. for(std::vector<cmInstallGenerator*>::iterator
  164. i = this->InstallGenerators.begin();
  165. i != this->InstallGenerators.end(); ++i)
  166. {
  167. delete *i;
  168. }
  169. for(std::vector<cmTestGenerator*>::iterator
  170. i = this->TestGenerators.begin();
  171. i != this->TestGenerators.end(); ++i)
  172. {
  173. delete *i;
  174. }
  175. for(std::vector<cmSourceFile*>::iterator i = this->SourceFiles.begin();
  176. i != this->SourceFiles.end(); ++i)
  177. {
  178. delete *i;
  179. }
  180. for(std::map<cmStdString, cmTest*>::iterator i = this->Tests.begin();
  181. i != this->Tests.end(); ++i)
  182. {
  183. delete i->second;
  184. }
  185. for(std::vector<cmTarget*>::iterator
  186. i = this->ImportedTargetsOwned.begin();
  187. i != this->ImportedTargetsOwned.end(); ++i)
  188. {
  189. delete *i;
  190. }
  191. for(unsigned int i=0; i < this->FinalPassCommands.size(); i++)
  192. {
  193. delete this->FinalPassCommands[i];
  194. }
  195. std::vector<cmFunctionBlocker*>::iterator pos;
  196. for (pos = this->FunctionBlockers.begin();
  197. pos != this->FunctionBlockers.end(); ++pos)
  198. {
  199. cmFunctionBlocker* b = *pos;
  200. delete b;
  201. }
  202. this->FunctionBlockers.clear();
  203. if (this->PolicyStack.size() != 1)
  204. {
  205. cmSystemTools::Error("Internal CMake Error, Policy Stack has not been"
  206. " popped properly");
  207. }
  208. }
  209. void cmMakefile::PrintStringVector(const char* s,
  210. const std::vector<std::string>& v) const
  211. {
  212. std::cout << s << ": ( \n";
  213. for(std::vector<std::string>::const_iterator i = v.begin();
  214. i != v.end(); ++i)
  215. {
  216. std::cout << (*i).c_str() << " ";
  217. }
  218. std::cout << " )\n";
  219. }
  220. void cmMakefile
  221. ::PrintStringVector(const char* s,
  222. const std::vector<std::pair<cmStdString, bool> >& v) const
  223. {
  224. std::cout << s << ": ( \n";
  225. for(std::vector<std::pair<cmStdString, bool> >::const_iterator i
  226. = v.begin(); i != v.end(); ++i)
  227. {
  228. std::cout << i->first.c_str() << " " << i->second;
  229. }
  230. std::cout << " )\n";
  231. }
  232. // call print on all the classes in the makefile
  233. void cmMakefile::Print()
  234. {
  235. // print the class lists
  236. std::cout << "classes:\n";
  237. std::cout << " this->Targets: ";
  238. for (cmTargets::iterator l = this->Targets.begin();
  239. l != this->Targets.end(); l++)
  240. {
  241. std::cout << l->first << std::endl;
  242. }
  243. std::cout << " this->StartOutputDirectory; " <<
  244. this->StartOutputDirectory.c_str() << std::endl;
  245. std::cout << " this->HomeOutputDirectory; " <<
  246. this->HomeOutputDirectory.c_str() << std::endl;
  247. std::cout << " this->cmStartDirectory; " <<
  248. this->cmStartDirectory.c_str() << std::endl;
  249. std::cout << " this->cmHomeDirectory; " <<
  250. this->cmHomeDirectory.c_str() << std::endl;
  251. std::cout << " this->ProjectName; "
  252. << this->ProjectName.c_str() << std::endl;
  253. this->PrintStringVector("this->LinkDirectories", this->LinkDirectories);
  254. #if defined(CMAKE_BUILD_WITH_CMAKE)
  255. for( std::vector<cmSourceGroup>::const_iterator i =
  256. this->SourceGroups.begin(); i != this->SourceGroups.end(); ++i)
  257. {
  258. std::cout << "Source Group: " << i->GetName() << std::endl;
  259. }
  260. #endif
  261. }
  262. bool cmMakefile::CommandExists(const char* name) const
  263. {
  264. return this->GetCMakeInstance()->CommandExists(name);
  265. }
  266. //----------------------------------------------------------------------------
  267. void cmMakefile::IssueMessage(cmake::MessageType t,
  268. std::string const& text) const
  269. {
  270. // Collect context information.
  271. cmListFileBacktrace backtrace;
  272. if(!this->CallStack.empty())
  273. {
  274. if((t == cmake::FATAL_ERROR) || (t == cmake::INTERNAL_ERROR))
  275. {
  276. this->CallStack.back().Status->SetNestedError(true);
  277. }
  278. this->GetBacktrace(backtrace);
  279. }
  280. else
  281. {
  282. cmListFileContext lfc;
  283. if(this->ListFileStack.empty())
  284. {
  285. // We are not processing the project. Add the directory-level context.
  286. lfc.FilePath = this->GetCurrentDirectory();
  287. lfc.FilePath += "/CMakeLists.txt";
  288. }
  289. else
  290. {
  291. // We are processing the project but are not currently executing a
  292. // command. Add whatever context information we have.
  293. lfc.FilePath = this->ListFileStack.back();
  294. }
  295. lfc.Line = 0;
  296. if(!this->GetCMakeInstance()->GetIsInTryCompile())
  297. {
  298. lfc.FilePath = this->LocalGenerator->Convert(lfc.FilePath.c_str(),
  299. cmLocalGenerator::HOME);
  300. }
  301. backtrace.push_back(lfc);
  302. }
  303. // Issue the message.
  304. this->GetCMakeInstance()->IssueMessage(t, text, backtrace);
  305. }
  306. //----------------------------------------------------------------------------
  307. bool cmMakefile::GetBacktrace(cmListFileBacktrace& backtrace) const
  308. {
  309. if(this->CallStack.empty())
  310. {
  311. return false;
  312. }
  313. for(CallStackType::const_reverse_iterator i = this->CallStack.rbegin();
  314. i != this->CallStack.rend(); ++i)
  315. {
  316. cmListFileContext lfc = *(*i).Context;
  317. lfc.FilePath = this->LocalGenerator->Convert(lfc.FilePath.c_str(),
  318. cmLocalGenerator::HOME);
  319. backtrace.push_back(lfc);
  320. }
  321. return true;
  322. }
  323. //----------------------------------------------------------------------------
  324. void cmMakefile::PrintCommandTrace(const cmListFileFunction& lff)
  325. {
  326. cmOStringStream msg;
  327. msg << lff.FilePath << "(" << lff.Line << "): ";
  328. msg << lff.Name << "(";
  329. for(std::vector<cmListFileArgument>::const_iterator i =
  330. lff.Arguments.begin(); i != lff.Arguments.end(); ++i)
  331. {
  332. msg << i->Value;
  333. msg << " ";
  334. }
  335. msg << ")";
  336. cmSystemTools::Message(msg.str().c_str());
  337. }
  338. //----------------------------------------------------------------------------
  339. bool cmMakefile::ExecuteCommand(const cmListFileFunction& lff,
  340. cmExecutionStatus &status)
  341. {
  342. bool result = true;
  343. // quick return if blocked
  344. if(this->IsFunctionBlocked(lff,status))
  345. {
  346. // No error.
  347. return result;
  348. }
  349. std::string name = lff.Name;
  350. // Place this call on the call stack.
  351. cmMakefileCall stack_manager(this, lff, status);
  352. static_cast<void>(stack_manager);
  353. // Lookup the command prototype.
  354. if(cmCommand* proto = this->GetCMakeInstance()->GetCommand(name.c_str()))
  355. {
  356. // Clone the prototype.
  357. cmsys::auto_ptr<cmCommand> pcmd(proto->Clone());
  358. pcmd->SetMakefile(this);
  359. // Decide whether to invoke the command.
  360. if(pcmd->GetEnabled() && !cmSystemTools::GetFatalErrorOccured() &&
  361. (this->GetCMakeInstance()->GetWorkingMode() != cmake::SCRIPT_MODE
  362. || pcmd->IsScriptable()))
  363. {
  364. // if trace is enabled, print out invoke information
  365. if(this->GetCMakeInstance()->GetTrace())
  366. {
  367. this->PrintCommandTrace(lff);
  368. }
  369. // Try invoking the command.
  370. if(!pcmd->InvokeInitialPass(lff.Arguments,status) ||
  371. status.GetNestedError())
  372. {
  373. if(!status.GetNestedError())
  374. {
  375. // The command invocation requested that we report an error.
  376. this->IssueMessage(cmake::FATAL_ERROR, pcmd->GetError());
  377. }
  378. result = false;
  379. if ( this->GetCMakeInstance()->GetWorkingMode() != cmake::NORMAL_MODE)
  380. {
  381. cmSystemTools::SetFatalErrorOccured();
  382. }
  383. }
  384. else if(pcmd->HasFinalPass())
  385. {
  386. // use the command
  387. this->FinalPassCommands.push_back(pcmd.release());
  388. }
  389. }
  390. else if ( this->GetCMakeInstance()->GetWorkingMode() == cmake::SCRIPT_MODE
  391. && !pcmd->IsScriptable() )
  392. {
  393. std::string error = "Command ";
  394. error += pcmd->GetName();
  395. error += "() is not scriptable";
  396. this->IssueMessage(cmake::FATAL_ERROR, error);
  397. result = false;
  398. cmSystemTools::SetFatalErrorOccured();
  399. }
  400. }
  401. else
  402. {
  403. if(!cmSystemTools::GetFatalErrorOccured())
  404. {
  405. std::string error = "Unknown CMake command \"";
  406. error += lff.Name;
  407. error += "\".";
  408. this->IssueMessage(cmake::FATAL_ERROR, error);
  409. result = false;
  410. cmSystemTools::SetFatalErrorOccured();
  411. }
  412. }
  413. return result;
  414. }
  415. //----------------------------------------------------------------------------
  416. class cmMakefile::IncludeScope
  417. {
  418. public:
  419. IncludeScope(cmMakefile* mf, const char* fname, bool noPolicyScope);
  420. ~IncludeScope();
  421. void Quiet() { this->ReportError = false; }
  422. private:
  423. cmMakefile* Makefile;
  424. const char* File;
  425. bool NoPolicyScope;
  426. bool CheckCMP0011;
  427. bool ReportError;
  428. void EnforceCMP0011();
  429. };
  430. //----------------------------------------------------------------------------
  431. cmMakefile::IncludeScope::IncludeScope(cmMakefile* mf, const char* fname,
  432. bool noPolicyScope):
  433. Makefile(mf), File(fname), NoPolicyScope(noPolicyScope),
  434. CheckCMP0011(false), ReportError(true)
  435. {
  436. if(!this->NoPolicyScope)
  437. {
  438. // Check CMP0011 to determine the policy scope type.
  439. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0011))
  440. {
  441. case cmPolicies::WARN:
  442. // We need to push a scope to detect whether the script sets
  443. // any policies that would affect the includer and therefore
  444. // requires a warning. We use a weak scope to simulate OLD
  445. // behavior by allowing policy changes to affect the includer.
  446. this->Makefile->PushPolicy(true);
  447. this->CheckCMP0011 = true;
  448. break;
  449. case cmPolicies::OLD:
  450. // OLD behavior is to not push a scope at all.
  451. this->NoPolicyScope = true;
  452. break;
  453. case cmPolicies::REQUIRED_IF_USED:
  454. case cmPolicies::REQUIRED_ALWAYS:
  455. // We should never make this policy required, but we handle it
  456. // here just in case.
  457. this->CheckCMP0011 = true;
  458. case cmPolicies::NEW:
  459. // NEW behavior is to push a (strong) scope.
  460. this->Makefile->PushPolicy();
  461. break;
  462. }
  463. }
  464. // The included file cannot pop our policy scope.
  465. this->Makefile->PushPolicyBarrier();
  466. }
  467. //----------------------------------------------------------------------------
  468. cmMakefile::IncludeScope::~IncludeScope()
  469. {
  470. // Enforce matching policy scopes inside the included file.
  471. this->Makefile->PopPolicyBarrier(this->ReportError);
  472. if(!this->NoPolicyScope)
  473. {
  474. // If we need to enforce policy CMP0011 then the top entry is the
  475. // one we pushed above. If the entry is empty, then the included
  476. // script did not set any policies that might affect the includer so
  477. // we do not need to enforce the policy.
  478. if(this->CheckCMP0011 && this->Makefile->PolicyStack.back().empty())
  479. {
  480. this->CheckCMP0011 = false;
  481. }
  482. // Pop the scope we pushed for the script.
  483. this->Makefile->PopPolicy();
  484. // We enforce the policy after the script's policy stack entry has
  485. // been removed.
  486. if(this->CheckCMP0011)
  487. {
  488. this->EnforceCMP0011();
  489. }
  490. }
  491. }
  492. //----------------------------------------------------------------------------
  493. void cmMakefile::IncludeScope::EnforceCMP0011()
  494. {
  495. // We check the setting of this policy again because the included
  496. // script might actually set this policy for its includer.
  497. cmPolicies* policies = this->Makefile->GetPolicies();
  498. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0011))
  499. {
  500. case cmPolicies::WARN:
  501. // Warn because the user did not set this policy.
  502. {
  503. cmOStringStream w;
  504. w << policies->GetPolicyWarning(cmPolicies::CMP0011) << "\n"
  505. << "The included script\n " << this->File << "\n"
  506. << "affects policy settings. "
  507. << "CMake is implying the NO_POLICY_SCOPE option for compatibility, "
  508. << "so the effects are applied to the including context.";
  509. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  510. }
  511. break;
  512. case cmPolicies::REQUIRED_IF_USED:
  513. case cmPolicies::REQUIRED_ALWAYS:
  514. {
  515. cmOStringStream e;
  516. e << policies->GetRequiredPolicyError(cmPolicies::CMP0011) << "\n"
  517. << "The included script\n " << this->File << "\n"
  518. << "affects policy settings, so it requires this policy to be set.";
  519. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  520. }
  521. break;
  522. case cmPolicies::OLD:
  523. case cmPolicies::NEW:
  524. // The script set this policy. We assume the purpose of the
  525. // script is to initialize policies for its includer, and since
  526. // the policy is now set for later scripts, we do not warn.
  527. break;
  528. }
  529. }
  530. //----------------------------------------------------------------------------
  531. // Parse the given CMakeLists.txt file executing all commands
  532. //
  533. bool cmMakefile::ReadListFile(const char* filename_in,
  534. const char *external_in,
  535. std::string* fullPath,
  536. bool noPolicyScope)
  537. {
  538. std::string currentParentFile
  539. = this->GetSafeDefinition("CMAKE_PARENT_LIST_FILE");
  540. std::string currentFile
  541. = this->GetSafeDefinition("CMAKE_CURRENT_LIST_FILE");
  542. this->AddDefinition("CMAKE_PARENT_LIST_FILE", filename_in);
  543. this->MarkVariableAsUsed("CMAKE_PARENT_LIST_FILE");
  544. const char* external = 0;
  545. std::string external_abs;
  546. const char* filename = filename_in;
  547. std::string filename_abs;
  548. if (external_in)
  549. {
  550. external_abs =
  551. cmSystemTools::CollapseFullPath(external_in,
  552. this->cmStartDirectory.c_str());
  553. external = external_abs.c_str();
  554. if (filename_in)
  555. {
  556. filename_abs =
  557. cmSystemTools::CollapseFullPath(filename_in,
  558. this->cmStartDirectory.c_str());
  559. filename = filename_abs.c_str();
  560. }
  561. }
  562. // keep track of the current file being read
  563. if (filename)
  564. {
  565. if(this->cmCurrentListFile != filename)
  566. {
  567. this->cmCurrentListFile = filename;
  568. }
  569. }
  570. // Now read the input file
  571. const char *filenametoread= filename;
  572. if( external)
  573. {
  574. filenametoread= external;
  575. }
  576. this->AddDefinition("CMAKE_CURRENT_LIST_FILE", filenametoread);
  577. this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_FILE");
  578. this->AddDefinition("CMAKE_CURRENT_LIST_DIR",
  579. cmSystemTools::GetFilenamePath(filenametoread).c_str());
  580. this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_DIR");
  581. // try to see if the list file is the top most
  582. // list file for a project, and if it is, then it
  583. // must have a project command. If there is not
  584. // one, then cmake will provide one via the
  585. // cmListFileCache class.
  586. bool requireProjectCommand = false;
  587. if(!external && this->cmStartDirectory == this->cmHomeDirectory)
  588. {
  589. if(cmSystemTools::LowerCase(
  590. cmSystemTools::GetFilenameName(filename)) == "cmakelists.txt")
  591. {
  592. requireProjectCommand = true;
  593. }
  594. }
  595. // push the listfile onto the stack
  596. this->ListFileStack.push_back(filenametoread);
  597. if(fullPath!=0)
  598. {
  599. *fullPath=filenametoread;
  600. }
  601. cmListFile cacheFile;
  602. if( !cacheFile.ParseFile(filenametoread, requireProjectCommand, this) )
  603. {
  604. // pop the listfile off the stack
  605. this->ListFileStack.pop_back();
  606. if(fullPath!=0)
  607. {
  608. *fullPath = "";
  609. }
  610. this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentParentFile.c_str());
  611. this->MarkVariableAsUsed("CMAKE_PARENT_LIST_FILE");
  612. this->AddDefinition("CMAKE_CURRENT_LIST_FILE", currentFile.c_str());
  613. this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_FILE");
  614. this->AddDefinition("CMAKE_CURRENT_LIST_DIR",
  615. cmSystemTools::GetFilenamePath(currentFile).c_str());
  616. this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_DIR");
  617. return false;
  618. }
  619. // add this list file to the list of dependencies
  620. this->ListFiles.push_back( filenametoread);
  621. // Enforce balanced blocks (if/endif, function/endfunction, etc.).
  622. {
  623. LexicalPushPop lexScope(this);
  624. IncludeScope incScope(this, filenametoread, noPolicyScope);
  625. // Run the parsed commands.
  626. const size_t numberFunctions = cacheFile.Functions.size();
  627. for(size_t i =0; i < numberFunctions; ++i)
  628. {
  629. cmExecutionStatus status;
  630. this->ExecuteCommand(cacheFile.Functions[i],status);
  631. if(cmSystemTools::GetFatalErrorOccured())
  632. {
  633. // Exit early due to error.
  634. lexScope.Quiet();
  635. incScope.Quiet();
  636. break;
  637. }
  638. if(status.GetReturnInvoked())
  639. {
  640. // Exit early due to return command.
  641. break;
  642. }
  643. }
  644. }
  645. // If this is the directory-level CMakeLists.txt file then perform
  646. // some extra checks.
  647. if(this->ListFileStack.size() == 1)
  648. {
  649. this->EnforceDirectoryLevelRules();
  650. }
  651. this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentParentFile.c_str());
  652. this->MarkVariableAsUsed("CMAKE_PARENT_LIST_FILE");
  653. this->AddDefinition("CMAKE_CURRENT_LIST_FILE", currentFile.c_str());
  654. this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_FILE");
  655. this->AddDefinition("CMAKE_CURRENT_LIST_DIR",
  656. cmSystemTools::GetFilenamePath(currentFile).c_str());
  657. this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_DIR");
  658. // pop the listfile off the stack
  659. this->ListFileStack.pop_back();
  660. // Check for unused variables
  661. this->CheckForUnusedVariables();
  662. return true;
  663. }
  664. //----------------------------------------------------------------------------
  665. void cmMakefile::EnforceDirectoryLevelRules()
  666. {
  667. // Diagnose a violation of CMP0000 if necessary.
  668. if(this->CheckCMP0000)
  669. {
  670. cmOStringStream msg;
  671. msg << "No cmake_minimum_required command is present. "
  672. << "A line of code such as\n"
  673. << " cmake_minimum_required(VERSION "
  674. << cmVersion::GetMajorVersion() << "."
  675. << cmVersion::GetMinorVersion()
  676. << ")\n"
  677. << "should be added at the top of the file. "
  678. << "The version specified may be lower if you wish to "
  679. << "support older CMake versions for this project. "
  680. << "For more information run "
  681. << "\"cmake --help-policy CMP0000\".";
  682. switch (this->GetPolicyStatus(cmPolicies::CMP0000))
  683. {
  684. case cmPolicies::WARN:
  685. // Warn because the user did not provide a mimimum required
  686. // version.
  687. this->IssueMessage(cmake::AUTHOR_WARNING, msg.str().c_str());
  688. case cmPolicies::OLD:
  689. // OLD behavior is to use policy version 2.4 set in
  690. // cmListFileCache.
  691. break;
  692. case cmPolicies::REQUIRED_IF_USED:
  693. case cmPolicies::REQUIRED_ALWAYS:
  694. case cmPolicies::NEW:
  695. // NEW behavior is to issue an error.
  696. this->IssueMessage(cmake::FATAL_ERROR, msg.str().c_str());
  697. cmSystemTools::SetFatalErrorOccured();
  698. return;
  699. }
  700. }
  701. }
  702. void cmMakefile::AddCommand(cmCommand* wg)
  703. {
  704. this->GetCMakeInstance()->AddCommand(wg);
  705. }
  706. // Set the make file
  707. void cmMakefile::SetLocalGenerator(cmLocalGenerator* lg)
  708. {
  709. this->LocalGenerator = lg;
  710. // the source groups need to access the global generator
  711. // so don't create them until the lg is set
  712. #if defined(CMAKE_BUILD_WITH_CMAKE)
  713. this->AddSourceGroup("", "^.*$");
  714. this->AddSourceGroup
  715. ("Source Files",
  716. "\\.(C|M|c|c\\+\\+|cc|cpp|cxx|f|f90|for|fpp"
  717. "|ftn|m|mm|rc|def|r|odl|idl|hpj|bat)$");
  718. this->AddSourceGroup("Header Files", CM_HEADER_REGEX);
  719. this->AddSourceGroup("CMake Rules", "\\.rule$");
  720. this->AddSourceGroup("Resources", "\\.plist$");
  721. this->AddSourceGroup("Object Files", "\\.(lo|o|obj)$");
  722. #endif
  723. this->WarnUnused = this->GetCMakeInstance()->GetWarnUnused();
  724. this->CheckSystemVars = this->GetCMakeInstance()->GetCheckSystemVars();
  725. }
  726. bool cmMakefile::NeedBackwardsCompatibility(unsigned int major,
  727. unsigned int minor,
  728. unsigned int patch)
  729. {
  730. if(this->LocalGenerator)
  731. {
  732. return
  733. this->LocalGenerator->NeedBackwardsCompatibility(major, minor, patch);
  734. }
  735. else
  736. {
  737. return false;
  738. }
  739. }
  740. namespace
  741. {
  742. struct file_not_persistent
  743. {
  744. bool operator()(const std::string& path) const
  745. {
  746. return !(path.find("CMakeTmp") == path.npos &&
  747. cmSystemTools::FileExists(path.c_str()));
  748. }
  749. };
  750. }
  751. void cmMakefile::FinalPass()
  752. {
  753. // do all the variable expansions here
  754. this->ExpandVariablesCMP0019();
  755. // give all the commands a chance to do something
  756. // after the file has been parsed before generation
  757. for(std::vector<cmCommand*>::iterator i = this->FinalPassCommands.begin();
  758. i != this->FinalPassCommands.end(); ++i)
  759. {
  760. (*i)->FinalPass();
  761. }
  762. //go through all configured files and see which ones still exist.
  763. //we don't want cmake to re-run if a configured file is created and deleted
  764. //during processing as that would make it a transient file that can't
  765. //influence the build process
  766. //remove_if will move all items that don't have a valid file name to the
  767. //back of the vector
  768. std::vector<std::string>::iterator new_output_files_end = std::remove_if(
  769. this->OutputFiles.begin(),
  770. this->OutputFiles.end(),
  771. file_not_persistent() );
  772. //we just have to erase all items at the back
  773. this->OutputFiles.erase(new_output_files_end, this->OutputFiles.end() );
  774. //if a configured file is used as input for another configured file,
  775. //and then deleted it will show up in the input list files so we
  776. //need to scan those too
  777. std::vector<std::string>::iterator new_list_files_end = std::remove_if(
  778. this->ListFiles.begin(),
  779. this->ListFiles.end(),
  780. file_not_persistent() );
  781. this->ListFiles.erase(new_list_files_end, this->ListFiles.end() );
  782. }
  783. // Generate the output file
  784. void cmMakefile::ConfigureFinalPass()
  785. {
  786. this->FinalPass();
  787. const char* oldValue
  788. = this->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  789. if (oldValue && atof(oldValue) <= 1.2)
  790. {
  791. cmSystemTools::Error("You have requested backwards compatibility "
  792. "with CMake version 1.2 or earlier. This version "
  793. "of CMake only supports backwards compatibility "
  794. "with CMake 1.4 or later. For compatibility with "
  795. "1.2 or earlier please use CMake 2.0");
  796. }
  797. for (cmTargets::iterator l = this->Targets.begin();
  798. l != this->Targets.end(); l++)
  799. {
  800. l->second.FinishConfigure();
  801. }
  802. }
  803. //----------------------------------------------------------------------------
  804. void
  805. cmMakefile::AddCustomCommandToTarget(const char* target,
  806. const std::vector<std::string>& depends,
  807. const cmCustomCommandLines& commandLines,
  808. cmTarget::CustomCommandType type,
  809. const char* comment,
  810. const char* workingDir,
  811. bool escapeOldStyle)
  812. {
  813. // Find the target to which to add the custom command.
  814. cmTargets::iterator ti = this->Targets.find(target);
  815. if(ti != this->Targets.end())
  816. {
  817. if(ti->second.GetType() == cmTarget::OBJECT_LIBRARY)
  818. {
  819. cmOStringStream e;
  820. e << "Target \"" << target << "\" is an OBJECT library "
  821. "that may not have PRE_BUILD, PRE_LINK, or POST_BUILD commands.";
  822. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  823. return;
  824. }
  825. // Add the command to the appropriate build step for the target.
  826. std::vector<std::string> no_output;
  827. cmCustomCommand cc(this, no_output, depends,
  828. commandLines, comment, workingDir);
  829. cc.SetEscapeOldStyle(escapeOldStyle);
  830. cc.SetEscapeAllowMakeVars(true);
  831. switch(type)
  832. {
  833. case cmTarget::PRE_BUILD:
  834. ti->second.GetPreBuildCommands().push_back(cc);
  835. break;
  836. case cmTarget::PRE_LINK:
  837. ti->second.GetPreLinkCommands().push_back(cc);
  838. break;
  839. case cmTarget::POST_BUILD:
  840. ti->second.GetPostBuildCommands().push_back(cc);
  841. break;
  842. }
  843. }
  844. }
  845. //----------------------------------------------------------------------------
  846. cmSourceFile*
  847. cmMakefile::AddCustomCommandToOutput(const std::vector<std::string>& outputs,
  848. const std::vector<std::string>& depends,
  849. const char* main_dependency,
  850. const cmCustomCommandLines& commandLines,
  851. const char* comment,
  852. const char* workingDir,
  853. bool replace,
  854. bool escapeOldStyle)
  855. {
  856. // Make sure there is at least one output.
  857. if(outputs.empty())
  858. {
  859. cmSystemTools::Error("Attempt to add a custom rule with no output!");
  860. return 0;
  861. }
  862. // Validate custom commands. TODO: More strict?
  863. for(cmCustomCommandLines::const_iterator i=commandLines.begin();
  864. i != commandLines.end(); ++i)
  865. {
  866. cmCustomCommandLine const& cl = *i;
  867. if(!cl.empty() && !cl[0].empty() && cl[0][0] == '"')
  868. {
  869. cmOStringStream e;
  870. e << "COMMAND may not contain literal quotes:\n " << cl[0] << "\n";
  871. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  872. return 0;
  873. }
  874. }
  875. // Choose a source file on which to store the custom command.
  876. cmSourceFile* file = 0;
  877. if(main_dependency && main_dependency[0])
  878. {
  879. // The main dependency was specified. Use it unless a different
  880. // custom command already used it.
  881. file = this->GetSource(main_dependency);
  882. if(file && file->GetCustomCommand() && !replace)
  883. {
  884. // The main dependency already has a custom command.
  885. if(commandLines == file->GetCustomCommand()->GetCommandLines())
  886. {
  887. // The existing custom command is identical. Silently ignore
  888. // the duplicate.
  889. return file;
  890. }
  891. else
  892. {
  893. // The existing custom command is different. We need to
  894. // generate a rule file for this new command.
  895. file = 0;
  896. }
  897. }
  898. else
  899. {
  900. // The main dependency does not have a custom command or we are
  901. // allowed to replace it. Use it to store the command.
  902. file = this->GetOrCreateSource(main_dependency);
  903. }
  904. }
  905. // Generate a rule file if the main dependency is not available.
  906. if(!file)
  907. {
  908. cmGlobalGenerator* gg = this->LocalGenerator->GetGlobalGenerator();
  909. // Construct a rule file associated with the first output produced.
  910. std::string outName = gg->GenerateRuleFile(outputs[0]);
  911. // Check if the rule file already exists.
  912. file = this->GetSource(outName.c_str());
  913. if(file && file->GetCustomCommand() && !replace)
  914. {
  915. // The rule file already exists.
  916. if(commandLines != file->GetCustomCommand()->GetCommandLines())
  917. {
  918. cmSystemTools::Error("Attempt to add a custom rule to output \"",
  919. outName.c_str(),
  920. "\" which already has a custom rule.");
  921. }
  922. return file;
  923. }
  924. // Create a cmSourceFile for the rule file.
  925. file = this->GetOrCreateSource(outName.c_str(), true);
  926. file->SetProperty("__CMAKE_RULE", "1");
  927. }
  928. // Always create the output sources and mark them generated.
  929. for(std::vector<std::string>::const_iterator o = outputs.begin();
  930. o != outputs.end(); ++o)
  931. {
  932. if(cmSourceFile* out = this->GetOrCreateSource(o->c_str(), true))
  933. {
  934. out->SetProperty("GENERATED", "1");
  935. }
  936. }
  937. // Construct a complete list of dependencies.
  938. std::vector<std::string> depends2(depends);
  939. if(main_dependency && main_dependency[0])
  940. {
  941. depends2.push_back(main_dependency);
  942. }
  943. // Attach the custom command to the file.
  944. if(file)
  945. {
  946. cmCustomCommand* cc =
  947. new cmCustomCommand(this, outputs, depends2, commandLines,
  948. comment, workingDir);
  949. cc->SetEscapeOldStyle(escapeOldStyle);
  950. cc->SetEscapeAllowMakeVars(true);
  951. file->SetCustomCommand(cc);
  952. this->UpdateOutputToSourceMap(outputs, file);
  953. }
  954. return file;
  955. }
  956. //----------------------------------------------------------------------------
  957. void
  958. cmMakefile::UpdateOutputToSourceMap(std::vector<std::string> const& outputs,
  959. cmSourceFile* source)
  960. {
  961. for(std::vector<std::string>::const_iterator o = outputs.begin();
  962. o != outputs.end(); ++o)
  963. {
  964. this->UpdateOutputToSourceMap(*o, source);
  965. }
  966. }
  967. //----------------------------------------------------------------------------
  968. void
  969. cmMakefile::UpdateOutputToSourceMap(std::string const& output,
  970. cmSourceFile* source)
  971. {
  972. OutputToSourceMap::iterator i = this->OutputToSource.find(output);
  973. if(i != this->OutputToSource.end())
  974. {
  975. // Multiple custom commands produce the same output but may
  976. // be attached to a different source file (MAIN_DEPENDENCY).
  977. // LinearGetSourceFileWithOutput would return the first one,
  978. // so keep the mapping for the first one.
  979. //
  980. // TODO: Warn the user about this case. However, the VS 8 generator
  981. // triggers it for separate generate.stamp rules in ZERO_CHECK and
  982. // individual targets.
  983. return;
  984. }
  985. this->OutputToSource[output] = source;
  986. }
  987. //----------------------------------------------------------------------------
  988. cmSourceFile*
  989. cmMakefile::AddCustomCommandToOutput(const char* output,
  990. const std::vector<std::string>& depends,
  991. const char* main_dependency,
  992. const cmCustomCommandLines& commandLines,
  993. const char* comment,
  994. const char* workingDir,
  995. bool replace,
  996. bool escapeOldStyle)
  997. {
  998. std::vector<std::string> outputs;
  999. outputs.push_back(output);
  1000. return this->AddCustomCommandToOutput(outputs, depends, main_dependency,
  1001. commandLines, comment, workingDir,
  1002. replace, escapeOldStyle);
  1003. }
  1004. //----------------------------------------------------------------------------
  1005. void
  1006. cmMakefile::AddCustomCommandOldStyle(const char* target,
  1007. const std::vector<std::string>& outputs,
  1008. const std::vector<std::string>& depends,
  1009. const char* source,
  1010. const cmCustomCommandLines& commandLines,
  1011. const char* comment)
  1012. {
  1013. // Translate the old-style signature to one of the new-style
  1014. // signatures.
  1015. if(strcmp(source, target) == 0)
  1016. {
  1017. // In the old-style signature if the source and target were the
  1018. // same then it added a post-build rule to the target. Preserve
  1019. // this behavior.
  1020. this->AddCustomCommandToTarget(target, depends, commandLines,
  1021. cmTarget::POST_BUILD, comment, 0);
  1022. return;
  1023. }
  1024. // Each output must get its own copy of this rule.
  1025. cmsys::RegularExpression sourceFiles("\\.(C|M|c|c\\+\\+|cc|cpp|cxx|m|mm|"
  1026. "rc|def|r|odl|idl|hpj|bat|h|h\\+\\+|"
  1027. "hm|hpp|hxx|in|txx|inl)$");
  1028. for(std::vector<std::string>::const_iterator oi = outputs.begin();
  1029. oi != outputs.end(); ++oi)
  1030. {
  1031. // Get the name of this output.
  1032. const char* output = oi->c_str();
  1033. cmSourceFile* sf;
  1034. // Choose whether to use a main dependency.
  1035. if(sourceFiles.find(source))
  1036. {
  1037. // The source looks like a real file. Use it as the main dependency.
  1038. sf = this->AddCustomCommandToOutput(output, depends, source,
  1039. commandLines, comment, 0);
  1040. }
  1041. else
  1042. {
  1043. // The source may not be a real file. Do not use a main dependency.
  1044. const char* no_main_dependency = 0;
  1045. std::vector<std::string> depends2 = depends;
  1046. depends2.push_back(source);
  1047. sf = this->AddCustomCommandToOutput(output, depends2, no_main_dependency,
  1048. commandLines, comment, 0);
  1049. }
  1050. // If the rule was added to the source (and not a .rule file),
  1051. // then add the source to the target to make sure the rule is
  1052. // included.
  1053. if(sf && !sf->GetPropertyAsBool("__CMAKE_RULE"))
  1054. {
  1055. if (this->Targets.find(target) != this->Targets.end())
  1056. {
  1057. this->Targets[target].AddSourceFile(sf);
  1058. }
  1059. else
  1060. {
  1061. cmSystemTools::Error("Attempt to add a custom rule to a target "
  1062. "that does not exist yet for target ", target);
  1063. return;
  1064. }
  1065. }
  1066. }
  1067. }
  1068. //----------------------------------------------------------------------------
  1069. void cmMakefile::AddUtilityCommand(const char* utilityName,
  1070. bool excludeFromAll,
  1071. const std::vector<std::string>& depends,
  1072. const char* workingDirectory,
  1073. const char* command,
  1074. const char* arg1,
  1075. const char* arg2,
  1076. const char* arg3,
  1077. const char* arg4)
  1078. {
  1079. // Construct the command line for the custom command.
  1080. cmCustomCommandLine commandLine;
  1081. commandLine.push_back(command);
  1082. if(arg1)
  1083. {
  1084. commandLine.push_back(arg1);
  1085. }
  1086. if(arg2)
  1087. {
  1088. commandLine.push_back(arg2);
  1089. }
  1090. if(arg3)
  1091. {
  1092. commandLine.push_back(arg3);
  1093. }
  1094. if(arg4)
  1095. {
  1096. commandLine.push_back(arg4);
  1097. }
  1098. cmCustomCommandLines commandLines;
  1099. commandLines.push_back(commandLine);
  1100. // Call the real signature of this method.
  1101. this->AddUtilityCommand(utilityName, excludeFromAll, workingDirectory,
  1102. depends, commandLines);
  1103. }
  1104. //----------------------------------------------------------------------------
  1105. cmTarget*
  1106. cmMakefile::AddUtilityCommand(const char* utilityName,
  1107. bool excludeFromAll,
  1108. const char* workingDirectory,
  1109. const std::vector<std::string>& depends,
  1110. const cmCustomCommandLines& commandLines,
  1111. bool escapeOldStyle, const char* comment)
  1112. {
  1113. // Create a target instance for this utility.
  1114. cmTarget* target = this->AddNewTarget(cmTarget::UTILITY, utilityName);
  1115. if (excludeFromAll)
  1116. {
  1117. target->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
  1118. }
  1119. if(!comment)
  1120. {
  1121. // Use an empty comment to avoid generation of default comment.
  1122. comment = "";
  1123. }
  1124. // Store the custom command in the target.
  1125. std::string force = this->GetStartOutputDirectory();
  1126. force += cmake::GetCMakeFilesDirectory();
  1127. force += "/";
  1128. force += utilityName;
  1129. const char* no_main_dependency = 0;
  1130. bool no_replace = false;
  1131. this->AddCustomCommandToOutput(force.c_str(), depends,
  1132. no_main_dependency,
  1133. commandLines, comment,
  1134. workingDirectory, no_replace,
  1135. escapeOldStyle);
  1136. cmSourceFile* sf = target->AddSource(force.c_str());
  1137. // The output is not actually created so mark it symbolic.
  1138. if(sf)
  1139. {
  1140. sf->SetProperty("SYMBOLIC", "1");
  1141. }
  1142. else
  1143. {
  1144. cmSystemTools::Error("Could not get source file entry for ",
  1145. force.c_str());
  1146. }
  1147. return target;
  1148. }
  1149. void cmMakefile::AddDefineFlag(const char* flag)
  1150. {
  1151. if (!flag)
  1152. {
  1153. return;
  1154. }
  1155. // Update the string used for the old DEFINITIONS property.
  1156. this->AddDefineFlag(flag, this->DefineFlagsOrig);
  1157. // If this is really a definition, update COMPILE_DEFINITIONS.
  1158. if(this->ParseDefineFlag(flag, false))
  1159. {
  1160. return;
  1161. }
  1162. // Add this flag that does not look like a definition.
  1163. this->AddDefineFlag(flag, this->DefineFlags);
  1164. }
  1165. void cmMakefile::AddDefineFlag(const char* flag, std::string& dflags)
  1166. {
  1167. // remove any \n\r
  1168. std::string ret = flag;
  1169. std::string::size_type pos = 0;
  1170. while((pos = ret.find('\n', pos)) != std::string::npos)
  1171. {
  1172. ret[pos] = ' ';
  1173. pos++;
  1174. }
  1175. pos = 0;
  1176. while((pos = ret.find('\r', pos)) != std::string::npos)
  1177. {
  1178. ret[pos] = ' ';
  1179. pos++;
  1180. }
  1181. dflags += " ";
  1182. dflags += ret;
  1183. }
  1184. void cmMakefile::RemoveDefineFlag(const char* flag)
  1185. {
  1186. // Check the length of the flag to remove.
  1187. std::string::size_type len = strlen(flag);
  1188. if(len < 1)
  1189. {
  1190. return;
  1191. }
  1192. // Update the string used for the old DEFINITIONS property.
  1193. this->RemoveDefineFlag(flag, len, this->DefineFlagsOrig);
  1194. // If this is really a definition, update COMPILE_DEFINITIONS.
  1195. if(this->ParseDefineFlag(flag, true))
  1196. {
  1197. return;
  1198. }
  1199. // Remove this flag that does not look like a definition.
  1200. this->RemoveDefineFlag(flag, len, this->DefineFlags);
  1201. }
  1202. void cmMakefile::RemoveDefineFlag(const char* flag,
  1203. std::string::size_type len,
  1204. std::string& dflags)
  1205. {
  1206. // Remove all instances of the flag that are surrounded by
  1207. // whitespace or the beginning/end of the string.
  1208. for(std::string::size_type lpos = dflags.find(flag, 0);
  1209. lpos != std::string::npos; lpos = dflags.find(flag, lpos))
  1210. {
  1211. std::string::size_type rpos = lpos + len;
  1212. if((lpos <= 0 || isspace(dflags[lpos-1])) &&
  1213. (rpos >= dflags.size() || isspace(dflags[rpos])))
  1214. {
  1215. dflags.erase(lpos, len);
  1216. }
  1217. else
  1218. {
  1219. ++lpos;
  1220. }
  1221. }
  1222. }
  1223. void cmMakefile::AddCompileOption(const char* option)
  1224. {
  1225. this->AppendProperty("COMPILE_OPTIONS", option);
  1226. }
  1227. bool cmMakefile::ParseDefineFlag(std::string const& def, bool remove)
  1228. {
  1229. // Create a regular expression to match valid definitions.
  1230. static cmsys::RegularExpression
  1231. valid("^[-/]D[A-Za-z_][A-Za-z0-9_]*(=.*)?$");
  1232. // Make sure the definition matches.
  1233. if(!valid.find(def.c_str()))
  1234. {
  1235. return false;
  1236. }
  1237. // VS6 IDE does not support definition values with spaces in
  1238. // combination with '"', '$', or ';'.
  1239. if((strcmp(this->LocalGenerator->GetGlobalGenerator()->GetName(),
  1240. "Visual Studio 6") == 0) &&
  1241. (def.find(" ") != def.npos && def.find_first_of("\"$;") != def.npos))
  1242. {
  1243. return false;
  1244. }
  1245. // Definitions with non-trivial values require a policy check.
  1246. static cmsys::RegularExpression
  1247. trivial("^[-/]D[A-Za-z_][A-Za-z0-9_]*(=[A-Za-z0-9_.]+)?$");
  1248. if(!trivial.find(def.c_str()))
  1249. {
  1250. // This definition has a non-trivial value.
  1251. switch(this->GetPolicyStatus(cmPolicies::CMP0005))
  1252. {
  1253. case cmPolicies::WARN:
  1254. this->IssueMessage(
  1255. cmake::AUTHOR_WARNING,
  1256. this->GetPolicies()->GetPolicyWarning(cmPolicies::CMP0005)
  1257. );
  1258. case cmPolicies::OLD:
  1259. // OLD behavior is to not escape the value. We should not
  1260. // convert the definition to use the property.
  1261. return false;
  1262. case cmPolicies::REQUIRED_IF_USED:
  1263. case cmPolicies::REQUIRED_ALWAYS:
  1264. this->IssueMessage(
  1265. cmake::FATAL_ERROR,
  1266. this->GetPolicies()->GetRequiredPolicyError(cmPolicies::CMP0005)
  1267. );
  1268. return false;
  1269. case cmPolicies::NEW:
  1270. // NEW behavior is to escape the value. Proceed to convert it
  1271. // to an entry in the property.
  1272. break;
  1273. }
  1274. }
  1275. // Get the definition part after the flag.
  1276. const char* define = def.c_str() + 2;
  1277. if(remove)
  1278. {
  1279. if(const char* cdefs = this->GetProperty("COMPILE_DEFINITIONS"))
  1280. {
  1281. // Expand the list.
  1282. std::vector<std::string> defs;
  1283. cmSystemTools::ExpandListArgument(cdefs, defs);
  1284. // Recompose the list without the definition.
  1285. std::string ndefs;
  1286. const char* sep = "";
  1287. for(std::vector<std::string>::const_iterator di = defs.begin();
  1288. di != defs.end(); ++di)
  1289. {
  1290. if(*di != define)
  1291. {
  1292. ndefs += sep;
  1293. sep = ";";
  1294. ndefs += *di;
  1295. }
  1296. }
  1297. // Store the new list.
  1298. this->SetProperty("COMPILE_DEFINITIONS", ndefs.c_str());
  1299. }
  1300. }
  1301. else
  1302. {
  1303. // Append the definition to the directory property.
  1304. this->AppendProperty("COMPILE_DEFINITIONS", define);
  1305. }
  1306. return true;
  1307. }
  1308. void cmMakefile::AddLinkLibrary(const char* lib,
  1309. cmTarget::LinkLibraryType llt)
  1310. {
  1311. cmTarget::LibraryID tmp;
  1312. tmp.first = lib;
  1313. tmp.second = llt;
  1314. this->LinkLibraries.push_back(tmp);
  1315. }
  1316. void cmMakefile::AddLinkLibraryForTarget(const char *target,
  1317. const char* lib,
  1318. cmTarget::LinkLibraryType llt)
  1319. {
  1320. cmTargets::iterator i = this->Targets.find(target);
  1321. if ( i != this->Targets.end())
  1322. {
  1323. cmTarget* tgt =
  1324. this->GetCMakeInstance()->GetGlobalGenerator()->FindTarget(0,lib);
  1325. if(tgt)
  1326. {
  1327. // CMake versions below 2.4 allowed linking to modules.
  1328. bool allowModules = this->NeedBackwardsCompatibility(2,2);
  1329. // if it is not a static or shared library then you can not link to it
  1330. if(!((tgt->GetType() == cmTarget::STATIC_LIBRARY) ||
  1331. (tgt->GetType() == cmTarget::SHARED_LIBRARY) ||
  1332. (tgt->GetType() == cmTarget::INTERFACE_LIBRARY) ||
  1333. tgt->IsExecutableWithExports()))
  1334. {
  1335. cmOStringStream e;
  1336. e << "Target \"" << lib << "\" of type "
  1337. << cmTarget::GetTargetTypeName(tgt->GetType())
  1338. << " may not be linked into another target. "
  1339. << "One may link only to STATIC or SHARED libraries, or "
  1340. << "to executables with the ENABLE_EXPORTS property set.";
  1341. // in older versions of cmake linking to modules was allowed
  1342. if( tgt->GetType() == cmTarget::MODULE_LIBRARY )
  1343. {
  1344. e << "\n"
  1345. << "If you are developing a new project, re-organize it to avoid "
  1346. << "linking to modules. "
  1347. << "If you are just trying to build an existing project, "
  1348. << "set CMAKE_BACKWARDS_COMPATIBILITY to 2.2 or lower to allow "
  1349. << "linking to modules.";
  1350. }
  1351. // if no modules are allowed then this is always an error
  1352. if(!allowModules ||
  1353. // if we allow modules but the type is not a module then it is
  1354. // still an error
  1355. (allowModules && tgt->GetType() != cmTarget::MODULE_LIBRARY))
  1356. {
  1357. this->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  1358. }
  1359. }
  1360. }
  1361. i->second.AddLinkLibrary( *this, target, lib, llt );
  1362. }
  1363. else
  1364. {
  1365. cmOStringStream e;
  1366. e << "Attempt to add link library \""
  1367. << lib << "\" to target \""
  1368. << target << "\" which is not built in this directory.";
  1369. this->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  1370. }
  1371. }
  1372. void cmMakefile::AddLinkDirectoryForTarget(const char *target,
  1373. const char* d)
  1374. {
  1375. cmTargets::iterator i = this->Targets.find(target);
  1376. if ( i != this->Targets.end())
  1377. {
  1378. if(this->IsAlias(target))
  1379. {
  1380. cmOStringStream e;
  1381. e << "ALIAS target \"" << target << "\" "
  1382. << "may not be linked into another target.";
  1383. this->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  1384. return;
  1385. }
  1386. i->second.AddLinkDirectory( d );
  1387. }
  1388. else
  1389. {
  1390. cmSystemTools::Error
  1391. ("Attempt to add link directories to non-existent target: ",
  1392. target, " for directory ", d);
  1393. }
  1394. }
  1395. void cmMakefile::AddLinkLibrary(const char* lib)
  1396. {
  1397. this->AddLinkLibrary(lib,cmTarget::GENERAL);
  1398. }
  1399. void cmMakefile::AddLinkDirectory(const char* dir)
  1400. {
  1401. // Don't add a link directory that is already present. Yes, this
  1402. // linear search results in n^2 behavior, but n won't be getting
  1403. // much bigger than 20. We cannot use a set because of order
  1404. // dependency of the link search path.
  1405. if(!dir)
  1406. {
  1407. return;
  1408. }
  1409. // remove trailing slashes
  1410. if(dir[strlen(dir)-1] == '/')
  1411. {
  1412. std::string newdir = dir;
  1413. newdir = newdir.substr(0, newdir.size()-1);
  1414. if(std::find(this->LinkDirectories.begin(),
  1415. this->LinkDirectories.end(),
  1416. newdir.c_str()) == this->LinkDirectories.end())
  1417. {
  1418. this->LinkDirectories.push_back(newdir);
  1419. }
  1420. }
  1421. else
  1422. {
  1423. if(std::find(this->LinkDirectories.begin(),
  1424. this->LinkDirectories.end(), dir)
  1425. == this->LinkDirectories.end())
  1426. {
  1427. this->LinkDirectories.push_back(dir);
  1428. }
  1429. }
  1430. }
  1431. void cmMakefile::InitializeFromParent()
  1432. {
  1433. cmMakefile *parent = this->LocalGenerator->GetParent()->GetMakefile();
  1434. // Initialize definitions with the closure of the parent scope.
  1435. this->Internal->VarStack.top() = parent->Internal->VarStack.top().Closure();
  1436. const std::vector<cmValueWithOrigin> parentIncludes =
  1437. parent->GetIncludeDirectoriesEntries();
  1438. this->IncludeDirectoriesEntries.insert(this->IncludeDirectoriesEntries.end(),
  1439. parentIncludes.begin(),
  1440. parentIncludes.end());
  1441. const std::vector<cmValueWithOrigin> parentOptions =
  1442. parent->GetCompileOptionsEntries();
  1443. this->CompileOptionsEntries.insert(this->CompileOptionsEntries.end(),
  1444. parentOptions.begin(),
  1445. parentOptions.end());
  1446. const std::vector<cmValueWithOrigin> parentDefines =
  1447. parent->GetCompileDefinitionsEntries();
  1448. this->CompileDefinitionsEntries.insert(this->CompileDefinitionsEntries.end(),
  1449. parentDefines.begin(),
  1450. parentDefines.end());
  1451. this->SystemIncludeDirectories = parent->SystemIncludeDirectories;
  1452. // define flags
  1453. this->DefineFlags = parent->DefineFlags;
  1454. this->DefineFlagsOrig = parent->DefineFlagsOrig;
  1455. // Include transform property. There is no per-config version.
  1456. {
  1457. const char* prop = "IMPLICIT_DEPENDS_INCLUDE_TRANSFORM";
  1458. this->SetProperty(prop, parent->GetProperty(prop));
  1459. }
  1460. // compile definitions property and per-config versions
  1461. {
  1462. this->SetProperty("COMPILE_DEFINITIONS",
  1463. parent->GetProperty("COMPILE_DEFINITIONS"));
  1464. std::vector<std::string> configs;
  1465. this->GetConfigurations(configs);
  1466. for(std::vector<std::string>::const_iterator ci = configs.begin();
  1467. ci != configs.end(); ++ci)
  1468. {
  1469. std::string defPropName = "COMPILE_DEFINITIONS_";
  1470. defPropName += cmSystemTools::UpperCase(*ci);
  1471. this->SetProperty(defPropName.c_str(),
  1472. parent->GetProperty(defPropName.c_str()));
  1473. }
  1474. }
  1475. // link libraries
  1476. this->LinkLibraries = parent->LinkLibraries;
  1477. // link directories
  1478. this->LinkDirectories = parent->LinkDirectories;
  1479. // the initial project name
  1480. this->ProjectName = parent->ProjectName;
  1481. // Copy include regular expressions.
  1482. this->IncludeFileRegularExpression = parent->IncludeFileRegularExpression;
  1483. this->ComplainFileRegularExpression = parent->ComplainFileRegularExpression;
  1484. // Imported targets.
  1485. this->ImportedTargets = parent->ImportedTargets;
  1486. }
  1487. void cmMakefile::ConfigureSubDirectory(cmLocalGenerator *lg2)
  1488. {
  1489. // copy our variables from the child makefile
  1490. lg2->GetMakefile()->InitializeFromParent();
  1491. lg2->GetMakefile()->MakeStartDirectoriesCurrent();
  1492. if (this->GetCMakeInstance()->GetDebugOutput())
  1493. {
  1494. std::string msg=" Entering ";
  1495. msg += lg2->GetMakefile()->GetCurrentDirectory();
  1496. cmSystemTools::Message(msg.c_str());
  1497. }
  1498. // finally configure the subdir
  1499. lg2->Configure();
  1500. if (this->GetCMakeInstance()->GetDebugOutput())
  1501. {
  1502. std::string msg=" Returning to ";
  1503. msg += this->GetCurrentDirectory();
  1504. cmSystemTools::Message(msg.c_str());
  1505. }
  1506. }
  1507. void cmMakefile::AddSubDirectory(const char* sub,
  1508. bool excludeFromAll, bool preorder)
  1509. {
  1510. // the source path must be made full if it isn't already
  1511. std::string srcPath = sub;
  1512. if (!cmSystemTools::FileIsFullPath(srcPath.c_str()))
  1513. {
  1514. srcPath = this->GetCurrentDirectory();
  1515. srcPath += "/";
  1516. srcPath += sub;
  1517. }
  1518. // binary path must be made full if it isn't already
  1519. std::string binPath = sub;
  1520. if (!cmSystemTools::FileIsFullPath(binPath.c_str()))
  1521. {
  1522. binPath = this->GetCurrentOutputDirectory();
  1523. binPath += "/";
  1524. binPath += sub;
  1525. }
  1526. this->AddSubDirectory(srcPath.c_str(), binPath.c_str(),
  1527. excludeFromAll, preorder, false);
  1528. }
  1529. void cmMakefile::AddSubDirectory(const char* srcPath, const char *binPath,
  1530. bool excludeFromAll, bool preorder,
  1531. bool immediate)
  1532. {
  1533. // Make sure the binary directory is unique.
  1534. if(!this->EnforceUniqueDir(srcPath, binPath))
  1535. {
  1536. return;
  1537. }
  1538. // create a new local generator and set its parent
  1539. cmLocalGenerator *lg2 =
  1540. this->LocalGenerator->GetGlobalGenerator()->CreateLocalGenerator();
  1541. lg2->SetParent(this->LocalGenerator);
  1542. this->LocalGenerator->GetGlobalGenerator()->AddLocalGenerator(lg2);
  1543. // set the subdirs start dirs
  1544. lg2->GetMakefile()->SetStartDirectory(srcPath);
  1545. lg2->GetMakefile()->SetStartOutputDirectory(binPath);
  1546. if(excludeFromAll)
  1547. {
  1548. lg2->GetMakefile()->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
  1549. }
  1550. lg2->GetMakefile()->SetPreOrder(preorder);
  1551. if (immediate)
  1552. {
  1553. this->ConfigureSubDirectory(lg2);
  1554. }
  1555. }
  1556. //----------------------------------------------------------------------------
  1557. void cmMakefile::AddIncludeDirectories(const std::vector<std::string> &incs,
  1558. bool before)
  1559. {
  1560. if (incs.empty())
  1561. {
  1562. return;
  1563. }
  1564. std::string incString;
  1565. std::string sep;
  1566. for(std::vector<std::string>::const_iterator li = incs.begin();
  1567. li != incs.end(); ++li)
  1568. {
  1569. incString += sep + *li;
  1570. sep = ";";
  1571. }
  1572. std::vector<cmValueWithOrigin>::iterator position =
  1573. before ? this->IncludeDirectoriesEntries.begin()
  1574. : this->IncludeDirectoriesEntries.end();
  1575. cmListFileBacktrace lfbt;
  1576. this->GetBacktrace(lfbt);
  1577. cmValueWithOrigin entry(incString, lfbt);
  1578. this->IncludeDirectoriesEntries.insert(position, entry);
  1579. // Property on each target:
  1580. for (cmTargets::iterator l = this->Targets.begin();
  1581. l != this->Targets.end(); ++l)
  1582. {
  1583. cmTarget &t = l->second;
  1584. t.InsertInclude(entry, before);
  1585. }
  1586. }
  1587. //----------------------------------------------------------------------------
  1588. void
  1589. cmMakefile::AddSystemIncludeDirectories(const std::set<cmStdString> &incs)
  1590. {
  1591. for(std::set<cmStdString>::const_iterator li = incs.begin();
  1592. li != incs.end(); ++li)
  1593. {
  1594. this->SystemIncludeDirectories.insert(*li);
  1595. }
  1596. for (cmTargets::iterator l = this->Targets.begin();
  1597. l != this->Targets.end(); ++l)
  1598. {
  1599. cmTarget &t = l->second;
  1600. t.AddSystemIncludeDirectories(incs);
  1601. }
  1602. }
  1603. void cmMakefile::AddDefinition(const char* name, const char* value)
  1604. {
  1605. if (!value )
  1606. {
  1607. return;
  1608. }
  1609. this->Internal->VarStack.top().Set(name, value);
  1610. if (this->Internal->VarUsageStack.size() &&
  1611. this->VariableInitialized(name))
  1612. {
  1613. this->CheckForUnused("changing definition", name);
  1614. this->Internal->VarUsageStack.top().erase(name);
  1615. }
  1616. this->Internal->VarInitStack.top().insert(name);
  1617. #ifdef CMAKE_BUILD_WITH_CMAKE
  1618. cmVariableWatch* vv = this->GetVariableWatch();
  1619. if ( vv )
  1620. {
  1621. vv->VariableAccessed(name,
  1622. cmVariableWatch::VARIABLE_MODIFIED_ACCESS,
  1623. value,
  1624. this);
  1625. }
  1626. #endif
  1627. }
  1628. void cmMakefile::AddCacheDefinition(const char* name, const char* value,
  1629. const char* doc,
  1630. cmCacheManager::CacheEntryType type,
  1631. bool force)
  1632. {
  1633. const char* val = value;
  1634. cmCacheManager::CacheIterator it =
  1635. this->GetCacheManager()->GetCacheIterator(name);
  1636. if(!it.IsAtEnd() && (it.GetType() == cmCacheManager::UNINITIALIZED) &&
  1637. it.Initialized())
  1638. {
  1639. // if this is not a force, then use the value from the cache
  1640. // if it is a force, then use the value being passed in
  1641. if(!force)
  1642. {
  1643. val = it.GetValue();
  1644. }
  1645. if ( type == cmCacheManager::PATH || type == cmCacheManager::FILEPATH )
  1646. {
  1647. std::vector<std::string>::size_type cc;
  1648. std::vector<std::string> files;
  1649. std::string nvalue = "";
  1650. cmSystemTools::ExpandListArgument(val, files);
  1651. for ( cc = 0; cc < files.size(); cc ++ )
  1652. {
  1653. if(!cmSystemTools::IsOff(files[cc].c_str()))
  1654. {
  1655. files[cc] = cmSystemTools::CollapseFullPath(files[cc].c_str());
  1656. }
  1657. if ( cc > 0 )
  1658. {
  1659. nvalue += ";";
  1660. }
  1661. nvalue += files[cc];
  1662. }
  1663. this->GetCacheManager()->AddCacheEntry(name, nvalue.c_str(), doc, type);
  1664. val = it.GetValue();
  1665. }
  1666. }
  1667. this->GetCacheManager()->AddCacheEntry(name, val, doc, type);
  1668. // if there was a definition then remove it
  1669. this->Internal->VarStack.top().Set(name, 0);
  1670. }
  1671. void cmMakefile::AddDefinition(const char* name, bool value)
  1672. {
  1673. this->Internal->VarStack.top().Set(name, value? "ON" : "OFF");
  1674. if (this->Internal->VarUsageStack.size() &&
  1675. this->VariableInitialized(name))
  1676. {
  1677. this->CheckForUnused("changing definition", name);
  1678. this->Internal->VarUsageStack.top().erase(name);
  1679. }
  1680. this->Internal->VarInitStack.top().insert(name);
  1681. #ifdef CMAKE_BUILD_WITH_CMAKE
  1682. cmVariableWatch* vv = this->GetVariableWatch();
  1683. if ( vv )
  1684. {
  1685. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_MODIFIED_ACCESS,
  1686. value?"ON":"OFF", this);
  1687. }
  1688. #endif
  1689. }
  1690. void cmMakefile::CheckForUnusedVariables() const
  1691. {
  1692. if (!this->WarnUnused)
  1693. {
  1694. return;
  1695. }
  1696. const cmDefinitions& defs = this->Internal->VarStack.top();
  1697. const std::set<cmStdString>& locals = defs.LocalKeys();
  1698. std::set<cmStdString>::const_iterator it = locals.begin();
  1699. for (; it != locals.end(); ++it)
  1700. {
  1701. this->CheckForUnused("out of scope", it->c_str());
  1702. }
  1703. }
  1704. void cmMakefile::MarkVariableAsUsed(const char* var)
  1705. {
  1706. this->Internal->VarUsageStack.top().insert(var);
  1707. }
  1708. bool cmMakefile::VariableInitialized(const char* var) const
  1709. {
  1710. if(this->Internal->VarInitStack.top().find(var) !=
  1711. this->Internal->VarInitStack.top().end())
  1712. {
  1713. return true;
  1714. }
  1715. return false;
  1716. }
  1717. bool cmMakefile::VariableUsed(const char* var) const
  1718. {
  1719. if(this->Internal->VarUsageStack.top().find(var) !=
  1720. this->Internal->VarUsageStack.top().end())
  1721. {
  1722. return true;
  1723. }
  1724. return false;
  1725. }
  1726. void cmMakefile::CheckForUnused(const char* reason, const char* name) const
  1727. {
  1728. if (this->WarnUnused && !this->VariableUsed(name))
  1729. {
  1730. cmStdString path;
  1731. cmListFileBacktrace bt;
  1732. if (this->CallStack.size())
  1733. {
  1734. const cmListFileContext* file = this->CallStack.back().Context;
  1735. bt.push_back(*file);
  1736. path = file->FilePath.c_str();
  1737. }
  1738. else
  1739. {
  1740. path = this->GetStartDirectory();
  1741. path += "/CMakeLists.txt";
  1742. cmListFileContext lfc;
  1743. lfc.FilePath = path;
  1744. lfc.Line = 0;
  1745. bt.push_back(lfc);
  1746. }
  1747. if (this->CheckSystemVars ||
  1748. cmSystemTools::IsSubDirectory(path.c_str(),
  1749. this->GetHomeDirectory()) ||
  1750. (cmSystemTools::IsSubDirectory(path.c_str(),
  1751. this->GetHomeOutputDirectory()) &&
  1752. !cmSystemTools::IsSubDirectory(path.c_str(),
  1753. cmake::GetCMakeFilesDirectory())))
  1754. {
  1755. cmOStringStream msg;
  1756. msg << "unused variable (" << reason << ") \'" << name << "\'";
  1757. this->GetCMakeInstance()->IssueMessage(cmake::AUTHOR_WARNING,
  1758. msg.str().c_str(),
  1759. bt);
  1760. }
  1761. }
  1762. }
  1763. void cmMakefile::RemoveDefinition(const char* name)
  1764. {
  1765. this->Internal->VarStack.top().Set(name, 0);
  1766. if (this->Internal->VarUsageStack.size() &&
  1767. this->VariableInitialized(name))
  1768. {
  1769. this->CheckForUnused("unsetting", name);
  1770. this->Internal->VarUsageStack.top().erase(name);
  1771. }
  1772. this->Internal->VarInitStack.top().insert(name);
  1773. #ifdef CMAKE_BUILD_WITH_CMAKE
  1774. cmVariableWatch* vv = this->GetVariableWatch();
  1775. if ( vv )
  1776. {
  1777. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_REMOVED_ACCESS,
  1778. 0, this);
  1779. }
  1780. #endif
  1781. }
  1782. void cmMakefile::RemoveCacheDefinition(const char* name)
  1783. {
  1784. this->GetCacheManager()->RemoveCacheEntry(name);
  1785. }
  1786. void cmMakefile::SetProjectName(const char* p)
  1787. {
  1788. this->ProjectName = p;
  1789. }
  1790. void cmMakefile::AddGlobalLinkInformation(const char* name, cmTarget& target)
  1791. {
  1792. // for these targets do not add anything
  1793. switch(target.GetType())
  1794. {
  1795. case cmTarget::UTILITY:
  1796. case cmTarget::GLOBAL_TARGET:
  1797. case cmTarget::INTERFACE_LIBRARY:
  1798. return;
  1799. default:;
  1800. }
  1801. std::vector<std::string>::iterator j;
  1802. for(j = this->LinkDirectories.begin();
  1803. j != this->LinkDirectories.end(); ++j)
  1804. {
  1805. target.AddLinkDirectory(j->c_str());
  1806. }
  1807. target.MergeLinkLibraries( *this, name, this->LinkLibraries );
  1808. }
  1809. void cmMakefile::AddAlias(const char* lname, cmTarget *tgt)
  1810. {
  1811. this->AliasTargets[lname] = tgt;
  1812. this->LocalGenerator->GetGlobalGenerator()->AddAlias(lname, tgt);
  1813. }
  1814. cmTarget* cmMakefile::AddLibrary(const char* lname, cmTarget::TargetType type,
  1815. const std::vector<std::string> &srcs,
  1816. bool excludeFromAll)
  1817. {
  1818. // wrong type ? default to STATIC
  1819. if ( (type != cmTarget::STATIC_LIBRARY)
  1820. && (type != cmTarget::SHARED_LIBRARY)
  1821. && (type != cmTarget::MODULE_LIBRARY)
  1822. && (type != cmTarget::OBJECT_LIBRARY)
  1823. && (type != cmTarget::INTERFACE_LIBRARY))
  1824. {
  1825. this->IssueMessage(cmake::INTERNAL_ERROR,
  1826. "cmMakefile::AddLibrary given invalid target type.");
  1827. type = cmTarget::STATIC_LIBRARY;
  1828. }
  1829. cmTarget* target = this->AddNewTarget(type, lname);
  1830. // Clear its dependencies. Otherwise, dependencies might persist
  1831. // over changes in CMakeLists.txt, making the information stale and
  1832. // hence useless.
  1833. target->ClearDependencyInformation( *this, lname );
  1834. if(excludeFromAll)
  1835. {
  1836. target->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
  1837. }
  1838. target->AddSources(srcs);
  1839. this->AddGlobalLinkInformation(lname, *target);
  1840. return target;
  1841. }
  1842. cmTarget* cmMakefile::AddExecutable(const char *exeName,
  1843. const std::vector<std::string> &srcs,
  1844. bool excludeFromAll)
  1845. {
  1846. cmTarget* target = this->AddNewTarget(cmTarget::EXECUTABLE, exeName);
  1847. if(excludeFromAll)
  1848. {
  1849. target->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
  1850. }
  1851. target->AddSources(srcs);
  1852. this->AddGlobalLinkInformation(exeName, *target);
  1853. return target;
  1854. }
  1855. //----------------------------------------------------------------------------
  1856. cmTarget*
  1857. cmMakefile::AddNewTarget(cmTarget::TargetType type, const char* name)
  1858. {
  1859. cmTargets::iterator it =
  1860. this->Targets.insert(cmTargets::value_type(name, cmTarget())).first;
  1861. cmTarget& target = it->second;
  1862. target.SetType(type, name);
  1863. target.SetMakefile(this);
  1864. this->LocalGenerator->GetGlobalGenerator()->AddTarget(&it->second);
  1865. return &it->second;
  1866. }
  1867. cmSourceFile *cmMakefile::LinearGetSourceFileWithOutput(const char *cname)
  1868. {
  1869. std::string name = cname;
  1870. std::string out;
  1871. // look through all the source files that have custom commands
  1872. // and see if the custom command has the passed source file as an output
  1873. for(std::vector<cmSourceFile*>::const_iterator i =
  1874. this->SourceFiles.begin(); i != this->SourceFiles.end(); ++i)
  1875. {
  1876. // does this source file have a custom command?
  1877. if ((*i)->GetCustomCommand())
  1878. {
  1879. // is the output of the custom command match the source files name
  1880. const std::vector<std::string>& outputs =
  1881. (*i)->GetCustomCommand()->GetOutputs();
  1882. for(std::vector<std::string>::const_iterator o = outputs.begin();
  1883. o != outputs.end(); ++o)
  1884. {
  1885. out = *o;
  1886. std::string::size_type pos = out.rfind(name);
  1887. // If the output matches exactly
  1888. if (pos != out.npos &&
  1889. pos == out.size() - name.size() &&
  1890. (pos ==0 || out[pos-1] == '/'))
  1891. {
  1892. return *i;
  1893. }
  1894. }
  1895. }
  1896. }
  1897. // otherwise return NULL
  1898. return 0;
  1899. }
  1900. cmSourceFile *cmMakefile::GetSourceFileWithOutput(const char *cname)
  1901. {
  1902. std::string name = cname;
  1903. // If the queried path is not absolute we use the backward compatible
  1904. // linear-time search for an output with a matching suffix.
  1905. if(!cmSystemTools::FileIsFullPath(cname))
  1906. {
  1907. return LinearGetSourceFileWithOutput(cname);
  1908. }
  1909. // Otherwise we use an efficient lookup map.
  1910. OutputToSourceMap::iterator o = this->OutputToSource.find(name);
  1911. if (o != this->OutputToSource.end())
  1912. {
  1913. return (*o).second;
  1914. }
  1915. return 0;
  1916. }
  1917. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1918. cmSourceGroup* cmMakefile::GetSourceGroup(const std::vector<std::string>&name)
  1919. {
  1920. cmSourceGroup* sg = 0;
  1921. // first look for source group starting with the same as the one we wants
  1922. for (std::vector<cmSourceGroup>::iterator sgIt = this->SourceGroups.begin();
  1923. sgIt != this->SourceGroups.end(); ++sgIt)
  1924. {
  1925. std::string sgName = sgIt->GetName();
  1926. if(sgName == name[0])
  1927. {
  1928. sg = &(*sgIt);
  1929. break;
  1930. }
  1931. }
  1932. if(sg != 0)
  1933. {
  1934. // iterate through its children to find match source group
  1935. for(unsigned int i=1; i<name.size(); ++i)
  1936. {
  1937. sg = sg->lookupChild(name[i].c_str());
  1938. if(sg == 0)
  1939. {
  1940. break;
  1941. }
  1942. }
  1943. }
  1944. return sg;
  1945. }
  1946. void cmMakefile::AddSourceGroup(const char* name,
  1947. const char* regex)
  1948. {
  1949. if (name)
  1950. {
  1951. std::vector<std::string> nameVector;
  1952. nameVector.push_back(name);
  1953. AddSourceGroup(nameVector, regex);
  1954. }
  1955. }
  1956. void cmMakefile::AddSourceGroup(const std::vector<std::string>& name,
  1957. const char* regex)
  1958. {
  1959. cmSourceGroup* sg = 0;
  1960. std::vector<std::string> currentName;
  1961. int i = 0;
  1962. const int lastElement = static_cast<int>(name.size()-1);
  1963. for(i=lastElement; i>=0; --i)
  1964. {
  1965. currentName.assign(name.begin(), name.begin()+i+1);
  1966. sg = this->GetSourceGroup(currentName);
  1967. if(sg != 0)
  1968. {
  1969. break;
  1970. }
  1971. }
  1972. // i now contains the index of the last found component
  1973. if(i==lastElement)
  1974. {
  1975. // group already exists, replace its regular expression
  1976. if ( regex )
  1977. {
  1978. // We only want to set the regular expression. If there are already
  1979. // source files in the group, we don't want to remove them.
  1980. sg->SetGroupRegex(regex);
  1981. }
  1982. return;
  1983. }
  1984. else if(i==-1)
  1985. {
  1986. // group does not exist nor belong to any existing group
  1987. // add its first component
  1988. this->SourceGroups.push_back(cmSourceGroup(name[0].c_str(), regex));
  1989. sg = this->GetSourceGroup(currentName);
  1990. i = 0; // last component found
  1991. }
  1992. // build the whole source group path
  1993. const char* fullname = sg->GetFullName();
  1994. cmGlobalGenerator* gg = this->LocalGenerator->GetGlobalGenerator();
  1995. if(strlen(fullname))
  1996. {
  1997. std::string guidName = "SG_Filter_";
  1998. guidName += fullname;
  1999. gg->CreateGUID(guidName.c_str());
  2000. }
  2001. for(++i; i<=lastElement; ++i)
  2002. {
  2003. sg->AddChild(cmSourceGroup(name[i].c_str(), 0, sg->GetFullName()));
  2004. sg = sg->lookupChild(name[i].c_str());
  2005. fullname = sg->GetFullName();
  2006. if(strlen(fullname))
  2007. {
  2008. std::string guidName = "SG_Filter_";
  2009. guidName += fullname;
  2010. gg->CreateGUID(guidName.c_str());
  2011. }
  2012. }
  2013. sg->SetGroupRegex(regex);
  2014. }
  2015. #endif
  2016. void cmMakefile::AddExtraDirectory(const char* dir)
  2017. {
  2018. this->AuxSourceDirectories.push_back(dir);
  2019. }
  2020. static bool mightExpandVariablesCMP0019(const char* s)
  2021. {
  2022. return s && *s && strstr(s,"${") && strchr(s,'}');
  2023. }
  2024. void cmMakefile::ExpandVariablesCMP0019()
  2025. {
  2026. // Drop this ancient compatibility behavior with a policy.
  2027. cmPolicies::PolicyStatus pol = this->GetPolicyStatus(cmPolicies::CMP0019);
  2028. if(pol != cmPolicies::OLD && pol != cmPolicies::WARN)
  2029. {
  2030. return;
  2031. }
  2032. cmOStringStream w;
  2033. const char *includeDirs = this->GetProperty("INCLUDE_DIRECTORIES");
  2034. if(mightExpandVariablesCMP0019(includeDirs))
  2035. {
  2036. std::string dirs = includeDirs;
  2037. this->ExpandVariablesInString(dirs, true, true);
  2038. if(pol == cmPolicies::WARN && dirs != includeDirs)
  2039. {
  2040. w << "Evaluated directory INCLUDE_DIRECTORIES\n"
  2041. << " " << includeDirs << "\n"
  2042. << "as\n"
  2043. << " " << dirs << "\n";
  2044. }
  2045. this->SetProperty("INCLUDE_DIRECTORIES", dirs.c_str());
  2046. }
  2047. // Also for each target's INCLUDE_DIRECTORIES property:
  2048. for (cmTargets::iterator l = this->Targets.begin();
  2049. l != this->Targets.end(); ++l)
  2050. {
  2051. cmTarget &t = l->second;
  2052. includeDirs = t.GetProperty("INCLUDE_DIRECTORIES");
  2053. if(mightExpandVariablesCMP0019(includeDirs))
  2054. {
  2055. std::string dirs = includeDirs;
  2056. this->ExpandVariablesInString(dirs, true, true);
  2057. if(pol == cmPolicies::WARN && dirs != includeDirs)
  2058. {
  2059. w << "Evaluated target " << t.GetName() << " INCLUDE_DIRECTORIES\n"
  2060. << " " << includeDirs << "\n"
  2061. << "as\n"
  2062. << " " << dirs << "\n";
  2063. }
  2064. t.SetProperty("INCLUDE_DIRECTORIES", dirs.c_str());
  2065. }
  2066. }
  2067. for(std::vector<std::string>::iterator d = this->LinkDirectories.begin();
  2068. d != this->LinkDirectories.end(); ++d)
  2069. {
  2070. if(mightExpandVariablesCMP0019(d->c_str()))
  2071. {
  2072. std::string orig = *d;
  2073. this->ExpandVariablesInString(*d, true, true);
  2074. if(pol == cmPolicies::WARN && *d != orig)
  2075. {
  2076. w << "Evaluated link directory\n"
  2077. << " " << orig << "\n"
  2078. << "as\n"
  2079. << " " << *d << "\n";
  2080. }
  2081. }
  2082. }
  2083. for(cmTarget::LinkLibraryVectorType::iterator l =
  2084. this->LinkLibraries.begin();
  2085. l != this->LinkLibraries.end(); ++l)
  2086. {
  2087. if(mightExpandVariablesCMP0019(l->first.c_str()))
  2088. {
  2089. std::string orig = l->first;
  2090. this->ExpandVariablesInString(l->first, true, true);
  2091. if(pol == cmPolicies::WARN && l->first != orig)
  2092. {
  2093. w << "Evaluated link library\n"
  2094. << " " << orig << "\n"
  2095. << "as\n"
  2096. << " " << l->first << "\n";
  2097. }
  2098. }
  2099. }
  2100. if(!w.str().empty())
  2101. {
  2102. cmOStringStream m;
  2103. m << this->GetPolicies()->GetPolicyWarning(cmPolicies::CMP0019)
  2104. << "\n"
  2105. << "The following variable evaluations were encountered:\n"
  2106. << w.str();
  2107. this->IssueMessage(cmake::AUTHOR_WARNING, m.str());
  2108. }
  2109. }
  2110. bool cmMakefile::IsOn(const char* name) const
  2111. {
  2112. const char* value = this->GetDefinition(name);
  2113. return cmSystemTools::IsOn(value);
  2114. }
  2115. bool cmMakefile::IsSet(const char* name) const
  2116. {
  2117. const char* value = this->GetDefinition(name);
  2118. if ( !value )
  2119. {
  2120. return false;
  2121. }
  2122. if ( ! *value )
  2123. {
  2124. return false;
  2125. }
  2126. if ( cmSystemTools::IsNOTFOUND(value) )
  2127. {
  2128. return false;
  2129. }
  2130. return true;
  2131. }
  2132. bool cmMakefile::PlatformIs64Bit() const
  2133. {
  2134. if(const char* sizeof_dptr = this->GetDefinition("CMAKE_SIZEOF_VOID_P"))
  2135. {
  2136. return atoi(sizeof_dptr) == 8;
  2137. }
  2138. return false;
  2139. }
  2140. const char* cmMakefile::GetSONameFlag(const char* language) const
  2141. {
  2142. std::string name = "CMAKE_SHARED_LIBRARY_SONAME";
  2143. if(language)
  2144. {
  2145. name += "_";
  2146. name += language;
  2147. }
  2148. name += "_FLAG";
  2149. return GetDefinition(name.c_str());
  2150. }
  2151. bool cmMakefile::CanIWriteThisFile(const char* fileName)
  2152. {
  2153. if ( !this->IsOn("CMAKE_DISABLE_SOURCE_CHANGES") )
  2154. {
  2155. return true;
  2156. }
  2157. // If we are doing an in-source build, then the test will always fail
  2158. if ( cmSystemTools::SameFile(this->GetHomeDirectory(),
  2159. this->GetHomeOutputDirectory()) )
  2160. {
  2161. if ( this->IsOn("CMAKE_DISABLE_IN_SOURCE_BUILD") )
  2162. {
  2163. return false;
  2164. }
  2165. return true;
  2166. }
  2167. // Check if this is a subdirectory of the source tree but not a
  2168. // subdirectory of the build tree
  2169. if ( cmSystemTools::IsSubDirectory(fileName,
  2170. this->GetHomeDirectory()) &&
  2171. !cmSystemTools::IsSubDirectory(fileName,
  2172. this->GetHomeOutputDirectory()) )
  2173. {
  2174. return false;
  2175. }
  2176. return true;
  2177. }
  2178. const char* cmMakefile::GetRequiredDefinition(const char* name) const
  2179. {
  2180. const char* ret = this->GetDefinition(name);
  2181. if(!ret)
  2182. {
  2183. cmSystemTools::Error("Error required internal CMake variable not "
  2184. "set, cmake may be not be built correctly.\n",
  2185. "Missing variable is:\n",
  2186. name);
  2187. return "";
  2188. }
  2189. return ret;
  2190. }
  2191. bool cmMakefile::IsDefinitionSet(const char* name) const
  2192. {
  2193. const char* def = this->Internal->VarStack.top().Get(name);
  2194. this->Internal->VarUsageStack.top().insert(name);
  2195. if(!def)
  2196. {
  2197. def = this->GetCacheManager()->GetCacheValue(name);
  2198. }
  2199. #ifdef CMAKE_BUILD_WITH_CMAKE
  2200. if(cmVariableWatch* vv = this->GetVariableWatch())
  2201. {
  2202. if(!def)
  2203. {
  2204. vv->VariableAccessed
  2205. (name, cmVariableWatch::UNKNOWN_VARIABLE_DEFINED_ACCESS,
  2206. def, this);
  2207. }
  2208. }
  2209. #endif
  2210. return def?true:false;
  2211. }
  2212. const char* cmMakefile::GetDefinition(const char* name) const
  2213. {
  2214. if (this->WarnUnused)
  2215. {
  2216. this->Internal->VarUsageStack.top().insert(name);
  2217. }
  2218. const char* def = this->Internal->VarStack.top().Get(name);
  2219. if(!def)
  2220. {
  2221. def = this->GetCacheManager()->GetCacheValue(name);
  2222. }
  2223. #ifdef CMAKE_BUILD_WITH_CMAKE
  2224. cmVariableWatch* vv = this->GetVariableWatch();
  2225. if ( vv )
  2226. {
  2227. if ( def )
  2228. {
  2229. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_READ_ACCESS,
  2230. def, this);
  2231. }
  2232. else
  2233. {
  2234. // are unknown access allowed
  2235. const char* allow = this->Internal->VarStack.top()
  2236. .Get("CMAKE_ALLOW_UNKNOWN_VARIABLE_READ_ACCESS");
  2237. if(cmSystemTools::IsOn(allow))
  2238. {
  2239. vv->VariableAccessed(name,
  2240. cmVariableWatch::ALLOWED_UNKNOWN_VARIABLE_READ_ACCESS, def, this);
  2241. }
  2242. else
  2243. {
  2244. vv->VariableAccessed(name,
  2245. cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS, def, this);
  2246. }
  2247. }
  2248. }
  2249. #endif
  2250. return def;
  2251. }
  2252. const char* cmMakefile::GetSafeDefinition(const char* def) const
  2253. {
  2254. const char* ret = this->GetDefinition(def);
  2255. if(!ret)
  2256. {
  2257. return "";
  2258. }
  2259. return ret;
  2260. }
  2261. std::vector<std::string> cmMakefile
  2262. ::GetDefinitions(int cacheonly /* = 0 */) const
  2263. {
  2264. std::set<cmStdString> definitions;
  2265. if ( !cacheonly )
  2266. {
  2267. definitions = this->Internal->VarStack.top().ClosureKeys();
  2268. }
  2269. cmCacheManager::CacheIterator cit =
  2270. this->GetCacheManager()->GetCacheIterator();
  2271. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  2272. {
  2273. definitions.insert(cit.GetName());
  2274. }
  2275. std::vector<std::string> res;
  2276. std::set<cmStdString>::iterator fit;
  2277. for ( fit = definitions.begin(); fit != definitions.end(); fit ++ )
  2278. {
  2279. res.push_back(*fit);
  2280. }
  2281. return res;
  2282. }
  2283. const char *cmMakefile::ExpandVariablesInString(std::string& source)
  2284. {
  2285. return this->ExpandVariablesInString(source, false, false);
  2286. }
  2287. const char *cmMakefile::ExpandVariablesInString(std::string& source,
  2288. bool escapeQuotes,
  2289. bool noEscapes,
  2290. bool atOnly,
  2291. const char* filename,
  2292. long line,
  2293. bool removeEmpty,
  2294. bool replaceAt)
  2295. {
  2296. if ( source.empty() || source.find_first_of("$@\\") == source.npos)
  2297. {
  2298. return source.c_str();
  2299. }
  2300. // Special-case the @ONLY mode.
  2301. if(atOnly)
  2302. {
  2303. if(!noEscapes || !removeEmpty || !replaceAt)
  2304. {
  2305. // This case should never be called. At-only is for
  2306. // configure-file/string which always does no escapes.
  2307. this->IssueMessage(cmake::INTERNAL_ERROR,
  2308. "ExpandVariablesInString @ONLY called "
  2309. "on something with escapes.");
  2310. }
  2311. // Store an original copy of the input.
  2312. std::string input = source;
  2313. // Start with empty output.
  2314. source = "";
  2315. // Look for one @VAR@ at a time.
  2316. const char* in = input.c_str();
  2317. while(this->cmAtVarRegex.find(in))
  2318. {
  2319. // Get the range of the string to replace.
  2320. const char* first = in + this->cmAtVarRegex.start();
  2321. const char* last = in + this->cmAtVarRegex.end();
  2322. // Store the unchanged part of the string now.
  2323. source.append(in, first-in);
  2324. // Lookup the definition of VAR.
  2325. std::string var(first+1, last-first-2);
  2326. if(const char* val = this->GetDefinition(var.c_str()))
  2327. {
  2328. // Store the value in the output escaping as requested.
  2329. if(escapeQuotes)
  2330. {
  2331. source.append(cmSystemTools::EscapeQuotes(val));
  2332. }
  2333. else
  2334. {
  2335. source.append(val);
  2336. }
  2337. }
  2338. // Continue looking for @VAR@ further along the string.
  2339. in = last;
  2340. }
  2341. // Append the rest of the unchanged part of the string.
  2342. source.append(in);
  2343. return source.c_str();
  2344. }
  2345. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  2346. // with GetDefinition(), if not found in the map, nothing is expanded.
  2347. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  2348. // the current environment variables.
  2349. cmCommandArgumentParserHelper parser;
  2350. parser.SetMakefile(this);
  2351. parser.SetLineFile(line, filename);
  2352. parser.SetEscapeQuotes(escapeQuotes);
  2353. parser.SetNoEscapeMode(noEscapes);
  2354. parser.SetReplaceAtSyntax(replaceAt);
  2355. parser.SetRemoveEmpty(removeEmpty);
  2356. int res = parser.ParseString(source.c_str(), 0);
  2357. const char* emsg = parser.GetError();
  2358. if ( res && !emsg[0] )
  2359. {
  2360. source = parser.GetResult();
  2361. }
  2362. else
  2363. {
  2364. // Construct the main error message.
  2365. cmOStringStream error;
  2366. error << "Syntax error in cmake code ";
  2367. if(filename && line > 0)
  2368. {
  2369. // This filename and line number may be more specific than the
  2370. // command context because one command invocation can have
  2371. // arguments on multiple lines.
  2372. error << "at\n"
  2373. << " " << filename << ":" << line << "\n";
  2374. }
  2375. error << "when parsing string\n"
  2376. << " " << source.c_str() << "\n";
  2377. error << emsg;
  2378. // If the parser failed ("res" is false) then this is a real
  2379. // argument parsing error, so the policy applies. Otherwise the
  2380. // parser reported an error message without failing because the
  2381. // helper implementation is unhappy, which has always reported an
  2382. // error.
  2383. cmake::MessageType mtype = cmake::FATAL_ERROR;
  2384. if(!res)
  2385. {
  2386. // This is a real argument parsing error. Use policy CMP0010 to
  2387. // decide whether it is an error.
  2388. switch(this->GetPolicyStatus(cmPolicies::CMP0010))
  2389. {
  2390. case cmPolicies::WARN:
  2391. error << "\n"
  2392. << (this->GetPolicies()
  2393. ->GetPolicyWarning(cmPolicies::CMP0010));
  2394. case cmPolicies::OLD:
  2395. // OLD behavior is to just warn and continue.
  2396. mtype = cmake::AUTHOR_WARNING;
  2397. break;
  2398. case cmPolicies::REQUIRED_IF_USED:
  2399. case cmPolicies::REQUIRED_ALWAYS:
  2400. error << "\n"
  2401. << (this->GetPolicies()
  2402. ->GetRequiredPolicyError(cmPolicies::CMP0010));
  2403. case cmPolicies::NEW:
  2404. // NEW behavior is to report the error.
  2405. cmSystemTools::SetFatalErrorOccured();
  2406. break;
  2407. }
  2408. }
  2409. this->IssueMessage(mtype, error.str());
  2410. }
  2411. return source.c_str();
  2412. }
  2413. void cmMakefile::RemoveVariablesInString(std::string& source,
  2414. bool atOnly) const
  2415. {
  2416. if(!atOnly)
  2417. {
  2418. cmsys::RegularExpression var("(\\${[A-Za-z_0-9]*})");
  2419. while (var.find(source))
  2420. {
  2421. source.erase(var.start(),var.end() - var.start());
  2422. }
  2423. }
  2424. if(!atOnly)
  2425. {
  2426. cmsys::RegularExpression varb("(\\$ENV{[A-Za-z_0-9]*})");
  2427. while (varb.find(source))
  2428. {
  2429. source.erase(varb.start(),varb.end() - varb.start());
  2430. }
  2431. }
  2432. cmsys::RegularExpression var2("(@[A-Za-z_0-9]*@)");
  2433. while (var2.find(source))
  2434. {
  2435. source.erase(var2.start(),var2.end() - var2.start());
  2436. }
  2437. }
  2438. /**
  2439. * Add the default definitions to the makefile. These values must not
  2440. * be dependent on anything that isn't known when this cmMakefile instance
  2441. * is constructed.
  2442. */
  2443. void cmMakefile::AddDefaultDefinitions()
  2444. {
  2445. /* Up to CMake 2.4 here only WIN32, UNIX and APPLE were set.
  2446. With CMake must separate between target and host platform. In most cases
  2447. the tests for WIN32, UNIX and APPLE will be for the target system, so an
  2448. additional set of variables for the host system is required ->
  2449. CMAKE_HOST_WIN32, CMAKE_HOST_UNIX, CMAKE_HOST_APPLE.
  2450. WIN32, UNIX and APPLE are now set in the platform files in
  2451. Modules/Platforms/.
  2452. To keep cmake scripts (-P) and custom language and compiler modules
  2453. working, these variables are still also set here in this place, but they
  2454. will be reset in CMakeSystemSpecificInformation.cmake before the platform
  2455. files are executed. */
  2456. #if defined(_WIN32)
  2457. this->AddDefinition("WIN32", "1");
  2458. this->AddDefinition("CMAKE_HOST_WIN32", "1");
  2459. #else
  2460. this->AddDefinition("UNIX", "1");
  2461. this->AddDefinition("CMAKE_HOST_UNIX", "1");
  2462. #endif
  2463. #if defined(__CYGWIN__)
  2464. if(cmSystemTools::IsOn(cmSystemTools::GetEnv("CMAKE_LEGACY_CYGWIN_WIN32")))
  2465. {
  2466. this->AddDefinition("WIN32", "1");
  2467. this->AddDefinition("CMAKE_HOST_WIN32", "1");
  2468. }
  2469. #endif
  2470. #if defined(__APPLE__)
  2471. this->AddDefinition("APPLE", "1");
  2472. this->AddDefinition("CMAKE_HOST_APPLE", "1");
  2473. #endif
  2474. char temp[1024];
  2475. sprintf(temp, "%d", cmVersion::GetMinorVersion());
  2476. this->AddDefinition("CMAKE_MINOR_VERSION", temp);
  2477. sprintf(temp, "%d", cmVersion::GetMajorVersion());
  2478. this->AddDefinition("CMAKE_MAJOR_VERSION", temp);
  2479. sprintf(temp, "%d", cmVersion::GetPatchVersion());
  2480. this->AddDefinition("CMAKE_PATCH_VERSION", temp);
  2481. sprintf(temp, "%d", cmVersion::GetTweakVersion());
  2482. this->AddDefinition("CMAKE_TWEAK_VERSION", temp);
  2483. this->AddDefinition("CMAKE_VERSION", cmVersion::GetCMakeVersion());
  2484. this->AddDefinition("CMAKE_FILES_DIRECTORY",
  2485. cmake::GetCMakeFilesDirectory());
  2486. }
  2487. //----------------------------------------------------------------------------
  2488. const char*
  2489. cmMakefile::GetConfigurations(std::vector<std::string>& configs,
  2490. bool single) const
  2491. {
  2492. if(this->LocalGenerator->GetGlobalGenerator()->IsMultiConfig())
  2493. {
  2494. if(const char* configTypes =
  2495. this->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  2496. {
  2497. cmSystemTools::ExpandListArgument(configTypes, configs);
  2498. }
  2499. return 0;
  2500. }
  2501. else
  2502. {
  2503. const char* buildType = this->GetDefinition("CMAKE_BUILD_TYPE");
  2504. if(single && buildType && *buildType)
  2505. {
  2506. configs.push_back(buildType);
  2507. }
  2508. return buildType;
  2509. }
  2510. }
  2511. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2512. /**
  2513. * Find a source group whose regular expression matches the filename
  2514. * part of the given source name. Search backward through the list of
  2515. * source groups, and take the first matching group found. This way
  2516. * non-inherited SOURCE_GROUP commands will have precedence over
  2517. * inherited ones.
  2518. */
  2519. cmSourceGroup&
  2520. cmMakefile::FindSourceGroup(const char* source,
  2521. std::vector<cmSourceGroup> &groups)
  2522. {
  2523. // First search for a group that lists the file explicitly.
  2524. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  2525. sg != groups.rend(); ++sg)
  2526. {
  2527. cmSourceGroup *result = sg->MatchChildrenFiles(source);
  2528. if(result)
  2529. {
  2530. return *result;
  2531. }
  2532. }
  2533. // Now search for a group whose regex matches the file.
  2534. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  2535. sg != groups.rend(); ++sg)
  2536. {
  2537. cmSourceGroup *result = sg->MatchChildrenRegex(source);
  2538. if(result)
  2539. {
  2540. return *result;
  2541. }
  2542. }
  2543. // Shouldn't get here, but just in case, return the default group.
  2544. return groups.front();
  2545. }
  2546. #endif
  2547. bool cmMakefile::IsFunctionBlocked(const cmListFileFunction& lff,
  2548. cmExecutionStatus &status)
  2549. {
  2550. // if there are no blockers get out of here
  2551. if (this->FunctionBlockers.begin() == this->FunctionBlockers.end())
  2552. {
  2553. return false;
  2554. }
  2555. // loop over all function blockers to see if any block this command
  2556. // evaluate in reverse, this is critical for balanced IF statements etc
  2557. std::vector<cmFunctionBlocker*>::reverse_iterator pos;
  2558. for (pos = this->FunctionBlockers.rbegin();
  2559. pos != this->FunctionBlockers.rend(); ++pos)
  2560. {
  2561. if((*pos)->IsFunctionBlocked(lff, *this, status))
  2562. {
  2563. return true;
  2564. }
  2565. }
  2566. return false;
  2567. }
  2568. //----------------------------------------------------------------------------
  2569. void cmMakefile::PushFunctionBlockerBarrier()
  2570. {
  2571. this->FunctionBlockerBarriers.push_back(this->FunctionBlockers.size());
  2572. }
  2573. //----------------------------------------------------------------------------
  2574. void cmMakefile::PopFunctionBlockerBarrier(bool reportError)
  2575. {
  2576. // Remove any extra entries pushed on the barrier.
  2577. FunctionBlockersType::size_type barrier =
  2578. this->FunctionBlockerBarriers.back();
  2579. while(this->FunctionBlockers.size() > barrier)
  2580. {
  2581. cmsys::auto_ptr<cmFunctionBlocker> fb(this->FunctionBlockers.back());
  2582. this->FunctionBlockers.pop_back();
  2583. if(reportError)
  2584. {
  2585. // Report the context in which the unclosed block was opened.
  2586. cmListFileContext const& lfc = fb->GetStartingContext();
  2587. cmOStringStream e;
  2588. e << "A logical block opening on the line\n"
  2589. << " " << lfc << "\n"
  2590. << "is not closed.";
  2591. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  2592. reportError = false;
  2593. }
  2594. }
  2595. // Remove the barrier.
  2596. this->FunctionBlockerBarriers.pop_back();
  2597. }
  2598. bool cmMakefile::ExpandArguments(
  2599. std::vector<cmListFileArgument> const& inArgs,
  2600. std::vector<std::string>& outArgs)
  2601. {
  2602. std::vector<cmListFileArgument>::const_iterator i;
  2603. std::string value;
  2604. outArgs.reserve(inArgs.size());
  2605. for(i = inArgs.begin(); i != inArgs.end(); ++i)
  2606. {
  2607. // Expand the variables in the argument.
  2608. value = i->Value;
  2609. this->ExpandVariablesInString(value, false, false, false,
  2610. i->FilePath, i->Line,
  2611. false, true);
  2612. // If the argument is quoted, it should be one argument.
  2613. // Otherwise, it may be a list of arguments.
  2614. if(i->Delim == cmListFileArgument::Quoted)
  2615. {
  2616. outArgs.push_back(value);
  2617. }
  2618. else
  2619. {
  2620. cmSystemTools::ExpandListArgument(value, outArgs);
  2621. }
  2622. }
  2623. return !cmSystemTools::GetFatalErrorOccured();
  2624. }
  2625. //----------------------------------------------------------------------------
  2626. void cmMakefile::AddFunctionBlocker(cmFunctionBlocker* fb)
  2627. {
  2628. if(!this->CallStack.empty())
  2629. {
  2630. // Record the context in which the blocker is created.
  2631. fb->SetStartingContext(*(this->CallStack.back().Context));
  2632. }
  2633. this->FunctionBlockers.push_back(fb);
  2634. }
  2635. cmsys::auto_ptr<cmFunctionBlocker>
  2636. cmMakefile::RemoveFunctionBlocker(cmFunctionBlocker* fb,
  2637. const cmListFileFunction& lff)
  2638. {
  2639. // Find the function blocker stack barrier for the current scope.
  2640. // We only remove a blocker whose index is not less than the barrier.
  2641. FunctionBlockersType::size_type barrier = 0;
  2642. if(!this->FunctionBlockerBarriers.empty())
  2643. {
  2644. barrier = this->FunctionBlockerBarriers.back();
  2645. }
  2646. // Search for the function blocker whose scope this command ends.
  2647. for(FunctionBlockersType::size_type
  2648. i = this->FunctionBlockers.size(); i > barrier; --i)
  2649. {
  2650. std::vector<cmFunctionBlocker*>::iterator pos =
  2651. this->FunctionBlockers.begin() + (i - 1);
  2652. if (*pos == fb)
  2653. {
  2654. // Warn if the arguments do not match, but always remove.
  2655. if(!(*pos)->ShouldRemove(lff, *this))
  2656. {
  2657. cmListFileContext const& lfc = fb->GetStartingContext();
  2658. cmOStringStream e;
  2659. e << "A logical block opening on the line\n"
  2660. << " " << lfc << "\n"
  2661. << "closes on the line\n"
  2662. << " " << lff << "\n"
  2663. << "with mis-matching arguments.";
  2664. this->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  2665. }
  2666. cmFunctionBlocker* b = *pos;
  2667. this->FunctionBlockers.erase(pos);
  2668. return cmsys::auto_ptr<cmFunctionBlocker>(b);
  2669. }
  2670. }
  2671. return cmsys::auto_ptr<cmFunctionBlocker>();
  2672. }
  2673. //----------------------------------------------------------------------------
  2674. cmMakefile::LexicalPushPop::LexicalPushPop(cmMakefile* mf):
  2675. Makefile(mf), ReportError(true)
  2676. {
  2677. this->Makefile->PushFunctionBlockerBarrier();
  2678. }
  2679. //----------------------------------------------------------------------------
  2680. cmMakefile::LexicalPushPop::~LexicalPushPop()
  2681. {
  2682. this->Makefile->PopFunctionBlockerBarrier(this->ReportError);
  2683. }
  2684. void cmMakefile::SetHomeDirectory(const char* dir)
  2685. {
  2686. this->cmHomeDirectory = dir;
  2687. cmSystemTools::ConvertToUnixSlashes(this->cmHomeDirectory);
  2688. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  2689. if ( !this->GetDefinition("CMAKE_CURRENT_SOURCE_DIR") )
  2690. {
  2691. this->AddDefinition("CMAKE_CURRENT_SOURCE_DIR", this->GetHomeDirectory());
  2692. }
  2693. }
  2694. void cmMakefile::SetHomeOutputDirectory(const char* lib)
  2695. {
  2696. this->HomeOutputDirectory = lib;
  2697. cmSystemTools::ConvertToUnixSlashes(this->HomeOutputDirectory);
  2698. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  2699. if ( !this->GetDefinition("CMAKE_CURRENT_BINARY_DIR") )
  2700. {
  2701. this->AddDefinition("CMAKE_CURRENT_BINARY_DIR",
  2702. this->GetHomeOutputDirectory());
  2703. }
  2704. }
  2705. void cmMakefile::SetScriptModeFile(const char* scriptfile)
  2706. {
  2707. this->AddDefinition("CMAKE_SCRIPT_MODE_FILE", scriptfile);
  2708. }
  2709. void cmMakefile::SetArgcArgv(const std::vector<std::string>& args)
  2710. {
  2711. cmOStringStream strStream;
  2712. strStream << args.size();
  2713. this->AddDefinition("CMAKE_ARGC", strStream.str().c_str());
  2714. //this->MarkVariableAsUsed("CMAKE_ARGC");
  2715. for (unsigned int t = 0; t < args.size(); ++t)
  2716. {
  2717. cmOStringStream tmpStream;
  2718. tmpStream << "CMAKE_ARGV" << t;
  2719. this->AddDefinition(tmpStream.str().c_str(), args[t].c_str());
  2720. //this->MarkVariableAsUsed(tmpStream.str().c_str());
  2721. }
  2722. }
  2723. //----------------------------------------------------------------------------
  2724. cmSourceFile* cmMakefile::GetSource(const char* sourceName)
  2725. {
  2726. cmSourceFileLocation sfl(this, sourceName);
  2727. for(std::vector<cmSourceFile*>::const_iterator
  2728. sfi = this->SourceFiles.begin();
  2729. sfi != this->SourceFiles.end(); ++sfi)
  2730. {
  2731. cmSourceFile* sf = *sfi;
  2732. if(sf->Matches(sfl))
  2733. {
  2734. return sf;
  2735. }
  2736. }
  2737. return 0;
  2738. }
  2739. //----------------------------------------------------------------------------
  2740. cmSourceFile* cmMakefile::GetOrCreateSource(const char* sourceName,
  2741. bool generated)
  2742. {
  2743. if(cmSourceFile* esf = this->GetSource(sourceName))
  2744. {
  2745. return esf;
  2746. }
  2747. else
  2748. {
  2749. cmSourceFile* sf = new cmSourceFile(this, sourceName);
  2750. if(generated)
  2751. {
  2752. sf->SetProperty("GENERATED", "1");
  2753. }
  2754. this->SourceFiles.push_back(sf);
  2755. return sf;
  2756. }
  2757. }
  2758. void cmMakefile::EnableLanguage(std::vector<std::string> const & lang,
  2759. bool optional)
  2760. {
  2761. this->AddDefinition("CMAKE_CFG_INTDIR",
  2762. this->LocalGenerator->GetGlobalGenerator()
  2763. ->GetCMakeCFGIntDir());
  2764. this->LocalGenerator->GetGlobalGenerator()->EnableLanguage(lang, this,
  2765. optional);
  2766. }
  2767. void cmMakefile::ExpandSourceListArguments(
  2768. std::vector<std::string> const& arguments,
  2769. std::vector<std::string>& newargs, unsigned int /* start */)
  2770. {
  2771. // now expand the args
  2772. unsigned int i;
  2773. for(i = 0; i < arguments.size(); ++i)
  2774. {
  2775. // List expansion will have been done already.
  2776. newargs.push_back(arguments[i]);
  2777. }
  2778. }
  2779. int cmMakefile::TryCompile(const char *srcdir, const char *bindir,
  2780. const char *projectName, const char *targetName,
  2781. bool fast,
  2782. const std::vector<std::string> *cmakeArgs,
  2783. std::string *output)
  2784. {
  2785. this->Internal->IsSourceFileTryCompile = fast;
  2786. // does the binary directory exist ? If not create it...
  2787. if (!cmSystemTools::FileIsDirectory(bindir))
  2788. {
  2789. cmSystemTools::MakeDirectory(bindir);
  2790. }
  2791. // change to the tests directory and run cmake
  2792. // use the cmake object instead of calling cmake
  2793. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  2794. cmSystemTools::ChangeDirectory(bindir);
  2795. // make sure the same generator is used
  2796. // use this program as the cmake to be run, it should not
  2797. // be run that way but the cmake object requires a vailid path
  2798. std::string cmakeCommand = this->GetDefinition("CMAKE_COMMAND");
  2799. cmake cm;
  2800. cm.SetIsInTryCompile(true);
  2801. cmGlobalGenerator *gg = cm.CreateGlobalGenerator
  2802. (this->LocalGenerator->GetGlobalGenerator()->GetName());
  2803. if (!gg)
  2804. {
  2805. cmSystemTools::Error(
  2806. "Internal CMake error, TryCompile bad GlobalGenerator");
  2807. // return to the original directory
  2808. cmSystemTools::ChangeDirectory(cwd.c_str());
  2809. this->Internal->IsSourceFileTryCompile = false;
  2810. return 1;
  2811. }
  2812. cm.SetGlobalGenerator(gg);
  2813. // do a configure
  2814. cm.SetHomeDirectory(srcdir);
  2815. cm.SetHomeOutputDirectory(bindir);
  2816. cm.SetStartDirectory(srcdir);
  2817. cm.SetStartOutputDirectory(bindir);
  2818. cm.SetCMakeCommand(cmakeCommand.c_str());
  2819. cm.SetGeneratorToolset(this->GetCMakeInstance()->GetGeneratorToolset());
  2820. cm.LoadCache();
  2821. if(!gg->IsMultiConfig())
  2822. {
  2823. if(const char* config =
  2824. this->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION"))
  2825. {
  2826. // Tell the single-configuration generator which one to use.
  2827. // Add this before the user-provided CMake arguments in case
  2828. // one of the arguments is -DCMAKE_BUILD_TYPE=...
  2829. cm.AddCacheEntry("CMAKE_BUILD_TYPE", config,
  2830. "Build configuration", cmCacheManager::STRING);
  2831. }
  2832. }
  2833. // if cmake args were provided then pass them in
  2834. if (cmakeArgs)
  2835. {
  2836. // FIXME: Workaround to ignore unused CLI variables in try-compile.
  2837. //
  2838. // Ideally we should use SetArgs to honor options like --warn-unused-vars.
  2839. // However, there is a subtle problem when certain arguments are passed to
  2840. // a macro wrapping around try_compile or try_run that does not escape
  2841. // semicolons in its parameters but just passes ${ARGV} or ${ARGN}. In
  2842. // this case a list argument like "-DVAR=a;b" gets split into multiple
  2843. // cmake arguments "-DVAR=a" and "b". Currently SetCacheArgs ignores
  2844. // argument "b" and uses just "-DVAR=a", leading to a subtle bug in that
  2845. // the try_compile or try_run does not get the proper value of VAR. If we
  2846. // call SetArgs here then it would treat "b" as the source directory and
  2847. // cause an error such as "The source directory .../CMakeFiles/CMakeTmp/b
  2848. // does not exist", thus breaking the try_compile or try_run completely.
  2849. //
  2850. // Strictly speaking the bug is in the wrapper macro because the CMake
  2851. // language has always flattened nested lists and the macro should escape
  2852. // the semicolons in its arguments before forwarding them. However, this
  2853. // bug is so subtle that projects typically work anyway, usually because
  2854. // the value VAR=a is sufficient for the try_compile or try_run to get the
  2855. // correct result. Calling SetArgs here would break such projects that
  2856. // previously built. Instead we work around the issue by never reporting
  2857. // unused arguments and ignoring options such as --warn-unused-vars.
  2858. cm.SetWarnUnusedCli(false);
  2859. //cm.SetArgs(*cmakeArgs, true);
  2860. cm.SetCacheArgs(*cmakeArgs);
  2861. }
  2862. // to save time we pass the EnableLanguage info directly
  2863. gg->EnableLanguagesFromGenerator
  2864. (this->LocalGenerator->GetGlobalGenerator(), this);
  2865. if(this->IsOn("CMAKE_SUPPRESS_DEVELOPER_WARNINGS"))
  2866. {
  2867. cm.AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS",
  2868. "TRUE", "", cmCacheManager::INTERNAL);
  2869. }
  2870. else
  2871. {
  2872. cm.AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS",
  2873. "FALSE", "", cmCacheManager::INTERNAL);
  2874. }
  2875. if (cm.Configure() != 0)
  2876. {
  2877. cmSystemTools::Error(
  2878. "Internal CMake error, TryCompile configure of cmake failed");
  2879. // return to the original directory
  2880. cmSystemTools::ChangeDirectory(cwd.c_str());
  2881. this->Internal->IsSourceFileTryCompile = false;
  2882. return 1;
  2883. }
  2884. if (cm.Generate() != 0)
  2885. {
  2886. cmSystemTools::Error(
  2887. "Internal CMake error, TryCompile generation of cmake failed");
  2888. // return to the original directory
  2889. cmSystemTools::ChangeDirectory(cwd.c_str());
  2890. this->Internal->IsSourceFileTryCompile = false;
  2891. return 1;
  2892. }
  2893. // finally call the generator to actually build the resulting project
  2894. int ret =
  2895. this->LocalGenerator->GetGlobalGenerator()->TryCompile(srcdir,bindir,
  2896. projectName,
  2897. targetName,
  2898. fast,
  2899. output,
  2900. this);
  2901. cmSystemTools::ChangeDirectory(cwd.c_str());
  2902. this->Internal->IsSourceFileTryCompile = false;
  2903. return ret;
  2904. }
  2905. bool cmMakefile::GetIsSourceFileTryCompile() const
  2906. {
  2907. return this->Internal->IsSourceFileTryCompile;
  2908. }
  2909. cmake *cmMakefile::GetCMakeInstance() const
  2910. {
  2911. if ( this->LocalGenerator && this->LocalGenerator->GetGlobalGenerator() )
  2912. {
  2913. return this->LocalGenerator->GetGlobalGenerator()->GetCMakeInstance();
  2914. }
  2915. return 0;
  2916. }
  2917. #ifdef CMAKE_BUILD_WITH_CMAKE
  2918. cmVariableWatch *cmMakefile::GetVariableWatch() const
  2919. {
  2920. if ( this->GetCMakeInstance() &&
  2921. this->GetCMakeInstance()->GetVariableWatch() )
  2922. {
  2923. return this->GetCMakeInstance()->GetVariableWatch();
  2924. }
  2925. return 0;
  2926. }
  2927. #endif
  2928. void cmMakefile::AddMacro(const char* name, const char* signature)
  2929. {
  2930. if ( !name || !signature )
  2931. {
  2932. return;
  2933. }
  2934. this->MacrosMap[name] = signature;
  2935. }
  2936. void cmMakefile::GetListOfMacros(std::string& macros)
  2937. {
  2938. StringStringMap::iterator it;
  2939. macros = "";
  2940. int cc = 0;
  2941. for ( it = this->MacrosMap.begin(); it != this->MacrosMap.end(); ++it )
  2942. {
  2943. if ( cc > 0 )
  2944. {
  2945. macros += ";";
  2946. }
  2947. macros += it->first;
  2948. cc ++;
  2949. }
  2950. }
  2951. cmCacheManager *cmMakefile::GetCacheManager() const
  2952. {
  2953. return this->GetCMakeInstance()->GetCacheManager();
  2954. }
  2955. void cmMakefile::DisplayStatus(const char* message, float s)
  2956. {
  2957. cmake* cm = this->GetLocalGenerator()->GetGlobalGenerator()
  2958. ->GetCMakeInstance();
  2959. if (cm->GetWorkingMode() == cmake::FIND_PACKAGE_MODE)
  2960. {
  2961. // don't output any STATUS message in FIND_PACKAGE_MODE, since they will
  2962. // directly be fed to the compiler, which will be confused.
  2963. return;
  2964. }
  2965. cm->UpdateProgress(message, s);
  2966. }
  2967. std::string cmMakefile::GetModulesFile(const char* filename)
  2968. {
  2969. std::string result;
  2970. // We search the module always in CMAKE_ROOT and in CMAKE_MODULE_PATH,
  2971. // and then decide based on the policy setting which one to return.
  2972. // See CMP0017 for more details.
  2973. // The specific problem was that KDE 4.5.0 installs a
  2974. // FindPackageHandleStandardArgs.cmake which doesn't have the new features
  2975. // of FPHSA.cmake introduced in CMake 2.8.3 yet, and by setting
  2976. // CMAKE_MODULE_PATH also e.g. FindZLIB.cmake from cmake included
  2977. // FPHSA.cmake from kdelibs and not from CMake, and tried to use the
  2978. // new features, which were not there in the version from kdelibs, and so
  2979. // failed ("
  2980. std::string moduleInCMakeRoot;
  2981. std::string moduleInCMakeModulePath;
  2982. // Always search in CMAKE_MODULE_PATH:
  2983. const char* cmakeModulePath = this->GetDefinition("CMAKE_MODULE_PATH");
  2984. if(cmakeModulePath)
  2985. {
  2986. std::vector<std::string> modulePath;
  2987. cmSystemTools::ExpandListArgument(cmakeModulePath, modulePath);
  2988. //Look through the possible module directories.
  2989. for(std::vector<std::string>::iterator i = modulePath.begin();
  2990. i != modulePath.end(); ++i)
  2991. {
  2992. std::string itempl = *i;
  2993. cmSystemTools::ConvertToUnixSlashes(itempl);
  2994. itempl += "/";
  2995. itempl += filename;
  2996. if(cmSystemTools::FileExists(itempl.c_str()))
  2997. {
  2998. moduleInCMakeModulePath = itempl;
  2999. break;
  3000. }
  3001. }
  3002. }
  3003. // Always search in the standard modules location.
  3004. const char* cmakeRoot = this->GetDefinition("CMAKE_ROOT");
  3005. if(cmakeRoot)
  3006. {
  3007. moduleInCMakeRoot = cmakeRoot;
  3008. moduleInCMakeRoot += "/Modules/";
  3009. moduleInCMakeRoot += filename;
  3010. cmSystemTools::ConvertToUnixSlashes(moduleInCMakeRoot);
  3011. if(!cmSystemTools::FileExists(moduleInCMakeRoot.c_str()))
  3012. {
  3013. moduleInCMakeRoot = "";
  3014. }
  3015. }
  3016. // Normally, prefer the files found in CMAKE_MODULE_PATH. Only when the file
  3017. // from which we are being called is located itself in CMAKE_ROOT, then
  3018. // prefer results from CMAKE_ROOT depending on the policy setting.
  3019. result = moduleInCMakeModulePath;
  3020. if (result.size() == 0)
  3021. {
  3022. result = moduleInCMakeRoot;
  3023. }
  3024. if ((moduleInCMakeModulePath.size()>0) && (moduleInCMakeRoot.size()>0))
  3025. {
  3026. const char* currentFile = this->GetDefinition("CMAKE_CURRENT_LIST_FILE");
  3027. if (currentFile && (strstr(currentFile, cmakeRoot) == currentFile))
  3028. {
  3029. switch (this->GetPolicyStatus(cmPolicies::CMP0017))
  3030. {
  3031. case cmPolicies::WARN:
  3032. {
  3033. cmOStringStream e;
  3034. e << "File " << currentFile << " includes "
  3035. << moduleInCMakeModulePath
  3036. << " (found via CMAKE_MODULE_PATH) which shadows "
  3037. << moduleInCMakeRoot << ". This may cause errors later on .\n"
  3038. << this->GetPolicies()->GetPolicyWarning(cmPolicies::CMP0017);
  3039. this->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  3040. // break; // fall through to OLD behaviour
  3041. }
  3042. case cmPolicies::OLD:
  3043. result = moduleInCMakeModulePath;
  3044. break;
  3045. case cmPolicies::REQUIRED_IF_USED:
  3046. case cmPolicies::REQUIRED_ALWAYS:
  3047. case cmPolicies::NEW:
  3048. default:
  3049. result = moduleInCMakeRoot;
  3050. break;
  3051. }
  3052. }
  3053. }
  3054. return result;
  3055. }
  3056. void cmMakefile::ConfigureString(const std::string& input,
  3057. std::string& output, bool atOnly,
  3058. bool escapeQuotes)
  3059. {
  3060. // Split input to handle one line at a time.
  3061. std::string::const_iterator lineStart = input.begin();
  3062. while(lineStart != input.end())
  3063. {
  3064. // Find the end of this line.
  3065. std::string::const_iterator lineEnd = lineStart;
  3066. while(lineEnd != input.end() && *lineEnd != '\n')
  3067. {
  3068. ++lineEnd;
  3069. }
  3070. // Copy the line.
  3071. std::string line(lineStart, lineEnd);
  3072. // Skip the newline character.
  3073. bool haveNewline = (lineEnd != input.end());
  3074. if(haveNewline)
  3075. {
  3076. ++lineEnd;
  3077. }
  3078. // Replace #cmakedefine instances.
  3079. if(this->cmDefineRegex.find(line))
  3080. {
  3081. const char* def =
  3082. this->GetDefinition(this->cmDefineRegex.match(1).c_str());
  3083. if(!cmSystemTools::IsOff(def))
  3084. {
  3085. cmSystemTools::ReplaceString(line, "#cmakedefine", "#define");
  3086. output += line;
  3087. }
  3088. else
  3089. {
  3090. output += "/* #undef ";
  3091. output += this->cmDefineRegex.match(1);
  3092. output += " */";
  3093. }
  3094. }
  3095. else if(this->cmDefine01Regex.find(line))
  3096. {
  3097. const char* def =
  3098. this->GetDefinition(this->cmDefine01Regex.match(1).c_str());
  3099. cmSystemTools::ReplaceString(line, "#cmakedefine01", "#define");
  3100. output += line;
  3101. if(!cmSystemTools::IsOff(def))
  3102. {
  3103. output += " 1";
  3104. }
  3105. else
  3106. {
  3107. output += " 0";
  3108. }
  3109. }
  3110. else
  3111. {
  3112. output += line;
  3113. }
  3114. if(haveNewline)
  3115. {
  3116. output += "\n";
  3117. }
  3118. // Move to the next line.
  3119. lineStart = lineEnd;
  3120. }
  3121. // Perform variable replacements.
  3122. this->ExpandVariablesInString(output, escapeQuotes, true,
  3123. atOnly, 0, -1, true);
  3124. }
  3125. int cmMakefile::ConfigureFile(const char* infile, const char* outfile,
  3126. bool copyonly, bool atOnly, bool escapeQuotes,
  3127. const cmNewLineStyle& newLine)
  3128. {
  3129. int res = 1;
  3130. if ( !this->CanIWriteThisFile(outfile) )
  3131. {
  3132. cmSystemTools::Error("Attempt to write file: ",
  3133. outfile, " into a source directory.");
  3134. return 0;
  3135. }
  3136. if ( !cmSystemTools::FileExists(infile) )
  3137. {
  3138. cmSystemTools::Error("File ", infile, " does not exist.");
  3139. return 0;
  3140. }
  3141. std::string soutfile = outfile;
  3142. std::string sinfile = infile;
  3143. this->AddCMakeDependFile(sinfile);
  3144. cmSystemTools::ConvertToUnixSlashes(soutfile);
  3145. // Re-generate if non-temporary outputs are missing.
  3146. //when we finalize the configuration we will remove all
  3147. //output files that now don't exist.
  3148. this->AddCMakeOutputFile(soutfile);
  3149. mode_t perm = 0;
  3150. cmSystemTools::GetPermissions(sinfile.c_str(), perm);
  3151. std::string::size_type pos = soutfile.rfind('/');
  3152. if(pos != std::string::npos)
  3153. {
  3154. std::string path = soutfile.substr(0, pos);
  3155. cmSystemTools::MakeDirectory(path.c_str());
  3156. }
  3157. if(copyonly)
  3158. {
  3159. if ( !cmSystemTools::CopyFileIfDifferent(sinfile.c_str(),
  3160. soutfile.c_str()))
  3161. {
  3162. return 0;
  3163. }
  3164. }
  3165. else
  3166. {
  3167. std::string newLineCharacters;
  3168. std::ios_base::openmode omode = std::ios_base::out | std::ios_base::trunc;
  3169. if (newLine.IsValid())
  3170. {
  3171. newLineCharacters = newLine.GetCharacters();
  3172. omode |= std::ios::binary;
  3173. }
  3174. else
  3175. {
  3176. newLineCharacters = "\n";
  3177. }
  3178. std::string tempOutputFile = soutfile;
  3179. tempOutputFile += ".tmp";
  3180. std::ofstream fout(tempOutputFile.c_str(), omode);
  3181. if(!fout)
  3182. {
  3183. cmSystemTools::Error(
  3184. "Could not open file for write in copy operation ",
  3185. tempOutputFile.c_str());
  3186. cmSystemTools::ReportLastSystemError("");
  3187. return 0;
  3188. }
  3189. std::ifstream fin(sinfile.c_str());
  3190. if(!fin)
  3191. {
  3192. cmSystemTools::Error("Could not open file for read in copy operation ",
  3193. sinfile.c_str());
  3194. return 0;
  3195. }
  3196. // now copy input to output and expand variables in the
  3197. // input file at the same time
  3198. std::string inLine;
  3199. std::string outLine;
  3200. while( cmSystemTools::GetLineFromStream(fin, inLine) )
  3201. {
  3202. outLine = "";
  3203. this->ConfigureString(inLine, outLine, atOnly, escapeQuotes);
  3204. fout << outLine.c_str() << newLineCharacters;
  3205. }
  3206. // close the files before attempting to copy
  3207. fin.close();
  3208. fout.close();
  3209. if ( !cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  3210. soutfile.c_str()) )
  3211. {
  3212. res = 0;
  3213. }
  3214. else
  3215. {
  3216. cmSystemTools::SetPermissions(soutfile.c_str(), perm);
  3217. }
  3218. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  3219. }
  3220. return res;
  3221. }
  3222. void cmMakefile::SetProperty(const char* prop, const char* value)
  3223. {
  3224. if (!prop)
  3225. {
  3226. return;
  3227. }
  3228. // handle special props
  3229. std::string propname = prop;
  3230. if ( propname == "LINK_DIRECTORIES" )
  3231. {
  3232. std::vector<std::string> varArgsExpanded;
  3233. if(value)
  3234. {
  3235. cmSystemTools::ExpandListArgument(value, varArgsExpanded);
  3236. }
  3237. this->SetLinkDirectories(varArgsExpanded);
  3238. return;
  3239. }
  3240. if (propname == "INCLUDE_DIRECTORIES")
  3241. {
  3242. this->IncludeDirectoriesEntries.clear();
  3243. if (!value)
  3244. {
  3245. return;
  3246. }
  3247. cmListFileBacktrace lfbt;
  3248. this->GetBacktrace(lfbt);
  3249. this->IncludeDirectoriesEntries.push_back(
  3250. cmValueWithOrigin(value, lfbt));
  3251. return;
  3252. }
  3253. if (propname == "COMPILE_OPTIONS")
  3254. {
  3255. this->CompileOptionsEntries.clear();
  3256. if (!value)
  3257. {
  3258. return;
  3259. }
  3260. cmListFileBacktrace lfbt;
  3261. this->GetBacktrace(lfbt);
  3262. this->CompileOptionsEntries.push_back(cmValueWithOrigin(value, lfbt));
  3263. return;
  3264. }
  3265. if (propname == "COMPILE_DEFINITIONS")
  3266. {
  3267. this->CompileDefinitionsEntries.clear();
  3268. if (!value)
  3269. {
  3270. return;
  3271. }
  3272. cmListFileBacktrace lfbt;
  3273. this->GetBacktrace(lfbt);
  3274. cmValueWithOrigin entry(value, lfbt);
  3275. this->CompileDefinitionsEntries.push_back(entry);
  3276. return;
  3277. }
  3278. if ( propname == "INCLUDE_REGULAR_EXPRESSION" )
  3279. {
  3280. this->SetIncludeRegularExpression(value);
  3281. return;
  3282. }
  3283. if ( propname == "ADDITIONAL_MAKE_CLEAN_FILES" )
  3284. {
  3285. // This property is not inherrited
  3286. if ( strcmp(this->GetCurrentDirectory(),
  3287. this->GetStartDirectory()) != 0 )
  3288. {
  3289. return;
  3290. }
  3291. }
  3292. this->Properties.SetProperty(prop,value,cmProperty::DIRECTORY);
  3293. }
  3294. void cmMakefile::AppendProperty(const char* prop, const char* value,
  3295. bool asString)
  3296. {
  3297. if (!prop)
  3298. {
  3299. return;
  3300. }
  3301. // handle special props
  3302. std::string propname = prop;
  3303. if (propname == "INCLUDE_DIRECTORIES")
  3304. {
  3305. cmListFileBacktrace lfbt;
  3306. this->GetBacktrace(lfbt);
  3307. this->IncludeDirectoriesEntries.push_back(
  3308. cmValueWithOrigin(value, lfbt));
  3309. return;
  3310. }
  3311. if (propname == "COMPILE_OPTIONS")
  3312. {
  3313. cmListFileBacktrace lfbt;
  3314. this->GetBacktrace(lfbt);
  3315. this->CompileOptionsEntries.push_back(
  3316. cmValueWithOrigin(value, lfbt));
  3317. return;
  3318. }
  3319. if (propname == "COMPILE_DEFINITIONS")
  3320. {
  3321. cmListFileBacktrace lfbt;
  3322. this->GetBacktrace(lfbt);
  3323. this->CompileDefinitionsEntries.push_back(
  3324. cmValueWithOrigin(value, lfbt));
  3325. return;
  3326. }
  3327. if ( propname == "LINK_DIRECTORIES" )
  3328. {
  3329. std::vector<std::string> varArgsExpanded;
  3330. cmSystemTools::ExpandListArgument(value, varArgsExpanded);
  3331. for(std::vector<std::string>::const_iterator vi = varArgsExpanded.begin();
  3332. vi != varArgsExpanded.end(); ++vi)
  3333. {
  3334. this->AddLinkDirectory(vi->c_str());
  3335. }
  3336. return;
  3337. }
  3338. this->Properties.AppendProperty(prop,value,cmProperty::DIRECTORY,asString);
  3339. }
  3340. const char *cmMakefile::GetPropertyOrDefinition(const char* prop)
  3341. {
  3342. const char *ret = this->GetProperty(prop, cmProperty::DIRECTORY);
  3343. if (!ret)
  3344. {
  3345. ret = this->GetDefinition(prop);
  3346. }
  3347. return ret;
  3348. }
  3349. const char *cmMakefile::GetProperty(const char* prop)
  3350. {
  3351. return this->GetProperty(prop, cmProperty::DIRECTORY);
  3352. }
  3353. const char *cmMakefile::GetProperty(const char* prop,
  3354. cmProperty::ScopeType scope)
  3355. {
  3356. if(!prop)
  3357. {
  3358. return 0;
  3359. }
  3360. // watch for specific properties
  3361. static std::string output;
  3362. output = "";
  3363. if (!strcmp("PARENT_DIRECTORY",prop))
  3364. {
  3365. if(cmLocalGenerator* plg = this->LocalGenerator->GetParent())
  3366. {
  3367. output = plg->GetMakefile()->GetStartDirectory();
  3368. }
  3369. return output.c_str();
  3370. }
  3371. else if (!strcmp("INCLUDE_REGULAR_EXPRESSION",prop) )
  3372. {
  3373. output = this->GetIncludeRegularExpression();
  3374. return output.c_str();
  3375. }
  3376. else if (!strcmp("LISTFILE_STACK",prop))
  3377. {
  3378. for (std::deque<cmStdString>::iterator i = this->ListFileStack.begin();
  3379. i != this->ListFileStack.end(); ++i)
  3380. {
  3381. if (i != this->ListFileStack.begin())
  3382. {
  3383. output += ";";
  3384. }
  3385. output += *i;
  3386. }
  3387. return output.c_str();
  3388. }
  3389. else if (!strcmp("VARIABLES",prop) || !strcmp("CACHE_VARIABLES",prop))
  3390. {
  3391. int cacheonly = 0;
  3392. if ( !strcmp("CACHE_VARIABLES",prop) )
  3393. {
  3394. cacheonly = 1;
  3395. }
  3396. std::vector<std::string> vars = this->GetDefinitions(cacheonly);
  3397. for (unsigned int cc = 0; cc < vars.size(); cc ++ )
  3398. {
  3399. if ( cc > 0 )
  3400. {
  3401. output += ";";
  3402. }
  3403. output += vars[cc];
  3404. }
  3405. return output.c_str();
  3406. }
  3407. else if (!strcmp("MACROS",prop))
  3408. {
  3409. this->GetListOfMacros(output);
  3410. return output.c_str();
  3411. }
  3412. else if (!strcmp("DEFINITIONS",prop))
  3413. {
  3414. output += this->DefineFlagsOrig;
  3415. return output.c_str();
  3416. }
  3417. else if (!strcmp("LINK_DIRECTORIES",prop))
  3418. {
  3419. cmOStringStream str;
  3420. for (std::vector<std::string>::const_iterator
  3421. it = this->GetLinkDirectories().begin();
  3422. it != this->GetLinkDirectories().end();
  3423. ++ it )
  3424. {
  3425. if ( it != this->GetLinkDirectories().begin())
  3426. {
  3427. str << ";";
  3428. }
  3429. str << it->c_str();
  3430. }
  3431. output = str.str();
  3432. return output.c_str();
  3433. }
  3434. else if (!strcmp("INCLUDE_DIRECTORIES",prop))
  3435. {
  3436. std::string sep;
  3437. for (std::vector<cmValueWithOrigin>::const_iterator
  3438. it = this->IncludeDirectoriesEntries.begin(),
  3439. end = this->IncludeDirectoriesEntries.end();
  3440. it != end; ++it)
  3441. {
  3442. output += sep;
  3443. output += it->Value;
  3444. sep = ";";
  3445. }
  3446. return output.c_str();
  3447. }
  3448. else if (!strcmp("COMPILE_OPTIONS",prop))
  3449. {
  3450. std::string sep;
  3451. for (std::vector<cmValueWithOrigin>::const_iterator
  3452. it = this->CompileOptionsEntries.begin(),
  3453. end = this->CompileOptionsEntries.end();
  3454. it != end; ++it)
  3455. {
  3456. output += sep;
  3457. output += it->Value;
  3458. sep = ";";
  3459. }
  3460. return output.c_str();
  3461. }
  3462. else if (!strcmp("COMPILE_DEFINITIONS",prop))
  3463. {
  3464. std::string sep;
  3465. for (std::vector<cmValueWithOrigin>::const_iterator
  3466. it = this->CompileDefinitionsEntries.begin(),
  3467. end = this->CompileDefinitionsEntries.end();
  3468. it != end; ++it)
  3469. {
  3470. output += sep;
  3471. output += it->Value;
  3472. sep = ";";
  3473. }
  3474. return output.c_str();
  3475. }
  3476. bool chain = false;
  3477. const char *retVal =
  3478. this->Properties.GetPropertyValue(prop, scope, chain);
  3479. if (chain)
  3480. {
  3481. if(this->LocalGenerator->GetParent())
  3482. {
  3483. return this->LocalGenerator->GetParent()->GetMakefile()->
  3484. GetProperty(prop, scope);
  3485. }
  3486. return this->GetCMakeInstance()->GetProperty(prop,scope);
  3487. }
  3488. return retVal;
  3489. }
  3490. bool cmMakefile::GetPropertyAsBool(const char* prop)
  3491. {
  3492. return cmSystemTools::IsOn(this->GetProperty(prop));
  3493. }
  3494. //----------------------------------------------------------------------------
  3495. const char* cmMakefile::GetFeature(const char* feature, const char* config)
  3496. {
  3497. // TODO: Define accumulation policy for features (prepend, append, replace).
  3498. // Currently we always replace.
  3499. if(config && *config)
  3500. {
  3501. std::string featureConfig = feature;
  3502. featureConfig += "_";
  3503. featureConfig += cmSystemTools::UpperCase(config);
  3504. if(const char* value = this->GetProperty(featureConfig.c_str()))
  3505. {
  3506. return value;
  3507. }
  3508. }
  3509. if(const char* value = this->GetProperty(feature))
  3510. {
  3511. return value;
  3512. }
  3513. if(cmLocalGenerator* parent = this->LocalGenerator->GetParent())
  3514. {
  3515. return parent->GetMakefile()->GetFeature(feature, config);
  3516. }
  3517. return 0;
  3518. }
  3519. cmTarget* cmMakefile::FindTarget(const char* name, bool excludeAliases)
  3520. {
  3521. if (!excludeAliases)
  3522. {
  3523. std::map<std::string, cmTarget*>::iterator i
  3524. = this->AliasTargets.find(name);
  3525. if (i != this->AliasTargets.end())
  3526. {
  3527. return i->second;
  3528. }
  3529. }
  3530. cmTargets& tgts = this->GetTargets();
  3531. cmTargets::iterator i = tgts.find ( name );
  3532. if ( i != tgts.end() )
  3533. {
  3534. return &i->second;
  3535. }
  3536. return 0;
  3537. }
  3538. //----------------------------------------------------------------------------
  3539. cmTest* cmMakefile::CreateTest(const char* testName)
  3540. {
  3541. if ( !testName )
  3542. {
  3543. return 0;
  3544. }
  3545. cmTest* test = this->GetTest(testName);
  3546. if ( test )
  3547. {
  3548. return test;
  3549. }
  3550. test = new cmTest(this);
  3551. test->SetName(testName);
  3552. this->Tests[testName] = test;
  3553. return test;
  3554. }
  3555. //----------------------------------------------------------------------------
  3556. cmTest* cmMakefile::GetTest(const char* testName) const
  3557. {
  3558. if(testName)
  3559. {
  3560. std::map<cmStdString, cmTest*>::const_iterator
  3561. mi = this->Tests.find(testName);
  3562. if(mi != this->Tests.end())
  3563. {
  3564. return mi->second;
  3565. }
  3566. }
  3567. return 0;
  3568. }
  3569. std::string cmMakefile::GetListFileStack()
  3570. {
  3571. cmOStringStream tmp;
  3572. size_t depth = this->ListFileStack.size();
  3573. if (depth > 0)
  3574. {
  3575. std::deque<cmStdString>::iterator it = this->ListFileStack.end();
  3576. do
  3577. {
  3578. if (depth != this->ListFileStack.size())
  3579. {
  3580. tmp << "\n ";
  3581. }
  3582. --it;
  3583. tmp << "[";
  3584. tmp << depth;
  3585. tmp << "]\t";
  3586. tmp << *it;
  3587. depth--;
  3588. }
  3589. while (it != this->ListFileStack.begin());
  3590. }
  3591. return tmp.str();
  3592. }
  3593. void cmMakefile::PushScope()
  3594. {
  3595. cmDefinitions* parent = &this->Internal->VarStack.top();
  3596. const std::set<cmStdString>& init = this->Internal->VarInitStack.top();
  3597. const std::set<cmStdString>& usage = this->Internal->VarUsageStack.top();
  3598. this->Internal->VarStack.push(cmDefinitions(parent));
  3599. this->Internal->VarInitStack.push(init);
  3600. this->Internal->VarUsageStack.push(usage);
  3601. }
  3602. void cmMakefile::PopScope()
  3603. {
  3604. cmDefinitions* current = &this->Internal->VarStack.top();
  3605. std::set<cmStdString> init = this->Internal->VarInitStack.top();
  3606. std::set<cmStdString> usage = this->Internal->VarUsageStack.top();
  3607. const std::set<cmStdString>& locals = current->LocalKeys();
  3608. // Remove initialization and usage information for variables in the local
  3609. // scope.
  3610. std::set<cmStdString>::const_iterator it = locals.begin();
  3611. for (; it != locals.end(); ++it)
  3612. {
  3613. init.erase(*it);
  3614. if (!this->VariableUsed(it->c_str()))
  3615. {
  3616. this->CheckForUnused("out of scope", it->c_str());
  3617. }
  3618. else
  3619. {
  3620. usage.erase(*it);
  3621. }
  3622. }
  3623. this->Internal->VarStack.pop();
  3624. this->Internal->VarInitStack.pop();
  3625. this->Internal->VarUsageStack.pop();
  3626. // Push initialization and usage up to the parent scope.
  3627. it = init.begin();
  3628. for (; it != init.end(); ++it)
  3629. {
  3630. this->Internal->VarInitStack.top().insert(*it);
  3631. }
  3632. it = usage.begin();
  3633. for (; it != usage.end(); ++it)
  3634. {
  3635. this->Internal->VarUsageStack.top().insert(*it);
  3636. }
  3637. }
  3638. void cmMakefile::RaiseScope(const char *var, const char *varDef)
  3639. {
  3640. if (!var || !strlen(var))
  3641. {
  3642. return;
  3643. }
  3644. cmDefinitions& cur = this->Internal->VarStack.top();
  3645. if(cmDefinitions* up = cur.GetParent())
  3646. {
  3647. // First localize the definition in the current scope.
  3648. cur.Get(var);
  3649. // Now update the definition in the parent scope.
  3650. up->Set(var, varDef);
  3651. }
  3652. else if(cmLocalGenerator* plg = this->LocalGenerator->GetParent())
  3653. {
  3654. // Update the definition in the parent directory top scope. This
  3655. // directory's scope was initialized by the closure of the parent
  3656. // scope, so we do not need to localize the definition first.
  3657. cmMakefile* parent = plg->GetMakefile();
  3658. if (varDef)
  3659. {
  3660. parent->AddDefinition(var, varDef);
  3661. }
  3662. else
  3663. {
  3664. parent->RemoveDefinition(var);
  3665. }
  3666. }
  3667. else
  3668. {
  3669. cmOStringStream m;
  3670. m << "Cannot set \"" << var << "\": current scope has no parent.";
  3671. this->IssueMessage(cmake::AUTHOR_WARNING, m.str());
  3672. }
  3673. }
  3674. // define properties
  3675. void cmMakefile::DefineProperties(cmake *cm)
  3676. {
  3677. cm->DefineProperty
  3678. ("RULE_LAUNCH_COMPILE", cmProperty::DIRECTORY,
  3679. "", "", true);
  3680. cm->DefineProperty
  3681. ("RULE_LAUNCH_LINK", cmProperty::DIRECTORY,
  3682. "", "", true);
  3683. cm->DefineProperty
  3684. ("RULE_LAUNCH_CUSTOM", cmProperty::DIRECTORY,
  3685. "", "", true);
  3686. }
  3687. //----------------------------------------------------------------------------
  3688. cmTarget*
  3689. cmMakefile::AddImportedTarget(const char* name, cmTarget::TargetType type,
  3690. bool global)
  3691. {
  3692. // Create the target.
  3693. cmsys::auto_ptr<cmTarget> target(new cmTarget);
  3694. target->SetType(type, name);
  3695. target->SetMakefile(this);
  3696. target->MarkAsImported();
  3697. // Add to the set of available imported targets.
  3698. this->ImportedTargets[name] = target.get();
  3699. if(global)
  3700. {
  3701. this->LocalGenerator->GetGlobalGenerator()->AddTarget(target.get());
  3702. }
  3703. // Transfer ownership to this cmMakefile object.
  3704. this->ImportedTargetsOwned.push_back(target.get());
  3705. return target.release();
  3706. }
  3707. //----------------------------------------------------------------------------
  3708. cmTarget* cmMakefile::FindTargetToUse(const char* name, bool excludeAliases)
  3709. {
  3710. // Look for an imported target. These take priority because they
  3711. // are more local in scope and do not have to be globally unique.
  3712. std::map<cmStdString, cmTarget*>::const_iterator
  3713. imported = this->ImportedTargets.find(name);
  3714. if(imported != this->ImportedTargets.end())
  3715. {
  3716. return imported->second;
  3717. }
  3718. // Look for a target built in this directory.
  3719. if(cmTarget* t = this->FindTarget(name, excludeAliases))
  3720. {
  3721. return t;
  3722. }
  3723. // Look for a target built in this project.
  3724. return this->LocalGenerator->GetGlobalGenerator()->FindTarget(0, name,
  3725. excludeAliases);
  3726. }
  3727. //----------------------------------------------------------------------------
  3728. bool cmMakefile::IsAlias(const char *name)
  3729. {
  3730. if (this->AliasTargets.find(name) != this->AliasTargets.end())
  3731. return true;
  3732. return this->GetLocalGenerator()->GetGlobalGenerator()->IsAlias(name);
  3733. }
  3734. //----------------------------------------------------------------------------
  3735. cmGeneratorTarget* cmMakefile::FindGeneratorTargetToUse(const char* name)
  3736. {
  3737. cmTarget *t = this->FindTargetToUse(name);
  3738. return this->LocalGenerator->GetGlobalGenerator()->GetGeneratorTarget(t);
  3739. }
  3740. //----------------------------------------------------------------------------
  3741. bool cmMakefile::EnforceUniqueName(std::string const& name, std::string& msg,
  3742. bool isCustom)
  3743. {
  3744. if(this->IsAlias(name.c_str()))
  3745. {
  3746. cmOStringStream e;
  3747. e << "cannot create target \"" << name
  3748. << "\" because an alias with the same name already exists.";
  3749. msg = e.str();
  3750. return false;
  3751. }
  3752. if(cmTarget* existing = this->FindTargetToUse(name.c_str()))
  3753. {
  3754. // The name given conflicts with an existing target. Produce an
  3755. // error in a compatible way.
  3756. if(existing->IsImported())
  3757. {
  3758. // Imported targets were not supported in previous versions.
  3759. // This is new code, so we can make it an error.
  3760. cmOStringStream e;
  3761. e << "cannot create target \"" << name
  3762. << "\" because an imported target with the same name already exists.";
  3763. msg = e.str();
  3764. return false;
  3765. }
  3766. else
  3767. {
  3768. // target names must be globally unique
  3769. switch (this->GetPolicyStatus(cmPolicies::CMP0002))
  3770. {
  3771. case cmPolicies::WARN:
  3772. this->IssueMessage(cmake::AUTHOR_WARNING, this->GetPolicies()->
  3773. GetPolicyWarning(cmPolicies::CMP0002));
  3774. case cmPolicies::OLD:
  3775. return true;
  3776. case cmPolicies::REQUIRED_IF_USED:
  3777. case cmPolicies::REQUIRED_ALWAYS:
  3778. this->IssueMessage(cmake::FATAL_ERROR,
  3779. this->GetPolicies()->GetRequiredPolicyError(cmPolicies::CMP0002)
  3780. );
  3781. return true;
  3782. case cmPolicies::NEW:
  3783. break;
  3784. }
  3785. // The conflict is with a non-imported target.
  3786. // Allow this if the user has requested support.
  3787. cmake* cm =
  3788. this->LocalGenerator->GetGlobalGenerator()->GetCMakeInstance();
  3789. if(isCustom && existing->GetType() == cmTarget::UTILITY &&
  3790. this != existing->GetMakefile() &&
  3791. cm->GetPropertyAsBool("ALLOW_DUPLICATE_CUSTOM_TARGETS"))
  3792. {
  3793. return true;
  3794. }
  3795. // Produce an error that tells the user how to work around the
  3796. // problem.
  3797. cmOStringStream e;
  3798. e << "cannot create target \"" << name
  3799. << "\" because another target with the same name already exists. "
  3800. << "The existing target is ";
  3801. switch(existing->GetType())
  3802. {
  3803. case cmTarget::EXECUTABLE:
  3804. e << "an executable ";
  3805. break;
  3806. case cmTarget::STATIC_LIBRARY:
  3807. e << "a static library ";
  3808. break;
  3809. case cmTarget::SHARED_LIBRARY:
  3810. e << "a shared library ";
  3811. break;
  3812. case cmTarget::MODULE_LIBRARY:
  3813. e << "a module library ";
  3814. break;
  3815. case cmTarget::UTILITY:
  3816. e << "a custom target ";
  3817. break;
  3818. default: break;
  3819. }
  3820. e << "created in source directory \""
  3821. << existing->GetMakefile()->GetCurrentDirectory() << "\". "
  3822. << "See documentation for policy CMP0002 for more details.";
  3823. msg = e.str();
  3824. return false;
  3825. }
  3826. }
  3827. return true;
  3828. }
  3829. //----------------------------------------------------------------------------
  3830. bool cmMakefile::EnforceUniqueDir(const char* srcPath, const char* binPath)
  3831. {
  3832. // Make sure the binary directory is unique.
  3833. cmGlobalGenerator* gg = this->LocalGenerator->GetGlobalGenerator();
  3834. if(gg->BinaryDirectoryIsNew(binPath))
  3835. {
  3836. return true;
  3837. }
  3838. cmOStringStream e;
  3839. switch (this->GetPolicyStatus(cmPolicies::CMP0013))
  3840. {
  3841. case cmPolicies::WARN:
  3842. // Print the warning.
  3843. e << this->GetPolicies()->GetPolicyWarning(cmPolicies::CMP0013)
  3844. << "\n"
  3845. << "The binary directory\n"
  3846. << " " << binPath << "\n"
  3847. << "is already used to build a source directory. "
  3848. << "This command uses it to build source directory\n"
  3849. << " " << srcPath << "\n"
  3850. << "which can generate conflicting build files. "
  3851. << "CMake does not support this use case but it used "
  3852. << "to work accidentally and is being allowed for "
  3853. << "compatibility.";
  3854. this->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  3855. case cmPolicies::OLD:
  3856. // OLD behavior does not warn.
  3857. return true;
  3858. case cmPolicies::REQUIRED_IF_USED:
  3859. case cmPolicies::REQUIRED_ALWAYS:
  3860. e << this->GetPolicies()->GetRequiredPolicyError(cmPolicies::CMP0013)
  3861. << "\n";
  3862. case cmPolicies::NEW:
  3863. // NEW behavior prints the error.
  3864. e << "The binary directory\n"
  3865. << " " << binPath << "\n"
  3866. << "is already used to build a source directory. "
  3867. << "It cannot be used to build source directory\n"
  3868. << " " << srcPath << "\n"
  3869. << "Specify a unique binary directory name.";
  3870. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  3871. break;
  3872. }
  3873. return false;
  3874. }
  3875. //----------------------------------------------------------------------------
  3876. cmPolicies::PolicyStatus
  3877. cmMakefile::GetPolicyStatus(cmPolicies::PolicyID id)
  3878. {
  3879. // Get the current setting of the policy.
  3880. cmPolicies::PolicyStatus cur = this->GetPolicyStatusInternal(id);
  3881. // If the policy is required to be set to NEW but is not, ignore the
  3882. // current setting and tell the caller.
  3883. if(cur != cmPolicies::NEW)
  3884. {
  3885. if(cur == cmPolicies::REQUIRED_ALWAYS ||
  3886. cur == cmPolicies::REQUIRED_IF_USED)
  3887. {
  3888. return cur;
  3889. }
  3890. cmPolicies::PolicyStatus def = this->GetPolicies()->GetPolicyStatus(id);
  3891. if(def == cmPolicies::REQUIRED_ALWAYS ||
  3892. def == cmPolicies::REQUIRED_IF_USED)
  3893. {
  3894. return def;
  3895. }
  3896. }
  3897. // The current setting is okay.
  3898. return cur;
  3899. }
  3900. //----------------------------------------------------------------------------
  3901. cmPolicies::PolicyStatus
  3902. cmMakefile::GetPolicyStatusInternal(cmPolicies::PolicyID id)
  3903. {
  3904. // Is the policy set in our stack?
  3905. for(PolicyStackType::reverse_iterator psi = this->PolicyStack.rbegin();
  3906. psi != this->PolicyStack.rend(); ++psi)
  3907. {
  3908. PolicyStackEntry::const_iterator pse = psi->find(id);
  3909. if(pse != psi->end())
  3910. {
  3911. return pse->second;
  3912. }
  3913. }
  3914. // If we have a parent directory, recurse up to it.
  3915. if(this->LocalGenerator->GetParent())
  3916. {
  3917. cmMakefile* parent = this->LocalGenerator->GetParent()->GetMakefile();
  3918. return parent->GetPolicyStatusInternal(id);
  3919. }
  3920. // The policy is not set. Use the default for this CMake version.
  3921. return this->GetPolicies()->GetPolicyStatus(id);
  3922. }
  3923. bool cmMakefile::SetPolicy(const char *id,
  3924. cmPolicies::PolicyStatus status)
  3925. {
  3926. cmPolicies::PolicyID pid;
  3927. if (!this->GetPolicies()->GetPolicyID(id, /* out */ pid))
  3928. {
  3929. cmOStringStream e;
  3930. e << "Policy \"" << id << "\" is not known to this version of CMake.";
  3931. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  3932. return false;
  3933. }
  3934. return this->SetPolicy(pid,status);
  3935. }
  3936. //----------------------------------------------------------------------------
  3937. bool cmMakefile::SetPolicy(cmPolicies::PolicyID id,
  3938. cmPolicies::PolicyStatus status)
  3939. {
  3940. // A REQUIRED_ALWAYS policy may be set only to NEW.
  3941. if(status != cmPolicies::NEW &&
  3942. this->GetPolicies()->GetPolicyStatus(id) ==
  3943. cmPolicies::REQUIRED_ALWAYS)
  3944. {
  3945. std::string msg =
  3946. this->GetPolicies()->GetRequiredAlwaysPolicyError(id);
  3947. this->IssueMessage(cmake::FATAL_ERROR, msg.c_str());
  3948. return false;
  3949. }
  3950. // Update the policy stack from the top to the top-most strong entry.
  3951. bool previous_was_weak = true;
  3952. for(PolicyStackType::reverse_iterator psi = this->PolicyStack.rbegin();
  3953. previous_was_weak && psi != this->PolicyStack.rend(); ++psi)
  3954. {
  3955. (*psi)[id] = status;
  3956. previous_was_weak = psi->Weak;
  3957. }
  3958. // Special hook for presenting compatibility variable as soon as
  3959. // the user requests it.
  3960. if(id == cmPolicies::CMP0001 &&
  3961. (status == cmPolicies::WARN || status == cmPolicies::OLD))
  3962. {
  3963. if(!(this->GetCacheManager()
  3964. ->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY")))
  3965. {
  3966. // Set it to 2.4 because that is the last version where the
  3967. // variable had meaning.
  3968. this->AddCacheDefinition
  3969. ("CMAKE_BACKWARDS_COMPATIBILITY", "2.4",
  3970. "For backwards compatibility, what version of CMake "
  3971. "commands and "
  3972. "syntax should this version of CMake try to support.",
  3973. cmCacheManager::STRING);
  3974. }
  3975. }
  3976. return true;
  3977. }
  3978. //----------------------------------------------------------------------------
  3979. cmMakefile::PolicyPushPop::PolicyPushPop(cmMakefile* m, bool weak,
  3980. cmPolicies::PolicyMap const& pm):
  3981. Makefile(m), ReportError(true)
  3982. {
  3983. this->Makefile->PushPolicy(weak, pm);
  3984. this->Makefile->PushPolicyBarrier();
  3985. }
  3986. //----------------------------------------------------------------------------
  3987. cmMakefile::PolicyPushPop::~PolicyPushPop()
  3988. {
  3989. this->Makefile->PopPolicyBarrier(this->ReportError);
  3990. this->Makefile->PopPolicy();
  3991. }
  3992. //----------------------------------------------------------------------------
  3993. void cmMakefile::PushPolicy(bool weak, cmPolicies::PolicyMap const& pm)
  3994. {
  3995. // Allocate a new stack entry.
  3996. this->PolicyStack.push_back(PolicyStackEntry(pm, weak));
  3997. }
  3998. //----------------------------------------------------------------------------
  3999. void cmMakefile::PopPolicy()
  4000. {
  4001. if(this->PolicyStack.size() > this->PolicyBarriers.back())
  4002. {
  4003. this->PolicyStack.pop_back();
  4004. }
  4005. else
  4006. {
  4007. this->IssueMessage(cmake::FATAL_ERROR,
  4008. "cmake_policy POP without matching PUSH");
  4009. }
  4010. }
  4011. //----------------------------------------------------------------------------
  4012. void cmMakefile::PushPolicyBarrier()
  4013. {
  4014. this->PolicyBarriers.push_back(this->PolicyStack.size());
  4015. }
  4016. //----------------------------------------------------------------------------
  4017. void cmMakefile::PopPolicyBarrier(bool reportError)
  4018. {
  4019. // Remove any extra entries pushed on the barrier.
  4020. PolicyStackType::size_type barrier = this->PolicyBarriers.back();
  4021. while(this->PolicyStack.size() > barrier)
  4022. {
  4023. if(reportError)
  4024. {
  4025. this->IssueMessage(cmake::FATAL_ERROR,
  4026. "cmake_policy PUSH without matching POP");
  4027. reportError = false;
  4028. }
  4029. this->PopPolicy();
  4030. }
  4031. // Remove the barrier.
  4032. this->PolicyBarriers.pop_back();
  4033. }
  4034. bool cmMakefile::SetPolicyVersion(const char *version)
  4035. {
  4036. return this->GetCMakeInstance()->GetPolicies()->
  4037. ApplyPolicyVersion(this,version);
  4038. }
  4039. cmPolicies *cmMakefile::GetPolicies()
  4040. {
  4041. if (!this->GetCMakeInstance())
  4042. {
  4043. return 0;
  4044. }
  4045. return this->GetCMakeInstance()->GetPolicies();
  4046. }
  4047. //----------------------------------------------------------------------------
  4048. void cmMakefile::RecordPolicies(cmPolicies::PolicyMap& pm)
  4049. {
  4050. /* Record the setting of every policy. */
  4051. typedef cmPolicies::PolicyID PolicyID;
  4052. for(PolicyID pid = cmPolicies::CMP0000;
  4053. pid != cmPolicies::CMPCOUNT; pid = PolicyID(pid+1))
  4054. {
  4055. pm[pid] = this->GetPolicyStatus(pid);
  4056. }
  4057. }