cmMakefile.cxx 80 KB

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