cmMakefile.cxx 80 KB

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