cmMakefile.cxx 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  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 c = cmSystemTools::EscapeSpaces(command);
  428. std::string combinedArgs;
  429. int i;
  430. for (i = 0; i < commandArgs.size(); ++i)
  431. {
  432. combinedArgs += cmSystemTools::EscapeSpaces(commandArgs[i].c_str());
  433. combinedArgs += " ";
  434. }
  435. cmCustomCommand cc(source,c.c_str(),combinedArgs.c_str(),depends,outputs);
  436. m_Targets[target].GetCustomCommands().push_back(cc);
  437. std::string cacheCommand = command;
  438. this->ExpandVariablesInString(cacheCommand);
  439. if(cmCacheManager::GetInstance()->GetCacheValue(cacheCommand.c_str()))
  440. {
  441. m_Targets[target].AddUtility(
  442. cmCacheManager::GetInstance()->GetCacheValue(cacheCommand.c_str()));
  443. }
  444. }
  445. }
  446. void cmMakefile::AddCustomCommand(const char* source,
  447. const char* command,
  448. const std::vector<std::string>& commandArgs,
  449. const std::vector<std::string>& depends,
  450. const char* output,
  451. const char *target)
  452. {
  453. std::vector<std::string> outputs;
  454. outputs.push_back(output);
  455. this->AddCustomCommand(source, command, commandArgs, depends, outputs, target);
  456. }
  457. void cmMakefile::AddDefineFlag(const char* flag)
  458. {
  459. m_DefineFlags += " ";
  460. m_DefineFlags += flag;
  461. }
  462. void cmMakefile::AddLinkLibrary(const char* lib, cmTarget::LinkLibraryType llt)
  463. {
  464. m_LinkLibraries.push_back(
  465. std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt));
  466. }
  467. void cmMakefile::AddLinkLibraryForTarget(const char *target,
  468. const char* lib,
  469. cmTarget::LinkLibraryType llt)
  470. {
  471. if (m_Targets.find(target) != m_Targets.end())
  472. {
  473. m_Targets[target].GetLinkLibraries().
  474. push_back(
  475. std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt));
  476. }
  477. else
  478. {
  479. cmSystemTools::Error("Attempt to add link libraries to non-existant target: ", target, " for lib ", lib);
  480. }
  481. }
  482. void cmMakefile::AddLinkLibrary(const char* lib)
  483. {
  484. this->AddLinkLibrary(lib,cmTarget::GENERAL);
  485. }
  486. void cmMakefile::AddLinkDirectory(const char* dir)
  487. {
  488. // Don't add a link directory that is already present. Yes, this
  489. // linear search results in n^2 behavior, but n won't be getting
  490. // much bigger than 20. We cannot use a set because of order
  491. // dependency of the link search path.
  492. if(std::find(m_LinkDirectories.begin(),
  493. m_LinkDirectories.end(), dir) == m_LinkDirectories.end())
  494. {
  495. m_LinkDirectories.push_back(dir);
  496. }
  497. }
  498. void cmMakefile::AddSubDirectory(const char* sub)
  499. {
  500. m_SubDirectories.push_back(sub);
  501. }
  502. void cmMakefile::AddSubdirDependency(const char* subdir,
  503. const char* dependency)
  504. {
  505. m_SubdirDepends[subdir].insert(dependency);
  506. }
  507. void cmMakefile::AddIncludeDirectory(const char* inc)
  508. {
  509. // Don't add an include directory that is already present. Yes,
  510. // this linear search results in n^2 behavior, but n won't be
  511. // getting much bigger than 20. We cannot use a set because of
  512. // order dependency of the include path.
  513. if(std::find(m_IncludeDirectories.begin(),
  514. m_IncludeDirectories.end(), inc) == m_IncludeDirectories.end())
  515. {
  516. m_IncludeDirectories.push_back(inc);
  517. }
  518. }
  519. void cmMakefile::AddDefinition(const char* name, const char* value)
  520. {
  521. m_Definitions.erase( DefinitionMap::key_type(name));
  522. m_Definitions.insert(DefinitionMap::value_type(name, value));
  523. }
  524. void cmMakefile::AddCacheDefinition(const char* name, const char* value,
  525. const char* doc,
  526. cmCacheManager::CacheEntryType type)
  527. {
  528. cmCacheManager::GetInstance()->AddCacheEntry(name, value, doc, type);
  529. this->AddDefinition(name, value);
  530. }
  531. void cmMakefile::AddDefinition(const char* name, bool value)
  532. {
  533. if(value)
  534. {
  535. m_Definitions.erase( DefinitionMap::key_type(name));
  536. m_Definitions.insert(DefinitionMap::value_type(name, "ON"));
  537. }
  538. else
  539. {
  540. m_Definitions.erase( DefinitionMap::key_type(name));
  541. m_Definitions.insert(DefinitionMap::value_type(name, "OFF"));
  542. }
  543. }
  544. void cmMakefile::AddCacheDefinition(const char* name, bool value, const char* doc)
  545. {
  546. cmCacheManager::GetInstance()->AddCacheEntry(name, value, doc);
  547. this->AddDefinition(name, value);
  548. }
  549. void cmMakefile::SetProjectName(const char* p)
  550. {
  551. m_ProjectName = p;
  552. }
  553. void cmMakefile::AddLibrary(const char* lname, int shared,
  554. const std::vector<std::string> &srcs)
  555. {
  556. cmTarget target;
  557. switch (shared)
  558. {
  559. case 0:
  560. target.SetType(cmTarget::STATIC_LIBRARY);
  561. break;
  562. case 1:
  563. target.SetType(cmTarget::SHARED_LIBRARY);
  564. break;
  565. case 2:
  566. target.SetType(cmTarget::MODULE_LIBRARY);
  567. break;
  568. default:
  569. target.SetType(cmTarget::STATIC_LIBRARY);
  570. }
  571. target.SetInAll(true);
  572. target.GetSourceLists() = srcs;
  573. m_Targets.insert(cmTargets::value_type(lname,target));
  574. // Add an entry into the cache
  575. cmCacheManager::GetInstance()->
  576. AddCacheEntry(lname,
  577. this->GetCurrentOutputDirectory(),
  578. "Path to a library", cmCacheManager::INTERNAL);
  579. // Add an entry into the cache
  580. std::string ltname = lname;
  581. ltname += "_LIBRARY_TYPE";
  582. switch (shared)
  583. {
  584. case 0:
  585. cmCacheManager::GetInstance()->
  586. AddCacheEntry(ltname.c_str(),
  587. "STATIC",
  588. "Whether a library is static, shared or module.",
  589. cmCacheManager::INTERNAL);
  590. break;
  591. case 1:
  592. cmCacheManager::GetInstance()->
  593. AddCacheEntry(ltname.c_str(),
  594. "SHARED",
  595. "Whether a library is static, shared or module.",
  596. cmCacheManager::INTERNAL);
  597. break;
  598. case 2:
  599. cmCacheManager::GetInstance()->
  600. AddCacheEntry(ltname.c_str(),
  601. "MODULE",
  602. "Whether a library is static, shared or module.",
  603. cmCacheManager::INTERNAL);
  604. break;
  605. default:
  606. cmCacheManager::GetInstance()->
  607. AddCacheEntry(ltname.c_str(),
  608. "STATIC",
  609. "Whether a library is static, shared or module.",
  610. cmCacheManager::INTERNAL);
  611. }
  612. }
  613. void cmMakefile::AddExecutable(const char *exeName,
  614. const std::vector<std::string> &srcs)
  615. {
  616. this->AddExecutable(exeName,srcs,false);
  617. }
  618. void cmMakefile::AddExecutable(const char *exeName,
  619. const std::vector<std::string> &srcs,
  620. bool win32)
  621. {
  622. cmTarget target;
  623. if (win32)
  624. {
  625. target.SetType(cmTarget::WIN32_EXECUTABLE);
  626. }
  627. else
  628. {
  629. target.SetType(cmTarget::EXECUTABLE);
  630. }
  631. target.SetInAll(true);
  632. target.GetSourceLists() = srcs;
  633. m_Targets.insert(cmTargets::value_type(exeName,target));
  634. // Add an entry into the cache
  635. cmCacheManager::GetInstance()->
  636. AddCacheEntry(exeName,
  637. this->GetCurrentOutputDirectory(),
  638. "Path to an executable", cmCacheManager::INTERNAL);
  639. }
  640. void cmMakefile::AddUtilityCommand(const char* utilityName,
  641. const char* command,
  642. const char* arguments,
  643. bool all)
  644. {
  645. std::vector<std::string> empty;
  646. this->AddUtilityCommand(utilityName,command,arguments,all,
  647. empty,empty);
  648. }
  649. void cmMakefile::AddUtilityCommand(const char* utilityName,
  650. const char* command,
  651. const char* arguments,
  652. bool all,
  653. const std::vector<std::string> &dep,
  654. const std::vector<std::string> &out)
  655. {
  656. cmTarget target;
  657. target.SetType(cmTarget::UTILITY);
  658. target.SetInAll(all);
  659. cmCustomCommand cc(utilityName, command, arguments, dep, out);
  660. target.GetCustomCommands().push_back(cc);
  661. m_Targets.insert(cmTargets::value_type(utilityName,target));
  662. }
  663. void cmMakefile::AddSourceGroup(const char* name, const char* regex)
  664. {
  665. // First see if the group exists. If so, replace its regular expression.
  666. for(std::vector<cmSourceGroup>::iterator sg = m_SourceGroups.begin();
  667. sg != m_SourceGroups.end(); ++sg)
  668. {
  669. std::string sgName = sg->GetName();
  670. if(sgName == name)
  671. {
  672. // We only want to set the regular expression. If there are already
  673. // source files in the group, we don't want to remove them.
  674. sg->SetGroupRegex(regex);
  675. return;
  676. }
  677. }
  678. // The group doesn't exist. Add it.
  679. m_SourceGroups.push_back(cmSourceGroup(name, regex));
  680. }
  681. void cmMakefile::AddExtraDirectory(const char* dir)
  682. {
  683. m_AuxSourceDirectories.push_back(dir);
  684. }
  685. // return the file name for the parent CMakeLists file to the
  686. // one passed in. Zero is returned if the CMakeLists file is the
  687. // one in the home directory or if for some reason a parent cmake lists
  688. // file cannot be found.
  689. std::string cmMakefile::GetParentListFileName(const char *currentFileName)
  690. {
  691. // extract the directory name
  692. std::string parentFile;
  693. std::string listsDir = currentFileName;
  694. std::string::size_type pos = listsDir.rfind('/');
  695. // if we could not find the directory return 0
  696. if(pos == std::string::npos)
  697. {
  698. return parentFile;
  699. }
  700. listsDir = listsDir.substr(0, pos);
  701. // if we are in the home directory then stop, return 0
  702. if(m_cmHomeDirectory == listsDir)
  703. {
  704. return parentFile;
  705. }
  706. // is there a parent directory we can check
  707. pos = listsDir.rfind('/');
  708. // if we could not find the directory return 0
  709. if(pos == std::string::npos)
  710. {
  711. return parentFile;
  712. }
  713. listsDir = listsDir.substr(0, pos);
  714. // is there a CMakeLists.txt file in the parent directory ?
  715. parentFile = listsDir;
  716. parentFile += "/CMakeLists.txt";
  717. if(!cmSystemTools::FileExists(parentFile.c_str()))
  718. {
  719. parentFile = "";
  720. return parentFile;
  721. }
  722. return parentFile;
  723. }
  724. // expance CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR in the
  725. // include and library directories.
  726. void cmMakefile::ExpandVariables()
  727. {
  728. // Now expand varibles in the include and link strings
  729. for(std::vector<std::string>::iterator d = m_IncludeDirectories.begin();
  730. d != m_IncludeDirectories.end(); ++d)
  731. {
  732. this->ExpandVariablesInString(*d);
  733. }
  734. for(std::vector<std::string>::iterator d = m_LinkDirectories.begin();
  735. d != m_LinkDirectories.end(); ++d)
  736. {
  737. this->ExpandVariablesInString(*d);
  738. }
  739. for(cmTarget::LinkLibraries::iterator l = m_LinkLibraries.begin();
  740. l != m_LinkLibraries.end(); ++l)
  741. {
  742. this->ExpandVariablesInString(l->first);
  743. }
  744. }
  745. bool cmMakefile::IsOn(const char* name)
  746. {
  747. const char* value = this->GetDefinition(name);
  748. return cmSystemTools::IsOn(value);
  749. }
  750. const char* cmMakefile::GetDefinition(const char* name) const
  751. {
  752. DefinitionMap::const_iterator pos = m_Definitions.find(name);
  753. if(pos != m_Definitions.end())
  754. {
  755. return (*pos).second.c_str();
  756. }
  757. return cmCacheManager::GetInstance()->GetCacheValue(name);
  758. }
  759. int cmMakefile::DumpDocumentationToFile(std::ostream& f)
  760. {
  761. // Open the supplied filename
  762. // Loop over all registered commands and print out documentation
  763. const char *name;
  764. const char *terse;
  765. const char *full;
  766. char tmp[1024];
  767. sprintf(tmp,"Version %d.%d", cmMakefile::GetMajorVersion(),
  768. cmMakefile::GetMinorVersion());
  769. f << "<html>\n";
  770. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  771. f << "<ul>\n";
  772. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  773. j != m_Commands.end(); ++j)
  774. {
  775. name = (*j).second->GetName();
  776. terse = (*j).second->GetTerseDocumentation();
  777. full = (*j).second->GetFullDocumentation();
  778. f << "<li><b>" << name << "</b> - " << terse << std::endl
  779. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  780. }
  781. f << "</ul></html>\n";
  782. return 1;
  783. }
  784. const char *cmMakefile::ExpandVariablesInString(std::string& source) const
  785. {
  786. return this->ExpandVariablesInString(source, false);
  787. }
  788. const char *cmMakefile::ExpandVariablesInString(std::string& source,
  789. bool escapeQuotes) const
  790. {
  791. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  792. // in the m_Definitions map, if not found in the map, nothing is expanded.
  793. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  794. // the current environment variables.
  795. // start by look for $ or @ in the string
  796. std::string::size_type markerPos = source.find_first_of("$@");
  797. // if not found, or found as the last character, then leave quickly as
  798. // nothing needs to be expanded
  799. if((markerPos == std::string::npos) || (markerPos >= source.size()-1))
  800. {
  801. return source.c_str();
  802. }
  803. // current position
  804. std::string::size_type currentPos =0; // start at 0
  805. std::string result; // string with replacements
  806. // go until the the end of the string
  807. while((markerPos != std::string::npos) && (markerPos < source.size()-1))
  808. {
  809. // grab string from currentPos to the start of the variable
  810. // and add it to the result
  811. result += source.substr(currentPos, markerPos - currentPos);
  812. char endVariableMarker; // what is the end of the variable @ or }
  813. int markerStartSize = 1; // size of the start marker 1 or 2 or 5
  814. if(source[markerPos] == '$')
  815. {
  816. // ${var} case
  817. if(source[markerPos+1] == '{')
  818. {
  819. endVariableMarker = '}';
  820. markerStartSize = 2;
  821. }
  822. // $ENV{var} case
  823. else if(markerPos+4 < source.size() &&
  824. source[markerPos+4] == '{' &&
  825. !source.substr(markerPos+1, 3).compare("ENV"))
  826. {
  827. endVariableMarker = '}';
  828. markerStartSize = 5;
  829. }
  830. else
  831. {
  832. // bogus $ with no { so add $ to result and move on
  833. result += '$'; // add bogus $ back into string
  834. currentPos = markerPos+1; // move on
  835. endVariableMarker = ' '; // set end var to space so we can tell bogus
  836. }
  837. }
  838. else
  839. {
  840. // @VAR case
  841. endVariableMarker = '@';
  842. }
  843. // if it was a valid variable (started with @ or ${ or $ENV{ )
  844. if(endVariableMarker != ' ')
  845. {
  846. markerPos += markerStartSize; // move past marker
  847. // find the end variable marker starting at the markerPos
  848. std::string::size_type endVariablePos =
  849. source.find(endVariableMarker, markerPos);
  850. if(endVariablePos == std::string::npos)
  851. {
  852. // no end marker found so add the bogus start
  853. if(endVariableMarker == '@')
  854. {
  855. result += '@';
  856. }
  857. else
  858. {
  859. result += (markerStartSize == 5 ? "$ENV{" : "${");
  860. }
  861. currentPos = markerPos;
  862. }
  863. else
  864. {
  865. // good variable remove it
  866. std::string var = source.substr(markerPos, endVariablePos - markerPos);
  867. bool found = false;
  868. if (markerStartSize == 5) // $ENV{
  869. {
  870. char *ptr = getenv(var.c_str());
  871. if (ptr)
  872. {
  873. if (escapeQuotes)
  874. {
  875. result += cmSystemTools::EscapeQuotes(ptr);
  876. }
  877. else
  878. {
  879. result += ptr;
  880. }
  881. found = true;
  882. }
  883. }
  884. else
  885. {
  886. const char* lookup = this->GetDefinition(var.c_str());
  887. if(lookup)
  888. {
  889. if (escapeQuotes)
  890. {
  891. result += cmSystemTools::EscapeQuotes(lookup);
  892. }
  893. else
  894. {
  895. result += lookup;
  896. }
  897. found = true;
  898. }
  899. }
  900. // if found add to result, if not, then it gets blanked
  901. if (!found)
  902. {
  903. // if no definition is found then add the var back
  904. if(endVariableMarker == '@')
  905. {
  906. result += "@";
  907. result += var;
  908. result += "@";
  909. }
  910. else
  911. {
  912. result += (markerStartSize == 5 ? "$ENV{" : "${");
  913. result += var;
  914. result += "}";
  915. }
  916. }
  917. // lookup var, and replace it
  918. currentPos = endVariablePos+1;
  919. }
  920. }
  921. markerPos = source.find_first_of("$@", currentPos);
  922. }
  923. result += source.substr(currentPos); // pick up the rest of the string
  924. source = result;
  925. return source.c_str();
  926. }
  927. void cmMakefile::RemoveVariablesInString(std::string& source) const
  928. {
  929. cmRegularExpression var("(\\${[A-Za-z_0-9]*})");
  930. while (var.find(source))
  931. {
  932. source.erase(var.start(),var.end() - var.start());
  933. }
  934. cmRegularExpression varb("(\\$ENV{[A-Za-z_0-9]*})");
  935. while (varb.find(source))
  936. {
  937. source.erase(varb.start(),varb.end() - varb.start());
  938. }
  939. cmRegularExpression var2("(@[A-Za-z_0-9]*@)");
  940. while (var2.find(source))
  941. {
  942. source.erase(var2.start(),var2.end() - var2.start());
  943. }
  944. }
  945. // recursive function to create a vector of cmMakefile objects
  946. // This is done by reading the sub directory CMakeLists.txt files,
  947. // then calling this function with the new cmMakefile object
  948. void
  949. cmMakefile::FindSubDirectoryCMakeListsFiles(std::vector<cmMakefile*>&
  950. makefiles)
  951. {
  952. // loop over all the sub directories of this makefile
  953. const std::vector<std::string>& subdirs = this->GetSubDirectories();
  954. for(std::vector<std::string>::const_iterator i = subdirs.begin();
  955. i != subdirs.end(); ++i)
  956. {
  957. std::string subdir = *i;
  958. // Create a path to the list file in the sub directory
  959. std::string listFile = this->GetCurrentDirectory();
  960. listFile += "/";
  961. listFile += subdir;
  962. listFile += "/CMakeLists.txt";
  963. // if there is a CMakeLists.txt file read it
  964. if(!cmSystemTools::FileExists(listFile.c_str()))
  965. {
  966. cmSystemTools::Error("CMakeLists.txt file missing from sub directory:",
  967. listFile.c_str());
  968. }
  969. else
  970. {
  971. cmMakefile* mf = new cmMakefile;
  972. makefiles.push_back(mf);
  973. // initialize new makefile
  974. mf->SetHomeOutputDirectory(this->GetHomeOutputDirectory());
  975. mf->SetHomeDirectory(this->GetHomeDirectory());
  976. // add the subdir to the start output directory
  977. std::string outdir = this->GetStartOutputDirectory();
  978. outdir += "/";
  979. outdir += subdir;
  980. mf->SetStartOutputDirectory(outdir.c_str());
  981. // add the subdir to the start source directory
  982. std::string currentDir = this->GetStartDirectory();
  983. currentDir += "/";
  984. currentDir += subdir;
  985. mf->SetStartDirectory(currentDir.c_str());
  986. // Parse the CMakeLists.txt file
  987. currentDir += "/CMakeLists.txt";
  988. mf->MakeStartDirectoriesCurrent();
  989. mf->ReadListFile(currentDir.c_str());
  990. // recurse into nextDir
  991. mf->FindSubDirectoryCMakeListsFiles(makefiles);
  992. }
  993. }
  994. }
  995. /**
  996. * Add the default definitions to the makefile. These values must not
  997. * be dependent on anything that isn't known when this cmMakefile instance
  998. * is constructed.
  999. */
  1000. void cmMakefile::AddDefaultDefinitions()
  1001. {
  1002. #if defined(_WIN32) && !defined(__CYGWIN__)
  1003. this->AddDefinition("CMAKE_CFG_INTDIR","$(IntDir)");
  1004. #else
  1005. this->AddDefinition("CMAKE_CFG_INTDIR",".");
  1006. #endif
  1007. char temp[1024];
  1008. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  1009. this->AddDefinition("CMAKE_MINOR_VERSION", temp);
  1010. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  1011. this->AddDefinition("CMAKE_MAJOR_VERSION", temp);
  1012. }
  1013. /**
  1014. * Find a source group whose regular expression matches the filename
  1015. * part of the given source name. Search backward through the list of
  1016. * source groups, and take the first matching group found. This way
  1017. * non-inherited SOURCE_GROUP commands will have precedence over
  1018. * inherited ones.
  1019. */
  1020. cmSourceGroup&
  1021. cmMakefile::FindSourceGroup(const char* source,
  1022. std::vector<cmSourceGroup> &groups)
  1023. {
  1024. std::string file = source;
  1025. std::string::size_type pos = file.rfind('/');
  1026. if(pos != std::string::npos)
  1027. {
  1028. file = file.substr(pos, file.length()-pos);
  1029. }
  1030. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1031. sg != groups.rend(); ++sg)
  1032. {
  1033. if(sg->Matches(file.c_str()))
  1034. {
  1035. return *sg;
  1036. }
  1037. }
  1038. // Shouldn't get here, but just in case, return the default group.
  1039. return groups.front();
  1040. }
  1041. bool cmMakefile::IsFunctionBlocked(const char *name,
  1042. std::vector<std::string> const&args)
  1043. {
  1044. // loop over all function blockers to see if any block this command
  1045. std::set<cmFunctionBlocker *>::const_iterator pos;
  1046. for (pos = m_FunctionBlockers.begin();
  1047. pos != m_FunctionBlockers.end(); ++pos)
  1048. {
  1049. if ((*pos)->IsFunctionBlocked(name, args, *this))
  1050. {
  1051. return true;
  1052. }
  1053. }
  1054. return false;
  1055. }
  1056. void cmMakefile::RemoveFunctionBlocker(const char *name,
  1057. const std::vector<std::string> &args)
  1058. {
  1059. // loop over all function blockers to see if any block this command
  1060. std::set<cmFunctionBlocker *>::const_iterator pos;
  1061. for (pos = m_FunctionBlockers.begin();
  1062. pos != m_FunctionBlockers.end(); ++pos)
  1063. {
  1064. if ((*pos)->ShouldRemove(name, args, *this))
  1065. {
  1066. cmFunctionBlocker* b = *pos;
  1067. m_FunctionBlockers.erase(*pos);
  1068. delete b;
  1069. return;
  1070. }
  1071. }
  1072. return;
  1073. }
  1074. void cmMakefile::SetHomeDirectory(const char* dir)
  1075. {
  1076. m_cmHomeDirectory = dir;
  1077. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  1078. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  1079. }
  1080. void cmMakefile::SetHomeOutputDirectory(const char* lib)
  1081. {
  1082. m_HomeOutputDirectory = lib;
  1083. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  1084. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  1085. }
  1086. /**
  1087. * Register the given cmData instance with its own name.
  1088. */
  1089. void cmMakefile::RegisterData(cmData* data)
  1090. {
  1091. std::string name = data->GetName();
  1092. DataMap::const_iterator d = m_DataMap.find(name);
  1093. if((d != m_DataMap.end()) && (d->second != NULL) && (d->second != data))
  1094. {
  1095. delete d->second;
  1096. }
  1097. m_DataMap[name] = data;
  1098. }
  1099. /**
  1100. * Register the given cmData instance with the given name. This can be used
  1101. * to register a NULL pointer.
  1102. */
  1103. void cmMakefile::RegisterData(const char* name, cmData* data)
  1104. {
  1105. DataMap::const_iterator d = m_DataMap.find(name);
  1106. if((d != m_DataMap.end()) && (d->second != NULL) && (d->second != data))
  1107. {
  1108. delete d->second;
  1109. }
  1110. m_DataMap[name] = data;
  1111. }
  1112. /**
  1113. * Lookup a cmData instance previously registered with the given name. If
  1114. * the instance cannot be found, return NULL.
  1115. */
  1116. cmData* cmMakefile::LookupData(const char* name) const
  1117. {
  1118. DataMap::const_iterator d = m_DataMap.find(name);
  1119. if(d != m_DataMap.end())
  1120. {
  1121. return d->second;
  1122. }
  1123. else
  1124. {
  1125. return NULL;
  1126. }
  1127. }