cmMakefile.cxx 40 KB

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