cmMakefile.cxx 99 KB

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