cmMakefile.cxx 107 KB

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