cmMakefile.cxx 120 KB

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