cmMakefile.cxx 37 KB

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