cmMakefile.cxx 98 KB

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