cmMakefile.cxx 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063
  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 "cmStandardIncludes.h"
  16. #include "cmSourceFile.h"
  17. #include "cmSystemTools.h"
  18. #include "cmGlobalGenerator.h"
  19. #include "cmLocalGenerator.h"
  20. #include "cmCommands.h"
  21. #include "cmCacheManager.h"
  22. #include "cmFunctionBlocker.h"
  23. #include "cmListFileCache.h"
  24. #include "cmVariableWatch.h"
  25. #include "cmake.h"
  26. #include <stdio.h> // required for sprintf
  27. #include <stdlib.h> // required for atoi
  28. #include <cmsys/RegularExpression.hxx>
  29. // default is not to be building executables
  30. cmMakefile::cmMakefile()
  31. {
  32. // Setup the default include file regular expression (match everything).
  33. m_IncludeFileRegularExpression = "^.*$";
  34. // Setup the default include complaint regular expression (match nothing).
  35. m_ComplainFileRegularExpression = "^$";
  36. // Source and header file extensions that we can handle
  37. // The "c" extension MUST precede the "C" extension.
  38. m_SourceFileExtensions.push_back( "c" );
  39. m_SourceFileExtensions.push_back( "C" );
  40. m_SourceFileExtensions.push_back( "c++" );
  41. m_SourceFileExtensions.push_back( "cc" );
  42. m_SourceFileExtensions.push_back( "cpp" );
  43. m_SourceFileExtensions.push_back( "cxx" );
  44. m_SourceFileExtensions.push_back( "m" );
  45. m_SourceFileExtensions.push_back( "M" );
  46. m_SourceFileExtensions.push_back( "mm" );
  47. m_HeaderFileExtensions.push_back( "h" );
  48. m_HeaderFileExtensions.push_back( "h++" );
  49. m_HeaderFileExtensions.push_back( "hm" );
  50. m_HeaderFileExtensions.push_back( "hpp" );
  51. m_HeaderFileExtensions.push_back( "hxx" );
  52. m_HeaderFileExtensions.push_back( "in" );
  53. m_HeaderFileExtensions.push_back( "txx" );
  54. m_DefineFlags = " ";
  55. m_LocalGenerator = 0;
  56. this->AddSourceGroup("", "^.*$");
  57. this->AddSourceGroup("Source Files",
  58. "\\.(C|M|c|c\\+\\+|cc|cpp|cxx|m|mm|rc|def|r|odl|idl|hpj|bat)$");
  59. this->AddSourceGroup("Header Files", "\\.(h|h\\+\\+|hm|hpp|hxx|in|txx|inl)$");
  60. this->AddSourceGroup("CMake Rules", "\\.rule$");
  61. this->AddDefaultDefinitions();
  62. }
  63. const char* cmMakefile::GetReleaseVersion()
  64. {
  65. #if CMake_VERSION_MINOR & 1
  66. return "development";
  67. #else
  68. return "patch " CMAKE_TO_STRING(CMake_VERSION_PATCH);
  69. #endif
  70. }
  71. unsigned int cmMakefile::GetCacheMajorVersion()
  72. {
  73. if(!this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"))
  74. {
  75. return 0;
  76. }
  77. return atoi(this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"));
  78. }
  79. unsigned int cmMakefile::GetCacheMinorVersion()
  80. {
  81. if(!this->GetCacheManager()->GetCacheValue("Cmake_Cache_MINOR_VERSION"))
  82. {
  83. return 0;
  84. }
  85. return atoi(this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MINOR_VERSION"));
  86. }
  87. cmMakefile::~cmMakefile()
  88. {
  89. for(std::vector<cmSourceFile*>::iterator i = m_SourceFiles.begin();
  90. i != m_SourceFiles.end(); ++i)
  91. {
  92. delete *i;
  93. }
  94. for(unsigned int i=0; i < m_UsedCommands.size(); i++)
  95. {
  96. delete m_UsedCommands[i];
  97. }
  98. for(DataMap::const_iterator d = m_DataMap.begin();
  99. d != m_DataMap.end(); ++d)
  100. {
  101. if(d->second)
  102. {
  103. delete d->second;
  104. }
  105. }
  106. std::list<cmFunctionBlocker *>::iterator pos;
  107. for (pos = m_FunctionBlockers.begin();
  108. pos != m_FunctionBlockers.end(); ++pos)
  109. {
  110. cmFunctionBlocker* b = *pos;
  111. delete b;
  112. }
  113. m_FunctionBlockers.clear();
  114. }
  115. void cmMakefile::PrintStringVector(const char* s, const std::vector<std::string>& v) const
  116. {
  117. std::cout << s << ": ( \n";
  118. for(std::vector<std::string>::const_iterator i = v.begin();
  119. i != v.end(); ++i)
  120. {
  121. std::cout << (*i).c_str() << " ";
  122. }
  123. std::cout << " )\n";
  124. }
  125. // call print on all the classes in the makefile
  126. void cmMakefile::Print() const
  127. {
  128. // print the class lists
  129. std::cout << "classes:\n";
  130. std::cout << " m_Targets: ";
  131. for (cmTargets::const_iterator l = m_Targets.begin();
  132. l != m_Targets.end(); l++)
  133. {
  134. std::cout << l->first << std::endl;
  135. }
  136. std::cout << " m_CurrentOutputDirectory; " <<
  137. m_CurrentOutputDirectory.c_str() << std::endl;
  138. std::cout << " m_StartOutputDirectory; " <<
  139. m_StartOutputDirectory.c_str() << std::endl;
  140. std::cout << " m_HomeOutputDirectory; " <<
  141. m_HomeOutputDirectory.c_str() << std::endl;
  142. std::cout << " m_cmCurrentDirectory; " <<
  143. m_cmCurrentDirectory.c_str() << std::endl;
  144. std::cout << " m_cmStartDirectory; " <<
  145. m_cmStartDirectory.c_str() << std::endl;
  146. std::cout << " m_cmHomeDirectory; " <<
  147. m_cmHomeDirectory.c_str() << std::endl;
  148. std::cout << " m_ProjectName; " << m_ProjectName.c_str() << std::endl;
  149. this->PrintStringVector("m_SubDirectories ", m_SubDirectories);
  150. this->PrintStringVector("m_IncludeDirectories;", m_IncludeDirectories);
  151. this->PrintStringVector("m_LinkDirectories", m_LinkDirectories);
  152. for( std::vector<cmSourceGroup>::const_iterator i = m_SourceGroups.begin();
  153. i != m_SourceGroups.end(); ++i)
  154. {
  155. std::cout << "Source Group: " << i->GetName() << std::endl;
  156. }
  157. }
  158. bool cmMakefile::CommandExists(const char* name) const
  159. {
  160. return this->GetCMakeInstance()->CommandExists(name);
  161. }
  162. bool cmMakefile::ExecuteCommand(const cmListFileFunction& lff)
  163. {
  164. bool result = true;
  165. // quick return if blocked
  166. if(this->IsFunctionBlocked(lff))
  167. {
  168. // No error.
  169. return result;
  170. }
  171. std::string name = lff.m_Name;
  172. // execute the command
  173. cmCommand *rm =
  174. this->GetCMakeInstance()->GetCommand(name.c_str());
  175. if(rm)
  176. {
  177. cmCommand* usedCommand = rm->Clone();
  178. usedCommand->SetMakefile(this);
  179. bool keepCommand = false;
  180. if(usedCommand->GetEnabled() && !cmSystemTools::GetFatalErrorOccured())
  181. {
  182. // if not running in inherit mode or
  183. // if the command is inherited then InitialPass it.
  184. if(!m_Inheriting || usedCommand->IsInherited())
  185. {
  186. if(!usedCommand->InvokeInitialPass(lff.m_Arguments))
  187. {
  188. cmOStringStream error;
  189. error << "Error in cmake code at\n"
  190. << lff.m_FilePath << ":" << lff.m_Line << ":\n"
  191. << usedCommand->GetError();
  192. cmSystemTools::Error(error.str().c_str());
  193. result = false;
  194. }
  195. else
  196. {
  197. // use the command
  198. keepCommand = true;
  199. m_UsedCommands.push_back(usedCommand);
  200. }
  201. }
  202. }
  203. // if the Cloned command was not used
  204. // then delete it
  205. if(!keepCommand)
  206. {
  207. delete usedCommand;
  208. }
  209. }
  210. else
  211. {
  212. if(!cmSystemTools::GetFatalErrorOccured())
  213. {
  214. cmOStringStream error;
  215. error << "Error in cmake code at\n"
  216. << lff.m_FilePath << ":" << lff.m_Line << ":\n"
  217. << "Unknown CMake command \"" << lff.m_Name.c_str() << "\".";
  218. cmSystemTools::Error(error.str().c_str());
  219. result = false;
  220. }
  221. }
  222. return result;
  223. }
  224. // Parse the given CMakeLists.txt file into a list of classes.
  225. // Reads in current CMakeLists file and all parent CMakeLists files
  226. // executing all inherited commands in the parents
  227. //
  228. // if external is non-zero, this means that we have branched to grab some
  229. // commands from a remote list-file (that is, the equivalent of a
  230. // #include has been called). We DO NOT look at the parents of this
  231. // list-file, and for all other purposes, the name of this list-file
  232. // is "filename" and not "external".
  233. bool cmMakefile::ReadListFile(const char* filename_in, const char* external_in)
  234. {
  235. // used to watch for blockers going out of scope
  236. // e.g. mismatched IF statement
  237. std::set<cmFunctionBlocker *> originalBlockers;
  238. const char* external = 0;
  239. std::string external_abs;
  240. const char* filename = filename_in;
  241. std::string filename_abs;
  242. if (external_in)
  243. {
  244. external_abs =
  245. cmSystemTools::CollapseFullPath(external_in,
  246. m_cmCurrentDirectory.c_str());
  247. external = external_abs.c_str();
  248. if (filename_in)
  249. {
  250. filename_abs =
  251. cmSystemTools::CollapseFullPath(filename_in,
  252. m_cmCurrentDirectory.c_str());
  253. filename = filename_abs.c_str();
  254. }
  255. }
  256. // keep track of the current file being read
  257. if (filename)
  258. {
  259. if(m_cmCurrentListFile != filename)
  260. {
  261. m_cmCurrentListFile = filename;
  262. }
  263. // loop over current function blockers and record them
  264. std::list<cmFunctionBlocker *>::iterator pos;
  265. for (pos = m_FunctionBlockers.begin();
  266. pos != m_FunctionBlockers.end(); ++pos)
  267. {
  268. originalBlockers.insert(*pos);
  269. }
  270. }
  271. // if this is not a remote makefile
  272. // (if it were, this would be called from the "filename" call,
  273. // rather than the "external" call)
  274. if (!external)
  275. {
  276. // is there a parent CMakeLists file that does not go beyond the
  277. // Home directory? if so recurse and read in that List file
  278. std::string parentList = this->GetParentListFileName(filename);
  279. if (parentList != "")
  280. {
  281. std::string srcdir = m_cmCurrentDirectory;
  282. std::string bindir = m_CurrentOutputDirectory;
  283. std::string::size_type pos = parentList.rfind('/');
  284. m_cmCurrentDirectory = parentList.substr(0, pos);
  285. m_CurrentOutputDirectory = m_HomeOutputDirectory + parentList.substr(m_cmHomeDirectory.size(), pos - m_cmHomeDirectory.size());
  286. // if not found, oops
  287. if(pos == std::string::npos)
  288. {
  289. cmSystemTools::Error("Trailing slash not found");
  290. }
  291. this->ReadListFile(parentList.c_str());
  292. // restore the current directory
  293. m_cmCurrentDirectory = srcdir;
  294. m_CurrentOutputDirectory = bindir;
  295. }
  296. }
  297. // are we at the start CMakeLists file or are we processing a parent
  298. // lists file
  299. //
  300. // this might, or might not be true, irrespective if we are
  301. // off looking at an external makefile.
  302. m_Inheriting = (m_cmCurrentDirectory != m_cmStartDirectory);
  303. // Now read the input file
  304. const char *filenametoread= filename;
  305. if( external)
  306. {
  307. filenametoread= external;
  308. }
  309. // try to see if the list file is the top most
  310. // list file for a project, and if it is, then it
  311. // must have a project command. If there is not
  312. // one, then cmake will provide one via the
  313. // cmListFileCache class.
  314. bool requireProjectCommand = false;
  315. if(!external && m_cmCurrentDirectory == m_cmHomeDirectory)
  316. {
  317. if(cmSystemTools::LowerCase(
  318. cmSystemTools::GetFilenameName(filename)) == "cmakelists.txt")
  319. {
  320. requireProjectCommand = true;
  321. }
  322. }
  323. cmListFile* lf =
  324. cmListFileCache::GetInstance()->GetFileCache(filenametoread,
  325. requireProjectCommand);
  326. if(!lf)
  327. {
  328. return false;
  329. }
  330. // add this list file to the list of dependencies
  331. m_ListFiles.push_back( filenametoread);
  332. const size_t numberFunctions = lf->m_Functions.size();
  333. for(size_t i =0; i < numberFunctions; ++i)
  334. {
  335. this->ExecuteCommand(lf->m_Functions[i]);
  336. }
  337. // send scope ended to and funciton blockers
  338. if (filename)
  339. {
  340. // loop over all function blockers to see if any block this command
  341. std::list<cmFunctionBlocker *>::iterator pos;
  342. for (pos = m_FunctionBlockers.begin();
  343. pos != m_FunctionBlockers.end(); ++pos)
  344. {
  345. // if this blocker was not in the original then send a
  346. // scope ended message
  347. if (originalBlockers.find(*pos) == originalBlockers.end())
  348. {
  349. (*pos)->ScopeEnded(*this);
  350. }
  351. }
  352. }
  353. return true;
  354. }
  355. void cmMakefile::AddCommand(cmCommand* wg)
  356. {
  357. this->GetCMakeInstance()->AddCommand(wg);
  358. }
  359. // Set the make file
  360. void cmMakefile::SetLocalGenerator(cmLocalGenerator* lg)
  361. {
  362. m_LocalGenerator = lg;
  363. }
  364. void cmMakefile::FinalPass()
  365. {
  366. // do all the variable expansions here
  367. this->ExpandVariables();
  368. // give all the commands a chance to do something
  369. // after the file has been parsed before generation
  370. for(std::vector<cmCommand*>::iterator i = m_UsedCommands.begin();
  371. i != m_UsedCommands.end(); ++i)
  372. {
  373. (*i)->FinalPass();
  374. }
  375. }
  376. // Generate the output file
  377. void cmMakefile::ConfigureFinalPass()
  378. {
  379. this->FinalPass();
  380. const char* versionValue
  381. = this->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  382. bool oldVersion = (!versionValue || atof(versionValue) < 1.4);
  383. // merge libraries
  384. for (cmTargets::iterator l = m_Targets.begin();
  385. l != m_Targets.end(); l++)
  386. {
  387. l->second.GenerateSourceFilesFromSourceLists(*this);
  388. // pick up any LINK_LIBRARIES that were added after the target
  389. if(oldVersion)
  390. {
  391. this->AddGlobalLinkInformation(l->first.c_str(), l->second);
  392. }
  393. l->second.AnalyzeLibDependencies(*this);
  394. }
  395. }
  396. // this is the old style signature, we convert to new style
  397. void cmMakefile::AddCustomCommand(const char* source,
  398. const char* command,
  399. const std::vector<std::string>& commandArgs,
  400. const std::vector<std::string>& depends,
  401. const std::vector<std::string>& outputs,
  402. const char *target,
  403. const char *comment)
  404. {
  405. if (strcmp(source,target))
  406. {
  407. // what a pain, for backwards compatibility we will try to
  408. // convert this to an output based rule... so for each output..
  409. for(std::vector<std::string>::const_iterator d = outputs.begin();
  410. d != outputs.end(); ++d)
  411. {
  412. // if this looks like a real file then use is as the main depend
  413. cmsys::RegularExpression SourceFiles("\\.(C|M|c|c\\+\\+|cc|cpp|cxx|m|mm|rc|def|r|odl|idl|hpj|bat|h|h\\+\\+|hm|hpp|hxx|in|txx|inl)$");
  414. if (SourceFiles.find(source))
  415. {
  416. this->AddCustomCommandToOutput(d->c_str(), command, commandArgs,
  417. source, depends, comment);
  418. }
  419. // otherwise do not use a main depend
  420. else
  421. {
  422. std::vector<std::string> depends2 = depends;
  423. depends2.push_back(source);
  424. this->AddCustomCommandToOutput(d->c_str(), command, commandArgs,
  425. 0, depends2, comment);
  426. }
  427. // add the output to the target?
  428. std::string sname = *d;
  429. sname += ".rule";
  430. this->ExpandVariablesInString(sname);
  431. // if the rule was added to the source,
  432. // then add the source to the target
  433. if (!this->GetSource(sname.c_str()))
  434. {
  435. if (m_Targets.find(target) != m_Targets.end())
  436. {
  437. m_Targets[target].GetSourceLists().push_back(source);
  438. }
  439. else
  440. {
  441. cmSystemTools::Error("Attempt to add a custom rule to a target that does not exist yet for target ", target);
  442. return;
  443. }
  444. }
  445. }
  446. }
  447. else
  448. {
  449. this->AddCustomCommandToTarget(target, command, commandArgs,
  450. cmTarget::POST_BUILD, comment, depends);
  451. }
  452. }
  453. void cmMakefile::AddCustomCommand(const char* source,
  454. const char* command,
  455. const std::vector<std::string>& commandArgs,
  456. const std::vector<std::string>& depends,
  457. const char* output,
  458. const char *target)
  459. {
  460. std::vector<std::string> outputs;
  461. outputs.push_back(output);
  462. this->AddCustomCommand(source, command, commandArgs, depends,
  463. outputs, target);
  464. }
  465. void cmMakefile::
  466. AddCustomCommandToOutput(const char* output,
  467. const char* command,
  468. const std::vector<std::string>& commandArgs,
  469. const char *main_dependency,
  470. const std::vector<std::string>& depends,
  471. const char *comment,
  472. bool replace)
  473. {
  474. std::string expandC;
  475. std::string combinedArgs;
  476. unsigned int i;
  477. for (i = 0; i < commandArgs.size(); ++i)
  478. {
  479. expandC = commandArgs[i].c_str();
  480. this->ExpandVariablesInString(expandC);
  481. combinedArgs += cmSystemTools::EscapeSpaces(expandC.c_str());
  482. combinedArgs += " ";
  483. }
  484. cmSourceFile *file = 0;
  485. // setup the output name and make sure we expand any variables
  486. std::string outName = output;
  487. outName += ".rule";
  488. this->ExpandVariablesInString(outName);
  489. // setup the main dependency name and expand vars of course
  490. std::string mainDepend;
  491. if (main_dependency && main_dependency[0] != '\0')
  492. {
  493. mainDepend = main_dependency;
  494. this->ExpandVariablesInString(mainDepend);
  495. }
  496. // OK this rule will be placed on a generated output file unless the main
  497. // depednency was specified.
  498. if (main_dependency && main_dependency[0] != '\0')
  499. {
  500. file = this->GetSource(mainDepend.c_str());
  501. if (file && file->GetCustomCommand() && !replace)
  502. {
  503. cmCustomCommand* cc = file->GetCustomCommand();
  504. // if the command and args are the same
  505. // as the command already there, then silently skip
  506. // this add command
  507. if(cc->IsEquivalent(command, combinedArgs.c_str()))
  508. {
  509. return;
  510. }
  511. // generate a source instead
  512. file = 0;
  513. }
  514. else
  515. {
  516. file = this->GetOrCreateSource(mainDepend.c_str());
  517. }
  518. }
  519. if (!file)
  520. {
  521. file = this->GetSource(outName.c_str());
  522. if (file && file->GetCustomCommand() && !replace)
  523. {
  524. cmCustomCommand* cc = file->GetCustomCommand();
  525. // if the command and args are the same
  526. // as the command already there, then silently skip
  527. // this add command
  528. if(cc->IsEquivalent(command, combinedArgs.c_str()))
  529. {
  530. return;
  531. }
  532. // produce error if two different commands are given to produce
  533. // the same output
  534. cmSystemTools::Error("Attempt to add a custom rule to an output that already"
  535. " has a custom rule. For output: ", output);
  536. return;
  537. }
  538. // create a cmSourceFile for the output
  539. file = this->GetOrCreateSource(outName.c_str(), true);
  540. // always mark as generated
  541. file->SetProperty("GENERATED","1");
  542. }
  543. // always create the output and mark it generated
  544. cmSourceFile *out = this->GetOrCreateSource(output, true);
  545. out->SetProperty("GENERATED","1");
  546. // process the command
  547. expandC = command;
  548. this->ExpandVariablesInString(expandC);
  549. std::string c = cmSystemTools::EscapeSpaces(expandC.c_str());
  550. std::vector<std::string> depends2(depends);
  551. if (main_dependency && main_dependency[0] != '\0')
  552. {
  553. depends2.push_back(mainDepend.c_str());
  554. }
  555. cmCustomCommand *cc =
  556. new cmCustomCommand(c.c_str(),combinedArgs.c_str(),depends2, output);
  557. if ( comment && comment[0] )
  558. {
  559. cc->SetComment(comment);
  560. }
  561. if (file->GetCustomCommand())
  562. {
  563. delete file->GetCustomCommand();
  564. }
  565. file->SetCustomCommand(cc);
  566. }
  567. void cmMakefile::
  568. AddCustomCommandToTarget(const char* target, const char* command,
  569. const std::vector<std::string>& commandArgs,
  570. cmTarget::CustomCommandType type,
  571. const char *comment)
  572. {
  573. std::vector<std::string> empty;
  574. this->AddCustomCommandToTarget(target,command,commandArgs,type,
  575. comment, empty);
  576. }
  577. void cmMakefile::
  578. AddCustomCommandToTarget(const char* target, const char* command,
  579. const std::vector<std::string>& commandArgs,
  580. cmTarget::CustomCommandType type,
  581. const char *comment,
  582. const std::vector<std::string>& depends)
  583. {
  584. // find the target,
  585. if (m_Targets.find(target) != m_Targets.end())
  586. {
  587. std::string expandC = command;
  588. this->ExpandVariablesInString(expandC);
  589. std::string c = cmSystemTools::EscapeSpaces(expandC.c_str());
  590. std::string combinedArgs;
  591. unsigned int i;
  592. for (i = 0; i < commandArgs.size(); ++i)
  593. {
  594. expandC = commandArgs[i].c_str();
  595. this->ExpandVariablesInString(expandC);
  596. combinedArgs += cmSystemTools::EscapeSpaces(expandC.c_str());
  597. combinedArgs += " ";
  598. }
  599. cmCustomCommand cc(c.c_str(),combinedArgs.c_str(),depends,0);
  600. if ( comment && comment[0] )
  601. {
  602. cc.SetComment(comment);
  603. }
  604. switch (type)
  605. {
  606. case cmTarget::PRE_BUILD:
  607. m_Targets[target].GetPreBuildCommands().push_back(cc);
  608. break;
  609. case cmTarget::PRE_LINK:
  610. m_Targets[target].GetPreLinkCommands().push_back(cc);
  611. break;
  612. case cmTarget::POST_BUILD:
  613. m_Targets[target].GetPostBuildCommands().push_back(cc);
  614. break;
  615. }
  616. std::string cacheCommand = command;
  617. this->ExpandVariablesInString(cacheCommand);
  618. if(this->GetCacheManager()->GetCacheValue(cacheCommand.c_str()))
  619. {
  620. m_Targets[target].AddUtility(
  621. this->GetCacheManager()->GetCacheValue(cacheCommand.c_str()));
  622. }
  623. }
  624. }
  625. void cmMakefile::AddDefineFlag(const char* flag)
  626. {
  627. m_DefineFlags += " ";
  628. m_DefineFlags += flag;
  629. }
  630. void cmMakefile::AddLinkLibrary(const char* lib, cmTarget::LinkLibraryType llt)
  631. {
  632. m_LinkLibraries.push_back(
  633. std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt));
  634. }
  635. void cmMakefile::AddLinkLibraryForTarget(const char *target,
  636. const char* lib,
  637. cmTarget::LinkLibraryType llt)
  638. {
  639. cmTargets::iterator i = m_Targets.find(target);
  640. if ( i != m_Targets.end())
  641. {
  642. i->second.AddLinkLibrary( *this, target, lib, llt );
  643. }
  644. else
  645. {
  646. cmSystemTools::Error("Attempt to add link libraries to non-existant target: ", target, " for lib ", lib);
  647. }
  648. }
  649. void cmMakefile::AddLinkDirectoryForTarget(const char *target,
  650. const char* d)
  651. {
  652. cmTargets::iterator i = m_Targets.find(target);
  653. if ( i != m_Targets.end())
  654. {
  655. i->second.AddLinkDirectory( d );
  656. }
  657. else
  658. {
  659. cmSystemTools::Error("Attempt to add link directories to non-existant target: ",
  660. target, " for directory ", d);
  661. }
  662. }
  663. void cmMakefile::AddLinkLibrary(const char* lib)
  664. {
  665. this->AddLinkLibrary(lib,cmTarget::GENERAL);
  666. }
  667. void cmMakefile::AddLinkDirectory(const char* dir)
  668. {
  669. // Don't add a link directory that is already present. Yes, this
  670. // linear search results in n^2 behavior, but n won't be getting
  671. // much bigger than 20. We cannot use a set because of order
  672. // dependency of the link search path.
  673. // remove trailing slashes
  674. if(dir && dir[strlen(dir)-1] == '/')
  675. {
  676. std::string newdir = dir;
  677. newdir = newdir.substr(0, newdir.size()-1);
  678. if(std::find(m_LinkDirectories.begin(),
  679. m_LinkDirectories.end(), newdir.c_str()) == m_LinkDirectories.end())
  680. {
  681. m_LinkDirectories.push_back(newdir);
  682. }
  683. }
  684. else
  685. {
  686. if(std::find(m_LinkDirectories.begin(),
  687. m_LinkDirectories.end(), dir) == m_LinkDirectories.end())
  688. {
  689. m_LinkDirectories.push_back(dir);
  690. }
  691. }
  692. }
  693. void cmMakefile::AddSubDirectory(const char* sub)
  694. {
  695. // make sure it isn't already there
  696. if (std::find(m_SubDirectories.begin(),
  697. m_SubDirectories.end(), sub) == m_SubDirectories.end())
  698. {
  699. m_SubDirectories.push_back(sub);
  700. }
  701. }
  702. void cmMakefile::AddIncludeDirectory(const char* inc, bool before)
  703. {
  704. // Don't add an include directory that is already present. Yes,
  705. // this linear search results in n^2 behavior, but n won't be
  706. // getting much bigger than 20. We cannot use a set because of
  707. // order dependency of the include path.
  708. if(std::find(m_IncludeDirectories.begin(),
  709. m_IncludeDirectories.end(), inc) == m_IncludeDirectories.end())
  710. {
  711. if (before)
  712. {
  713. // WARNING: this *is* expensive (linear time) since it's a vector
  714. m_IncludeDirectories.insert(m_IncludeDirectories.begin(), inc);
  715. }
  716. else
  717. {
  718. m_IncludeDirectories.push_back(inc);
  719. }
  720. }
  721. }
  722. void cmMakefile::AddDefinition(const char* name, const char* value)
  723. {
  724. if (!value )
  725. {
  726. return;
  727. }
  728. m_TemporaryDefinitionKey = name;
  729. m_Definitions[m_TemporaryDefinitionKey] = value;
  730. cmVariableWatch* vv = this->GetVariableWatch();
  731. if ( vv )
  732. {
  733. vv->VariableAccessed(m_TemporaryDefinitionKey,
  734. cmVariableWatch::VARIABLE_MODIFIED_ACCESS);
  735. }
  736. }
  737. void cmMakefile::AddCacheDefinition(const char* name, const char* value,
  738. const char* doc,
  739. cmCacheManager::CacheEntryType type)
  740. {
  741. this->GetCacheManager()->AddCacheEntry(name, value, doc, type);
  742. this->AddDefinition(name, value);
  743. }
  744. void cmMakefile::AddDefinition(const char* name, bool value)
  745. {
  746. if(value)
  747. {
  748. m_Definitions.erase( DefinitionMap::key_type(name));
  749. m_Definitions.insert(DefinitionMap::value_type(name, "ON"));
  750. }
  751. else
  752. {
  753. m_Definitions.erase( DefinitionMap::key_type(name));
  754. m_Definitions.insert(DefinitionMap::value_type(name, "OFF"));
  755. }
  756. cmVariableWatch* vv = this->GetVariableWatch();
  757. if ( vv )
  758. {
  759. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_MODIFIED_ACCESS);
  760. }
  761. }
  762. void cmMakefile::AddCacheDefinition(const char* name, bool value, const char* doc)
  763. {
  764. this->GetCacheManager()->AddCacheEntry(name, value, doc);
  765. this->AddDefinition(name, value);
  766. }
  767. void cmMakefile::RemoveDefinition(const char* name)
  768. {
  769. m_Definitions.erase(DefinitionMap::key_type(name));
  770. cmVariableWatch* vv = this->GetVariableWatch();
  771. if ( vv )
  772. {
  773. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_REMOVED_ACCESS);
  774. }
  775. }
  776. void cmMakefile::SetProjectName(const char* p)
  777. {
  778. m_ProjectName = p;
  779. }
  780. void cmMakefile::AddGlobalLinkInformation(const char* name, cmTarget& target)
  781. {
  782. // for these targets do not add anything
  783. switch(target.GetType())
  784. {
  785. case cmTarget::UTILITY:
  786. case cmTarget::INSTALL_FILES:
  787. case cmTarget::INSTALL_PROGRAMS:
  788. return;
  789. default:;
  790. }
  791. std::vector<std::string>::iterator j;
  792. for(j = m_LinkDirectories.begin();
  793. j != m_LinkDirectories.end(); ++j)
  794. {
  795. target.AddLinkDirectory(j->c_str());
  796. }
  797. target.MergeLinkLibraries( *this, name, m_LinkLibraries );
  798. }
  799. void cmMakefile::AddLibrary(const char* lname, int shared,
  800. const std::vector<std::string> &srcs)
  801. {
  802. cmTarget target;
  803. switch (shared)
  804. {
  805. case 0:
  806. target.SetType(cmTarget::STATIC_LIBRARY);
  807. break;
  808. case 1:
  809. target.SetType(cmTarget::SHARED_LIBRARY);
  810. break;
  811. case 2:
  812. target.SetType(cmTarget::MODULE_LIBRARY);
  813. break;
  814. default:
  815. target.SetType(cmTarget::STATIC_LIBRARY);
  816. }
  817. // Clear its dependencies. Otherwise, dependencies might persist
  818. // over changes in CMakeLists.txt, making the information stale and
  819. // hence useless.
  820. target.ClearDependencyInformation( *this, lname );
  821. target.SetInAll(true);
  822. target.GetSourceLists() = srcs;
  823. this->AddGlobalLinkInformation(lname, target);
  824. m_Targets.insert(cmTargets::value_type(lname,target));
  825. // Add an entry into the cache
  826. std::string libPath = lname;
  827. libPath += "_CMAKE_PATH";
  828. this->GetCacheManager()->
  829. AddCacheEntry(libPath.c_str(),
  830. this->GetCurrentOutputDirectory(),
  831. "Path to a library", cmCacheManager::INTERNAL);
  832. // Add an entry into the cache
  833. std::string ltname = lname;
  834. ltname += "_LIBRARY_TYPE";
  835. switch (shared)
  836. {
  837. case 0:
  838. this->GetCacheManager()->AddCacheEntry(ltname.c_str(),"STATIC",
  839. "Whether a library is static, shared or module.",
  840. cmCacheManager::INTERNAL);
  841. break;
  842. case 1:
  843. this->GetCacheManager()->
  844. AddCacheEntry(ltname.c_str(),
  845. "SHARED",
  846. "Whether a library is static, shared or module.",
  847. cmCacheManager::INTERNAL);
  848. break;
  849. case 2:
  850. this->GetCacheManager()->
  851. AddCacheEntry(ltname.c_str(),
  852. "MODULE",
  853. "Whether a library is static, shared or module.",
  854. cmCacheManager::INTERNAL);
  855. break;
  856. default:
  857. this->GetCacheManager()->
  858. AddCacheEntry(ltname.c_str(),
  859. "STATIC",
  860. "Whether a library is static, shared or module.",
  861. cmCacheManager::INTERNAL);
  862. }
  863. }
  864. void cmMakefile::AddExecutable(const char *exeName,
  865. const std::vector<std::string> &srcs)
  866. {
  867. this->AddExecutable(exeName,srcs,false);
  868. }
  869. void cmMakefile::AddExecutable(const char *exeName,
  870. const std::vector<std::string> &srcs,
  871. bool win32)
  872. {
  873. cmTarget target;
  874. if (win32)
  875. {
  876. target.SetType(cmTarget::WIN32_EXECUTABLE);
  877. }
  878. else
  879. {
  880. target.SetType(cmTarget::EXECUTABLE);
  881. }
  882. target.SetInAll(true);
  883. target.GetSourceLists() = srcs;
  884. this->AddGlobalLinkInformation(exeName, target);
  885. m_Targets.insert(cmTargets::value_type(exeName,target));
  886. // Add an entry into the cache
  887. std::string exePath = exeName;
  888. exePath += "_CMAKE_PATH";
  889. this->GetCacheManager()->
  890. AddCacheEntry(exePath.c_str(),
  891. this->GetCurrentOutputDirectory(),
  892. "Path to an executable", cmCacheManager::INTERNAL);
  893. }
  894. void cmMakefile::AddUtilityCommand(const char* utilityName,
  895. const char* command,
  896. const char* arguments,
  897. bool all,
  898. const std::vector<std::string> &depends)
  899. {
  900. std::vector<std::string> empty;
  901. this->AddUtilityCommand(utilityName,command,arguments,all,
  902. depends, empty);
  903. }
  904. void cmMakefile::AddUtilityCommand(const char* utilityName,
  905. const char* command,
  906. const char* arguments,
  907. bool all,
  908. const std::vector<std::string> &dep,
  909. const std::vector<std::string> &out)
  910. {
  911. cmTarget target;
  912. target.SetType(cmTarget::UTILITY);
  913. target.SetInAll(all);
  914. if (out.size() > 1)
  915. {
  916. cmSystemTools::Error(
  917. "Utility targets can only have one output. For utilityNamed: ",
  918. utilityName);
  919. return;
  920. }
  921. if (out.size())
  922. {
  923. cmCustomCommand cc(command, arguments, dep, out[0].c_str());
  924. target.GetPostBuildCommands().push_back(cc);
  925. }
  926. else
  927. {
  928. cmCustomCommand cc(command, arguments, dep, (const char *)0);
  929. target.GetPostBuildCommands().push_back(cc);
  930. }
  931. m_Targets.insert(cmTargets::value_type(utilityName,target));
  932. }
  933. cmSourceFile *cmMakefile::GetSourceFileWithOutput(const char *cname)
  934. {
  935. std::string name = cname;
  936. // look through all the source files that have custom commands
  937. // and see if the custom command has the passed source file as an output
  938. // keep in mind the possible .rule extension that may be tacked on
  939. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  940. i != m_SourceFiles.end(); ++i)
  941. {
  942. // does this source file have a custom command?
  943. if ((*i)->GetCustomCommand())
  944. {
  945. // is the output of the custom command match the source files name
  946. const std::string &out = (*i)->GetCustomCommand()->GetOutput();
  947. if (out.rfind(name) != out.npos &&
  948. out.rfind(name) == out.size() - name.size())
  949. {
  950. return *i;
  951. }
  952. }
  953. }
  954. // otherwise return NULL
  955. return 0;
  956. }
  957. cmSourceGroup* cmMakefile::GetSourceGroup(const char* name)
  958. {
  959. // First see if the group exists. If so, replace its regular expression.
  960. for(std::vector<cmSourceGroup>::iterator sg = m_SourceGroups.begin();
  961. sg != m_SourceGroups.end(); ++sg)
  962. {
  963. std::string sgName = sg->GetName();
  964. if(sgName == name)
  965. {
  966. return &(*sg);
  967. }
  968. }
  969. return 0;
  970. }
  971. void cmMakefile::AddSourceGroup(const char* name, const char* regex)
  972. {
  973. // First see if the group exists. If so, replace its regular expression.
  974. for(std::vector<cmSourceGroup>::iterator sg = m_SourceGroups.begin();
  975. sg != m_SourceGroups.end(); ++sg)
  976. {
  977. std::string sgName = sg->GetName();
  978. if(sgName == name)
  979. {
  980. if ( regex )
  981. {
  982. // We only want to set the regular expression. If there are already
  983. // source files in the group, we don't want to remove them.
  984. sg->SetGroupRegex(regex);
  985. }
  986. return;
  987. }
  988. }
  989. // The group doesn't exist. Add it.
  990. m_SourceGroups.push_back(cmSourceGroup(name, regex));
  991. }
  992. void cmMakefile::AddExtraDirectory(const char* dir)
  993. {
  994. m_AuxSourceDirectories.push_back(dir);
  995. }
  996. // return the file name for the parent CMakeLists file to the
  997. // one passed in. Zero is returned if the CMakeLists file is the
  998. // one in the home directory or if for some reason a parent cmake lists
  999. // file cannot be found.
  1000. std::string cmMakefile::GetParentListFileName(const char *currentFileName)
  1001. {
  1002. // extract the directory name
  1003. std::string parentFile;
  1004. std::string listsDir = currentFileName;
  1005. std::string::size_type pos = listsDir.rfind('/');
  1006. // if we could not find the directory return 0
  1007. if(pos == std::string::npos)
  1008. {
  1009. return parentFile;
  1010. }
  1011. listsDir = listsDir.substr(0, pos);
  1012. // if we are in the home directory then stop, return 0
  1013. if(m_cmHomeDirectory == listsDir)
  1014. {
  1015. return parentFile;
  1016. }
  1017. // is there a parent directory we can check
  1018. pos = listsDir.rfind('/');
  1019. // if we could not find the directory return 0
  1020. if(pos == std::string::npos)
  1021. {
  1022. return parentFile;
  1023. }
  1024. listsDir = listsDir.substr(0, pos);
  1025. // is there a CMakeLists.txt file in the parent directory ?
  1026. parentFile = listsDir;
  1027. parentFile += "/CMakeLists.txt";
  1028. while(!cmSystemTools::FileExists(parentFile.c_str()))
  1029. {
  1030. // There is no CMakeLists.txt file in the parent directory. This
  1031. // can occur when coming out of a subdirectory resulting from a
  1032. // SUBDIRS(Foo/Bar) command (coming out of Bar into Foo). Try
  1033. // walking up until a CMakeLists.txt is found or the home
  1034. // directory is hit.
  1035. // if we are in the home directory then stop, return 0
  1036. if(m_cmHomeDirectory == listsDir) { return ""; }
  1037. // is there a parent directory we can check
  1038. pos = listsDir.rfind('/');
  1039. // if we could not find the directory return 0
  1040. if(pos == std::string::npos) { return ""; }
  1041. listsDir = listsDir.substr(0, pos);
  1042. parentFile = listsDir;
  1043. parentFile += "/CMakeLists.txt";
  1044. }
  1045. return parentFile;
  1046. }
  1047. // expance CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR in the
  1048. // include and library directories.
  1049. void cmMakefile::ExpandVariables()
  1050. {
  1051. // Now expand variables in the include and link strings
  1052. for(std::vector<std::string>::iterator d = m_IncludeDirectories.begin();
  1053. d != m_IncludeDirectories.end(); ++d)
  1054. {
  1055. this->ExpandVariablesInString(*d);
  1056. }
  1057. for(std::vector<std::string>::iterator d = m_LinkDirectories.begin();
  1058. d != m_LinkDirectories.end(); ++d)
  1059. {
  1060. this->ExpandVariablesInString(*d);
  1061. }
  1062. for(cmTarget::LinkLibraries::iterator l = m_LinkLibraries.begin();
  1063. l != m_LinkLibraries.end(); ++l)
  1064. {
  1065. this->ExpandVariablesInString(l->first);
  1066. }
  1067. }
  1068. void cmMakefile::ExpandVariablesInCustomCommands()
  1069. {
  1070. // do source files
  1071. for(std::vector<cmSourceFile*>::iterator i = m_SourceFiles.begin();
  1072. i != m_SourceFiles.end(); ++i)
  1073. {
  1074. cmCustomCommand *cc = (*i)->GetCustomCommand();
  1075. if (cc)
  1076. {
  1077. cc->ExpandVariables(*this);
  1078. }
  1079. }
  1080. // now do targets
  1081. std::vector<cmCustomCommand>::iterator ic;
  1082. for (cmTargets::iterator l = m_Targets.begin();
  1083. l != m_Targets.end(); l++)
  1084. {
  1085. for (ic = l->second.GetPreBuildCommands().begin();
  1086. ic != l->second.GetPreBuildCommands().end(); ++ic)
  1087. {
  1088. ic->ExpandVariables(*this);
  1089. }
  1090. for (ic = l->second.GetPreLinkCommands().begin();
  1091. ic != l->second.GetPreLinkCommands().end(); ++ic)
  1092. {
  1093. ic->ExpandVariables(*this);
  1094. }
  1095. for (ic = l->second.GetPostBuildCommands().begin();
  1096. ic != l->second.GetPostBuildCommands().end(); ++ic)
  1097. {
  1098. ic->ExpandVariables(*this);
  1099. }
  1100. }
  1101. }
  1102. bool cmMakefile::IsOn(const char* name) const
  1103. {
  1104. const char* value = this->GetDefinition(name);
  1105. return cmSystemTools::IsOn(value);
  1106. }
  1107. const char* cmMakefile::GetDefinition(const char* name) const
  1108. {
  1109. const char* def = 0;
  1110. DefinitionMap::const_iterator pos = m_Definitions.find(name);
  1111. if(pos != m_Definitions.end())
  1112. {
  1113. def = (*pos).second.c_str();
  1114. }
  1115. else
  1116. {
  1117. def = this->GetCacheManager()->GetCacheValue(name);
  1118. }
  1119. cmVariableWatch* vv = this->GetVariableWatch();
  1120. if ( vv )
  1121. {
  1122. if ( def )
  1123. {
  1124. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_READ_ACCESS);
  1125. }
  1126. else
  1127. {
  1128. // are unknown access allowed
  1129. DefinitionMap::const_iterator pos2 =
  1130. m_Definitions.find("CMAKE_ALLOW_UNKNOWN_VARIABLE_READ_ACCESS");
  1131. if (pos2 != m_Definitions.end() &&
  1132. cmSystemTools::IsOn((*pos2).second.c_str()))
  1133. {
  1134. vv->VariableAccessed(name,
  1135. cmVariableWatch::ALLOWED_UNKNOWN_VARIABLE_READ_ACCESS);
  1136. }
  1137. else
  1138. {
  1139. vv->VariableAccessed(name, cmVariableWatch::
  1140. UNKNOWN_VARIABLE_READ_ACCESS);
  1141. }
  1142. }
  1143. }
  1144. return def;
  1145. }
  1146. std::vector<std::string> cmMakefile::GetDefinitions(int cacheonly /* = 0 */) const
  1147. {
  1148. std::map<std::string, int> definitions;
  1149. if ( !cacheonly )
  1150. {
  1151. DefinitionMap::const_iterator it;
  1152. for ( it = m_Definitions.begin(); it != m_Definitions.end(); it ++ )
  1153. {
  1154. definitions[it->first] = 1;
  1155. }
  1156. }
  1157. cmCacheManager::CacheIterator cit = this->GetCacheManager()->GetCacheIterator();
  1158. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  1159. {
  1160. definitions[cit.GetName()] = 1;
  1161. }
  1162. std::vector<std::string> res;
  1163. std::map<std::string, int>::iterator fit;
  1164. for ( fit = definitions.begin(); fit != definitions.end(); fit ++ )
  1165. {
  1166. res.push_back(fit->first);
  1167. }
  1168. return res;
  1169. }
  1170. const char *cmMakefile::ExpandVariablesInString(std::string& source) const
  1171. {
  1172. return this->ExpandVariablesInString(source, false);
  1173. }
  1174. const char *cmMakefile::ExpandVariablesInString(std::string& source,
  1175. bool escapeQuotes,
  1176. bool atOnly) const
  1177. {
  1178. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  1179. // with GetDefinition(), if not found in the map, nothing is expanded.
  1180. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  1181. // the current environment variables.
  1182. // start by look for $ or @ in the string
  1183. std::string::size_type markerPos;
  1184. if(atOnly)
  1185. {
  1186. markerPos = source.find_first_of("@");
  1187. }
  1188. else
  1189. {
  1190. markerPos = source.find_first_of("$@");
  1191. }
  1192. // if not found, or found as the last character, then leave quickly as
  1193. // nothing needs to be expanded
  1194. if((markerPos == std::string::npos) || (markerPos >= source.size()-1))
  1195. {
  1196. return source.c_str();
  1197. }
  1198. // current position
  1199. std::string::size_type currentPos =0; // start at 0
  1200. std::string result; // string with replacements
  1201. // go until the the end of the string
  1202. while((markerPos != std::string::npos) && (markerPos < source.size()-1))
  1203. {
  1204. // grab string from currentPos to the start of the variable
  1205. // and add it to the result
  1206. result += source.substr(currentPos, markerPos - currentPos);
  1207. char endVariableMarker; // what is the end of the variable @ or }
  1208. int markerStartSize = 1; // size of the start marker 1 or 2 or 5
  1209. if(!atOnly && source[markerPos] == '$')
  1210. {
  1211. // ${var} case
  1212. if(source[markerPos+1] == '{')
  1213. {
  1214. endVariableMarker = '}';
  1215. markerStartSize = 2;
  1216. }
  1217. // $ENV{var} case
  1218. else if(markerPos+4 < source.size() &&
  1219. source[markerPos+4] == '{' &&
  1220. !source.substr(markerPos+1, 3).compare("ENV"))
  1221. {
  1222. endVariableMarker = '}';
  1223. markerStartSize = 5;
  1224. }
  1225. else
  1226. {
  1227. // bogus $ with no { so add $ to result and move on
  1228. result += '$'; // add bogus $ back into string
  1229. currentPos = markerPos+1; // move on
  1230. endVariableMarker = ' '; // set end var to space so we can tell bogus
  1231. }
  1232. }
  1233. else
  1234. {
  1235. // @VAR case
  1236. endVariableMarker = '@';
  1237. }
  1238. // if it was a valid variable (started with @ or ${ or $ENV{ )
  1239. if(endVariableMarker != ' ')
  1240. {
  1241. markerPos += markerStartSize; // move past marker
  1242. // find the end variable marker starting at the markerPos
  1243. std::string::size_type endVariablePos =
  1244. source.find(endVariableMarker, markerPos);
  1245. if(endVariablePos == std::string::npos)
  1246. {
  1247. // no end marker found so add the bogus start
  1248. if(endVariableMarker == '@')
  1249. {
  1250. result += '@';
  1251. }
  1252. else
  1253. {
  1254. result += (markerStartSize == 5 ? "$ENV{" : "${");
  1255. }
  1256. currentPos = markerPos;
  1257. }
  1258. else
  1259. {
  1260. // good variable remove it
  1261. std::string var = source.substr(markerPos, endVariablePos - markerPos);
  1262. bool found = false;
  1263. if (markerStartSize == 5) // $ENV{
  1264. {
  1265. char *ptr = getenv(var.c_str());
  1266. if (ptr)
  1267. {
  1268. if (escapeQuotes)
  1269. {
  1270. result += cmSystemTools::EscapeQuotes(ptr);
  1271. }
  1272. else
  1273. {
  1274. result += ptr;
  1275. }
  1276. found = true;
  1277. }
  1278. }
  1279. else
  1280. {
  1281. const char* lookup = this->GetDefinition(var.c_str());
  1282. if(lookup)
  1283. {
  1284. if (escapeQuotes)
  1285. {
  1286. result += cmSystemTools::EscapeQuotes(lookup);
  1287. }
  1288. else
  1289. {
  1290. result += lookup;
  1291. }
  1292. found = true;
  1293. }
  1294. }
  1295. // if found add to result, if not, then it gets blanked
  1296. if (!found)
  1297. {
  1298. // if no definition is found then add the var back
  1299. if(endVariableMarker == '@')
  1300. {
  1301. result += "@";
  1302. result += var;
  1303. result += "@";
  1304. }
  1305. }
  1306. // lookup var, and replace it
  1307. currentPos = endVariablePos+1;
  1308. }
  1309. }
  1310. if(atOnly)
  1311. {
  1312. markerPos = source.find_first_of("@", currentPos);
  1313. }
  1314. else
  1315. {
  1316. markerPos = source.find_first_of("$@", currentPos);
  1317. }
  1318. }
  1319. result += source.substr(currentPos); // pick up the rest of the string
  1320. source = result;
  1321. return source.c_str();
  1322. }
  1323. void cmMakefile::RemoveVariablesInString(std::string& source,
  1324. bool atOnly) const
  1325. {
  1326. if(!atOnly)
  1327. {
  1328. cmsys::RegularExpression var("(\\${[A-Za-z_0-9]*})");
  1329. while (var.find(source))
  1330. {
  1331. source.erase(var.start(),var.end() - var.start());
  1332. }
  1333. }
  1334. if(!atOnly)
  1335. {
  1336. cmsys::RegularExpression varb("(\\$ENV{[A-Za-z_0-9]*})");
  1337. while (varb.find(source))
  1338. {
  1339. source.erase(varb.start(),varb.end() - varb.start());
  1340. }
  1341. }
  1342. cmsys::RegularExpression var2("(@[A-Za-z_0-9]*@)");
  1343. while (var2.find(source))
  1344. {
  1345. source.erase(var2.start(),var2.end() - var2.start());
  1346. }
  1347. }
  1348. /**
  1349. * Add the default definitions to the makefile. These values must not
  1350. * be dependent on anything that isn't known when this cmMakefile instance
  1351. * is constructed.
  1352. */
  1353. void cmMakefile::AddDefaultDefinitions()
  1354. {
  1355. #if defined(_WIN32) || defined(__CYGWIN__)
  1356. this->AddDefinition("WIN32", "1");
  1357. #else
  1358. this->AddDefinition("UNIX", "1");
  1359. #endif
  1360. // Cygwin is more like unix so enable the unix commands
  1361. #if defined(__CYGWIN__)
  1362. this->AddDefinition("UNIX", "1");
  1363. this->AddDefinition("CYGWIN", "1");
  1364. #endif
  1365. #if defined(__APPLE__)
  1366. this->AddDefinition("APPLE", "1");
  1367. #endif
  1368. char temp[1024];
  1369. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  1370. this->AddDefinition("CMAKE_MINOR_VERSION", temp);
  1371. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  1372. this->AddDefinition("CMAKE_MAJOR_VERSION", temp);
  1373. }
  1374. /**
  1375. * Find a source group whose regular expression matches the filename
  1376. * part of the given source name. Search backward through the list of
  1377. * source groups, and take the first matching group found. This way
  1378. * non-inherited SOURCE_GROUP commands will have precedence over
  1379. * inherited ones.
  1380. */
  1381. cmSourceGroup&
  1382. cmMakefile::FindSourceGroup(const char* source,
  1383. std::vector<cmSourceGroup> &groups)
  1384. {
  1385. std::string file = source;
  1386. std::string::size_type pos = file.rfind('/');
  1387. if(pos != std::string::npos)
  1388. {
  1389. file = file.substr(pos+1);
  1390. }
  1391. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1392. sg != groups.rend(); ++sg)
  1393. {
  1394. if(sg->Matches(file.c_str()))
  1395. {
  1396. return *sg;
  1397. }
  1398. }
  1399. // Shouldn't get here, but just in case, return the default group.
  1400. return groups.front();
  1401. }
  1402. bool cmMakefile::IsFunctionBlocked(const cmListFileFunction& lff)
  1403. {
  1404. // if there are no blockers get out of here
  1405. if (m_FunctionBlockers.begin() == m_FunctionBlockers.end())
  1406. {
  1407. return false;
  1408. }
  1409. // loop over all function blockers to see if any block this command
  1410. // evaluate in reverse, this is critical for balanced IF statements etc
  1411. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1412. for (pos = m_FunctionBlockers.rbegin();
  1413. pos != m_FunctionBlockers.rend(); ++pos)
  1414. {
  1415. if((*pos)->IsFunctionBlocked(lff, *this))
  1416. {
  1417. return true;
  1418. }
  1419. }
  1420. return false;
  1421. }
  1422. void cmMakefile::ExpandArguments(
  1423. std::vector<cmListFileArgument> const& inArgs,
  1424. std::vector<std::string>& outArgs)
  1425. {
  1426. std::vector<cmListFileArgument>::const_iterator i;
  1427. std::string value;
  1428. outArgs.reserve(inArgs.size());
  1429. for(i = inArgs.begin(); i != inArgs.end(); ++i)
  1430. {
  1431. // Expand the variables in the argument.
  1432. value = i->Value;
  1433. this->ExpandVariablesInString(value);
  1434. // If the argument is quoted, it should be one argument.
  1435. // Otherwise, it may be a list of arguments.
  1436. if(i->Quoted)
  1437. {
  1438. outArgs.push_back(value);
  1439. }
  1440. else
  1441. {
  1442. cmSystemTools::ExpandListArgument(value, outArgs);
  1443. }
  1444. }
  1445. }
  1446. void cmMakefile::RemoveFunctionBlocker(const cmListFileFunction& lff)
  1447. {
  1448. // loop over all function blockers to see if any block this command
  1449. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1450. for (pos = m_FunctionBlockers.rbegin();
  1451. pos != m_FunctionBlockers.rend(); ++pos)
  1452. {
  1453. if ((*pos)->ShouldRemove(lff, *this))
  1454. {
  1455. cmFunctionBlocker* b = *pos;
  1456. m_FunctionBlockers.remove(b);
  1457. delete b;
  1458. break;
  1459. }
  1460. }
  1461. return;
  1462. }
  1463. void cmMakefile::SetHomeDirectory(const char* dir)
  1464. {
  1465. m_cmHomeDirectory = dir;
  1466. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  1467. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  1468. }
  1469. void cmMakefile::SetHomeOutputDirectory(const char* lib)
  1470. {
  1471. m_HomeOutputDirectory = lib;
  1472. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  1473. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  1474. }
  1475. /**
  1476. * Register the given cmData instance with its own name.
  1477. */
  1478. void cmMakefile::RegisterData(cmData* data)
  1479. {
  1480. std::string name = data->GetName();
  1481. DataMap::const_iterator d = m_DataMap.find(name);
  1482. if((d != m_DataMap.end()) && (d->second != 0) && (d->second != data))
  1483. {
  1484. delete d->second;
  1485. }
  1486. m_DataMap[name] = data;
  1487. }
  1488. /**
  1489. * Register the given cmData instance with the given name. This can be used
  1490. * to register a NULL pointer.
  1491. */
  1492. void cmMakefile::RegisterData(const char* name, cmData* data)
  1493. {
  1494. DataMap::const_iterator d = m_DataMap.find(name);
  1495. if((d != m_DataMap.end()) && (d->second != 0) && (d->second != data))
  1496. {
  1497. delete d->second;
  1498. }
  1499. m_DataMap[name] = data;
  1500. }
  1501. /**
  1502. * Lookup a cmData instance previously registered with the given name. If
  1503. * the instance cannot be found, return NULL.
  1504. */
  1505. cmData* cmMakefile::LookupData(const char* name) const
  1506. {
  1507. DataMap::const_iterator d = m_DataMap.find(name);
  1508. if(d != m_DataMap.end())
  1509. {
  1510. return d->second;
  1511. }
  1512. else
  1513. {
  1514. return 0;
  1515. }
  1516. }
  1517. cmSourceFile* cmMakefile::GetSource(const char* sourceName) const
  1518. {
  1519. // if the source is provided with a full path use it, otherwise
  1520. // by default it is in the current source dir
  1521. std::string path = cmSystemTools::GetFilenamePath(sourceName);
  1522. if (path.empty())
  1523. {
  1524. path = this->GetCurrentDirectory();
  1525. }
  1526. std::string sname =
  1527. cmSystemTools::GetFilenameWithoutLastExtension(sourceName);
  1528. // compute the extension
  1529. std::string ext
  1530. = cmSystemTools::GetFilenameLastExtension(sourceName);
  1531. if ( ext.length() && ext[0] == '.' )
  1532. {
  1533. ext = ext.substr(1);
  1534. }
  1535. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  1536. i != m_SourceFiles.end(); ++i)
  1537. {
  1538. if ((*i)->GetSourceName() == sname &&
  1539. cmSystemTools::GetFilenamePath((*i)->GetFullPath()) == path &&
  1540. (ext.size() == 0 || (ext == (*i)->GetSourceExtension())))
  1541. {
  1542. return *i;
  1543. }
  1544. }
  1545. // geeze, if it wasn't found maybe it is listed under the output dir
  1546. if (!cmSystemTools::GetFilenamePath(sourceName).empty())
  1547. {
  1548. return 0;
  1549. }
  1550. path = this->GetCurrentOutputDirectory();
  1551. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  1552. i != m_SourceFiles.end(); ++i)
  1553. {
  1554. if ((*i)->GetSourceName() == sname &&
  1555. cmSystemTools::GetFilenamePath((*i)->GetFullPath()) == path &&
  1556. (ext.size() == 0 || (ext == (*i)->GetSourceExtension())))
  1557. {
  1558. return *i;
  1559. }
  1560. }
  1561. return 0;
  1562. }
  1563. cmSourceFile* cmMakefile::GetOrCreateSource(const char* sourceName,
  1564. bool generated)
  1565. {
  1566. // make it a full path first
  1567. std::string src = sourceName;
  1568. bool relative = !cmSystemTools::FileIsFullPath(sourceName);
  1569. std::string srcTreeFile = this->GetCurrentDirectory();
  1570. srcTreeFile += "/";
  1571. srcTreeFile += sourceName;
  1572. if(relative)
  1573. {
  1574. src = srcTreeFile;
  1575. }
  1576. // check to see if it exists
  1577. cmSourceFile* ret = this->GetSource(src.c_str());
  1578. if (ret)
  1579. {
  1580. return ret;
  1581. }
  1582. // OK a source file object doesn't exist for the source
  1583. // maybe we made a bad call on assuming it was in the src tree
  1584. std::string buildTreeFile = this->GetCurrentOutputDirectory();
  1585. buildTreeFile += "/";
  1586. buildTreeFile += sourceName;
  1587. if (relative)
  1588. {
  1589. src = buildTreeFile;
  1590. ret = this->GetSource(src.c_str());
  1591. if (ret)
  1592. {
  1593. return ret;
  1594. }
  1595. // if it has not been marked generated check to see if it exists in the
  1596. // src tree
  1597. if(!generated)
  1598. {
  1599. // see if the file is in the source tree, otherwise assume it
  1600. // is in the binary tree
  1601. if (cmSystemTools::FileExists(srcTreeFile.c_str()) &&
  1602. !cmSystemTools::FileIsDirectory(srcTreeFile.c_str()))
  1603. {
  1604. src = srcTreeFile;
  1605. }
  1606. else
  1607. {
  1608. if ( cmSystemTools::GetFilenameLastExtension(srcTreeFile.c_str()).size() == 0)
  1609. {
  1610. if (cmSystemTools::DoesFileExistWithExtensions(
  1611. srcTreeFile.c_str(), this->GetSourceExtensions()))
  1612. {
  1613. src = srcTreeFile;
  1614. }
  1615. else if (cmSystemTools::DoesFileExistWithExtensions(
  1616. srcTreeFile.c_str(), this->GetHeaderExtensions()))
  1617. {
  1618. src = srcTreeFile;
  1619. }
  1620. }
  1621. }
  1622. }
  1623. }
  1624. // a cmSourceFile instance does not exist yet so we must create one
  1625. // go back to looking in the source directory for it
  1626. // we must create one
  1627. cmSourceFile file;
  1628. std::string path = cmSystemTools::GetFilenamePath(src);
  1629. if(generated)
  1630. {
  1631. std::string ext = cmSystemTools::GetFilenameLastExtension(src);
  1632. std::string name_no_ext = cmSystemTools::GetFilenameName(src.c_str());
  1633. name_no_ext = name_no_ext.substr(0, name_no_ext.length()-ext.length());
  1634. if ( ext.length() && ext[0] == '.' )
  1635. {
  1636. ext = ext.substr(1);
  1637. }
  1638. file.SetName(name_no_ext.c_str(), path.c_str(), ext.c_str(), false);
  1639. }
  1640. else
  1641. {
  1642. file.SetName(cmSystemTools::GetFilenameName(src.c_str()).c_str(),
  1643. path.c_str(),
  1644. this->GetSourceExtensions(),
  1645. this->GetHeaderExtensions());
  1646. }
  1647. // add the source file to the makefile
  1648. this->AddSource(file);
  1649. src = file.GetFullPath();
  1650. ret = this->GetSource(src.c_str());
  1651. if (!ret)
  1652. {
  1653. cmSystemTools::Error(
  1654. "CMake failed to properly look up cmSourceFile: ", sourceName);
  1655. }
  1656. return ret;
  1657. }
  1658. cmSourceFile* cmMakefile::AddSource(cmSourceFile const&sf)
  1659. {
  1660. // check to see if it exists
  1661. cmSourceFile* ret = this->GetSource(sf.GetFullPath().c_str());
  1662. if(ret)
  1663. {
  1664. return ret;
  1665. }
  1666. ret = new cmSourceFile(sf);
  1667. m_SourceFiles.push_back(ret);
  1668. return ret;
  1669. }
  1670. void cmMakefile::EnableLanguage(const char* lang)
  1671. {
  1672. m_LocalGenerator->GetGlobalGenerator()->EnableLanguage(lang, this);
  1673. }
  1674. void cmMakefile::ExpandSourceListArguments(
  1675. std::vector<std::string> const& arguments,
  1676. std::vector<std::string>& newargs, unsigned int start)
  1677. {
  1678. // first figure out if we need to handle version 1.2 style source lists
  1679. int oldVersion = 1;
  1680. const char* versionValue
  1681. = this->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  1682. if (versionValue && atof(versionValue) > 1.2)
  1683. {
  1684. oldVersion = 0;
  1685. }
  1686. // now expand the args
  1687. unsigned int i;
  1688. for(i = 0; i < arguments.size(); ++i)
  1689. {
  1690. // is the arg defined ?, if so use the def
  1691. const char *def = this->GetDefinition(arguments[i].c_str());
  1692. if (def && oldVersion && i >= start)
  1693. {
  1694. // Definition lookup could result in a list that needs to be
  1695. // expanded.
  1696. cmSystemTools::ExpandListArgument(def, newargs);
  1697. }
  1698. else
  1699. {
  1700. // List expansion will have been done already.
  1701. newargs.push_back(arguments[i]);
  1702. }
  1703. }
  1704. }
  1705. int cmMakefile::TryCompile(const char *srcdir, const char *bindir,
  1706. const char *projectName, const char *targetName,
  1707. const std::vector<std::string> *cmakeArgs,
  1708. std::string *output)
  1709. {
  1710. // does the binary directory exist ? If not create it...
  1711. if (!cmSystemTools::FileIsDirectory(bindir))
  1712. {
  1713. cmSystemTools::MakeDirectory(bindir);
  1714. }
  1715. // change to the tests directory and run cmake
  1716. // use the cmake object instead of calling cmake
  1717. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1718. cmSystemTools::ChangeDirectory(bindir);
  1719. // make sure the same generator is used
  1720. // use this program as the cmake to be run, it should not
  1721. // be run that way but the cmake object requires a vailid path
  1722. std::string cmakeCommand = this->GetDefinition("CMAKE_COMMAND");
  1723. cmake cm;
  1724. cm.SetIsInTryCompile(true);
  1725. cmGlobalGenerator *gg =
  1726. cm.CreateGlobalGenerator(m_LocalGenerator->GetGlobalGenerator()->GetName());
  1727. if (!gg)
  1728. {
  1729. cmSystemTools::Error(
  1730. "Internal CMake error, TryCompile bad GlobalGenerator");
  1731. // return to the original directory
  1732. cmSystemTools::ChangeDirectory(cwd.c_str());
  1733. return 1;
  1734. }
  1735. cm.SetGlobalGenerator(gg);
  1736. // do a configure
  1737. cm.SetHomeDirectory(srcdir);
  1738. cm.SetHomeOutputDirectory(bindir);
  1739. cm.SetStartDirectory(srcdir);
  1740. cm.SetStartOutputDirectory(bindir);
  1741. cm.SetCMakeCommand(cmakeCommand.c_str());
  1742. cm.LoadCache();
  1743. // if cmake args were provided then pass them in
  1744. if (cmakeArgs)
  1745. {
  1746. cm.SetCacheArgs(*cmakeArgs);
  1747. }
  1748. // to save time we pass the EnableLanguage info directly
  1749. gg->EnableLanguagesFromGenerator(m_LocalGenerator->GetGlobalGenerator());
  1750. if (cm.Configure() != 0)
  1751. {
  1752. cmSystemTools::Error(
  1753. "Internal CMake error, TryCompile configure of cmake failed");
  1754. // return to the original directory
  1755. cmSystemTools::ChangeDirectory(cwd.c_str());
  1756. return 1;
  1757. }
  1758. if (cm.Generate() != 0)
  1759. {
  1760. cmSystemTools::Error(
  1761. "Internal CMake error, TryCompile generation of cmake failed");
  1762. // return to the original directory
  1763. cmSystemTools::ChangeDirectory(cwd.c_str());
  1764. return 1;
  1765. }
  1766. // finally call the generator to actually build the resulting project
  1767. int ret =
  1768. m_LocalGenerator->GetGlobalGenerator()->TryCompile(srcdir,bindir,
  1769. projectName,
  1770. targetName,
  1771. output);
  1772. cmSystemTools::ChangeDirectory(cwd.c_str());
  1773. return ret;
  1774. }
  1775. cmake *cmMakefile::GetCMakeInstance() const
  1776. {
  1777. if ( m_LocalGenerator && m_LocalGenerator->GetGlobalGenerator() )
  1778. {
  1779. return m_LocalGenerator->GetGlobalGenerator()->GetCMakeInstance();
  1780. }
  1781. return 0;
  1782. }
  1783. cmVariableWatch *cmMakefile::GetVariableWatch() const
  1784. {
  1785. if ( this->GetCMakeInstance() &&
  1786. this->GetCMakeInstance()->GetVariableWatch() )
  1787. {
  1788. return this->GetCMakeInstance()->GetVariableWatch();
  1789. }
  1790. return 0;
  1791. }
  1792. cmCacheManager *cmMakefile::GetCacheManager() const
  1793. {
  1794. return this->GetCMakeInstance()->GetCacheManager();
  1795. }
  1796. bool cmMakefile::GetLocal() const
  1797. {
  1798. return this->GetCMakeInstance()->GetLocal();
  1799. }
  1800. void cmMakefile::DisplayStatus(const char* message, float s)
  1801. {
  1802. this->GetLocalGenerator()->GetGlobalGenerator()
  1803. ->GetCMakeInstance()->UpdateProgress(message, s);
  1804. }
  1805. /**
  1806. * Find the library with the given name. Searches the given path and then
  1807. * the system search path. Returns the full path to the library if it is
  1808. * found. Otherwise, the empty string is returned.
  1809. */
  1810. std::string cmMakefile::FindLibrary(const char* name,
  1811. const std::vector<std::string>& userPaths)
  1812. {
  1813. // See if the executable exists as written.
  1814. if(cmSystemTools::FileExists(name))
  1815. {
  1816. return cmSystemTools::CollapseFullPath(name);
  1817. }
  1818. // Add the system search path to our path.
  1819. std::vector<std::string> path = userPaths;
  1820. cmSystemTools::GetPath(path);
  1821. // Add some lib directories specific to compilers, depending on the
  1822. // current generator, so that library that might have been stored here
  1823. // can be found too.
  1824. // i.e. Microsoft Visual Studio or .Net: path to compiler/../Lib
  1825. // Borland: path to compiler/../Lib
  1826. const char* genName = this->GetDefinition("CMAKE_GENERATOR");
  1827. if (genName)
  1828. {
  1829. if (!strcmp(genName, "NMake Makefiles") ||
  1830. !strcmp(genName, "Visual Studio 6"))
  1831. {
  1832. const char* compiler = this->GetDefinition("CMAKE_CXX_COMPILER");
  1833. if (compiler)
  1834. {
  1835. std::string compiler_path = cmSystemTools::FindProgram(compiler);
  1836. if (compiler_path.size())
  1837. {
  1838. std::string lib_path =
  1839. cmSystemTools::GetFilenamePath(
  1840. cmSystemTools::GetFilenamePath(compiler_path)) + "/Lib";
  1841. path.push_back(lib_path);
  1842. }
  1843. }
  1844. }
  1845. else if (!strcmp(genName, "Visual Studio 7"))
  1846. {
  1847. // It is likely that the compiler won't be in the path for .Net, but
  1848. // we know where devenv is.
  1849. const char* devenv = this->GetDefinition("MICROSOFT_DEVENV");
  1850. if (devenv)
  1851. {
  1852. std::string devenv_path = cmSystemTools::FindProgram(devenv);
  1853. if (devenv_path.size())
  1854. {
  1855. std::string vc7_path =
  1856. cmSystemTools::GetFilenamePath(
  1857. cmSystemTools::GetFilenamePath(
  1858. cmSystemTools::GetFilenamePath(devenv_path))) + "/Vc7";
  1859. path.push_back(vc7_path + "/lib");
  1860. path.push_back(vc7_path + "/PlatformSDK/lib");
  1861. }
  1862. }
  1863. }
  1864. else if (!strcmp(genName, "Borland Makefiles"))
  1865. {
  1866. const char* bcb_bin_path = this->GetDefinition("BCB_BIN_PATH");
  1867. if (bcb_bin_path)
  1868. {
  1869. std::string lib_path =
  1870. cmSystemTools::GetFilenamePath(bcb_bin_path) + "/Lib";
  1871. path.push_back(lib_path);
  1872. }
  1873. }
  1874. }
  1875. return cmSystemTools::FindLibrary(name, path);
  1876. }