cmMakefile.cxx 38 KB

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