cmMakefile.cxx 80 KB

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