cmMakefile.cxx 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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)->NeedExpandedVariables())
  1083. {
  1084. if ((*pos)->IsFunctionBlocked(name, expandedArguments, *this))
  1085. {
  1086. return true;
  1087. }
  1088. }
  1089. else
  1090. {
  1091. if ((*pos)->IsFunctionBlocked(name, args, *this))
  1092. {
  1093. return true;
  1094. }
  1095. }
  1096. }
  1097. return false;
  1098. }
  1099. void cmMakefile::RemoveFunctionBlocker(const char *name,
  1100. const std::vector<std::string> &args)
  1101. {
  1102. // loop over all function blockers to see if any block this command
  1103. std::set<cmFunctionBlocker *>::const_iterator pos;
  1104. for (pos = m_FunctionBlockers.begin();
  1105. pos != m_FunctionBlockers.end(); ++pos)
  1106. {
  1107. if ((*pos)->ShouldRemove(name, args, *this))
  1108. {
  1109. cmFunctionBlocker* b = *pos;
  1110. m_FunctionBlockers.erase(*pos);
  1111. delete b;
  1112. return;
  1113. }
  1114. }
  1115. return;
  1116. }
  1117. void cmMakefile::SetHomeDirectory(const char* dir)
  1118. {
  1119. m_cmHomeDirectory = dir;
  1120. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  1121. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  1122. }
  1123. void cmMakefile::SetHomeOutputDirectory(const char* lib)
  1124. {
  1125. m_HomeOutputDirectory = lib;
  1126. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  1127. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  1128. }
  1129. /**
  1130. * Register the given cmData instance with its own name.
  1131. */
  1132. void cmMakefile::RegisterData(cmData* data)
  1133. {
  1134. std::string name = data->GetName();
  1135. DataMap::const_iterator d = m_DataMap.find(name);
  1136. if((d != m_DataMap.end()) && (d->second != NULL) && (d->second != data))
  1137. {
  1138. delete d->second;
  1139. }
  1140. m_DataMap[name] = data;
  1141. }
  1142. /**
  1143. * Register the given cmData instance with the given name. This can be used
  1144. * to register a NULL pointer.
  1145. */
  1146. void cmMakefile::RegisterData(const char* name, cmData* data)
  1147. {
  1148. DataMap::const_iterator d = m_DataMap.find(name);
  1149. if((d != m_DataMap.end()) && (d->second != NULL) && (d->second != data))
  1150. {
  1151. delete d->second;
  1152. }
  1153. m_DataMap[name] = data;
  1154. }
  1155. /**
  1156. * Lookup a cmData instance previously registered with the given name. If
  1157. * the instance cannot be found, return NULL.
  1158. */
  1159. cmData* cmMakefile::LookupData(const char* name) const
  1160. {
  1161. DataMap::const_iterator d = m_DataMap.find(name);
  1162. if(d != m_DataMap.end())
  1163. {
  1164. return d->second;
  1165. }
  1166. else
  1167. {
  1168. return NULL;
  1169. }
  1170. }