cmMakefile.cxx 46 KB

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