cmMakefile.cxx 37 KB

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