cmMakefile.cxx 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmMakefile.h"
  14. #include "cmVersion.h"
  15. #include "cmCommand.h"
  16. #include "cmSourceFile.h"
  17. #include "cmSystemTools.h"
  18. #include "cmGlobalGenerator.h"
  19. #include "cmLocalGenerator.h"
  20. #include "cmCommands.h"
  21. #include "cmCacheManager.h"
  22. #include "cmFunctionBlocker.h"
  23. #include "cmListFileCache.h"
  24. #include "cmCommandArgumentParserHelper.h"
  25. #include "cmTest.h"
  26. #ifdef CMAKE_BUILD_WITH_CMAKE
  27. # include "cmVariableWatch.h"
  28. #endif
  29. #include "cmInstallGenerator.h"
  30. #include "cmake.h"
  31. #include <stdlib.h> // required for atoi
  32. #include <cmsys/RegularExpression.hxx>
  33. #include <ctype.h> // for isspace
  34. // default is not to be building executables
  35. cmMakefile::cmMakefile()
  36. {
  37. // Setup the default include file regular expression (match everything).
  38. this->IncludeFileRegularExpression = "^.*$";
  39. // Setup the default include complaint regular expression (match nothing).
  40. this->ComplainFileRegularExpression = "^$";
  41. // Source and header file extensions that we can handle
  42. // Set up a list of source and header extensions
  43. // these are used to find files when the extension
  44. // is not given
  45. // The "c" extension MUST precede the "C" extension.
  46. this->SourceFileExtensions.push_back( "c" );
  47. this->SourceFileExtensions.push_back( "C" );
  48. this->SourceFileExtensions.push_back( "c++" );
  49. this->SourceFileExtensions.push_back( "cc" );
  50. this->SourceFileExtensions.push_back( "cpp" );
  51. this->SourceFileExtensions.push_back( "cxx" );
  52. this->SourceFileExtensions.push_back( "m" );
  53. this->SourceFileExtensions.push_back( "M" );
  54. this->SourceFileExtensions.push_back( "mm" );
  55. this->HeaderFileExtensions.push_back( "h" );
  56. this->HeaderFileExtensions.push_back( "hh" );
  57. this->HeaderFileExtensions.push_back( "h++" );
  58. this->HeaderFileExtensions.push_back( "hm" );
  59. this->HeaderFileExtensions.push_back( "hpp" );
  60. this->HeaderFileExtensions.push_back( "hxx" );
  61. this->HeaderFileExtensions.push_back( "in" );
  62. this->HeaderFileExtensions.push_back( "txx" );
  63. this->DefineFlags = " ";
  64. this->LocalGenerator = 0;
  65. #if defined(CMAKE_BUILD_WITH_CMAKE)
  66. this->AddSourceGroup("", "^.*$");
  67. this->AddSourceGroup
  68. ("Source Files",
  69. "\\.(C|M|c|c\\+\\+|cc|cpp|cxx|m|mm|rc|def|r|odl|idl|hpj|bat)$");
  70. this->AddSourceGroup("Header Files",
  71. "\\.(h|hh|h\\+\\+|hm|hpp|hxx|in|txx|inl)$");
  72. this->AddSourceGroup("CMake Rules", "\\.rule$");
  73. this->AddSourceGroup("Resources", "\\.plist$");
  74. #endif
  75. this->AddDefaultDefinitions();
  76. this->cmDefineRegex.compile("#cmakedefine[ \t]+([A-Za-z_0-9]*)");
  77. this->cmDefine01Regex.compile("#cmakedefine01[ \t]+([A-Za-z_0-9]*)");
  78. this->PreOrder = false;
  79. }
  80. cmMakefile::cmMakefile(const cmMakefile& mf)
  81. {
  82. this->Prefix = mf.Prefix;
  83. this->AuxSourceDirectories = mf.AuxSourceDirectories;
  84. this->cmStartDirectory = mf.cmStartDirectory;
  85. this->StartOutputDirectory = mf.StartOutputDirectory;
  86. this->cmHomeDirectory = mf.cmHomeDirectory;
  87. this->HomeOutputDirectory = mf.HomeOutputDirectory;
  88. this->cmCurrentListFile = mf.cmCurrentListFile;
  89. this->ProjectName = mf.ProjectName;
  90. this->Targets = mf.Targets;
  91. this->SourceFiles = mf.SourceFiles;
  92. this->Tests = mf.Tests;
  93. this->IncludeDirectories = mf.IncludeDirectories;
  94. this->LinkDirectories = mf.LinkDirectories;
  95. this->SystemIncludeDirectories = mf.SystemIncludeDirectories;
  96. this->ListFiles = mf.ListFiles;
  97. this->OutputFiles = mf.OutputFiles;
  98. this->LinkLibraries = mf.LinkLibraries;
  99. this->InstallGenerators = mf.InstallGenerators;
  100. this->IncludeFileRegularExpression = mf.IncludeFileRegularExpression;
  101. this->ComplainFileRegularExpression = mf.ComplainFileRegularExpression;
  102. this->SourceFileExtensions = mf.SourceFileExtensions;
  103. this->HeaderFileExtensions = mf.HeaderFileExtensions;
  104. this->DefineFlags = mf.DefineFlags;
  105. #if defined(CMAKE_BUILD_WITH_CMAKE)
  106. this->SourceGroups = mf.SourceGroups;
  107. #endif
  108. this->Definitions = mf.Definitions;
  109. this->LocalGenerator = mf.LocalGenerator;
  110. this->FunctionBlockers = mf.FunctionBlockers;
  111. this->DataMap = mf.DataMap;
  112. this->MacrosMap = mf.MacrosMap;
  113. this->SubDirectoryOrder = mf.SubDirectoryOrder;
  114. this->TemporaryDefinitionKey = mf.TemporaryDefinitionKey;
  115. this->Properties = mf.Properties;
  116. this->PreOrder = mf.PreOrder;
  117. this->ListFileStack = mf.ListFileStack;
  118. }
  119. unsigned int cmMakefile::GetCacheMajorVersion()
  120. {
  121. if(const char* vstr =
  122. this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"))
  123. {
  124. unsigned int v=0;
  125. if(sscanf(vstr, "%u", &v) == 1)
  126. {
  127. return v;
  128. }
  129. }
  130. return 0;
  131. }
  132. unsigned int cmMakefile::GetCacheMinorVersion()
  133. {
  134. if(const char* vstr =
  135. this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MINOR_VERSION"))
  136. {
  137. unsigned int v=0;
  138. if(sscanf(vstr, "%u", &v) == 1)
  139. {
  140. return v;
  141. }
  142. }
  143. return 0;
  144. }
  145. cmMakefile::~cmMakefile()
  146. {
  147. for(std::vector<cmInstallGenerator*>::iterator
  148. i = this->InstallGenerators.begin();
  149. i != this->InstallGenerators.end(); ++i)
  150. {
  151. delete *i;
  152. }
  153. for(std::vector<cmSourceFile*>::iterator i = this->SourceFiles.begin();
  154. i != this->SourceFiles.end(); ++i)
  155. {
  156. delete *i;
  157. }
  158. for(std::vector<cmTest*>::iterator i = this->Tests.begin();
  159. i != this->Tests.end(); ++i)
  160. {
  161. delete *i;
  162. }
  163. for(unsigned int i=0; i < this->UsedCommands.size(); i++)
  164. {
  165. delete this->UsedCommands[i];
  166. }
  167. for(DataMapType::const_iterator d = this->DataMap.begin();
  168. d != this->DataMap.end(); ++d)
  169. {
  170. if(d->second)
  171. {
  172. delete d->second;
  173. }
  174. }
  175. std::list<cmFunctionBlocker *>::iterator pos;
  176. for (pos = this->FunctionBlockers.begin();
  177. pos != this->FunctionBlockers.end(); ++pos)
  178. {
  179. cmFunctionBlocker* b = *pos;
  180. delete b;
  181. }
  182. this->FunctionBlockers.clear();
  183. }
  184. void cmMakefile::PrintStringVector(const char* s,
  185. const std::vector<std::string>& v) const
  186. {
  187. std::cout << s << ": ( \n";
  188. for(std::vector<std::string>::const_iterator i = v.begin();
  189. i != v.end(); ++i)
  190. {
  191. std::cout << (*i).c_str() << " ";
  192. }
  193. std::cout << " )\n";
  194. }
  195. void cmMakefile
  196. ::PrintStringVector(const char* s,
  197. const std::vector<std::pair<cmStdString, bool> >& v) const
  198. {
  199. std::cout << s << ": ( \n";
  200. for(std::vector<std::pair<cmStdString, bool> >::const_iterator i
  201. = v.begin(); i != v.end(); ++i)
  202. {
  203. std::cout << i->first.c_str() << " " << i->second;
  204. }
  205. std::cout << " )\n";
  206. }
  207. // call print on all the classes in the makefile
  208. void cmMakefile::Print()
  209. {
  210. // print the class lists
  211. std::cout << "classes:\n";
  212. std::cout << " this->Targets: ";
  213. for (cmTargets::iterator l = this->Targets.begin();
  214. l != this->Targets.end(); l++)
  215. {
  216. std::cout << l->first << std::endl;
  217. }
  218. std::cout << " this->StartOutputDirectory; " <<
  219. this->StartOutputDirectory.c_str() << std::endl;
  220. std::cout << " this->HomeOutputDirectory; " <<
  221. this->HomeOutputDirectory.c_str() << std::endl;
  222. std::cout << " this->cmStartDirectory; " <<
  223. this->cmStartDirectory.c_str() << std::endl;
  224. std::cout << " this->cmHomeDirectory; " <<
  225. this->cmHomeDirectory.c_str() << std::endl;
  226. std::cout << " this->ProjectName; "
  227. << this->ProjectName.c_str() << std::endl;
  228. this->PrintStringVector("this->IncludeDirectories;",
  229. this->IncludeDirectories);
  230. this->PrintStringVector("this->LinkDirectories", this->LinkDirectories);
  231. #if defined(CMAKE_BUILD_WITH_CMAKE)
  232. for( std::vector<cmSourceGroup>::const_iterator i =
  233. this->SourceGroups.begin(); i != this->SourceGroups.end(); ++i)
  234. {
  235. std::cout << "Source Group: " << i->GetName() << std::endl;
  236. }
  237. #endif
  238. }
  239. bool cmMakefile::CommandExists(const char* name) const
  240. {
  241. return this->GetCMakeInstance()->CommandExists(name);
  242. }
  243. bool cmMakefile::ExecuteCommand(const cmListFileFunction& lff)
  244. {
  245. bool result = true;
  246. // quick return if blocked
  247. if(this->IsFunctionBlocked(lff))
  248. {
  249. // No error.
  250. return result;
  251. }
  252. std::string name = lff.Name;
  253. // execute the command
  254. cmCommand *rm =
  255. this->GetCMakeInstance()->GetCommand(name.c_str());
  256. if(rm)
  257. {
  258. const char* versionValue
  259. = this->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  260. int major = 0;
  261. int minor = 0;
  262. if ( versionValue )
  263. {
  264. sscanf(versionValue, "%d.%d", &major, &minor);
  265. }
  266. if ( rm->IsDeprecated(major, minor) )
  267. {
  268. cmOStringStream error;
  269. error << "Error in cmake code at\n"
  270. << lff.FilePath << ":" << lff.Line << ":\n"
  271. << rm->GetError() << std::endl
  272. << "Current CMake stack: " << this->GetListFileStack().c_str();
  273. cmSystemTools::Error(error.str().c_str());
  274. return false;
  275. }
  276. cmCommand* usedCommand = rm->Clone();
  277. usedCommand->SetMakefile(this);
  278. bool keepCommand = false;
  279. if(usedCommand->GetEnabled() && !cmSystemTools::GetFatalErrorOccured() &&
  280. (!this->GetCMakeInstance()->GetScriptMode() ||
  281. usedCommand->IsScriptable()))
  282. {
  283. if(!usedCommand->InvokeInitialPass(lff.Arguments))
  284. {
  285. cmOStringStream error;
  286. error << "Error in cmake code at\n"
  287. << lff.FilePath << ":" << lff.Line << ":\n"
  288. << usedCommand->GetError() << std::endl
  289. << "Current CMake stack: " << this->GetListFileStack().c_str();
  290. cmSystemTools::Error(error.str().c_str());
  291. result = false;
  292. if ( this->GetCMakeInstance()->GetScriptMode() )
  293. {
  294. cmSystemTools::SetFatalErrorOccured();
  295. }
  296. }
  297. else
  298. {
  299. // use the command
  300. keepCommand = true;
  301. this->UsedCommands.push_back(usedCommand);
  302. }
  303. }
  304. else if ( this->GetCMakeInstance()->GetScriptMode()
  305. && !usedCommand->IsScriptable() )
  306. {
  307. cmOStringStream error;
  308. error << "Error in cmake code at\n"
  309. << lff.FilePath << ":" << lff.Line << ":\n"
  310. << "Command " << usedCommand->GetName()
  311. << " not scriptable" << std::endl;
  312. cmSystemTools::Error(error.str().c_str());
  313. result = false;
  314. cmSystemTools::SetFatalErrorOccured();
  315. }
  316. // if the Cloned command was not used
  317. // then delete it
  318. if(!keepCommand)
  319. {
  320. delete usedCommand;
  321. }
  322. }
  323. else
  324. {
  325. if(!cmSystemTools::GetFatalErrorOccured())
  326. {
  327. cmOStringStream error;
  328. error << "Error in cmake code at\n"
  329. << lff.FilePath << ":" << lff.Line << ":\n"
  330. << "Unknown CMake command \"" << lff.Name.c_str() << "\".";
  331. cmSystemTools::Error(error.str().c_str());
  332. result = false;
  333. cmSystemTools::SetFatalErrorOccured();
  334. }
  335. }
  336. return result;
  337. }
  338. // Parse the given CMakeLists.txt file executing all commands
  339. //
  340. bool cmMakefile::ReadListFile(const char* filename_in,
  341. const char *external_in)
  342. {
  343. std::string currentFile = this->GetSafeDefinition("CMAKE_PARENT_LIST_FILE");
  344. this->AddDefinition("CMAKE_PARENT_LIST_FILE", filename_in);
  345. // used to watch for blockers going out of scope
  346. // e.g. mismatched IF statement
  347. std::set<cmFunctionBlocker *> originalBlockers;
  348. const char* external = 0;
  349. std::string external_abs;
  350. const char* filename = filename_in;
  351. std::string filename_abs;
  352. if (external_in)
  353. {
  354. external_abs =
  355. cmSystemTools::CollapseFullPath(external_in,
  356. this->cmStartDirectory.c_str());
  357. external = external_abs.c_str();
  358. if (filename_in)
  359. {
  360. filename_abs =
  361. cmSystemTools::CollapseFullPath(filename_in,
  362. this->cmStartDirectory.c_str());
  363. filename = filename_abs.c_str();
  364. }
  365. }
  366. // keep track of the current file being read
  367. if (filename)
  368. {
  369. if(this->cmCurrentListFile != filename)
  370. {
  371. this->cmCurrentListFile = filename;
  372. }
  373. // loop over current function blockers and record them
  374. std::list<cmFunctionBlocker *>::iterator pos;
  375. for (pos = this->FunctionBlockers.begin();
  376. pos != this->FunctionBlockers.end(); ++pos)
  377. {
  378. originalBlockers.insert(*pos);
  379. }
  380. }
  381. // Now read the input file
  382. const char *filenametoread= filename;
  383. if( external)
  384. {
  385. filenametoread= external;
  386. }
  387. // try to see if the list file is the top most
  388. // list file for a project, and if it is, then it
  389. // must have a project command. If there is not
  390. // one, then cmake will provide one via the
  391. // cmListFileCache class.
  392. bool requireProjectCommand = false;
  393. if(!external && this->cmStartDirectory == this->cmHomeDirectory)
  394. {
  395. if(cmSystemTools::LowerCase(
  396. cmSystemTools::GetFilenameName(filename)) == "cmakelists.txt")
  397. {
  398. requireProjectCommand = true;
  399. }
  400. }
  401. // push the listfile onto the stack
  402. this->ListFileStack.push_back(filenametoread);
  403. cmListFile cacheFile;
  404. if( !cacheFile.ParseFile(filenametoread, requireProjectCommand) )
  405. {
  406. this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentFile.c_str());
  407. return false;
  408. }
  409. // add this list file to the list of dependencies
  410. this->ListFiles.push_back( filenametoread);
  411. const size_t numberFunctions = cacheFile.Functions.size();
  412. for(size_t i =0; i < numberFunctions; ++i)
  413. {
  414. this->ExecuteCommand(cacheFile.Functions[i]);
  415. if ( cmSystemTools::GetFatalErrorOccured() )
  416. {
  417. // pop the listfile off the stack
  418. this->ListFileStack.pop_back();
  419. this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentFile.c_str());
  420. return true;
  421. }
  422. }
  423. // send scope ended to and function blockers
  424. if (filename)
  425. {
  426. // loop over all function blockers to see if any block this command
  427. std::list<cmFunctionBlocker *>::iterator pos;
  428. for (pos = this->FunctionBlockers.begin();
  429. pos != this->FunctionBlockers.end(); ++pos)
  430. {
  431. // if this blocker was not in the original then send a
  432. // scope ended message
  433. if (originalBlockers.find(*pos) == originalBlockers.end())
  434. {
  435. (*pos)->ScopeEnded(*this);
  436. }
  437. }
  438. }
  439. this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentFile.c_str());
  440. // pop the listfile off the stack
  441. this->ListFileStack.pop_back();
  442. return true;
  443. }
  444. void cmMakefile::AddCommand(cmCommand* wg)
  445. {
  446. this->GetCMakeInstance()->AddCommand(wg);
  447. }
  448. // Set the make file
  449. void cmMakefile::SetLocalGenerator(cmLocalGenerator* lg)
  450. {
  451. this->LocalGenerator = lg;
  452. }
  453. void cmMakefile::FinalPass()
  454. {
  455. // do all the variable expansions here
  456. this->ExpandVariables();
  457. // give all the commands a chance to do something
  458. // after the file has been parsed before generation
  459. for(std::vector<cmCommand*>::iterator i = this->UsedCommands.begin();
  460. i != this->UsedCommands.end(); ++i)
  461. {
  462. (*i)->FinalPass();
  463. }
  464. }
  465. // Generate the output file
  466. void cmMakefile::ConfigureFinalPass()
  467. {
  468. this->FinalPass();
  469. const char* oldValue
  470. = this->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  471. if (oldValue && atof(oldValue) <= 1.2)
  472. {
  473. cmSystemTools::Error("You have requested backwards compatibility "
  474. "with CMake version 1.2 or earlier. This version "
  475. "of CMake only supports backwards compatibility "
  476. "with CMake 1.4 or later. For compatibility with "
  477. "1.2 or earlier please use CMake 2.0");
  478. }
  479. for (cmTargets::iterator l = this->Targets.begin();
  480. l != this->Targets.end(); l++)
  481. {
  482. l->second.GenerateSourceFilesFromSourceLists(*this);
  483. l->second.AnalyzeLibDependencies(*this);
  484. }
  485. }
  486. //----------------------------------------------------------------------------
  487. void
  488. cmMakefile::AddCustomCommandToTarget(const char* target,
  489. const std::vector<std::string>& depends,
  490. const cmCustomCommandLines& commandLines,
  491. cmTarget::CustomCommandType type,
  492. const char* comment,
  493. const char* workingDir,
  494. bool escapeOldStyle)
  495. {
  496. // Find the target to which to add the custom command.
  497. cmTargets::iterator ti = this->Targets.find(target);
  498. if(ti != this->Targets.end())
  499. {
  500. // Add the command to the appropriate build step for the target.
  501. std::vector<std::string> no_output;
  502. cmCustomCommand cc(no_output, depends, commandLines, comment, workingDir);
  503. cc.SetEscapeOldStyle(escapeOldStyle);
  504. switch(type)
  505. {
  506. case cmTarget::PRE_BUILD:
  507. ti->second.GetPreBuildCommands().push_back(cc);
  508. break;
  509. case cmTarget::PRE_LINK:
  510. ti->second.GetPreLinkCommands().push_back(cc);
  511. break;
  512. case cmTarget::POST_BUILD:
  513. ti->second.GetPostBuildCommands().push_back(cc);
  514. break;
  515. }
  516. // Add dependencies on commands CMake knows how to build.
  517. for(cmCustomCommandLines::const_iterator cli = commandLines.begin();
  518. cli != commandLines.end(); ++cli)
  519. {
  520. std::string cacheCommand = *cli->begin();
  521. if(const char* knownTarget =
  522. this->GetCacheManager()->GetCacheValue(cacheCommand.c_str()))
  523. {
  524. ti->second.AddUtility(knownTarget);
  525. }
  526. }
  527. }
  528. }
  529. //----------------------------------------------------------------------------
  530. void
  531. cmMakefile::AddCustomCommandToOutput(const std::vector<std::string>& outputs,
  532. const std::vector<std::string>& depends,
  533. const char* main_dependency,
  534. const cmCustomCommandLines& commandLines,
  535. const char* comment,
  536. const char* workingDir,
  537. bool replace,
  538. bool escapeOldStyle)
  539. {
  540. // Make sure there is at least one output.
  541. if(outputs.empty())
  542. {
  543. cmSystemTools::Error("Attempt to add a custom rule with no output!");
  544. return;
  545. }
  546. // Choose a source file on which to store the custom command.
  547. cmSourceFile* file = 0;
  548. if(main_dependency && main_dependency[0])
  549. {
  550. // The main dependency was specified. Use it unless a different
  551. // custom command already used it.
  552. file = this->GetSource(main_dependency);
  553. if(file && file->GetCustomCommand() && !replace)
  554. {
  555. // The main dependency already has a custom command.
  556. if(commandLines == file->GetCustomCommand()->GetCommandLines())
  557. {
  558. // The existing custom command is identical. Silently ignore
  559. // the duplicate.
  560. return;
  561. }
  562. else
  563. {
  564. // The existing custom command is different. We need to
  565. // generate a rule file for this new command.
  566. file = 0;
  567. }
  568. }
  569. else
  570. {
  571. // The main dependency does not have a custom command or we are
  572. // allowed to replace it. Use it to store the command.
  573. file = this->GetOrCreateSource(main_dependency);
  574. }
  575. }
  576. // Generate a rule file if the main dependency is not available.
  577. if(!file)
  578. {
  579. // Construct a rule file associated with the first output produced.
  580. std::string outName = outputs[0];
  581. outName += ".rule";
  582. // Check if the rule file already exists.
  583. file = this->GetSource(outName.c_str());
  584. if(file && file->GetCustomCommand() && !replace)
  585. {
  586. // The rule file already exists.
  587. if(commandLines != file->GetCustomCommand()->GetCommandLines())
  588. {
  589. cmSystemTools::Error("Attempt to add a custom rule to output \"",
  590. outName.c_str(),
  591. "\" which already has a custom rule.");
  592. }
  593. return;
  594. }
  595. // Create a cmSourceFile for the rule file.
  596. file = this->GetOrCreateSource(outName.c_str(), true);
  597. }
  598. // Always create the output sources and mark them generated.
  599. for(std::vector<std::string>::const_iterator o = outputs.begin();
  600. o != outputs.end(); ++o)
  601. {
  602. if(cmSourceFile* out = this->GetOrCreateSource(o->c_str(), true))
  603. {
  604. out->SetProperty("GENERATED", "1");
  605. }
  606. }
  607. // Construct a complete list of dependencies.
  608. std::vector<std::string> depends2(depends);
  609. if(main_dependency && main_dependency[0])
  610. {
  611. depends2.push_back(main_dependency);
  612. }
  613. // Attach the custom command to the file.
  614. if(file)
  615. {
  616. cmCustomCommand* cc =
  617. new cmCustomCommand(outputs, depends2, commandLines,
  618. comment, workingDir);
  619. cc->SetEscapeOldStyle(escapeOldStyle);
  620. file->SetCustomCommand(cc);
  621. }
  622. }
  623. //----------------------------------------------------------------------------
  624. void
  625. cmMakefile::AddCustomCommandToOutput(const char* output,
  626. const std::vector<std::string>& depends,
  627. const char* main_dependency,
  628. const cmCustomCommandLines& commandLines,
  629. const char* comment,
  630. const char* workingDir,
  631. bool replace,
  632. bool escapeOldStyle)
  633. {
  634. std::vector<std::string> outputs;
  635. outputs.push_back(output);
  636. this->AddCustomCommandToOutput(outputs, depends, main_dependency,
  637. commandLines, comment, workingDir,
  638. replace, escapeOldStyle);
  639. }
  640. //----------------------------------------------------------------------------
  641. void
  642. cmMakefile::AddCustomCommandOldStyle(const char* target,
  643. const std::vector<std::string>& outputs,
  644. const std::vector<std::string>& depends,
  645. const char* source,
  646. const cmCustomCommandLines& commandLines,
  647. const char* comment)
  648. {
  649. // Translate the old-style signature to one of the new-style
  650. // signatures.
  651. if(strcmp(source, target) == 0)
  652. {
  653. // In the old-style signature if the source and target were the
  654. // same then it added a post-build rule to the target. Preserve
  655. // this behavior.
  656. this->AddCustomCommandToTarget(target, depends, commandLines,
  657. cmTarget::POST_BUILD, comment, 0);
  658. return;
  659. }
  660. // Each output must get its own copy of this rule.
  661. cmsys::RegularExpression sourceFiles("\\.(C|M|c|c\\+\\+|cc|cpp|cxx|m|mm|"
  662. "rc|def|r|odl|idl|hpj|bat|h|h\\+\\+|"
  663. "hm|hpp|hxx|in|txx|inl)$");
  664. for(std::vector<std::string>::const_iterator oi = outputs.begin();
  665. oi != outputs.end(); ++oi)
  666. {
  667. // Get the name of this output.
  668. const char* output = oi->c_str();
  669. // Choose whether to use a main dependency.
  670. if(sourceFiles.find(source))
  671. {
  672. // The source looks like a real file. Use it as the main dependency.
  673. this->AddCustomCommandToOutput(output, depends, source,
  674. commandLines, comment, 0);
  675. }
  676. else
  677. {
  678. // The source may not be a real file. Do not use a main dependency.
  679. const char* no_main_dependency = 0;
  680. std::vector<std::string> depends2 = depends;
  681. depends2.push_back(source);
  682. this->AddCustomCommandToOutput(output, depends2, no_main_dependency,
  683. commandLines, comment, 0);
  684. }
  685. // If the rule was added to the source (and not a .rule file),
  686. // then add the source to the target to make sure the rule is
  687. // included.
  688. std::string sname = output;
  689. sname += ".rule";
  690. if(!this->GetSource(sname.c_str()))
  691. {
  692. if (this->Targets.find(target) != this->Targets.end())
  693. {
  694. this->Targets[target].GetSourceLists().push_back(source);
  695. }
  696. else
  697. {
  698. cmSystemTools::Error("Attempt to add a custom rule to a target "
  699. "that does not exist yet for target ", target);
  700. return;
  701. }
  702. }
  703. }
  704. }
  705. //----------------------------------------------------------------------------
  706. void cmMakefile::AddUtilityCommand(const char* utilityName, bool all,
  707. const std::vector<std::string>& depends,
  708. const char* workingDirectory,
  709. const char* command,
  710. const char* arg1,
  711. const char* arg2,
  712. const char* arg3,
  713. const char* arg4)
  714. {
  715. // Construct the command line for the custom command.
  716. cmCustomCommandLine commandLine;
  717. commandLine.push_back(command);
  718. if(arg1)
  719. {
  720. commandLine.push_back(arg1);
  721. }
  722. if(arg2)
  723. {
  724. commandLine.push_back(arg2);
  725. }
  726. if(arg3)
  727. {
  728. commandLine.push_back(arg3);
  729. }
  730. if(arg4)
  731. {
  732. commandLine.push_back(arg4);
  733. }
  734. cmCustomCommandLines commandLines;
  735. commandLines.push_back(commandLine);
  736. // Call the real signature of this method.
  737. this->AddUtilityCommand(utilityName, all, workingDirectory,
  738. depends, commandLines);
  739. }
  740. //----------------------------------------------------------------------------
  741. void cmMakefile::AddUtilityCommand(const char* utilityName, bool all,
  742. const char* workingDirectory,
  743. const std::vector<std::string>& depends,
  744. const cmCustomCommandLines& commandLines,
  745. bool escapeOldStyle, const char* comment)
  746. {
  747. // Create a target instance for this utility.
  748. cmTarget target;
  749. target.SetType(cmTarget::UTILITY, utilityName);
  750. target.SetInAll(all);
  751. target.SetMakefile(this);
  752. if(!comment)
  753. {
  754. // Use an empty comment to avoid generation of default comment.
  755. comment = "";
  756. }
  757. // Store the custom command in the target.
  758. std::string force = this->GetStartOutputDirectory();
  759. force += cmake::GetCMakeFilesDirectory();
  760. force += "/";
  761. force += utilityName;
  762. const char* no_main_dependency = 0;
  763. bool no_replace = false;
  764. this->AddCustomCommandToOutput(force.c_str(), depends,
  765. no_main_dependency,
  766. commandLines, comment,
  767. workingDirectory, no_replace,
  768. escapeOldStyle);
  769. target.GetSourceLists().push_back(force);
  770. // The output is not actually created so mark it symbolic.
  771. if(cmSourceFile* sf = this->GetSource(force.c_str()))
  772. {
  773. sf->SetProperty("SYMBOLIC", "1");
  774. }
  775. else
  776. {
  777. cmSystemTools::Error("Could not get source file entry for ",
  778. force.c_str());
  779. }
  780. // Add the target to the set of targets.
  781. cmTargets::iterator it =
  782. this->Targets.insert(cmTargets::value_type(utilityName,target)).first;
  783. this->LocalGenerator->GetGlobalGenerator()->AddTarget(*it);
  784. }
  785. void cmMakefile::AddDefineFlag(const char* flag)
  786. {
  787. this->DefineFlags += " ";
  788. this->DefineFlags += flag;
  789. }
  790. void cmMakefile::RemoveDefineFlag(const char* flag)
  791. {
  792. // Check the length of the flag to remove.
  793. std::string::size_type len = strlen(flag);
  794. if(len < 1)
  795. {
  796. return;
  797. }
  798. // Remove all instances of the flag that are surrounded by
  799. // whitespace or the beginning/end of the string.
  800. for(std::string::size_type lpos = this->DefineFlags.find(flag, 0);
  801. lpos != std::string::npos; lpos = this->DefineFlags.find(flag, lpos))
  802. {
  803. std::string::size_type rpos = lpos + len;
  804. if((lpos <= 0 || isspace(this->DefineFlags[lpos-1])) &&
  805. (rpos >= this->DefineFlags.size() || isspace(this->DefineFlags[rpos])))
  806. {
  807. this->DefineFlags.erase(lpos, len);
  808. }
  809. else
  810. {
  811. ++lpos;
  812. }
  813. }
  814. }
  815. void cmMakefile::AddLinkLibrary(const char* lib,
  816. cmTarget::LinkLibraryType llt)
  817. {
  818. cmTarget::LibraryID tmp;
  819. tmp.first = lib;
  820. tmp.second = llt;
  821. this->LinkLibraries.push_back(tmp);
  822. }
  823. void cmMakefile::AddLinkLibraryForTarget(const char *target,
  824. const char* lib,
  825. cmTarget::LinkLibraryType llt)
  826. {
  827. cmTargets::iterator i = this->Targets.find(target);
  828. if ( i != this->Targets.end())
  829. {
  830. cmTarget* tgt =
  831. this->GetCMakeInstance()->GetGlobalGenerator()->FindTarget(0, lib);
  832. if(tgt)
  833. {
  834. bool allowModules = true;
  835. const char* versionValue
  836. = this->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  837. if (versionValue && (atof(versionValue) >= 2.4) )
  838. {
  839. allowModules = false;
  840. }
  841. // if it is not a static or shared library then you can not link to it
  842. if(!((tgt->GetType() == cmTarget::STATIC_LIBRARY) ||
  843. (tgt->GetType() == cmTarget::SHARED_LIBRARY)))
  844. {
  845. cmOStringStream e;
  846. e << "Attempt to add link target " << lib << " of type: "
  847. << cmTarget::TargetTypeNames[static_cast<int>(tgt->GetType())]
  848. << "\nto target " << target
  849. << ". You can only link to STATIC or SHARED libraries.";
  850. // in older versions of cmake linking to modules was allowed
  851. if( tgt->GetType() == cmTarget::MODULE_LIBRARY )
  852. {
  853. e <<
  854. "\nTo allow linking of modules set "
  855. "CMAKE_BACKWARDS_COMPATIBILITY to 2.2 or lower\n";
  856. }
  857. // if no modules are allowed then this is always an error
  858. if(!allowModules ||
  859. // if we allow modules but the type is not a module then it is
  860. // still an error
  861. (allowModules && tgt->GetType() != cmTarget::MODULE_LIBRARY))
  862. {
  863. cmSystemTools::Error(e.str().c_str());
  864. }
  865. }
  866. }
  867. i->second.AddLinkLibrary( *this, target, lib, llt );
  868. }
  869. else
  870. {
  871. cmOStringStream e;
  872. e << "Attempt to add link library \""
  873. << lib << "\" to target \""
  874. << target << "\" which is not built by this project.";
  875. cmSystemTools::Error(e.str().c_str());
  876. }
  877. }
  878. void cmMakefile::AddLinkDirectoryForTarget(const char *target,
  879. const char* d)
  880. {
  881. cmTargets::iterator i = this->Targets.find(target);
  882. if ( i != this->Targets.end())
  883. {
  884. i->second.AddLinkDirectory( d );
  885. }
  886. else
  887. {
  888. cmSystemTools::Error
  889. ("Attempt to add link directories to non-existant target: ",
  890. target, " for directory ", d);
  891. }
  892. }
  893. void cmMakefile::AddLinkLibrary(const char* lib)
  894. {
  895. this->AddLinkLibrary(lib,cmTarget::GENERAL);
  896. }
  897. void cmMakefile::AddLinkDirectory(const char* dir)
  898. {
  899. // Don't add a link directory that is already present. Yes, this
  900. // linear search results in n^2 behavior, but n won't be getting
  901. // much bigger than 20. We cannot use a set because of order
  902. // dependency of the link search path.
  903. // remove trailing slashes
  904. if(dir && dir[strlen(dir)-1] == '/')
  905. {
  906. std::string newdir = dir;
  907. newdir = newdir.substr(0, newdir.size()-1);
  908. if(std::find(this->LinkDirectories.begin(),
  909. this->LinkDirectories.end(),
  910. newdir.c_str()) == this->LinkDirectories.end())
  911. {
  912. this->LinkDirectories.push_back(newdir);
  913. }
  914. }
  915. else
  916. {
  917. if(std::find(this->LinkDirectories.begin(),
  918. this->LinkDirectories.end(), dir)
  919. == this->LinkDirectories.end())
  920. {
  921. this->LinkDirectories.push_back(dir);
  922. }
  923. }
  924. }
  925. void cmMakefile::InitializeFromParent()
  926. {
  927. cmMakefile *parent = this->LocalGenerator->GetParent()->GetMakefile();
  928. // copy the definitions
  929. this->Definitions = parent->Definitions;
  930. // copy include paths
  931. this->IncludeDirectories = parent->IncludeDirectories;
  932. this->SystemIncludeDirectories = parent->SystemIncludeDirectories;
  933. // define flags
  934. this->DefineFlags = parent->DefineFlags;
  935. // link libraries
  936. this->LinkLibraries = parent->LinkLibraries;
  937. // link directories
  938. this->LinkDirectories = parent->LinkDirectories;
  939. // the initial project name
  940. this->ProjectName = parent->ProjectName;
  941. // Copy include regular expressions.
  942. this->IncludeFileRegularExpression = parent->IncludeFileRegularExpression;
  943. this->ComplainFileRegularExpression = parent->ComplainFileRegularExpression;
  944. }
  945. void cmMakefile::ConfigureSubDirectory(cmLocalGenerator *lg2)
  946. {
  947. // copy our variables from the child makefile
  948. lg2->GetMakefile()->InitializeFromParent();
  949. lg2->GetMakefile()->MakeStartDirectoriesCurrent();
  950. // finally configure the subdir
  951. lg2->Configure();
  952. }
  953. void cmMakefile::AddSubDirectory(const char* sub,
  954. bool topLevel, bool preorder)
  955. {
  956. // the source path must be made full if it isn't already
  957. std::string srcPath = sub;
  958. if (!cmSystemTools::FileIsFullPath(srcPath.c_str()))
  959. {
  960. srcPath = this->GetCurrentDirectory();
  961. srcPath += "/";
  962. srcPath += sub;
  963. }
  964. // binary path must be made full if it isn't already
  965. std::string binPath = sub;
  966. if (!cmSystemTools::FileIsFullPath(binPath.c_str()))
  967. {
  968. binPath = this->GetCurrentOutputDirectory();
  969. binPath += "/";
  970. binPath += sub;
  971. }
  972. this->AddSubDirectory(srcPath.c_str(), binPath.c_str(),
  973. topLevel, preorder, false);
  974. }
  975. void cmMakefile::AddSubDirectory(const char* srcPath, const char *binPath,
  976. bool topLevel, bool preorder,
  977. bool immediate)
  978. {
  979. std::vector<cmLocalGenerator *>& children =
  980. this->LocalGenerator->GetChildren();
  981. // has this directory already been added? If so error
  982. unsigned int i;
  983. for (i = 0; i < children.size(); ++i)
  984. {
  985. if (srcPath == children[i]->GetMakefile()->GetStartDirectory())
  986. {
  987. cmSystemTools::Error
  988. ("Attempt to add subdirectory multiple times for directory.\n",
  989. srcPath);
  990. return;
  991. }
  992. }
  993. // create a new local generator and set its parent
  994. cmLocalGenerator *lg2 =
  995. this->LocalGenerator->GetGlobalGenerator()->CreateLocalGenerator();
  996. lg2->SetParent(this->LocalGenerator);
  997. this->LocalGenerator->GetGlobalGenerator()->AddLocalGenerator(lg2);
  998. // set the subdirs start dirs
  999. lg2->GetMakefile()->SetStartDirectory(srcPath);
  1000. lg2->GetMakefile()->SetStartOutputDirectory(binPath);
  1001. lg2->SetExcludeAll(!topLevel);
  1002. lg2->GetMakefile()->SetPreOrder(preorder);
  1003. if (immediate)
  1004. {
  1005. this->ConfigureSubDirectory(lg2);
  1006. }
  1007. }
  1008. void cmMakefile::AddIncludeDirectory(const char* inc, bool before)
  1009. {
  1010. // Don't add an include directory that is already present. Yes,
  1011. // this linear search results in n^2 behavior, but n won't be
  1012. // getting much bigger than 20. We cannot use a set because of
  1013. // order dependency of the include path.
  1014. std::vector<std::string>::iterator i =
  1015. std::find(this->IncludeDirectories.begin(),
  1016. this->IncludeDirectories.end(), inc);
  1017. if(i == this->IncludeDirectories.end())
  1018. {
  1019. if (before)
  1020. {
  1021. // WARNING: this *is* expensive (linear time) since it's a vector
  1022. this->IncludeDirectories.insert(this->IncludeDirectories.begin(), inc);
  1023. }
  1024. else
  1025. {
  1026. this->IncludeDirectories.push_back(inc);
  1027. }
  1028. }
  1029. else
  1030. {
  1031. if(before)
  1032. {
  1033. // if this before and already in the path then remove it
  1034. this->IncludeDirectories.erase(i);
  1035. // WARNING: this *is* expensive (linear time) since it's a vector
  1036. this->IncludeDirectories.insert(this->IncludeDirectories.begin(), inc);
  1037. }
  1038. }
  1039. }
  1040. //----------------------------------------------------------------------------
  1041. void cmMakefile::AddSystemIncludeDirectory(const char* dir)
  1042. {
  1043. this->SystemIncludeDirectories.insert(dir);
  1044. }
  1045. //----------------------------------------------------------------------------
  1046. bool cmMakefile::IsSystemIncludeDirectory(const char* dir)
  1047. {
  1048. return (this->SystemIncludeDirectories.find(dir) !=
  1049. this->SystemIncludeDirectories.end());
  1050. }
  1051. void cmMakefile::AddDefinition(const char* name, const char* value)
  1052. {
  1053. if (!value )
  1054. {
  1055. return;
  1056. }
  1057. this->TemporaryDefinitionKey = name;
  1058. this->Definitions[this->TemporaryDefinitionKey] = value;
  1059. #ifdef CMAKE_BUILD_WITH_CMAKE
  1060. cmVariableWatch* vv = this->GetVariableWatch();
  1061. if ( vv )
  1062. {
  1063. vv->VariableAccessed(this->TemporaryDefinitionKey,
  1064. cmVariableWatch::VARIABLE_MODIFIED_ACCESS);
  1065. }
  1066. #endif
  1067. }
  1068. void cmMakefile::AddCacheDefinition(const char* name, const char* value,
  1069. const char* doc,
  1070. cmCacheManager::CacheEntryType type)
  1071. {
  1072. const char* val = value;
  1073. cmCacheManager::CacheIterator it =
  1074. this->GetCacheManager()->GetCacheIterator(name);
  1075. if(!it.IsAtEnd() && (it.GetType() == cmCacheManager::UNINITIALIZED) &&
  1076. it.Initialized())
  1077. {
  1078. val = it.GetValue();
  1079. if ( type == cmCacheManager::PATH || type == cmCacheManager::FILEPATH )
  1080. {
  1081. std::vector<std::string>::size_type cc;
  1082. std::vector<std::string> files;
  1083. std::string nvalue = "";
  1084. cmSystemTools::ExpandListArgument(val, files);
  1085. for ( cc = 0; cc < files.size(); cc ++ )
  1086. {
  1087. files[cc] = cmSystemTools::CollapseFullPath(files[cc].c_str());
  1088. if ( cc > 0 )
  1089. {
  1090. nvalue += ";";
  1091. }
  1092. nvalue += files[cc];
  1093. }
  1094. this->GetCacheManager()->AddCacheEntry(name, nvalue.c_str(), doc, type);
  1095. val = it.GetValue();
  1096. }
  1097. }
  1098. this->GetCacheManager()->AddCacheEntry(name, val, doc, type);
  1099. this->AddDefinition(name, val);
  1100. }
  1101. void cmMakefile::AddDefinition(const char* name, bool value)
  1102. {
  1103. if(value)
  1104. {
  1105. this->Definitions.erase( DefinitionMap::key_type(name));
  1106. this->Definitions.insert(DefinitionMap::value_type(name, "ON"));
  1107. }
  1108. else
  1109. {
  1110. this->Definitions.erase( DefinitionMap::key_type(name));
  1111. this->Definitions.insert(DefinitionMap::value_type(name, "OFF"));
  1112. }
  1113. #ifdef CMAKE_BUILD_WITH_CMAKE
  1114. cmVariableWatch* vv = this->GetVariableWatch();
  1115. if ( vv )
  1116. {
  1117. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_MODIFIED_ACCESS);
  1118. }
  1119. #endif
  1120. }
  1121. void cmMakefile::AddCacheDefinition(const char* name,
  1122. bool value,
  1123. const char* doc)
  1124. {
  1125. bool val = value;
  1126. cmCacheManager::CacheIterator it =
  1127. this->GetCacheManager()->GetCacheIterator(name);
  1128. if(!it.IsAtEnd() && (it.GetType() == cmCacheManager::UNINITIALIZED) &&
  1129. it.Initialized())
  1130. {
  1131. val = it.GetValueAsBool();
  1132. }
  1133. this->GetCacheManager()->AddCacheEntry(name, val, doc);
  1134. this->AddDefinition(name, val);
  1135. }
  1136. void cmMakefile::RemoveDefinition(const char* name)
  1137. {
  1138. this->Definitions.erase(DefinitionMap::key_type(name));
  1139. #ifdef CMAKE_BUILD_WITH_CMAKE
  1140. cmVariableWatch* vv = this->GetVariableWatch();
  1141. if ( vv )
  1142. {
  1143. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_REMOVED_ACCESS);
  1144. }
  1145. #endif
  1146. }
  1147. void cmMakefile::SetProjectName(const char* p)
  1148. {
  1149. this->ProjectName = p;
  1150. }
  1151. void cmMakefile::AddGlobalLinkInformation(const char* name, cmTarget& target)
  1152. {
  1153. // for these targets do not add anything
  1154. switch(target.GetType())
  1155. {
  1156. case cmTarget::UTILITY:
  1157. case cmTarget::GLOBAL_TARGET:
  1158. case cmTarget::INSTALL_FILES:
  1159. case cmTarget::INSTALL_PROGRAMS:
  1160. return;
  1161. default:;
  1162. }
  1163. std::vector<std::string>::iterator j;
  1164. for(j = this->LinkDirectories.begin();
  1165. j != this->LinkDirectories.end(); ++j)
  1166. {
  1167. target.AddLinkDirectory(j->c_str());
  1168. }
  1169. target.MergeLinkLibraries( *this, name, this->LinkLibraries );
  1170. }
  1171. void cmMakefile::AddLibrary(const char* lname, int shared,
  1172. const std::vector<std::string> &srcs,
  1173. bool in_all)
  1174. {
  1175. cmTarget target;
  1176. switch (shared)
  1177. {
  1178. case 0:
  1179. target.SetType(cmTarget::STATIC_LIBRARY, lname);
  1180. break;
  1181. case 1:
  1182. target.SetType(cmTarget::SHARED_LIBRARY, lname);
  1183. break;
  1184. case 2:
  1185. target.SetType(cmTarget::MODULE_LIBRARY, lname);
  1186. break;
  1187. default:
  1188. target.SetType(cmTarget::STATIC_LIBRARY, lname);
  1189. }
  1190. // Clear its dependencies. Otherwise, dependencies might persist
  1191. // over changes in CMakeLists.txt, making the information stale and
  1192. // hence useless.
  1193. target.ClearDependencyInformation( *this, lname );
  1194. target.SetInAll(in_all);
  1195. target.GetSourceLists() = srcs;
  1196. target.SetMakefile(this);
  1197. this->AddGlobalLinkInformation(lname, target);
  1198. cmTargets::iterator it =
  1199. this->Targets.insert(cmTargets::value_type(lname,target)).first;
  1200. this->LocalGenerator->GetGlobalGenerator()->AddTarget(*it);
  1201. }
  1202. cmTarget* cmMakefile::AddExecutable(const char *exeName,
  1203. const std::vector<std::string> &srcs,
  1204. bool in_all)
  1205. {
  1206. cmTarget target;
  1207. target.SetType(cmTarget::EXECUTABLE, exeName);
  1208. target.SetInAll(in_all);
  1209. target.GetSourceLists() = srcs;
  1210. target.SetMakefile(this);
  1211. this->AddGlobalLinkInformation(exeName, target);
  1212. cmTargets::iterator it =
  1213. this->Targets.insert(cmTargets::value_type(exeName,target)).first;
  1214. this->LocalGenerator->GetGlobalGenerator()->AddTarget(*it);
  1215. return &it->second;
  1216. }
  1217. cmSourceFile *cmMakefile::GetSourceFileWithOutput(const char *cname)
  1218. {
  1219. std::string name = cname;
  1220. std::string out;
  1221. // look through all the source files that have custom commands
  1222. // and see if the custom command has the passed source file as an output
  1223. // keep in mind the possible .rule extension that may be tacked on
  1224. for(std::vector<cmSourceFile*>::const_iterator i =
  1225. this->SourceFiles.begin(); i != this->SourceFiles.end(); ++i)
  1226. {
  1227. // does this source file have a custom command?
  1228. if ((*i)->GetCustomCommand())
  1229. {
  1230. // is the output of the custom command match the source files name
  1231. const std::vector<std::string>& outputs =
  1232. (*i)->GetCustomCommand()->GetOutputs();
  1233. for(std::vector<std::string>::const_iterator o = outputs.begin();
  1234. o != outputs.end(); ++o)
  1235. {
  1236. out = *o;
  1237. std::string::size_type pos = out.rfind(name);
  1238. // If the output matches exactly
  1239. if (pos != out.npos &&
  1240. pos == out.size() - name.size() &&
  1241. (pos ==0 || out[pos-1] == '/'))
  1242. {
  1243. return *i;
  1244. }
  1245. }
  1246. }
  1247. }
  1248. // otherwise return NULL
  1249. return 0;
  1250. }
  1251. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1252. cmSourceGroup* cmMakefile::GetSourceGroup(const char* name)
  1253. {
  1254. // First see if the group exists. If so, replace its regular expression.
  1255. for(std::vector<cmSourceGroup>::iterator sg = this->SourceGroups.begin();
  1256. sg != this->SourceGroups.end(); ++sg)
  1257. {
  1258. std::string sgName = sg->GetName();
  1259. if(sgName == name)
  1260. {
  1261. return &(*sg);
  1262. }
  1263. else
  1264. {
  1265. cmSourceGroup *target = sg->lookupChild(name);
  1266. if(target)
  1267. {
  1268. return target;
  1269. }
  1270. }
  1271. }
  1272. return 0;
  1273. }
  1274. void cmMakefile::AddSourceGroup(const char* name,
  1275. const char* regex,
  1276. const char *parent)
  1277. {
  1278. // First see if the group exists. If so, replace its regular expression.
  1279. for(unsigned int i=0;i<this->SourceGroups.size();++i)
  1280. {
  1281. cmSourceGroup *sg = &this->SourceGroups[i];
  1282. std::string sgName = sg->GetName();
  1283. if(!parent)
  1284. {
  1285. if(sgName == name)
  1286. {
  1287. if ( regex )
  1288. {
  1289. // We only want to set the regular expression. If there are already
  1290. // source files in the group, we don't want to remove them.
  1291. sg->SetGroupRegex(regex);
  1292. }
  1293. return;
  1294. }
  1295. }
  1296. else
  1297. {
  1298. if(sgName == parent)
  1299. {
  1300. cmSourceGroup *localtarget = sg->lookupChild(name);
  1301. if(localtarget)
  1302. {
  1303. if ( regex )
  1304. {
  1305. // We only want to set the regular expression. If there are
  1306. // already source files in the group, we don't want to remove
  1307. // them.
  1308. localtarget->SetGroupRegex(regex);
  1309. }
  1310. }
  1311. else
  1312. {
  1313. sg->AddChild(cmSourceGroup(name, regex));
  1314. }
  1315. return;
  1316. }
  1317. else
  1318. {
  1319. cmSourceGroup *localtarget = sg->lookupChild(parent);
  1320. if(localtarget)
  1321. {
  1322. cmSourceGroup *addtarget = localtarget->lookupChild(name);
  1323. if(addtarget)
  1324. {
  1325. if ( regex )
  1326. {
  1327. // We only want to set the regular expression. If there are
  1328. // already source files in the group, we don't want to
  1329. // remove them.
  1330. addtarget->SetGroupRegex(regex);
  1331. }
  1332. }
  1333. else
  1334. {
  1335. localtarget->AddChild(cmSourceGroup(name, regex));
  1336. }
  1337. return;
  1338. }
  1339. }
  1340. }
  1341. }
  1342. // The group doesn't exist. Add it.
  1343. this->SourceGroups.push_back(cmSourceGroup(name, regex));
  1344. }
  1345. #endif
  1346. void cmMakefile::AddExtraDirectory(const char* dir)
  1347. {
  1348. this->AuxSourceDirectories.push_back(dir);
  1349. }
  1350. // expance CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR in the
  1351. // include and library directories.
  1352. void cmMakefile::ExpandVariables()
  1353. {
  1354. // Now expand variables in the include and link strings
  1355. for(std::vector<std::string>::iterator d = this->IncludeDirectories.begin();
  1356. d != this->IncludeDirectories.end(); ++d)
  1357. {
  1358. this->ExpandVariablesInString(*d, true, true);
  1359. }
  1360. for(std::vector<std::string>::iterator d = this->LinkDirectories.begin();
  1361. d != this->LinkDirectories.end(); ++d)
  1362. {
  1363. this->ExpandVariablesInString(*d, true, true);
  1364. }
  1365. for(cmTarget::LinkLibraryVectorType::iterator l =
  1366. this->LinkLibraries.begin();
  1367. l != this->LinkLibraries.end(); ++l)
  1368. {
  1369. this->ExpandVariablesInString(l->first, true, true);
  1370. }
  1371. }
  1372. bool cmMakefile::IsOn(const char* name) const
  1373. {
  1374. const char* value = this->GetDefinition(name);
  1375. return cmSystemTools::IsOn(value);
  1376. }
  1377. bool cmMakefile::IsSet(const char* name) const
  1378. {
  1379. const char* value = this->GetDefinition(name);
  1380. if ( !value )
  1381. {
  1382. return false;
  1383. }
  1384. if ( ! *value )
  1385. {
  1386. return false;
  1387. }
  1388. if ( cmSystemTools::IsNOTFOUND(value) )
  1389. {
  1390. return false;
  1391. }
  1392. return true;
  1393. }
  1394. bool cmMakefile::CanIWriteThisFile(const char* fileName)
  1395. {
  1396. if ( !this->IsOn("CMAKE_DISABLE_SOURCE_CHANGES") )
  1397. {
  1398. return true;
  1399. }
  1400. // If we are doing an in-source build, than the test will always fail
  1401. if ( cmSystemTools::SameFile(this->GetHomeDirectory(),
  1402. this->GetHomeOutputDirectory()) )
  1403. {
  1404. if ( this->IsOn("CMAKE_DISABLE_IN_SOURCE_BUILD") )
  1405. {
  1406. return false;
  1407. }
  1408. return true;
  1409. }
  1410. // Check if this is subdirectory of the source tree but not a
  1411. // subdirectory of a build tree
  1412. if ( cmSystemTools::IsSubDirectory(fileName,
  1413. this->GetHomeDirectory()) &&
  1414. !cmSystemTools::IsSubDirectory(fileName,
  1415. this->GetHomeOutputDirectory()) )
  1416. {
  1417. return false;
  1418. }
  1419. return true;
  1420. }
  1421. const char* cmMakefile::GetRequiredDefinition(const char* name) const
  1422. {
  1423. const char* ret = this->GetDefinition(name);
  1424. if(!ret)
  1425. {
  1426. cmSystemTools::Error("Error required internal CMake variable not "
  1427. "set, cmake may be not be built correctly.\n",
  1428. "Missing variable is:\n",
  1429. name);
  1430. return "";
  1431. }
  1432. return ret;
  1433. }
  1434. const char* cmMakefile::GetDefinition(const char* name) const
  1435. {
  1436. const char* def = 0;
  1437. DefinitionMap::const_iterator pos = this->Definitions.find(name);
  1438. if(pos != this->Definitions.end())
  1439. {
  1440. def = (*pos).second.c_str();
  1441. }
  1442. else
  1443. {
  1444. def = this->GetCacheManager()->GetCacheValue(name);
  1445. }
  1446. #ifdef CMAKE_BUILD_WITH_CMAKE
  1447. cmVariableWatch* vv = this->GetVariableWatch();
  1448. if ( vv )
  1449. {
  1450. if ( def )
  1451. {
  1452. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_READ_ACCESS);
  1453. }
  1454. else
  1455. {
  1456. // are unknown access allowed
  1457. DefinitionMap::const_iterator pos2 =
  1458. this->Definitions.find("CMAKE_ALLOW_UNKNOWN_VARIABLE_READ_ACCESS");
  1459. if (pos2 != this->Definitions.end() &&
  1460. cmSystemTools::IsOn((*pos2).second.c_str()))
  1461. {
  1462. vv->VariableAccessed
  1463. (name, cmVariableWatch::ALLOWED_UNKNOWN_VARIABLE_READ_ACCESS);
  1464. }
  1465. else
  1466. {
  1467. vv->VariableAccessed(name, cmVariableWatch::
  1468. UNKNOWN_VARIABLE_READ_ACCESS);
  1469. }
  1470. }
  1471. }
  1472. #endif
  1473. return def;
  1474. }
  1475. const char* cmMakefile::GetSafeDefinition(const char* def) const
  1476. {
  1477. const char* ret = this->GetDefinition(def);
  1478. if(!ret)
  1479. {
  1480. return "";
  1481. }
  1482. return ret;
  1483. }
  1484. std::vector<std::string> cmMakefile
  1485. ::GetDefinitions(int cacheonly /* = 0 */) const
  1486. {
  1487. std::map<cmStdString, int> definitions;
  1488. if ( !cacheonly )
  1489. {
  1490. DefinitionMap::const_iterator it;
  1491. for ( it = this->Definitions.begin();
  1492. it != this->Definitions.end(); it ++ )
  1493. {
  1494. definitions[it->first] = 1;
  1495. }
  1496. }
  1497. cmCacheManager::CacheIterator cit =
  1498. this->GetCacheManager()->GetCacheIterator();
  1499. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  1500. {
  1501. definitions[cit.GetName()] = 1;
  1502. }
  1503. std::vector<std::string> res;
  1504. std::map<cmStdString, int>::iterator fit;
  1505. for ( fit = definitions.begin(); fit != definitions.end(); fit ++ )
  1506. {
  1507. res.push_back(fit->first);
  1508. }
  1509. return res;
  1510. }
  1511. const char *cmMakefile::ExpandVariablesInString(std::string& source) const
  1512. {
  1513. return this->ExpandVariablesInString(source, false, false);
  1514. }
  1515. const char *cmMakefile::ExpandVariablesInString(std::string& source,
  1516. bool escapeQuotes,
  1517. bool noEscapes,
  1518. bool atOnly,
  1519. const char* filename,
  1520. long line,
  1521. bool removeEmpty,
  1522. bool replaceAt) const
  1523. {
  1524. if ( source.empty() || source.find_first_of("$@\\") == source.npos)
  1525. {
  1526. return source.c_str();
  1527. }
  1528. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  1529. // with GetDefinition(), if not found in the map, nothing is expanded.
  1530. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  1531. // the current environment variables.
  1532. bool notParsed = true;
  1533. if ( !atOnly )
  1534. {
  1535. cmCommandArgumentParserHelper parser;
  1536. parser.SetMakefile(this);
  1537. parser.SetLineFile(line, filename);
  1538. parser.SetEscapeQuotes(escapeQuotes);
  1539. parser.SetNoEscapeMode(noEscapes);
  1540. parser.SetReplaceAtSyntax(replaceAt);
  1541. int res = parser.ParseString(source.c_str(), 0);
  1542. if ( res )
  1543. {
  1544. source = parser.GetResult();
  1545. notParsed = false;
  1546. }
  1547. else
  1548. {
  1549. cmOStringStream error;
  1550. error << "Syntax error in cmake code at\n"
  1551. << (filename?filename:"(no filename given)")
  1552. << ":" << line << ":\n"
  1553. << parser.GetError() << ", when parsing string \""
  1554. << source.c_str() << "\"";
  1555. const char* versionValue
  1556. = this->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  1557. int major = 0;
  1558. int minor = 0;
  1559. if ( versionValue )
  1560. {
  1561. sscanf(versionValue, "%d.%d", &major, &minor);
  1562. }
  1563. if ( major < 2 || major == 2 && minor < 1 )
  1564. {
  1565. cmSystemTools::Error(error.str().c_str());
  1566. cmSystemTools::SetFatalErrorOccured();
  1567. return source.c_str();
  1568. }
  1569. else
  1570. {
  1571. cmSystemTools::Message(error.str().c_str());
  1572. }
  1573. }
  1574. }
  1575. if ( notParsed )
  1576. {
  1577. // start by look for $ or @ in the string
  1578. std::string::size_type markerPos;
  1579. if(atOnly)
  1580. {
  1581. markerPos = source.find_first_of("@");
  1582. }
  1583. else
  1584. {
  1585. markerPos = source.find_first_of("$@");
  1586. }
  1587. // if not found, or found as the last character, then leave quickly as
  1588. // nothing needs to be expanded
  1589. if((markerPos == std::string::npos) || (markerPos >= source.size()-1))
  1590. {
  1591. return source.c_str();
  1592. }
  1593. // current position
  1594. std::string::size_type currentPos =0; // start at 0
  1595. std::string result; // string with replacements
  1596. // go until the the end of the string
  1597. while((markerPos != std::string::npos) && (markerPos < source.size()-1))
  1598. {
  1599. // grab string from currentPos to the start of the variable
  1600. // and add it to the result
  1601. result += source.substr(currentPos, markerPos - currentPos);
  1602. char endVariableMarker; // what is the end of the variable @ or }
  1603. int markerStartSize = 1; // size of the start marker 1 or 2 or 5
  1604. if(!atOnly && source[markerPos] == '$')
  1605. {
  1606. // ${var} case
  1607. if(source[markerPos+1] == '{')
  1608. {
  1609. endVariableMarker = '}';
  1610. markerStartSize = 2;
  1611. }
  1612. // $ENV{var} case
  1613. else if(markerPos+4 < source.size() &&
  1614. source[markerPos+4] == '{' &&
  1615. !source.substr(markerPos+1, 3).compare("ENV"))
  1616. {
  1617. endVariableMarker = '}';
  1618. markerStartSize = 5;
  1619. }
  1620. else
  1621. {
  1622. // bogus $ with no { so add $ to result and move on
  1623. result += '$'; // add bogus $ back into string
  1624. currentPos = markerPos+1; // move on
  1625. // set end var to space so we can tell bogus
  1626. endVariableMarker = ' ';
  1627. }
  1628. }
  1629. else
  1630. {
  1631. // @VAR case
  1632. endVariableMarker = '@';
  1633. }
  1634. // if it was a valid variable (started with @ or ${ or $ENV{ )
  1635. if(endVariableMarker != ' ')
  1636. {
  1637. markerPos += markerStartSize; // move past marker
  1638. // find the end variable marker starting at the markerPos
  1639. // make sure it is a valid variable between
  1640. std::string::size_type endVariablePos =
  1641. source.find_first_not_of(
  1642. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
  1643. markerPos);
  1644. if(endVariablePos != std::string::npos &&
  1645. source[endVariablePos] != endVariableMarker)
  1646. {
  1647. endVariablePos = std::string::npos;
  1648. }
  1649. if(endVariablePos == std::string::npos)
  1650. {
  1651. // no end marker found so add the bogus start
  1652. if(endVariableMarker == '@')
  1653. {
  1654. result += '@';
  1655. }
  1656. else
  1657. {
  1658. result += (markerStartSize == 5 ? "$ENV{" : "${");
  1659. }
  1660. currentPos = markerPos;
  1661. }
  1662. else
  1663. {
  1664. // good variable remove it
  1665. std::string var =
  1666. source.substr(markerPos, endVariablePos - markerPos);
  1667. bool found = false;
  1668. if (markerStartSize == 5) // $ENV{
  1669. {
  1670. char *ptr = getenv(var.c_str());
  1671. if (ptr)
  1672. {
  1673. if (escapeQuotes)
  1674. {
  1675. result += cmSystemTools::EscapeQuotes(ptr);
  1676. }
  1677. else
  1678. {
  1679. result += ptr;
  1680. }
  1681. found = true;
  1682. }
  1683. }
  1684. else
  1685. {
  1686. const char* lookup = this->GetDefinition(var.c_str());
  1687. if(lookup)
  1688. {
  1689. if (escapeQuotes)
  1690. {
  1691. result += cmSystemTools::EscapeQuotes(lookup);
  1692. }
  1693. else
  1694. {
  1695. result += lookup;
  1696. }
  1697. found = true;
  1698. }
  1699. else if(filename && (var == "CMAKE_CURRENT_LIST_FILE"))
  1700. {
  1701. result += filename;
  1702. found = true;
  1703. }
  1704. else if(line >= 0 && (var == "CMAKE_CURRENT_LIST_LINE"))
  1705. {
  1706. cmOStringStream ostr;
  1707. ostr << line;
  1708. result += ostr.str();
  1709. found = true;
  1710. }
  1711. }
  1712. // if found add to result, if not, then it gets blanked
  1713. if (!found)
  1714. {
  1715. // if no definition is found then add the var back
  1716. if(!removeEmpty && endVariableMarker == '@')
  1717. {
  1718. result += "@";
  1719. result += var;
  1720. result += "@";
  1721. }
  1722. }
  1723. // lookup var, and replace it
  1724. currentPos = endVariablePos+1;
  1725. }
  1726. }
  1727. if(atOnly)
  1728. {
  1729. markerPos = source.find_first_of("@", currentPos);
  1730. }
  1731. else
  1732. {
  1733. markerPos = source.find_first_of("$@", currentPos);
  1734. }
  1735. }
  1736. result += source.substr(currentPos); // pick up the rest of the string
  1737. source = result;
  1738. }
  1739. return source.c_str();
  1740. }
  1741. void cmMakefile::RemoveVariablesInString(std::string& source,
  1742. bool atOnly) const
  1743. {
  1744. if(!atOnly)
  1745. {
  1746. cmsys::RegularExpression var("(\\${[A-Za-z_0-9]*})");
  1747. while (var.find(source))
  1748. {
  1749. source.erase(var.start(),var.end() - var.start());
  1750. }
  1751. }
  1752. if(!atOnly)
  1753. {
  1754. cmsys::RegularExpression varb("(\\$ENV{[A-Za-z_0-9]*})");
  1755. while (varb.find(source))
  1756. {
  1757. source.erase(varb.start(),varb.end() - varb.start());
  1758. }
  1759. }
  1760. cmsys::RegularExpression var2("(@[A-Za-z_0-9]*@)");
  1761. while (var2.find(source))
  1762. {
  1763. source.erase(var2.start(),var2.end() - var2.start());
  1764. }
  1765. }
  1766. /**
  1767. * Add the default definitions to the makefile. These values must not
  1768. * be dependent on anything that isn't known when this cmMakefile instance
  1769. * is constructed.
  1770. */
  1771. void cmMakefile::AddDefaultDefinitions()
  1772. {
  1773. #if defined(_WIN32) || defined(__CYGWIN__)
  1774. this->AddDefinition("WIN32", "1");
  1775. #else
  1776. this->AddDefinition("UNIX", "1");
  1777. #endif
  1778. // Cygwin is more like unix so enable the unix commands
  1779. #if defined(__CYGWIN__)
  1780. this->AddDefinition("UNIX", "1");
  1781. this->AddDefinition("CYGWIN", "1");
  1782. #endif
  1783. #if defined(__APPLE__)
  1784. this->AddDefinition("APPLE", "1");
  1785. #endif
  1786. #if defined(__QNXNTO__)
  1787. this->AddDefinition("QNXNTO", "1");
  1788. #endif
  1789. #if defined(__BEOS__)
  1790. this->AddDefinition("BEOS", "1");
  1791. #endif
  1792. char temp[1024];
  1793. sprintf(temp, "%d", cmVersion::GetMinorVersion());
  1794. this->AddDefinition("CMAKE_MINOR_VERSION", temp);
  1795. sprintf(temp, "%d", cmVersion::GetMajorVersion());
  1796. this->AddDefinition("CMAKE_MAJOR_VERSION", temp);
  1797. sprintf(temp, "%d", cmVersion::GetPatchVersion());
  1798. this->AddDefinition("CMAKE_PATCH_VERSION", temp);
  1799. this->AddDefinition("CMAKE_FILES_DIRECTORY",
  1800. cmake::GetCMakeFilesDirectory());
  1801. }
  1802. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1803. /**
  1804. * Find a source group whose regular expression matches the filename
  1805. * part of the given source name. Search backward through the list of
  1806. * source groups, and take the first matching group found. This way
  1807. * non-inherited SOURCE_GROUP commands will have precedence over
  1808. * inherited ones.
  1809. */
  1810. cmSourceGroup&
  1811. cmMakefile::FindSourceGroup(const char* source,
  1812. std::vector<cmSourceGroup> &groups)
  1813. {
  1814. // First search for a group that lists the file explicitly.
  1815. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1816. sg != groups.rend(); ++sg)
  1817. {
  1818. cmSourceGroup *result = sg->MatchChildrenFiles(source);
  1819. if(result)
  1820. {
  1821. return *result;
  1822. }
  1823. }
  1824. // Now search for a group whose regex matches the file.
  1825. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1826. sg != groups.rend(); ++sg)
  1827. {
  1828. cmSourceGroup *result = sg->MatchChildrenRegex(source);
  1829. if(result)
  1830. {
  1831. return *result;
  1832. }
  1833. }
  1834. // Shouldn't get here, but just in case, return the default group.
  1835. return groups.front();
  1836. }
  1837. #endif
  1838. bool cmMakefile::IsFunctionBlocked(const cmListFileFunction& lff)
  1839. {
  1840. // if there are no blockers get out of here
  1841. if (this->FunctionBlockers.begin() == this->FunctionBlockers.end())
  1842. {
  1843. return false;
  1844. }
  1845. // loop over all function blockers to see if any block this command
  1846. // evaluate in reverse, this is critical for balanced IF statements etc
  1847. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1848. for (pos = this->FunctionBlockers.rbegin();
  1849. pos != this->FunctionBlockers.rend(); ++pos)
  1850. {
  1851. if((*pos)->IsFunctionBlocked(lff, *this))
  1852. {
  1853. return true;
  1854. }
  1855. }
  1856. return false;
  1857. }
  1858. void cmMakefile::ExpandArguments(
  1859. std::vector<cmListFileArgument> const& inArgs,
  1860. std::vector<std::string>& outArgs)
  1861. {
  1862. std::vector<cmListFileArgument>::const_iterator i;
  1863. std::string value;
  1864. outArgs.reserve(inArgs.size());
  1865. for(i = inArgs.begin(); i != inArgs.end(); ++i)
  1866. {
  1867. // Expand the variables in the argument.
  1868. value = i->Value;
  1869. this->ExpandVariablesInString(value, false, false, false,
  1870. i->FilePath, i->Line,
  1871. false, true);
  1872. // If the argument is quoted, it should be one argument.
  1873. // Otherwise, it may be a list of arguments.
  1874. if(i->Quoted)
  1875. {
  1876. outArgs.push_back(value);
  1877. }
  1878. else
  1879. {
  1880. cmSystemTools::ExpandListArgument(value, outArgs);
  1881. }
  1882. }
  1883. }
  1884. void cmMakefile::RemoveFunctionBlocker(const cmListFileFunction& lff)
  1885. {
  1886. // loop over all function blockers to see if any block this command
  1887. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1888. for (pos = this->FunctionBlockers.rbegin();
  1889. pos != this->FunctionBlockers.rend(); ++pos)
  1890. {
  1891. if ((*pos)->ShouldRemove(lff, *this))
  1892. {
  1893. cmFunctionBlocker* b = *pos;
  1894. this->FunctionBlockers.remove(b);
  1895. delete b;
  1896. break;
  1897. }
  1898. }
  1899. return;
  1900. }
  1901. void cmMakefile::SetHomeDirectory(const char* dir)
  1902. {
  1903. this->cmHomeDirectory = dir;
  1904. cmSystemTools::ConvertToUnixSlashes(this->cmHomeDirectory);
  1905. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  1906. if ( !this->GetDefinition("CMAKE_CURRENT_SOURCE_DIR") )
  1907. {
  1908. this->AddDefinition("CMAKE_CURRENT_SOURCE_DIR", this->GetHomeDirectory());
  1909. }
  1910. }
  1911. void cmMakefile::SetHomeOutputDirectory(const char* lib)
  1912. {
  1913. this->HomeOutputDirectory = lib;
  1914. cmSystemTools::ConvertToUnixSlashes(this->HomeOutputDirectory);
  1915. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  1916. if ( !this->GetDefinition("CMAKE_CURRENT_BINARY_DIR") )
  1917. {
  1918. this->AddDefinition("CMAKE_CURRENT_BINARY_DIR",
  1919. this->GetHomeOutputDirectory());
  1920. }
  1921. }
  1922. /**
  1923. * Register the given cmData instance with its own name.
  1924. */
  1925. void cmMakefile::RegisterData(cmData* data)
  1926. {
  1927. std::string name = data->GetName();
  1928. DataMapType::const_iterator d = this->DataMap.find(name);
  1929. if((d != this->DataMap.end()) && (d->second != 0) && (d->second != data))
  1930. {
  1931. delete d->second;
  1932. }
  1933. this->DataMap[name] = data;
  1934. }
  1935. /**
  1936. * Register the given cmData instance with the given name. This can be used
  1937. * to register a NULL pointer.
  1938. */
  1939. void cmMakefile::RegisterData(const char* name, cmData* data)
  1940. {
  1941. DataMapType::const_iterator d = this->DataMap.find(name);
  1942. if((d != this->DataMap.end()) && (d->second != 0) && (d->second != data))
  1943. {
  1944. delete d->second;
  1945. }
  1946. this->DataMap[name] = data;
  1947. }
  1948. /**
  1949. * Lookup a cmData instance previously registered with the given name. If
  1950. * the instance cannot be found, return NULL.
  1951. */
  1952. cmData* cmMakefile::LookupData(const char* name) const
  1953. {
  1954. DataMapType::const_iterator d = this->DataMap.find(name);
  1955. if(d != this->DataMap.end())
  1956. {
  1957. return d->second;
  1958. }
  1959. else
  1960. {
  1961. return 0;
  1962. }
  1963. }
  1964. cmSourceFile* cmMakefile::GetSource(const char* sourceName) const
  1965. {
  1966. // if the source is provided with a full path use it, otherwise
  1967. // by default it is in the current source dir
  1968. std::string path;
  1969. if (cmSystemTools::FileIsFullPath(sourceName))
  1970. {
  1971. path = cmSystemTools::GetFilenamePath(sourceName);
  1972. }
  1973. else
  1974. {
  1975. path = this->GetCurrentDirectory();
  1976. // even though it is not a full path, it may still be relative
  1977. std::string subpath = cmSystemTools::GetFilenamePath(sourceName);
  1978. if (!subpath.empty())
  1979. {
  1980. path += "/";
  1981. path += cmSystemTools::GetFilenamePath(sourceName);
  1982. }
  1983. }
  1984. std::string sname =
  1985. cmSystemTools::GetFilenameWithoutLastExtension(sourceName);
  1986. // compute the extension
  1987. std::string ext
  1988. = cmSystemTools::GetFilenameLastExtension(sourceName);
  1989. if ( ext.length() && ext[0] == '.' )
  1990. {
  1991. ext = ext.substr(1);
  1992. }
  1993. for(std::vector<cmSourceFile*>::const_iterator i =
  1994. this->SourceFiles.begin();
  1995. i != this->SourceFiles.end(); ++i)
  1996. {
  1997. if ((*i)->GetSourceNameWithoutLastExtension() == sname &&
  1998. cmSystemTools::GetFilenamePath((*i)->GetFullPath()) == path &&
  1999. (ext.size() == 0 || (ext == (*i)->GetSourceExtension())))
  2000. {
  2001. return *i;
  2002. }
  2003. }
  2004. // geeze, if it wasn't found maybe it is listed under the output dir
  2005. if (!cmSystemTools::GetFilenamePath(sourceName).empty())
  2006. {
  2007. return 0;
  2008. }
  2009. path = this->GetCurrentOutputDirectory();
  2010. for(std::vector<cmSourceFile*>::const_iterator i =
  2011. this->SourceFiles.begin();
  2012. i != this->SourceFiles.end(); ++i)
  2013. {
  2014. if ((*i)->GetSourceName() == sname &&
  2015. cmSystemTools::GetFilenamePath((*i)->GetFullPath()) == path &&
  2016. (ext.size() == 0 || (ext == (*i)->GetSourceExtension())))
  2017. {
  2018. return *i;
  2019. }
  2020. }
  2021. return 0;
  2022. }
  2023. cmSourceFile* cmMakefile::GetOrCreateSource(const char* sourceName,
  2024. bool generated)
  2025. {
  2026. // make it a full path first
  2027. std::string src = sourceName;
  2028. bool relative = !cmSystemTools::FileIsFullPath(sourceName);
  2029. std::string srcTreeFile = this->GetCurrentDirectory();
  2030. srcTreeFile += "/";
  2031. srcTreeFile += sourceName;
  2032. if(relative)
  2033. {
  2034. src = srcTreeFile;
  2035. }
  2036. // check to see if it exists
  2037. cmSourceFile* ret = this->GetSource(src.c_str());
  2038. if (ret)
  2039. {
  2040. return ret;
  2041. }
  2042. // OK a source file object doesn't exist for the source
  2043. // maybe we made a bad call on assuming it was in the src tree
  2044. std::string buildTreeFile = this->GetCurrentOutputDirectory();
  2045. buildTreeFile += "/";
  2046. buildTreeFile += sourceName;
  2047. if (relative)
  2048. {
  2049. src = buildTreeFile;
  2050. ret = this->GetSource(src.c_str());
  2051. if (ret)
  2052. {
  2053. return ret;
  2054. }
  2055. // if it has not been marked generated check to see if it exists in the
  2056. // src tree
  2057. if(!generated)
  2058. {
  2059. // see if the file is in the source tree, otherwise assume it
  2060. // is in the binary tree
  2061. if (cmSystemTools::FileExists(srcTreeFile.c_str()) &&
  2062. !cmSystemTools::FileIsDirectory(srcTreeFile.c_str()))
  2063. {
  2064. src = srcTreeFile;
  2065. }
  2066. else
  2067. {
  2068. if ( cmSystemTools::GetFilenameLastExtension
  2069. (srcTreeFile.c_str()).size() == 0)
  2070. {
  2071. if (cmSystemTools::DoesFileExistWithExtensions(
  2072. srcTreeFile.c_str(), this->GetSourceExtensions()))
  2073. {
  2074. src = srcTreeFile;
  2075. }
  2076. else if (cmSystemTools::DoesFileExistWithExtensions(
  2077. srcTreeFile.c_str(), this->GetHeaderExtensions()))
  2078. {
  2079. src = srcTreeFile;
  2080. }
  2081. }
  2082. }
  2083. }
  2084. }
  2085. // a cmSourceFile instance does not exist yet so we must create one
  2086. // go back to looking in the source directory for it
  2087. // we must create one
  2088. cmSourceFile file;
  2089. std::string path = cmSystemTools::GetFilenamePath(src);
  2090. if(generated)
  2091. {
  2092. std::string ext = cmSystemTools::GetFilenameLastExtension(src);
  2093. std::string name_no_ext = cmSystemTools::GetFilenameName(src.c_str());
  2094. name_no_ext = name_no_ext.substr(0, name_no_ext.length()-ext.length());
  2095. if ( ext.length() && ext[0] == '.' )
  2096. {
  2097. ext = ext.substr(1);
  2098. }
  2099. bool headerFile =
  2100. !(std::find( this->HeaderFileExtensions.begin(),
  2101. this->HeaderFileExtensions.end(), ext ) ==
  2102. this->HeaderFileExtensions.end());
  2103. file.SetName(name_no_ext.c_str(), path.c_str(), ext.c_str(), headerFile);
  2104. }
  2105. else
  2106. {
  2107. std::string relPath = cmSystemTools::GetFilenamePath(sourceName);
  2108. if (relative && relPath.size())
  2109. {
  2110. // we need to keep the relative part of the filename
  2111. std::string fullPathLessRel = path;
  2112. std::string::size_type pos = fullPathLessRel.rfind(relPath);
  2113. if (pos == std::string::npos)
  2114. {
  2115. cmSystemTools::Error(
  2116. "CMake failed to properly look up relative cmSourceFile: ",
  2117. sourceName);
  2118. }
  2119. fullPathLessRel.erase(pos-1);
  2120. file.SetName(sourceName, fullPathLessRel.c_str(),
  2121. this->GetSourceExtensions(),
  2122. this->GetHeaderExtensions());
  2123. }
  2124. else
  2125. {
  2126. file.SetName(cmSystemTools::GetFilenameName(src.c_str()).c_str(),
  2127. path.c_str(),
  2128. this->GetSourceExtensions(),
  2129. this->GetHeaderExtensions());
  2130. }
  2131. }
  2132. // add the source file to the makefile
  2133. this->AddSource(file);
  2134. src = file.GetFullPath();
  2135. ret = this->GetSource(src.c_str());
  2136. if (!ret)
  2137. {
  2138. cmSystemTools::Error(
  2139. "CMake failed to properly look up cmSourceFile: ", sourceName);
  2140. }
  2141. return ret;
  2142. }
  2143. cmSourceFile* cmMakefile::AddSource(cmSourceFile const&sf)
  2144. {
  2145. // check to see if it exists
  2146. cmSourceFile* ret = this->GetSource(sf.GetFullPath().c_str());
  2147. if(ret)
  2148. {
  2149. return ret;
  2150. }
  2151. ret = new cmSourceFile(sf);
  2152. this->SourceFiles.push_back(ret);
  2153. return ret;
  2154. }
  2155. void cmMakefile::EnableLanguage(std::vector<std::string> const & lang)
  2156. {
  2157. this->AddDefinition("CMAKE_CFG_INTDIR",
  2158. this->LocalGenerator->GetGlobalGenerator()->GetCMakeCFGInitDirectory());
  2159. this->LocalGenerator->GetGlobalGenerator()->EnableLanguage(lang, this);
  2160. }
  2161. void cmMakefile::ExpandSourceListArguments(
  2162. std::vector<std::string> const& arguments,
  2163. std::vector<std::string>& newargs, unsigned int /* start */)
  2164. {
  2165. // now expand the args
  2166. unsigned int i;
  2167. for(i = 0; i < arguments.size(); ++i)
  2168. {
  2169. // List expansion will have been done already.
  2170. newargs.push_back(arguments[i]);
  2171. }
  2172. }
  2173. int cmMakefile::TryCompile(const char *srcdir, const char *bindir,
  2174. const char *projectName, const char *targetName,
  2175. const std::vector<std::string> *cmakeArgs,
  2176. std::string *output)
  2177. {
  2178. // does the binary directory exist ? If not create it...
  2179. if (!cmSystemTools::FileIsDirectory(bindir))
  2180. {
  2181. cmSystemTools::MakeDirectory(bindir);
  2182. }
  2183. // change to the tests directory and run cmake
  2184. // use the cmake object instead of calling cmake
  2185. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  2186. cmSystemTools::ChangeDirectory(bindir);
  2187. // make sure the same generator is used
  2188. // use this program as the cmake to be run, it should not
  2189. // be run that way but the cmake object requires a vailid path
  2190. std::string cmakeCommand = this->GetDefinition("CMAKE_COMMAND");
  2191. cmake cm;
  2192. cm.SetIsInTryCompile(true);
  2193. cmGlobalGenerator *gg = cm.CreateGlobalGenerator
  2194. (this->LocalGenerator->GetGlobalGenerator()->GetName());
  2195. if (!gg)
  2196. {
  2197. cmSystemTools::Error(
  2198. "Internal CMake error, TryCompile bad GlobalGenerator");
  2199. // return to the original directory
  2200. cmSystemTools::ChangeDirectory(cwd.c_str());
  2201. return 1;
  2202. }
  2203. cm.SetGlobalGenerator(gg);
  2204. // do a configure
  2205. cm.SetHomeDirectory(srcdir);
  2206. cm.SetHomeOutputDirectory(bindir);
  2207. cm.SetStartDirectory(srcdir);
  2208. cm.SetStartOutputDirectory(bindir);
  2209. cm.SetCMakeCommand(cmakeCommand.c_str());
  2210. cm.LoadCache();
  2211. // if cmake args were provided then pass them in
  2212. if (cmakeArgs)
  2213. {
  2214. cm.SetCacheArgs(*cmakeArgs);
  2215. }
  2216. // to save time we pass the EnableLanguage info directly
  2217. gg->EnableLanguagesFromGenerator
  2218. (this->LocalGenerator->GetGlobalGenerator());
  2219. if (cm.Configure() != 0)
  2220. {
  2221. cmSystemTools::Error(
  2222. "Internal CMake error, TryCompile configure of cmake failed");
  2223. // return to the original directory
  2224. cmSystemTools::ChangeDirectory(cwd.c_str());
  2225. return 1;
  2226. }
  2227. if (cm.Generate() != 0)
  2228. {
  2229. cmSystemTools::Error(
  2230. "Internal CMake error, TryCompile generation of cmake failed");
  2231. // return to the original directory
  2232. cmSystemTools::ChangeDirectory(cwd.c_str());
  2233. return 1;
  2234. }
  2235. // finally call the generator to actually build the resulting project
  2236. int ret =
  2237. this->LocalGenerator->GetGlobalGenerator()->TryCompile(srcdir,bindir,
  2238. projectName,
  2239. targetName,
  2240. output,
  2241. this);
  2242. cmSystemTools::ChangeDirectory(cwd.c_str());
  2243. return ret;
  2244. }
  2245. cmake *cmMakefile::GetCMakeInstance() const
  2246. {
  2247. if ( this->LocalGenerator && this->LocalGenerator->GetGlobalGenerator() )
  2248. {
  2249. return this->LocalGenerator->GetGlobalGenerator()->GetCMakeInstance();
  2250. }
  2251. return 0;
  2252. }
  2253. #ifdef CMAKE_BUILD_WITH_CMAKE
  2254. cmVariableWatch *cmMakefile::GetVariableWatch() const
  2255. {
  2256. if ( this->GetCMakeInstance() &&
  2257. this->GetCMakeInstance()->GetVariableWatch() )
  2258. {
  2259. return this->GetCMakeInstance()->GetVariableWatch();
  2260. }
  2261. return 0;
  2262. }
  2263. #endif
  2264. void cmMakefile::AddMacro(const char* name, const char* signature)
  2265. {
  2266. if ( !name || !signature )
  2267. {
  2268. return;
  2269. }
  2270. this->MacrosMap[name] = signature;
  2271. }
  2272. void cmMakefile::GetListOfMacros(std::string& macros)
  2273. {
  2274. StringStringMap::iterator it;
  2275. macros = "";
  2276. int cc = 0;
  2277. for ( it = this->MacrosMap.begin(); it != this->MacrosMap.end(); ++it )
  2278. {
  2279. if ( cc > 0 )
  2280. {
  2281. macros += ";";
  2282. }
  2283. macros += it->first;
  2284. cc ++;
  2285. }
  2286. }
  2287. cmCacheManager *cmMakefile::GetCacheManager() const
  2288. {
  2289. return this->GetCMakeInstance()->GetCacheManager();
  2290. }
  2291. void cmMakefile::DisplayStatus(const char* message, float s)
  2292. {
  2293. this->GetLocalGenerator()->GetGlobalGenerator()
  2294. ->GetCMakeInstance()->UpdateProgress(message, s);
  2295. }
  2296. std::string cmMakefile::GetModulesFile(const char* filename)
  2297. {
  2298. std::vector<std::string> modulePath;
  2299. const char* def = this->GetDefinition("CMAKE_MODULE_PATH");
  2300. if(def)
  2301. {
  2302. cmSystemTools::ExpandListArgument(def, modulePath);
  2303. }
  2304. // Also search in the standard modules location.
  2305. def = this->GetDefinition("CMAKE_ROOT");
  2306. if(def)
  2307. {
  2308. std::string rootModules = def;
  2309. rootModules += "/Modules";
  2310. modulePath.push_back(rootModules);
  2311. }
  2312. //std::string Look through the possible module directories.
  2313. for(std::vector<std::string>::iterator i = modulePath.begin();
  2314. i != modulePath.end(); ++i)
  2315. {
  2316. std::string itempl = *i;
  2317. cmSystemTools::ConvertToUnixSlashes(itempl);
  2318. itempl += "/";
  2319. itempl += filename;
  2320. if(cmSystemTools::FileExists(itempl.c_str()))
  2321. {
  2322. return itempl;
  2323. }
  2324. }
  2325. return "";
  2326. }
  2327. void cmMakefile::ConfigureString(const std::string& input,
  2328. std::string& output, bool atOnly,
  2329. bool escapeQuotes)
  2330. {
  2331. // Split input to handle one line at a time.
  2332. std::string::const_iterator lineStart = input.begin();
  2333. while(lineStart != input.end())
  2334. {
  2335. // Find the end of this line.
  2336. std::string::const_iterator lineEnd = lineStart;
  2337. while(lineEnd != input.end() && *lineEnd != '\n')
  2338. {
  2339. ++lineEnd;
  2340. }
  2341. // Copy the line.
  2342. std::string line(lineStart, lineEnd);
  2343. // Skip the newline character.
  2344. bool haveNewline = (lineEnd != input.end());
  2345. if(haveNewline)
  2346. {
  2347. ++lineEnd;
  2348. }
  2349. // Replace #cmakedefine instances.
  2350. if(this->cmDefineRegex.find(line))
  2351. {
  2352. const char* def =
  2353. this->GetDefinition(this->cmDefineRegex.match(1).c_str());
  2354. if(!cmSystemTools::IsOff(def))
  2355. {
  2356. cmSystemTools::ReplaceString(line, "#cmakedefine", "#define");
  2357. output += line;
  2358. }
  2359. else
  2360. {
  2361. cmSystemTools::ReplaceString(line, "#cmakedefine", "#undef");
  2362. output += "/* ";
  2363. output += line;
  2364. output += " */";
  2365. }
  2366. }
  2367. else if(this->cmDefine01Regex.find(line))
  2368. {
  2369. const char* def =
  2370. this->GetDefinition(this->cmDefine01Regex.match(1).c_str());
  2371. cmSystemTools::ReplaceString(line, "#cmakedefine01", "#define");
  2372. output += line;
  2373. if(!cmSystemTools::IsOff(def))
  2374. {
  2375. output += " 1";
  2376. }
  2377. else
  2378. {
  2379. output += " 0";
  2380. }
  2381. }
  2382. else
  2383. {
  2384. output += line;
  2385. }
  2386. if(haveNewline)
  2387. {
  2388. output += "\n";
  2389. }
  2390. // Move to the next line.
  2391. lineStart = lineEnd;
  2392. }
  2393. // Perform variable replacements.
  2394. this->ExpandVariablesInString(output, escapeQuotes, true,
  2395. atOnly, 0, -1, true);
  2396. }
  2397. int cmMakefile::ConfigureFile(const char* infile, const char* outfile,
  2398. bool copyonly, bool atOnly, bool escapeQuotes)
  2399. {
  2400. int res = 1;
  2401. if ( !this->CanIWriteThisFile(outfile) )
  2402. {
  2403. cmSystemTools::Error("Attempt to write file: ",
  2404. outfile, " into a source directory.");
  2405. return 0;
  2406. }
  2407. if ( !cmSystemTools::FileExists(infile) )
  2408. {
  2409. cmSystemTools::Error("File ", infile, " does not exist.");
  2410. return 0;
  2411. }
  2412. std::string soutfile = outfile;
  2413. std::string sinfile = infile;
  2414. this->AddCMakeDependFile(infile);
  2415. cmSystemTools::ConvertToUnixSlashes(soutfile);
  2416. mode_t perm = 0;
  2417. cmSystemTools::GetPermissions(sinfile.c_str(), perm);
  2418. std::string::size_type pos = soutfile.rfind('/');
  2419. if(pos != std::string::npos)
  2420. {
  2421. std::string path = soutfile.substr(0, pos);
  2422. cmSystemTools::MakeDirectory(path.c_str());
  2423. }
  2424. if(copyonly)
  2425. {
  2426. if ( !cmSystemTools::CopyFileIfDifferent(sinfile.c_str(),
  2427. soutfile.c_str()))
  2428. {
  2429. return 0;
  2430. }
  2431. }
  2432. else
  2433. {
  2434. std::string tempOutputFile = soutfile;
  2435. tempOutputFile += ".tmp";
  2436. std::ofstream fout(tempOutputFile.c_str());
  2437. if(!fout)
  2438. {
  2439. cmSystemTools::Error(
  2440. "Could not open file for write in copy operation ",
  2441. tempOutputFile.c_str());
  2442. cmSystemTools::ReportLastSystemError("");
  2443. return 0;
  2444. }
  2445. std::ifstream fin(sinfile.c_str());
  2446. if(!fin)
  2447. {
  2448. cmSystemTools::Error("Could not open file for read in copy operation ",
  2449. sinfile.c_str());
  2450. return 0;
  2451. }
  2452. // now copy input to output and expand variables in the
  2453. // input file at the same time
  2454. std::string inLine;
  2455. std::string outLine;
  2456. while( cmSystemTools::GetLineFromStream(fin, inLine) )
  2457. {
  2458. outLine = "";
  2459. this->ConfigureString(inLine, outLine, atOnly, escapeQuotes);
  2460. fout << outLine.c_str() << "\n";
  2461. }
  2462. // close the files before attempting to copy
  2463. fin.close();
  2464. fout.close();
  2465. if ( !cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  2466. soutfile.c_str()) )
  2467. {
  2468. res = 0;
  2469. }
  2470. else
  2471. {
  2472. cmSystemTools::SetPermissions(soutfile.c_str(), perm);
  2473. }
  2474. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  2475. }
  2476. return res;
  2477. }
  2478. void cmMakefile::AddWrittenFile(const char* file)
  2479. { this->GetCMakeInstance()->AddWrittenFile(file); }
  2480. bool cmMakefile::HasWrittenFile(const char* file)
  2481. { return this->GetCMakeInstance()->HasWrittenFile(file); }
  2482. bool cmMakefile::CheckInfiniteLoops()
  2483. {
  2484. std::vector<std::string>::iterator it;
  2485. for ( it = this->ListFiles.begin();
  2486. it != this->ListFiles.end();
  2487. ++ it )
  2488. {
  2489. if ( this->HasWrittenFile(it->c_str()) )
  2490. {
  2491. cmOStringStream str;
  2492. str << "File " << it->c_str() <<
  2493. " is written by WRITE_FILE (or FILE WRITE) command and should "
  2494. "not be used as input to CMake. Please use CONFIGURE_FILE to "
  2495. "be safe. Refer to the note next to FILE WRITE command.";
  2496. cmSystemTools::Error(str.str().c_str());
  2497. return false;
  2498. }
  2499. }
  2500. return true;
  2501. }
  2502. void cmMakefile::SetProperty(const char* prop, const char* value)
  2503. {
  2504. if (!prop)
  2505. {
  2506. return;
  2507. }
  2508. if (!value)
  2509. {
  2510. value = "NOTFOUND";
  2511. }
  2512. this->Properties[prop] = value;
  2513. }
  2514. const char *cmMakefile::GetProperty(const char* prop)
  2515. {
  2516. // watch for specific properties
  2517. if (!strcmp("PARENT_DIRECTORY",prop))
  2518. {
  2519. return this->LocalGenerator->GetParent()
  2520. ->GetMakefile()->GetStartDirectory();
  2521. }
  2522. // watch for specific properties
  2523. if (!strcmp("LISTFILE_STACK",prop))
  2524. {
  2525. std::string tmp;
  2526. for (std::deque<cmStdString>::iterator i = this->ListFileStack.begin();
  2527. i != this->ListFileStack.end(); ++i)
  2528. {
  2529. if (i != this->ListFileStack.begin())
  2530. {
  2531. tmp += ";";
  2532. }
  2533. tmp += *i;
  2534. }
  2535. this->SetProperty("LISTFILE_STACK",tmp.c_str());
  2536. }
  2537. std::map<cmStdString,cmStdString>::const_iterator i =
  2538. this->Properties.find(prop);
  2539. if (i != this->Properties.end())
  2540. {
  2541. return i->second.c_str();
  2542. }
  2543. return 0;
  2544. }
  2545. bool cmMakefile::GetPropertyAsBool(const char* prop) const
  2546. {
  2547. std::map<cmStdString,cmStdString>::const_iterator i =
  2548. this->Properties.find(prop);
  2549. if (i != this->Properties.end())
  2550. {
  2551. return cmSystemTools::IsOn(i->second.c_str());
  2552. }
  2553. return false;
  2554. }
  2555. cmTarget* cmMakefile::FindTarget(const char* name)
  2556. {
  2557. cmTargets& tgts = this->GetTargets();
  2558. cmTargets::iterator i = tgts.find(name);
  2559. if (i == tgts.end())
  2560. {
  2561. return 0;
  2562. }
  2563. return &i->second;
  2564. }
  2565. cmTest* cmMakefile::CreateTest(const char* testName)
  2566. {
  2567. if ( !testName )
  2568. {
  2569. return 0;
  2570. }
  2571. cmTest* test = this->GetTest(testName);
  2572. if ( test )
  2573. {
  2574. return test;
  2575. }
  2576. test = new cmTest;
  2577. test->SetName(testName);
  2578. this->Tests.push_back(test);
  2579. return test;
  2580. }
  2581. cmTest* cmMakefile::GetTest(const char* testName) const
  2582. {
  2583. if ( !testName )
  2584. {
  2585. return 0;
  2586. }
  2587. std::vector<cmTest*>::const_iterator it;
  2588. for ( it = this->Tests.begin(); it != this->Tests.end(); ++ it )
  2589. {
  2590. if ( strcmp((*it)->GetName(), testName) == 0 )
  2591. {
  2592. return *it;
  2593. }
  2594. }
  2595. return 0;
  2596. }
  2597. const std::vector<cmTest*> *cmMakefile::GetTests() const
  2598. {
  2599. return &this->Tests;
  2600. }
  2601. std::vector<cmTest*> *cmMakefile::GetTests()
  2602. {
  2603. return &this->Tests;
  2604. }
  2605. std::string cmMakefile::GetListFileStack()
  2606. {
  2607. std::string tmp;
  2608. for (std::deque<cmStdString>::iterator i = this->ListFileStack.begin();
  2609. i != this->ListFileStack.end(); ++i)
  2610. {
  2611. if (i != this->ListFileStack.begin())
  2612. {
  2613. tmp += ";";
  2614. }
  2615. tmp += *i;
  2616. }
  2617. return tmp;
  2618. }