cmMakefile.cxx 81 KB

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