cmMakefile.cxx 108 KB

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