cmMakefile.cxx 35 KB

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