cmMakefile.cxx 37 KB

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