cmMakefile.cxx 41 KB

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