cmMakefile.cxx 37 KB

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