cmMakefile.cxx 116 KB

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