cmMakefile.cxx 96 KB

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