cmMakefile.cxx 116 KB

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