cmMakefile.cxx 41 KB

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