cmMakefile.cxx 80 KB

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