cmMakefile.cxx 119 KB

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