cmMakefile.cxx 82 KB

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